Skip to content

Commit

Permalink
minimum tests for comptime pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaztahir committed Feb 10, 2025
1 parent 498ddef commit e08d67b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
52 changes: 52 additions & 0 deletions lang/tests/src/comptime/pointers.ch
Original file line number Diff line number Diff line change
@@ -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';
})
}
2 changes: 2 additions & 0 deletions lang/tests/src/tests.ch
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

0 comments on commit e08d67b

Please sign in to comment.