diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-02-25 21:42:45 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-02-25 21:42:45 -0500 |
commit | dd778f5d65a3dc31c7c5611b4b3be699eb298303 (patch) | |
tree | e9a43db9fe9015df7c8506044c702e68997235f6 /tests | |
parent | 77a2c20ea02e1eadb2a639e6c1b0d25669025ac8 (diff) |
cleanup and new test: arr3
Diffstat (limited to 'tests')
-rw-r--r-- | tests/arr3/arr3.toc | 54 | ||||
-rw-r--r-- | tests/arr3/expected | 200 | ||||
-rw-r--r-- | tests/arr3/io.toc | 57 | ||||
-rwxr-xr-x | tests/arr3/test.sh | 3 | ||||
-rwxr-xr-x | tests/test.sh | 1 |
5 files changed, 315 insertions, 0 deletions
diff --git a/tests/arr3/arr3.toc b/tests/arr3/arr3.toc new file mode 100644 index 0000000..a6d1b14 --- /dev/null +++ b/tests/arr3/arr3.toc @@ -0,0 +1,54 @@ +#include "io.toc"; + +Arr ::= struct (t :: Type) { + data: []t; + len, cap: int; +}; + + +arr_add ::= fn(t ::=, a : &Arr(t), x : t) { + if a.len >= a.cap { + a.cap = a.cap * 2 + 2; + new_data := new(t, a.cap); + for i := 0..a.len-1 { + new_data[i] = a.data[i]; + } + a.data = new_data; + } + a.data[a.len] = x; + a.len += 1; +}; + +square ::= fn(t ::=, x : t) t { + a : Arr(t); + for i := 1,2..2*x-1 { + arr_add(&a, i); + }; + sum := 0 as t; + for i := 0..a.len-1 { + sum += a.data[i]; + }; + sum +}; + + +// ArrInt ::= Arr(int); + +inc ::= fn(t ::=, x : t) t { + x + 1 +}; + +main ::= fn() { + arr : Arr(int); + farr : Arr(float); + for i := 1..100 { + arr_add(&arr, inc(square(i))); + arr_add(&farr, inc(square(i as float))); + } + for i := 0..arr.len - 1 { + puti(arr.data[i]); + } + for i := 0..farr.len - 1 { + puti(farr.data[i] as int); + } +}; diff --git a/tests/arr3/expected b/tests/arr3/expected new file mode 100644 index 0000000..101ae98 --- /dev/null +++ b/tests/arr3/expected @@ -0,0 +1,200 @@ +2 +5 +10 +17 +26 +37 +50 +65 +82 +101 +122 +145 +170 +197 +226 +257 +290 +325 +362 +401 +442 +485 +530 +577 +626 +677 +730 +785 +842 +901 +962 +1025 +1090 +1157 +1226 +1297 +1370 +1445 +1522 +1601 +1682 +1765 +1850 +1937 +2026 +2117 +2210 +2305 +2402 +2501 +2602 +2705 +2810 +2917 +3026 +3137 +3250 +3365 +3482 +3601 +3722 +3845 +3970 +4097 +4226 +4357 +4490 +4625 +4762 +4901 +5042 +5185 +5330 +5477 +5626 +5777 +5930 +6085 +6242 +6401 +6562 +6725 +6890 +7057 +7226 +7397 +7570 +7745 +7922 +8101 +8282 +8465 +8650 +8837 +9026 +9217 +9410 +9605 +9802 +10001 +2 +5 +10 +17 +26 +37 +50 +65 +82 +101 +122 +145 +170 +197 +226 +257 +290 +325 +362 +401 +442 +485 +530 +577 +626 +677 +730 +785 +842 +901 +962 +1025 +1090 +1157 +1226 +1297 +1370 +1445 +1522 +1601 +1682 +1765 +1850 +1937 +2026 +2117 +2210 +2305 +2402 +2501 +2602 +2705 +2810 +2917 +3026 +3137 +3250 +3365 +3482 +3601 +3722 +3845 +3970 +4097 +4226 +4357 +4490 +4625 +4762 +4901 +5042 +5185 +5330 +5477 +5626 +5777 +5930 +6085 +6242 +6401 +6562 +6725 +6890 +7057 +7226 +7397 +7570 +7745 +7922 +8101 +8282 +8465 +8650 +8837 +9026 +9217 +9410 +9605 +9802 +10001 diff --git a/tests/arr3/io.toc b/tests/arr3/io.toc new file mode 100644 index 0000000..fac4921 --- /dev/null +++ b/tests/arr3/io.toc @@ -0,0 +1,57 @@ +get_type_with_size ::= fn(size :: i64) Type { + if size == 1 { i8 } + elif size == 2 { i16 } + elif size == 4 { i32 } + elif size == 8 { i64 } + else { f32 } +}; + +get_utype_with_size ::= fn(size :: i64) Type { + if size == 1 { u8 } + elif size == 2 { u16 } + elif size == 4 { u32 } + elif size == 8 { u64 } + else { f32 } +}; + +c_int ::= get_type_with_size(#builtin("sizeof int")); +c_size_t ::= get_utype_with_size(#builtin("sizeof size_t")); + +c_putchar :: fn(c_int) c_int = #foreign "putchar", "libc.so.6"; +toc_putchar ::= fn(x: char) { + c_putchar(x as c_int); +}; + +c_fwrite :: fn(&u8, c_size_t, c_size_t, &u8) = #foreign "fwrite", "libc.so.6"; + +stdout_fwrite ::= fn(data: &u8, size: u64, nmemb: u64) { + c_fwrite(data, size as c_size_t, nmemb as c_size_t, #builtin("stdout")); +}; + +puts ::= fn(x: []char) { + stdout_fwrite(&x[0] as &u8, 1, x.len as u64); + toc_putchar('\n'); +}; + +puti ::= fn(x: int) { + if x < 0 { + toc_putchar('-'); + // NOTE: don't do x = -x; here to make sure I64_MIN works + } + if x == 0 { + toc_putchar('0'); + } else { + abs ::= fn(x: int) int { if x < 0 { -x } else { x } }; + scan_digit := 1000000000000000000; + started := false; + while scan_digit > 0 { + digit := abs((x / scan_digit) % 10); + if digit > 0 { started = true; } + if started { + toc_putchar((('0' as int) + digit) as char); + } + scan_digit /= 10; + } + } + toc_putchar('\n'); +}; diff --git a/tests/arr3/test.sh b/tests/arr3/test.sh new file mode 100755 index 0000000..135a6e0 --- /dev/null +++ b/tests/arr3/test.sh @@ -0,0 +1,3 @@ +#!/bin/sh +./arr3.bin > got || exit 1 +diff got expected || exit 1 diff --git a/tests/test.sh b/tests/test.sh index 4852218..6d534d3 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -34,6 +34,7 @@ STARTPWD="$(pwd)" do_tests bf do_tests arr do_tests arr2 +do_tests arr3 do_tests foreign do_tests params do_tests nms |