additional testing changes

This commit is contained in:
koniifer 2025-02-01 13:42:21 +00:00
parent ad7960150d
commit 28c7391920
4 changed files with 62 additions and 45 deletions

33
build
View file

@ -72,13 +72,7 @@ parse_args() {
shift
;;
-u | --test)
if [ -z "${2:-}" ]; then
test="all"
else
[ "$2" = "none" ] && warn "'--test none' is redundant"
test=$2
shift
fi
test="all"
;;
--test=?*)
if [ "${1#*=}" = "none" ]; then warn "'--test=none' is redundant"; fi
@ -189,29 +183,31 @@ run_test() {
rc=$?
[ $rc -ne 0 ] && {
# todo: quiet mode to hide compilation errors.
error "$name: compilation failed: $(cat "$BUILD_PATH/test/$name.o")"
error "$name: compilation failed (exit: $(pretty_exitcode "$rc")): $(cat "$BUILD_PATH/test/$name.o")"
return 1
}
$linker "$BUILD_PATH/test/$name.o" -o "$BUILD_PATH/test/$name" || die "linking failed:"
if [ -n "$timeout_val" ]; then
# shellcheck disable=SC2086
output=$(timeout "$timeout_val" "$BUILD_PATH/test/$name" $args_input 2>&1)
rc=$?
[ $rc -eq 124 ] && {
error "timeout"
error "$name: timeout"
failed=1
}
else
# shellcheck disable=SC2086
output=$("$BUILD_PATH/test/$name" $args_input 2>&1)
rc=$?
fi
[ "$rc" != "$exit_code" ] && {
error "exit code $rc != $exit_code"
error "$name: exit code $(pretty_exitcode "$rc") != $exit_code"
failed=1
}
[ -n "$stdout_expected" ] && [ "$output" != "$stdout_expected" ] && {
error "output mismatch
error "$name: output mismatch
expected: $stdout_expected
got: $output"
failed=1
@ -219,6 +215,17 @@ got: $output"
return $failed
}
pretty_exitcode() {
exit_code=$1
signal=$((exit_code - 128))
if [ $signal -gt 0 ] && [ $signal -lt 32 ]; then
echo "$exit_code (SIG$(kill -l $signal 2>/dev/null || echo "???"))"
else
echo "$exit_code"
fi
}
# todo: this spawns every test all at once. consider limiting to $(nproc) tests at once.
do_tests() {
detect_linker
@ -249,10 +256,12 @@ do_build() {
hbc_flags="$hbc_flags --dump-asm"
out="/dev/stdout"
}
# shellcheck disable=SC2086
hbc "$SRC_DIR/main.hb" $hbc_flags --target="$target" >"$out" || build_failed
else
[ -z "$linker" ] && detect_linker
[ "$target" != "default" ] && hbc_flags="$hbc_flags --target=$target"
# shellcheck disable=SC2086
hbc "$SRC_DIR/main.hb" $hbc_flags >"$BUILD_PATH/$target/lily.o" || build_failed
$linker "$BUILD_PATH/$target/lily.o" -o "$BUILD_PATH/$target/lily"
fi
@ -270,7 +279,7 @@ main() {
none) do_build ;;
lang | lily | all)
[ "$test" = "all" ] && test=""
[ $dump_asm -eq 1 ] && die "--dump-asm incompatible with tests"
[ $dump_asm -eq 1 ] && die "--dump-asm incompatible with tests (for now)"
[ "$target" != "default" ] && die "--target incompatible with tests"
do_tests
;;

View file

@ -2,7 +2,7 @@
tests are written in [src/test](../../src/test/)
1. tests that test the language will be in the [lang](../../src/test/lang) subdirectory
2. tests that test lily will be in the [lily](../../src/test/lily) subdirectory
3. all tests should return a `u8` as a status code.
3. all tests should return `u8`, `void`, or `bool` as a status code.
> follow standard status code practices for value.<br>
> `0` for success, `1` for error, etc
4. all tests should contain the test specification at the top of the file
@ -13,6 +13,8 @@ tests are written in [src/test](../../src/test/)
- `args` are given to the application executable
- `exit` is the exit status of the program (return code)
5. if test compilation fails, the test will be considered failed
6. tests for lily should try to limit the number of unrelated structures/functions tested.
7. tests for lang should be headless (not rely on lily at all)
the following are all of the (currently) supported test arguments:
```rust

View file

@ -2,18 +2,36 @@
* exit: 0
*/
$unix_timestamp := fn(year: uint, month: u8, day: u8, hour: u8, minute: u8, second: u8): uint {
sum_days := .[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]
unix_timestamp_secs_lookup_table := fn(year: uint, month: uint, day: uint, hour: uint, minute: uint, second: uint): uint {
is_leap := year % 4 == 0 & (year % 100 != 0 | year % 400 == 0)
days_since_epoch := (year - 1970) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 - 477
idx := month - 1
sum_nonleap := (idx < 2) * idx * 31 + (idx >= 2) * (59 + (153 * (idx - 2) + 2) / 5)
total_days := days_since_epoch + day + sum_nonleap + is_leap * (month > 2)
days_since_epoch := year * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 - 719527
total_days := days_since_epoch + day + sum_days[month - 1] + is_leap * (month > 2) - 1;
return total_days * 86400 + hour * 3600 + minute * 60 + second
}
main := fn(): u8 {
r0 := unix_timestamp(2025, 1, 31, 21, 53, 26) != 1738360406
r1 := unix_timestamp(1970, 1, 1, 0, 0, 0) != 0
r2 := unix_timestamp(2038, 1, 19, 3, 14, 8) != 1 << 31
return r0 | r1 | r2
unix_timestamp_secs := fn(year: uint, month: uint, day: uint, hour: uint, minute: uint, second: uint): uint {
is_leap := year % 4 == 0 & (year % 100 != 0 | year % 400 == 0)
days_since_epoch := year * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 - 719527
// relies on underflowing in (153 * month - 162) / 5 when month = 1
sum_nonleap := (month < 3) * (month - 1) * 31 + (month >= 3) * (153 * month - 162) / 5
total_days := days_since_epoch + day + sum_nonleap + is_leap * (month > 2) - 1
return total_days * 86400 + hour * 3600 + minute * 60 + second
}
main := fn(): bool {
// random date
r := unix_timestamp_secs(2025, 1, 31, 21, 53, 26) != 1738360406
// unix epoch
r |= unix_timestamp_secs(1970, 1, 1, 0, 0, 0) != 0
// y2k38
r |= unix_timestamp_secs(2038, 1, 19, 3, 14, 8) != 1 << 31
// end of year
r |= unix_timestamp_secs(1970, 12, 31, 23, 59, 59) != 31535999
// this doesnt pass: inlining here gives incorrect value.
r |= @inline(unix_timestamp_secs, 2025, 1, 31, 21, 53, 26) != unix_timestamp_secs(2025, 1, 31, 21, 53, 26)
r |= unix_timestamp_secs_lookup_table(2025, 1, 31, 21, 53, 26) != unix_timestamp_secs(2025, 1, 31, 21, 53, 26)
r |= @inline(unix_timestamp_secs_lookup_table, 2025, 1, 31, 21, 53, 26) != unix_timestamp_secs(2025, 1, 31, 21, 53, 26)
return r
}

View file

@ -4,26 +4,14 @@
lily := @use("../../lily/lib.hb")
main := fn(argc: int, argv: [][]u8): u8 {
alloc := lily.alloc.ArenaAllocator.new()
defer alloc.deinit()
vec := lily.collections.Vec(u8, lily.alloc.ArenaAllocator).new(&alloc)
vec.push(69)
vec.push(69)
vec.push(69)
vec2 := lily.collections.Vec(u8, lily.alloc.ArenaAllocator).new(&alloc)
vec2.push(69)
vec2.push(69)
vec2.push(69)
vec.push(69)
vec.push(69)
vec.push(69)
ptr_one := alloc.alloc(u8, 6)
ptr_two := alloc.alloc(u8, 18)
allocator := lily.alloc.ArenaAllocator.new()
defer allocator.deinit()
b := allocator.alloc(u8, 100)
if b == null return 1
c := allocator.alloc(u8, 100)
if c == null return 1
d := allocator.realloc(u8, c, 100)
if d == null return 1
if d != c return 1
return 0
}
}