From a05d691e5918fc332c374a8eba9cc0cd2b8a03cd Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 24 Jan 2023 21:18:52 +0200 Subject: [PATCH] Put all pointer tests into the same file (#139) * Put all pointer tests into the same place * windows workarounds * Revert "windows workarounds" This reverts commit 1463aa6ca079ea58f085051cb240912f973f5b16. * better? windows u happy now?.. * lol --- tests/should_succeed/indexing_bug.jou | 7 ------ tests/should_succeed/malloc.jou | 10 -------- tests/should_succeed/null.jou | 13 ---------- tests/should_succeed/pointer.jou | 34 ++++++++++++++++++++------- 4 files changed, 25 insertions(+), 39 deletions(-) delete mode 100644 tests/should_succeed/indexing_bug.jou delete mode 100644 tests/should_succeed/malloc.jou delete mode 100644 tests/should_succeed/null.jou diff --git a/tests/should_succeed/indexing_bug.jou b/tests/should_succeed/indexing_bug.jou deleted file mode 100644 index eb3dfa30..00000000 --- a/tests/should_succeed/indexing_bug.jou +++ /dev/null @@ -1,7 +0,0 @@ -from "stdlib/io.jou" import putchar - -def main() -> int: - # Output: i - putchar("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789abcdefghi"[128 as byte]) - putchar('\n') - return 0 diff --git a/tests/should_succeed/malloc.jou b/tests/should_succeed/malloc.jou deleted file mode 100644 index 4735f6a5..00000000 --- a/tests/should_succeed/malloc.jou +++ /dev/null @@ -1,10 +0,0 @@ -from "stdlib/io.jou" import printf -from "stdlib/mem.jou" import malloc, free - -def main() -> int: - # TODO: no sizeof() yet, must hard code sizeof(int) == 4 - foo: int* = malloc(4) - *foo = 123 - printf("%d\n", *foo) # Output: 123 - free(foo) - return 0 diff --git a/tests/should_succeed/null.jou b/tests/should_succeed/null.jou deleted file mode 100644 index 30a0defd..00000000 --- a/tests/should_succeed/null.jou +++ /dev/null @@ -1,13 +0,0 @@ -from "stdlib/io.jou" import printf - -def foo(ptr: int*) -> void: - if ptr == NULL: - printf("Got NULL\n") - else: - printf("Got %d\n", *ptr) - -def main() -> int: - x = 123 - foo(&x) # Output: Got 123 - foo(NULL) # Output: Got NULL - return 0 diff --git a/tests/should_succeed/pointer.jou b/tests/should_succeed/pointer.jou index d06ded11..c5e4ed59 100644 --- a/tests/should_succeed/pointer.jou +++ b/tests/should_succeed/pointer.jou @@ -1,4 +1,4 @@ -from "stdlib/io.jou" import putchar, puts +from "stdlib/io.jou" import putchar, puts, printf from "stdlib/mem.jou" import malloc, free def putchar_pointer(ch: int*) -> void: @@ -16,7 +16,22 @@ def myputs2(s: byte*) -> void: while *s != '\0': putchar(*s++) +def foo(ptr: int*) -> void: + if ptr == NULL: + printf("Got NULL\n") + else: + printf("Got %d\n", *ptr) + def main() -> int: + # Test assigning to pointer + foo: byte* = malloc(4) + *foo = 'a' + foo[1] = 'b' + foo[2] = 'c' + foo[3] = '\0' + puts(foo) # Output: abc + free(foo) + # Output: hhii putchar2('h') putchar2('i') @@ -26,16 +41,17 @@ def main() -> int: putchar("hey"[1+1]) # Output: y putchar('\n') + # Make sure that unsigned indexes work. The compiler must + # convert them to signed for LLVM to be happy. + # Output: i + putchar("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789abcdefghi"[128 as byte]) + putchar('\n') + myputs1("asd\n") # Output: asd myputs2("foo\n") # Output: foo - # Test assigning to pointer - foo: byte* = malloc(4) - foo[0] = 'a' - foo[1] = 'b' - foo[2] = 'c' - foo[3] = '\0' - puts(foo) # Output: abc - free(foo) + x = 123 + foo(&x) # Output: Got 123 + foo(NULL) # Output: Got NULL return 0