From dd778f5d65a3dc31c7c5611b4b3be699eb298303 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 25 Feb 2020 21:42:45 -0500 Subject: cleanup and new test: arr3 --- tests/arr3/arr3.toc | 54 ++++++++++++++ tests/arr3/expected | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/arr3/io.toc | 57 +++++++++++++++ tests/arr3/test.sh | 3 + 4 files changed, 314 insertions(+) create mode 100644 tests/arr3/arr3.toc create mode 100644 tests/arr3/expected create mode 100644 tests/arr3/io.toc create mode 100755 tests/arr3/test.sh (limited to 'tests/arr3') 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 -- cgit v1.2.3