From 5ac84ed230234599cf979e13ea80c97ede21a769 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Mon, 25 Nov 2024 08:05:12 +0100 Subject: [PATCH] Added more tests for better coverage --- CMakeLists.txt | 4 +- test/garbage_shenanigans.c | 123 +++++++++++++++++++++++++++++++++ test/stacktrace.c | 136 +++++++++++++++++++++++++++++++++++++ 3 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 test/garbage_shenanigans.c create mode 100644 test/stacktrace.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c662c7..c6a5755 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.14) project(ink C) include(CTest) @@ -41,6 +41,8 @@ endfunction() add_success_compiled_test(sequence_of_20s "[20]+") add_success_compiled_test(macro_optimized "1000") add_success_compiled_test(array_shenanigans "Hello World\n\\[ 104 2 9 9 12 64 119 12 15 9 1 42 \\]\n") +add_success_compiled_test(garbage_shenanigans "[Hello World\n\\[ 104 2 9 9 12 64 119 12 15 9 1 42 \\]\n]*") +add_success_compiled_test(stacktrace "\\_\\_-MAIN-\\_\\_.*\nbeatrice.*\ngolden.*\nwitch.*\nsys\\.trace\n.*23\n.*16\n.*15\n.*8\n.*4") if(MSVC) target_compile_options(ink PRIVATE /W4 /WX) diff --git a/test/garbage_shenanigans.c b/test/garbage_shenanigans.c new file mode 100644 index 0000000..5d9bf9d --- /dev/null +++ b/test/garbage_shenanigans.c @@ -0,0 +1,123 @@ +#include "ink.h" + +int main(int argc, char** argv) { + struct context* ctx; + ctx = ink_make_default_context(); + + ink_compile( + ctx, + "fn encrypt do\n" + " 3 pluck array.size\n" + " # array add_key modulo_key index\n" + " loop:\n" + " 1 - dup 5 pluck\n" + " # array add_key modulo_key index index array\n" + " array.index \n" + " # array add_key modulo_key index v\n" + " 4 pluck +\n" + " # array add_key modulo_key index (v + add_key)\n" + " 3 pluck %\n" + " # array add_key modulo_key index ((v + add_key) % modulo_key)\n" + " 2 pluck\n" + " # array add_key modulo_key index ((v + add_key) % modulo_key) index\n" + " 6 pluck\n" + " # array add_key modulo_key index ((v + add_key) % modulo_key) index array\n" + " array.set\n" + " # array add_key modulo_key index\n" + " dup 0 != loop jump_if drop drop drop drop\n" + "end\n" + "fn string.dump do\n" + " dup array.size 0\n" + " # array end it\n" + " 91 print_utf8\n" + " 32 print_utf8\n" + " loop:\n" + " dup\n" + " # array end it it\n" + " 4 pluck\n" + " # array end it it array\n" + " array.index print_int\n" + " 32 print_utf8\n" + " 1 +\n" + " # array end it\n" + " 2 pluck 2 pluck > loop jump_if\n" + " # array end it\n" + " 93 print_utf8\n" + " drop drop drop\n" + "end\n" + "fn string.print do\n" + " dup array.size 0\n" + " # array end it\n" + " loop:\n" + " dup\n" + " # array end it it\n" + " 4 pluck\n" + " # array end it it array\n" + " array.index print_utf8\n" + " 1 +\n" + " # array end it\n" + " 2 pluck 2 pluck > loop jump_if\n" + " # array end it\n" + " drop drop drop\n" + "end" + ); + + ink_compile( + ctx, + "# Clones an array, creating a new array\n" + "#\n" + "# @param array The array to clone into a new array\n" + "# @return a new array that contains the same elements as the source array\n" + "#\n" + "# array -> new_array\n" + "fn array.clone do\n" + " array.new 2 pluck array.size 0\n" + " # array new_array end it\n" + " 2 pluck 2 pluck == l jump_if\n" + " # array new_array end it\n" + " loop:\n" + " dup 5 pluck\n" + " # array new_array end it it array\n" + " array.index 4 pluck\n" + " # array new_array end it v new_array\n" + " array.push\n" + " # array new_array end it\n" + " 1 +\n" + " 2 pluck 2 pluck > loop jump_if\n" + " l: drop drop swap drop\n" + " # new_array\n" + "end" + ); + + ink_compile( + ctx, + "[ 72 101 108 108 111 32 87 111 114 108 100 10 ] dup dup dup string.print\n" + "32 131 encrypt string.dump\n" + "[ 72 101 108 108 111 32 87 111 114 108 100 10 ] dup dup dup string.print\n" + "32 131 encrypt string.dump\n" + "[ 72 101 108 108 111 32 87 111 114 108 100 10 ] dup dup dup string.print\n" + "32 131 encrypt string.dump\n" + "[ 72 101 108 108 111 32 87 111 114 108 100 10 ] dup dup dup string.print\n" + "32 131 encrypt string.dump\n" + "[ 72 101 108 108 111 32 87 111 114 108 100 10 ] dup dup dup string.print\n" + "32 131 encrypt string.dump\n" + ); + + //ctx->routines[coro].panic = 0; + + int increment = 16; + int counter = increment; + int c; + + while(ink_can_run(ctx)) { + ink_step_everyone(ctx); + + + if(ctx->steps > counter) { + ink_gc(ctx); + counter += increment; + } + } + + //return ctx->routines[coro].panic != INK_ROUTINE_SUCCESS && ctx->routines[coro].panic != INK_ROUTINE_CAN_REUSE; +} \ No newline at end of file diff --git a/test/stacktrace.c b/test/stacktrace.c new file mode 100644 index 0000000..356b261 --- /dev/null +++ b/test/stacktrace.c @@ -0,0 +1,136 @@ +#include "ink.h" + +int main(int argc, char** argv) { + struct context* ctx; + ctx = ink_make_default_context(); + + ink_compile( + ctx, + "fn encrypt do\n" + " 3 pluck array.size\n" + " # array add_key modulo_key index\n" + " loop:\n" + " 1 - dup 5 pluck\n" + " # array add_key modulo_key index index array\n" + " array.index \n" + " # array add_key modulo_key index v\n" + " 4 pluck +\n" + " # array add_key modulo_key index (v + add_key)\n" + " 3 pluck %\n" + " # array add_key modulo_key index ((v + add_key) % modulo_key)\n" + " 2 pluck\n" + " # array add_key modulo_key index ((v + add_key) % modulo_key) index\n" + " 6 pluck\n" + " # array add_key modulo_key index ((v + add_key) % modulo_key) index array\n" + " array.set\n" + " # array add_key modulo_key index\n" + " dup 0 != loop jump_if drop drop drop drop\n" + "end\n" + "fn string.dump do\n" + " dup array.size 0\n" + " # array end it\n" + " 91 print_utf8\n" + " 32 print_utf8\n" + " loop:\n" + " dup\n" + " # array end it it\n" + " 4 pluck\n" + " # array end it it array\n" + " array.index print_int\n" + " 32 print_utf8\n" + " 1 +\n" + " # array end it\n" + " 2 pluck 2 pluck > loop jump_if\n" + " # array end it\n" + " 93 print_utf8\n" + " drop drop drop\n" + "end\n" + "fn string.print do\n" + " dup array.size 0\n" + " # array end it\n" + " loop:\n" + " dup\n" + " # array end it it\n" + " 4 pluck\n" + " # array end it it array\n" + " array.index print_utf8\n" + " 1 +\n" + " # array end it\n" + " 2 pluck 2 pluck > loop jump_if\n" + " # array end it\n" + " drop drop drop\n" + "end" + ); + + ink_compile( + ctx, + "# Clones an array, creating a new array\n" + "#\n" + "# @param array The array to clone into a new array\n" + "# @return a new array that contains the same elements as the source array\n" + "#\n" + "# array -> new_array\n" + "fn array.clone do\n" + " array.new 2 pluck array.size 0\n" + " # array new_array end it\n" + " 2 pluck 2 pluck == l jump_if\n" + " # array new_array end it\n" + " loop:\n" + " dup 5 pluck\n" + " # array new_array end it it array\n" + " array.index 4 pluck\n" + " # array new_array end it v new_array\n" + " array.push\n" + " # array new_array end it\n" + " 1 +\n" + " 2 pluck 2 pluck > loop jump_if\n" + " l: drop drop swap drop\n" + " # new_array\n" + "end" + ); + + ink_compile( + ctx, + "fn witch do\n" + " drop drop drop sys.trace\n" + "end\n" + ); + + ink_compile( + ctx, + "fn golden do\n" + " 42 64190 witch\n" + "end\n" + ); + + ink_compile( + ctx, + "fn beatrice do\n" + " 10 golden\n" + "end\n" + ); + + ink_compile( + ctx, + "beatrice 4 8 15 16 23 stack.dump\n" + ); + + //ctx->routines[coro].panic = 0; + + int increment = 1 << 16; + int counter = increment; + int c; + + while(ink_can_run(ctx)) { + for(c = 0; c < 64; ++c) + ink_step_everyone(ctx); + + + if(ctx->steps < counter) { + ink_gc(ctx); + counter += increment; + } + } + + //return ctx->routines[coro].panic != INK_ROUTINE_SUCCESS && ctx->routines[coro].panic != INK_ROUTINE_CAN_REUSE; +} \ No newline at end of file