[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)
This commit is contained in:
Bill Currie 2022-01-14 22:42:56 +09:00
parent f4eeed36b7
commit c8362c28fe
4 changed files with 98 additions and 0 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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

View File

@ -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"