import stdio
import stdlib
import t, c
import testcheck
import memhub
import _list


# ============================================================
# 泛型类 list[T] 测试
# ============================================================
def generic_test() -> int:
    testcheck.begin("GenericTest: list[T] 泛型类测试")

    # 创建 mbuddy arena
    arena: bytes = stdlib.malloc(65536)
    bd: memhub.MemBuddy | t.CPtr = memhub.MemBuddy(arena, 65536)

    # === Test 1: 创建和 append ===
    testcheck.section("Test 1: 创建和 append")
    nums = list[int](bd)
    nums.append(10)
    nums.append(20)
    nums.append(30)
    v0: t.CInt = nums.get(0)
    v1: t.CInt = nums.get(1)
    v2: t.CInt = nums.get(2)
    stdio.printf("v0=%d v1=%d v2=%d\n", v0, v1, v2)
    testcheck.check(v0 == 10 and v1 == 20 and v2 == 30,
                    "append+get OK (10,20,30)", "append+get FAILED")

    # === Test 2: __len__ ===
    testcheck.section("Test 2: __len__")
    n: t.CSizeT = nums.__len__()
    stdio.printf("len=%lu\n", n)
    testcheck.check(n == 3, "__len__ OK (3)", "__len__ FAILED expect 3")

    # === Test 3: set ===
    testcheck.section("Test 3: set")
    nums.set(1, 99)
    v1b: t.CInt = nums.get(1)
    stdio.printf("after set(1,99): v1=%d\n", v1b)
    testcheck.check(v1b == 99, "set OK (idx1=99)", "set FAILED")

    # === Test 4: pop ===
    testcheck.section("Test 4: pop")
    popped: t.CInt = nums.pop()
    stdio.printf("popped=%d\n", popped)
    testcheck.check(popped == 30, "pop OK (30)", "pop FAILED expect 30")
    n2: t.CSizeT = nums.__len__()
    testcheck.check(n2 == 2, "pop len OK (2)", "pop len FAILED expect 2")

    # === Test 5: clear ===
    testcheck.section("Test 5: clear")
    nums.clear()
    n3: t.CSizeT = nums.__len__()
    testcheck.check(n3 == 0, "clear OK (0)", "clear FAILED expect 0")

    # === Test 6: 容量增长 (append 超过初始容量 8) ===
    testcheck.section("Test 6: 容量增长")
    nums2 = list[int](bd)
    i: t.CInt
    for i in range(20):
        nums2.append(i * 5)
    n4: t.CSizeT = nums2.__len__()
    stdio.printf("after 20 appends: len=%lu\n", n4)
    testcheck.check(n4 == 20, "grow len OK (20)", "grow len FAILED")
    ok: t.CInt = 1
    for i in range(20):
        v: t.CInt = nums2.get(i)
        if v != i * 5:
            stdio.printf("MISMATCH at %d: got %d expect %d\n", i, v, i * 5)
            ok = 0
            break
    testcheck.check(ok == 1, "grow data OK", "grow data FAILED")

    # === Test 7: 多个 list[int] 实例独立 ===
    testcheck.section("Test 7: 多实例独立")
    lst_a = list[int](bd)
    lst_b = list[int](bd)
    lst_a.append(111)
    lst_b.append(222)
    va: t.CInt = lst_a.get(0)
    vb: t.CInt = lst_b.get(0)
    stdio.printf("lst_a[0]=%d lst_b[0]=%d\n", va, vb)
    testcheck.check(va == 111 and vb == 222,
                    "multi-instance OK (111,222)", "multi-instance FAILED")

    # === Test 8: __getitem__ / __setitem__ ===
    testcheck.section("Test 8: __getitem__/__setitem__")
    nums3 = list[int](bd)
    nums3.append(1)
    nums3.append(2)
    nums3.append(3)
    nums3[0] = 100
    gv: t.CInt = nums3[0]
    stdio.printf("nums3[0]=%d after setitem\n", gv)
    testcheck.check(gv == 100, "__setitem__/__getitem__ OK",
                    "__setitem__/__getitem__ FAILED")

    stdlib.free(arena)
    return testcheck.end()