Skip to content

Commit

Permalink
tested pointers with runtime methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaztahir committed Feb 10, 2025
1 parent e08d67b commit 8f93d49
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions lang/tests/src/comptime/pointers.ch
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import "./test.ch"

@comptime
func give_comptime_char_by_offset(str : *char, offset : int) : char {
func give_char_by_offset(str : *char, offset : int) : char {
return *(str + offset);
}

@comptime
func give_comptime_char_by_ptr_offset(str : *char, offset : int) : char {
func give_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 {
func test_inc_dec_work_ptr1() : char {
const str = "hello world"
const ptr = str as *char;
var ptr = str as *char;
ptr++
return *ptr
}

@comptime
func test_inc_dec_work_comptime_ptr2() : char {
func test_inc_dec_work_ptr2() : char {
const str = "hello world"
const ptr = str as *char;
var ptr = str as *char;
ptr++
ptr--
return *ptr
Expand All @@ -32,21 +28,21 @@ func test_inc_dec_work_comptime_ptr2() : char {

func test_pointers_in_comptime() {
test("comptime pointers work - 1", () => {
return give_comptime_char_by_offset("hello", 0) == 'h';
return comptime { give_char_by_offset("hello", 0) } == give_char_by_offset("hello", 0)
})
test("comptime pointers work - 2", () => {
return give_comptime_char_by_ptr_offset("hello", 0) == 'h'
return comptime { give_char_by_ptr_offset("hello", 0) } == give_char_by_ptr_offset("hello", 0)
})
test("comptime pointers work - 3", () => {
return give_comptime_char_by_ptr_offset("hello", 1) == 'e'
return comptime { give_char_by_ptr_offset("hello", 1) } == give_char_by_ptr_offset("hello", 1)
})
test("comptime pointers work - 4", () => {
return give_comptime_char_by_ptr_offset("hello", 4) == 'o'
return comptime { give_char_by_ptr_offset("hello", 4) } == give_char_by_ptr_offset("hello", 4)
})
test("comptime pointers work with increment decrement - 1", () => {
return test_inc_dec_work_comptime_ptr1() == 'e'
return comptime { test_inc_dec_work_ptr1() } == test_inc_dec_work_ptr1()
})
test("comptime pointers work with increment decrement - 2", () => {
return test_inc_dec_work_comptime_ptr2() == 'h';
return comptime { test_inc_dec_work_ptr2() } == test_inc_dec_work_ptr2()
})
}

0 comments on commit 8f93d49

Please sign in to comment.