import t import stdio import string import c import testcheck import inline_test # ============================================================ # Test 1: string pointer iteration, char by char until NUL # ============================================================ def test_string_ptr_iter(): testcheck.section("Test 1: string pointer iteration") s: char* = "Hello" p: char* = s count: int = 0 while *p != 0: count += 1 p += 1 testcheck.check(count == 5, "ptr iter (count=5)", "ptr iter (count expect 5)") # ============================================================ # Test 2: sum ASCII values via string pointer # ============================================================ def test_string_ascii_sum(): testcheck.section("Test 2: string ASCII sum") s: char* = "ABC" p: char* = s total: int = 0 while *p != 0: total += int(*p) p += 1 # A=65, B=66, C=67 => 198 testcheck.check(total == 198, "ASCII sum (total=198)", "ASCII sum (total expect 198)") # ============================================================ # Test 3: manual strcpy via pointers # ============================================================ def test_manual_strcpy(): testcheck.section("Test 3: manual strcpy via pointers") src: char* = "Test" dst: t.CArray[char, 32] = [0] sp: char* = src dp: char* = &dst while *sp != 0: *dp = *sp sp += 1 dp += 1 *dp = 0 testcheck.check(dst[0] == 84 and dst[1] == 101 and dst[2] == 115 and dst[3] == 116, "manual strcpy (T=84 e=101 s=115 t=116)", "manual strcpy (expect T=84 e=101 s=115 t=116)") # ============================================================ # Test 4: pointer comparison and offset # ============================================================ def test_ptr_offset_compare(): testcheck.section("Test 4: pointer offset and compare") arr: t.CArray[int, 5] = [0] for i in range(5): arr[i] = i * 10 p: int* = &arr start: int* = p total: int = 0 while p != start + 5: total += *p p += 1 testcheck.check(total == 100, "ptr offset (total=100)", "ptr offset (total expect 100)") # ============================================================ # Test 5: manual string length # ============================================================ def test_manual_strlen(): testcheck.section("Test 5: manual strlen") s: char* = "Hello World!" p: char* = s length: sizet = 0 while *p != 0: length += 1 p += 1 testcheck.check(length == 12, "manual strlen (len=12)", "manual strlen (len expect 12)") # ============================================================ # Test 6: reverse string via pointer # ============================================================ def test_reverse_string_ptr(): testcheck.section("Test 6: reverse string via pointer") src: char* = "abcd" dst: t.CArray[char, 32] = [0] # Find end of src p: char* = src length: int = 0 while *p != 0: length += 1 p += 1 # Copy in reverse dp: char* = &dst p = src + length - 1 while p >= src: *dp = *p dp += 1 p -= 1 *dp = 0 # "abcd" reversed = "dcba" = 100,99,98,97 testcheck.check(dst[0] == 100 and dst[1] == 99 and dst[2] == 98 and dst[3] == 97, "reverse string (d=100 c=99 b=98 a=97)", "reverse string (expect d=100 c=99 b=98 a=97)") # ============================================================ # Test 7: multi-level pointer (pointer to pointer) # ============================================================ def test_double_pointer(): testcheck.section("Test 7: double pointer") x: int = 42 px: int* = &x ppx: int** = &px # Read through double pointer val: int = *(*ppx) testcheck.check(val == 42, "double pointer (val=42)", "double pointer (val expect 42)") # ============================================================ # Test 8: pointer arithmetic, array element access # ============================================================ def test_ptr_arithmetic(): testcheck.section("Test 8: pointer arithmetic") arr: t.CArray[int, 8] = [0] for i in range(8): arr[i] = (i + 1) * (i + 1) p: int* = &arr # Access arr[3] via pointer: p + 3 val: int = *(p + 3) # arr[3] = 4*4 = 16 testcheck.check(val == 16, "ptr arithmetic (arr[3]=16)", "ptr arithmetic (arr[3] expect 16)") # ============================================================ # Test 9: manual string compare via char pointers # ============================================================ def test_manual_strcmp(): testcheck.section("Test 9: manual strcmp via pointers") s1: char* = "Hello" s2: char* = "Hello" p1: char* = s1 p2: char* = s2 equal: int = 1 while *p1 != 0 and *p2 != 0: if *p1 != *p2: equal = 0 break p1 += 1 p2 += 1 if *p1 != *p2: equal = 0 testcheck.check(equal == 1, "manual strcmp (equal=1)", "manual strcmp (equal expect 1)") # ============================================================ # Test 10: struct pointer access # ============================================================ class Vec2: x: int y: int def test_struct_ptr_access(): testcheck.section("Test 10: struct pointer access") v: Vec2 = Vec2() v.x = 10 v.y = 20 p: Vec2* = &v # Modify through pointer (*p).x = 100 (*p).y = 200 testcheck.check(v.x == 100 and v.y == 200, "struct ptr (x=100 y=200)", "struct ptr (x y expect 100 200)") def main() -> int: testcheck.begin("StringIterTest: String/Pointer Iteration Tests") test_string_ptr_iter() test_string_ascii_sum() test_manual_strcpy() test_ptr_offset_compare() test_manual_strlen() test_reverse_string_ptr() test_double_pointer() test_ptr_arithmetic() test_manual_strcmp() test_struct_ptr_access() inline_test.inline_array_test() return testcheck.end()