diff --git a/lang/tests/src/comptime/pointers.ch b/lang/tests/src/comptime/pointers.ch new file mode 100644 index 000000000..565bcf7b9 --- /dev/null +++ b/lang/tests/src/comptime/pointers.ch @@ -0,0 +1,52 @@ +import "./test.ch" + +@comptime +func give_comptime_char_by_offset(str : *char, offset : int) : char { + return *(str + offset); +} + +@comptime +func give_comptime_char_by_ptr_offset(str : *char, offset : int) : char { + var x = str as *char + x += offset + return *x; +} + +@comptime +func test_inc_dec_work_comptime_ptr1() : char { + const str = "hello world" + const ptr = str as *char; + ptr++ + return *ptr +} + +@comptime +func test_inc_dec_work_comptime_ptr2() : char { + const str = "hello world" + const ptr = str as *char; + ptr++ + ptr-- + return *ptr +} + + +func test_pointers_in_comptime() { + test("comptime pointers work - 1", () => { + return give_comptime_char_by_offset("hello", 0) == 'h'; + }) + test("comptime pointers work - 2", () => { + return give_comptime_char_by_ptr_offset("hello", 0) == 'h' + }) + test("comptime pointers work - 3", () => { + return give_comptime_char_by_ptr_offset("hello", 1) == 'e' + }) + test("comptime pointers work - 4", () => { + return give_comptime_char_by_ptr_offset("hello", 4) == 'o' + }) + test("comptime pointers work with increment decrement - 1", () => { + return test_inc_dec_work_comptime_ptr1() == 'e' + }) + test("comptime pointers work with increment decrement - 2", () => { + return test_inc_dec_work_comptime_ptr2() == 'h'; + }) +} \ No newline at end of file diff --git a/lang/tests/src/tests.ch b/lang/tests/src/tests.ch index 3d42338e0..e973de31b 100644 --- a/lang/tests/src/tests.ch +++ b/lang/tests/src/tests.ch @@ -23,6 +23,7 @@ import "basic/interface/static.ch" import "nodes/union.ch" import "nodes/namespaces.ch" import "comptime/basic.ch" +import "comptime/pointers.ch" import "comptime/expressions.ch" import "comptime/vector.ch" import "basic/external.ch" @@ -90,6 +91,7 @@ public func main() : int { test_typealias(); test_inc_dec(); test_static_interfaces(); + test_pointers_in_comptime(); print_test_stats(); return 0; } \ No newline at end of file