harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 319ed2a3bac70ce5fef7e7f62abcbfa26f3151c4
parent edf4fbf95375fec8a848c9d137cb79cc485ba323
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Fri, 22 Jan 2021 12:34:25 -0500

Add allocation tests

Diffstat:
Atests/17-alloc.ha | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/configure | 3++-
2 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/tests/17-alloc.ha b/tests/17-alloc.ha @@ -0,0 +1,59 @@ +type my_struct = struct { + x: int, + y: int, +}; + +type my_struct_ptr = *my_struct; + +fn allocation() void = { + let x = alloc(*int, 1234); + assert(*x == 1234); + free(x); + + let y = alloc(nullable *int, 1234); + // TODO: Type promotion + if (y != null: nullable *int) { + assert(*(y: *int) == 1234); + }; + free(y); + + let z = alloc(my_struct_ptr, struct { + x: int = 42, + y: int = 69, + }); + assert(z.x == 42 && z.y == 69); + free(z); +}; + +fn assignment() void = { + let x = alloc(*int, 1234); + *x = 4321; + assert(*x == 4321); + free(x); +}; + +fn double_pointer() void = { + let x = alloc(*int, 1234); + let y = alloc(**int, x); + *x = 4321; + assert(**y == 4321); + **y = 1337; + assert(*x == 1337); + free(y); + free(x); +}; + +fn double_alloc() void = { + let x = alloc(*int, 1234); + let y = alloc(*int, 4321); + assert(x != y && *x != *y); + free(x); + free(y); +}; + +export fn main() void = { + assignment(); + allocation(); + double_pointer(); + double_alloc(); +}; diff --git a/tests/configure b/tests/configure @@ -19,7 +19,8 @@ tests() { 13-tagged \ 14-switch \ 15-enums \ - 16-defer + 16-defer \ + 17-alloc do cat <<EOF tests/$t: libhart.a tests/$t.ha