From 5234fc73a882a1406fb61e71492529a4defc9ac2 Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Mon, 23 Jan 2023 21:36:44 +0100 Subject: [PATCH] testcases for instructions INSERT, INSERT2, SELECT and ASS --- examples/http_server/parse.noja | 2 +- src/lib/assembler/assemble.c | 2 +- src/lib/run.c | 11 +++---- src/lib/runtime.h | 2 -- tests/runtime/ass/ass_00.noja-test | 17 ++++++++++ tests/runtime/ass/ass_01.noja-test | 28 ++++++++++++++++ .../runtime/insert/insert_into_list.noja-test | 24 ++++++++++++++ ...ert_into_list_more_than_capacity.noja-test | 24 ++++++++++++++ .../runtime/insert/insert_into_map.noja-test | 25 +++++++++++++++ ...sert_into_map_more_than_capacity.noja-test | 25 +++++++++++++++ .../insert2/insert2_into_list.noja-test | 31 ++++++++++++++++++ ...rt2_into_list_more_than_capacity.noja-test | 31 ++++++++++++++++++ .../insert2/insert2_into_map.noja-test | 32 +++++++++++++++++++ ...ert2_into_map_more_than_capacity.noja-test | 32 +++++++++++++++++++ .../runtime/select/select_from_list.noja-test | 27 ++++++++++++++++ .../runtime/select/select_from_map.noja-test | 28 ++++++++++++++++ .../select/select_from_map_2.noja-test | 28 ++++++++++++++++ 17 files changed, 359 insertions(+), 10 deletions(-) create mode 100644 tests/runtime/ass/ass_00.noja-test create mode 100644 tests/runtime/ass/ass_01.noja-test create mode 100644 tests/runtime/insert/insert_into_list.noja-test create mode 100644 tests/runtime/insert/insert_into_list_more_than_capacity.noja-test create mode 100644 tests/runtime/insert/insert_into_map.noja-test create mode 100644 tests/runtime/insert/insert_into_map_more_than_capacity.noja-test create mode 100644 tests/runtime/insert2/insert2_into_list.noja-test create mode 100644 tests/runtime/insert2/insert2_into_list_more_than_capacity.noja-test create mode 100644 tests/runtime/insert2/insert2_into_map.noja-test create mode 100644 tests/runtime/insert2/insert2_into_map_more_than_capacity.noja-test create mode 100644 tests/runtime/select/select_from_list.noja-test create mode 100644 tests/runtime/select/select_from_map.noja-test create mode 100644 tests/runtime/select/select_from_map_2.noja-test diff --git a/examples/http_server/parse.noja b/examples/http_server/parse.noja index 82ba40f..518063d 100644 --- a/examples/http_server/parse.noja +++ b/examples/http_server/parse.noja @@ -312,7 +312,7 @@ fun parseHead(src: String) { return none, "Invalid header (missing \":\" after name)"; scan->consume(); char = scan->consumeSpaces(); - + # Header body until \r body = ""; do { diff --git a/src/lib/assembler/assemble.c b/src/lib/assembler/assemble.c index ae94c75..f373e7f 100644 --- a/src/lib/assembler/assemble.c +++ b/src/lib/assembler/assemble.c @@ -463,7 +463,7 @@ Executable *assemble(Source *src, Error *error, int *error_offset) goto done; } } - + size_t unresolved_count = LabelList_GetUnresolvedCount(list); if(unresolved_count > 0) { *error_offset = -1; diff --git a/src/lib/run.c b/src/lib/run.c index 6e487b0..32ba557 100644 --- a/src/lib/run.c +++ b/src/lib/run.c @@ -497,8 +497,7 @@ static _Bool runInstruction(Runtime *runtime, Error *error) ASSERT(runtime != NULL); ASSERT(error->occurred == 0); - Stack *stack = Runtime_GetStack(runtime); - Heap *heap = Runtime_GetHeap(runtime); + Heap *heap = Runtime_GetHeap(runtime); Executable *exe = Runtime_GetCurrentExecutable(runtime); ASSERT(exe != NULL); int index = Runtime_GetCurrentIndex(runtime); @@ -855,14 +854,14 @@ static _Bool runInstruction(Runtime *runtime, Error *error) return Object_Insert(col, key, val, heap, error); } - + case OPCODE_INSERT2: { ASSERT(opc == 0); - Object *val = Stack_Top(stack, -2); - Object *col = Stack_Top(stack, -1); - Object *key = Stack_Top(stack, 0); + Object *val = Runtime_Top(runtime, -2); + Object *col = Runtime_Top(runtime, -1); + Object *key = Runtime_Top(runtime, 0); if (val == NULL || col == NULL || key == NULL) { Error_Report(error, ErrorType_INTERNAL, "Frame has not enough values on the stack to run INSERT2 instruction"); return 0; diff --git a/src/lib/runtime.h b/src/lib/runtime.h index eeb1d56..bac78ba 100644 --- a/src/lib/runtime.h +++ b/src/lib/runtime.h @@ -35,7 +35,6 @@ #include "timing.h" #include "executable.h" #include "utils/error.h" -#include "utils/stack.h" #include "objects/objects.h" typedef struct xRuntime Runtime; @@ -90,7 +89,6 @@ bool Runtime_CollectGarbage(Runtime *runtime, Error *error); void Runtime_PrintStackTrace(Runtime *runtime, FILE *stream); void Runtime_Interrupt(Runtime *runtime); Heap* Runtime_GetHeap(Runtime *runtime); -Stack* Runtime_GetStack(Runtime *runtime); int Runtime_GetCurrentIndex(Runtime *runtime); Executable *Runtime_GetCurrentExecutable(Runtime *runtime); Executable *Runtime_GetMostRecentExecutable(Runtime *runtime); diff --git a/tests/runtime/ass/ass_00.noja-test b/tests/runtime/ass/ass_00.noja-test new file mode 100644 index 0000000..ff2ca1e --- /dev/null +++ b/tests/runtime/ass/ass_00.noja-test @@ -0,0 +1,17 @@ +@type [runtime] + +@bytecode + + PUSHNNE; + ASS "xyz"; + POP 1; + + PUSHVAR "xyz"; + + PUSHVAR "print"; + CALL 1, 1; + POP 1; + + EXIT; + +@output [none] \ No newline at end of file diff --git a/tests/runtime/ass/ass_01.noja-test b/tests/runtime/ass/ass_01.noja-test new file mode 100644 index 0000000..db85cbd --- /dev/null +++ b/tests/runtime/ass/ass_01.noja-test @@ -0,0 +1,28 @@ +@type [runtime] + +@bytecode + + PUSHNNE; + ASS "xyz"; + POP 1; + PUSHVAR "xyz"; + PUSHVAR "print"; + CALL 1, 1; + POP 1; + + PUSHSTR "/"; + PUSHVAR "print"; + CALL 1, 1; + POP 1; + + PUSHINT 99; + ASS "xyz"; + POP 1; + PUSHVAR "xyz"; + PUSHVAR "print"; + CALL 1, 1; + POP 1; + + EXIT; + +@output [none/99] \ No newline at end of file diff --git a/tests/runtime/insert/insert_into_list.noja-test b/tests/runtime/insert/insert_into_list.noja-test new file mode 100644 index 0000000..d6947cf --- /dev/null +++ b/tests/runtime/insert/insert_into_list.noja-test @@ -0,0 +1,24 @@ +@type [runtime] + +@bytecode + + PUSHLST 3; + + PUSHINT 0; + PUSHSTR "A"; + INSERT; + + PUSHINT 1; + PUSHSTR "B"; + INSERT; + + PUSHINT 2; + PUSHSTR "C"; + INSERT; + + PUSHVAR "print"; + CALL 1, 1; + POP 1; + EXIT; + +@output {[A, B, C]} \ No newline at end of file diff --git a/tests/runtime/insert/insert_into_list_more_than_capacity.noja-test b/tests/runtime/insert/insert_into_list_more_than_capacity.noja-test new file mode 100644 index 0000000..2f0cd39 --- /dev/null +++ b/tests/runtime/insert/insert_into_list_more_than_capacity.noja-test @@ -0,0 +1,24 @@ +@type [runtime] + +@bytecode + + PUSHLST 2; + + PUSHINT 0; + PUSHSTR "A"; + INSERT; + + PUSHINT 1; + PUSHSTR "B"; + INSERT; + + PUSHINT 2; + PUSHSTR "C"; + INSERT; + + PUSHVAR "print"; + CALL 1, 1; + POP 1; + EXIT; + +@output {[A, B, C]} \ No newline at end of file diff --git a/tests/runtime/insert/insert_into_map.noja-test b/tests/runtime/insert/insert_into_map.noja-test new file mode 100644 index 0000000..d95d5cd --- /dev/null +++ b/tests/runtime/insert/insert_into_map.noja-test @@ -0,0 +1,25 @@ +@type [runtime] + +@bytecode + + PUSHMAP 3; + + PUSHINT 0; + PUSHSTR "A"; + INSERT; + + PUSHINT 1; + PUSHSTR "B"; + INSERT; + + PUSHINT 2; + PUSHSTR "C"; + INSERT; + + PUSHVAR "print"; + CALL 1, 1; + POP 1; + + EXIT; + +@output [{0: A, 1: B, 2: C}] \ No newline at end of file diff --git a/tests/runtime/insert/insert_into_map_more_than_capacity.noja-test b/tests/runtime/insert/insert_into_map_more_than_capacity.noja-test new file mode 100644 index 0000000..e766cc1 --- /dev/null +++ b/tests/runtime/insert/insert_into_map_more_than_capacity.noja-test @@ -0,0 +1,25 @@ +@type [runtime] + +@bytecode + + PUSHMAP 2; + + PUSHINT 0; + PUSHSTR "A"; + INSERT; + + PUSHINT 1; + PUSHSTR "B"; + INSERT; + + PUSHINT 2; + PUSHSTR "C"; + INSERT; + + PUSHVAR "print"; + CALL 1, 1; + POP 1; + + EXIT; + +@output [{0: A, 1: B, 2: C}] \ No newline at end of file diff --git a/tests/runtime/insert2/insert2_into_list.noja-test b/tests/runtime/insert2/insert2_into_list.noja-test new file mode 100644 index 0000000..cf63b39 --- /dev/null +++ b/tests/runtime/insert2/insert2_into_list.noja-test @@ -0,0 +1,31 @@ +@type [runtime] + +@bytecode + + PUSHLST 3; + ASS "list"; + POP 1; + + PUSHSTR "A"; + PUSHVAR "list"; + PUSHINT 0; + INSERT2; + + PUSHSTR "B"; + PUSHVAR "list"; + PUSHINT 1; + INSERT2; + + PUSHSTR "C"; + PUSHVAR "list"; + PUSHINT 2; + INSERT2; + + PUSHVAR "list"; + + PUSHVAR "print"; + CALL 4, 1; + POP 1; + EXIT; + +@output {[A, B, C]CBA} \ No newline at end of file diff --git a/tests/runtime/insert2/insert2_into_list_more_than_capacity.noja-test b/tests/runtime/insert2/insert2_into_list_more_than_capacity.noja-test new file mode 100644 index 0000000..32d9e26 --- /dev/null +++ b/tests/runtime/insert2/insert2_into_list_more_than_capacity.noja-test @@ -0,0 +1,31 @@ +@type [runtime] + +@bytecode + + PUSHLST 2; + ASS "list"; + POP 1; + + PUSHSTR "A"; + PUSHVAR "list"; + PUSHINT 0; + INSERT2; + + PUSHSTR "B"; + PUSHVAR "list"; + PUSHINT 1; + INSERT2; + + PUSHSTR "C"; + PUSHVAR "list"; + PUSHINT 2; + INSERT2; + + PUSHVAR "list"; + + PUSHVAR "print"; + CALL 4, 1; + POP 1; + EXIT; + +@output {[A, B, C]CBA} \ No newline at end of file diff --git a/tests/runtime/insert2/insert2_into_map.noja-test b/tests/runtime/insert2/insert2_into_map.noja-test new file mode 100644 index 0000000..9928e71 --- /dev/null +++ b/tests/runtime/insert2/insert2_into_map.noja-test @@ -0,0 +1,32 @@ +@type [runtime] + +@bytecode + + PUSHMAP 3; + ASS "map"; + POP 1; + + PUSHSTR "A"; + PUSHVAR "map"; + PUSHINT 0; + INSERT2; + + PUSHSTR "B"; + PUSHVAR "map"; + PUSHINT 1; + INSERT2; + + PUSHSTR "C"; + PUSHVAR "map"; + PUSHINT 2; + INSERT2; + + PUSHVAR "map"; + + PUSHVAR "print"; + CALL 4, 1; + POP 1; + + EXIT; + +@output [{0: A, 1: B, 2: C}CBA] \ No newline at end of file diff --git a/tests/runtime/insert2/insert2_into_map_more_than_capacity.noja-test b/tests/runtime/insert2/insert2_into_map_more_than_capacity.noja-test new file mode 100644 index 0000000..411f05f --- /dev/null +++ b/tests/runtime/insert2/insert2_into_map_more_than_capacity.noja-test @@ -0,0 +1,32 @@ +@type [runtime] + +@bytecode + + PUSHMAP 2; + ASS "map"; + POP 1; + + PUSHSTR "A"; + PUSHVAR "map"; + PUSHINT 0; + INSERT2; + + PUSHSTR "B"; + PUSHVAR "map"; + PUSHINT 1; + INSERT2; + + PUSHSTR "C"; + PUSHVAR "map"; + PUSHINT 2; + INSERT2; + + PUSHVAR "map"; + + PUSHVAR "print"; + CALL 4, 1; + POP 1; + + EXIT; + +@output [{0: A, 1: B, 2: C}CBA] \ No newline at end of file diff --git a/tests/runtime/select/select_from_list.noja-test b/tests/runtime/select/select_from_list.noja-test new file mode 100644 index 0000000..04a8715 --- /dev/null +++ b/tests/runtime/select/select_from_list.noja-test @@ -0,0 +1,27 @@ +@type [runtime] + +@bytecode + + PUSHLST 3; + + PUSHINT 0; + PUSHSTR "A"; + INSERT; + + PUSHINT 1; + PUSHSTR "B"; + INSERT; + + PUSHINT 2; + PUSHSTR "C"; + INSERT; + + PUSHINT 1; + SELECT; + + PUSHVAR "print"; + CALL 1, 1; + POP 1; + EXIT; + +@output [B] \ No newline at end of file diff --git a/tests/runtime/select/select_from_map.noja-test b/tests/runtime/select/select_from_map.noja-test new file mode 100644 index 0000000..fc18eb6 --- /dev/null +++ b/tests/runtime/select/select_from_map.noja-test @@ -0,0 +1,28 @@ +@type [runtime] + +@bytecode + + PUSHMAP 3; + + PUSHINT 0; + PUSHSTR "A"; + INSERT; + + PUSHINT 1; + PUSHSTR "B"; + INSERT; + + PUSHINT 2; + PUSHSTR "C"; + INSERT; + + PUSHINT 1; + SELECT; + + PUSHVAR "print"; + CALL 1, 1; + POP 1; + + EXIT; + +@output [B] \ No newline at end of file diff --git a/tests/runtime/select/select_from_map_2.noja-test b/tests/runtime/select/select_from_map_2.noja-test new file mode 100644 index 0000000..484bd71 --- /dev/null +++ b/tests/runtime/select/select_from_map_2.noja-test @@ -0,0 +1,28 @@ +@type [runtime] + +@bytecode + + PUSHMAP 3; + + PUSHINT 0; + PUSHSTR "A"; + INSERT; + + PUSHINT 1; + PUSHSTR "B"; + INSERT; + + PUSHINT 2; + PUSHSTR "C"; + INSERT; + + PUSHINT 8; + SELECT; + + PUSHVAR "print"; + CALL 1, 1; + POP 1; + + EXIT; + +@output [none] \ No newline at end of file