From c8362c28fec60038246b1be24d19c65732695779 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 14 Jan 2022 22:42:56 +0900 Subject: [PATCH] [gamecode] Add tests for string ops Does not include string concatenation because I don't feel like messing with zone init, but all the other operators are tested (currently failing due to bool convention) --- libs/gamecode/test/Makemodule.am | 6 +++ libs/gamecode/test/head.c | 2 + libs/gamecode/test/main.c | 8 ++++ libs/gamecode/test/test-string.c | 82 ++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 libs/gamecode/test/test-string.c diff --git a/libs/gamecode/test/Makemodule.am b/libs/gamecode/test/Makemodule.am index 8bb1e3772..ba3bc174a 100644 --- a/libs/gamecode/test/Makemodule.am +++ b/libs/gamecode/test/Makemodule.am @@ -15,6 +15,7 @@ libs_gamecode_tests = \ libs/gamecode/test/test-scale \ libs/gamecode/test/test-stack \ libs/gamecode/test/test-store \ + libs/gamecode/test/test-string \ libs/gamecode/test/test-unsigned \ libs/gamecode/test/test-vector @@ -108,6 +109,11 @@ libs_gamecode_test_test_store_SOURCES= \ libs_gamecode_test_test_store_LDADD= $(test_gamecode_libs) libs_gamecode_test_test_store_DEPENDENCIES= $(test_gamecode_libs) +libs_gamecode_test_test_string_SOURCES= \ + libs/gamecode/test/test-string.c +libs_gamecode_test_test_string_LDADD= $(test_gamecode_libs) +libs_gamecode_test_test_string_DEPENDENCIES= $(test_gamecode_libs) + libs_gamecode_test_test_vector_SOURCES= \ libs/gamecode/test/test-vector.c libs_gamecode_test_test_vector_LDADD= $(test_gamecode_libs) diff --git a/libs/gamecode/test/head.c b/libs/gamecode/test/head.c index 36985c821..478a74f09 100644 --- a/libs/gamecode/test/head.c +++ b/libs/gamecode/test/head.c @@ -34,4 +34,6 @@ typedef struct { dstatement_t *statements; pr_int_t *init_globals; pr_int_t *expect_globals; + const char *strings; + pr_uint_t string_size; } test_t; diff --git a/libs/gamecode/test/main.c b/libs/gamecode/test/main.c index 0579463b2..8f433225c 100644 --- a/libs/gamecode/test/main.c +++ b/libs/gamecode/test/main.c @@ -44,10 +44,15 @@ test_debug_handler (prdebug_t event, void *param, void *data) case prd_trace: dstatement_t *st = test_pr.pr_statements + test_pr.pr_xstatement; if (verbose > 1) { + printf ("---\n"); printf ("debug: trace %05x %04x %04x %04x %04x%s\n", test_pr.pr_xstatement, st->op, st->a, st->b, st->c, pr->globals.stack ? va (0, " %05x", *pr->globals.stack) : ""); + printf (" %04x %04x %04x\n", + st->a + PR_BASE (pr, st, A), + st->b + PR_BASE (pr, st, B), + st->c + PR_BASE (pr, st, C)); } if (verbose > 0) { PR_PrintStatement (&test_pr, st, 0); @@ -111,6 +116,9 @@ setup_test (test_t *test) (test->num_statements + 1) * sizeof (dstatement_t)); test_pr.pr_statements[test->num_statements] = (dstatement_t) { OP_BREAK, 0, 0, 0 }; + + test_pr.pr_strings = (char *) test->strings; + test_pr.pr_stringsize = test->string_size; } static int diff --git a/libs/gamecode/test/test-string.c b/libs/gamecode/test/test-string.c new file mode 100644 index 000000000..4e2e7faad --- /dev/null +++ b/libs/gamecode/test/test-string.c @@ -0,0 +1,82 @@ +#include "head.c" + +const char test_strings[] = + "\0" + "abc\0" + "def\0" + "abc\0"; + +static pr_int_t string_globals_init[] = { + 0, 1, 5, 9, // string pointers + 0, 0, 0, 0, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, +}; + +static pr_int_t string_globals_expect[] = { + 0, 1, 5, 9, // string pointers + 0, 0, 8, 0, + +// "\0" "abc" "def" "abc" + -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, -1, 0, 0, -1, 0, -1, // eq + 0, -1, -1, -1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, // lt + 0, 0, 0, 0, -1, 0, 0, 0, -1, -1, 0, -1, -1, 0, 0, 0, // gt + 0,-97,-100,-97, 97, 0, -3, 0, 100, 3, 0, 3, 97, 0, -3, 0, // cmp + -1, 0, 0, 0, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, 0, -1, // ge + -1, -1, -1, -1, 0, -1, -1, -1, 0, 0, -1, 0, 0, -1, -1, -1, // le + -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // not +}; + +static dstatement_t string_statements[] = { + { OP(0, 0, 0, OP_LEA_A), 24, 0, 6 }, // init k +// for (i = 4; i-- > 0; ) { + { OP(0, 0, 0, OP_LEA_A), 4, 0, 4 }, + { OP(0, 0, 0, OP_IFA_A), 2, 0, 4 }, + { OP(0, 0, 0, OP_BREAK), 0, 0, 0 }, + { OP(0, 0, 0, OP_LEA_C), 4, -1, 4 }, // dec i + + { OP(0, 0, 0, OP_WITH), 4, 4, 1 }, // load i + +// for (j = 4; j-- > 0; ) { + { OP(0, 0, 0, OP_LEA_A), 4, 0, 5 }, // init j + + { OP(0, 0, 0, OP_IFA_A), 2, 0, 5 }, + { OP(0, 0, 0, OP_JUMP_A), -6, 0, 0 }, + { OP(0, 0, 0, OP_LEA_C), 5, -1, 5 }, // dec j + + { OP(0, 0, 0, OP_WITH), 4, 5, 2 }, // load j + + { OP(0, 0, 0, OP_LEA_C), 6, -1, 6 }, // dec k + { OP(0, 0, 0, OP_WITH), 4, 6, 3 }, // load k + + // i j k + { OP(1, 2, 3, OP_EQ_S), 0, 0, 0 }, + { OP(1, 2, 3, OP_LT_S), 0, 0, 16 }, + { OP(1, 2, 3, OP_GT_S), 0, 0, 32 }, + { OP(1, 2, 3, OP_CMP_S), 0, 0, 48 }, + { OP(1, 2, 3, OP_GE_S), 0, 0, 64 }, + { OP(1, 2, 3, OP_LE_S), 0, 0, 80 }, + { OP(1, 2, 3, OP_NOT_S), 0, 0, 96 }, + + { OP(0, 0, 0, OP_JUMP_A), -13, 0, 0 }, +}; + +test_t tests[] = { + { + .desc = "string", + .num_globals = num_globals (string_globals_init, string_globals_expect), + .num_statements = num_statements (string_statements), + .statements = string_statements, + .init_globals = string_globals_init, + .expect_globals = string_globals_expect, + .strings = test_strings, + .string_size = sizeof (test_strings), + }, +}; + +#include "main.c"