From 664006b1c42aae84a3c749d9b71c1047e0b8ffcf Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 2 Mar 2023 20:03:52 -0800 Subject: Initial commit. --- vm/test/test.h | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ vm/test/vm_test.c | 182 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 367 insertions(+) create mode 100644 vm/test/test.h create mode 100644 vm/test/vm_test.c (limited to 'vm/test') diff --git a/vm/test/test.h b/vm/test/test.h new file mode 100644 index 0000000..fd8dc22 --- /dev/null +++ b/vm/test/test.h @@ -0,0 +1,185 @@ +// SPDX-License-Identifier: MIT +#pragma once + +#ifdef UNIT_TEST + +#include +#include +#include +#include + +#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ + defined(__NetBSD__) || defined(__OpenBSD__) +#define USE_SYSCTL_FOR_ARGS 1 +// clang-format off +#include +#include +// clang-format on +#include // getpid +#endif + +struct test_file_metadata; + +struct test_failure { + bool present; + const char *message; + const char *file; + int line; +}; + +struct test_case_metadata { + void (*fn)(struct test_case_metadata *, struct test_file_metadata *); + struct test_failure failure; + const char *name; + struct test_case_metadata *next; +}; + +struct test_file_metadata { + bool registered; + const char *name; + struct test_file_metadata *next; + struct test_case_metadata *tests; +}; + +struct test_file_metadata __attribute__((weak)) * test_file_head; + +#define SET_FAILURE(_message) \ + metadata->failure = (struct test_failure) { \ + .message = _message, .file = __FILE__, .line = __LINE__, .present = true, \ + } + +#define TEST_EQUAL(a, b) \ + do { \ + if ((a) != (b)) { \ + SET_FAILURE(#a " != " #b); \ + return; \ + } \ + } while (0) + +#define TEST_TRUE(a) \ + do { \ + if (!(a)) { \ + SET_FAILURE(#a " is not true"); \ + return; \ + } \ + } while (0) + +#define TEST_STREQUAL(a, b) \ + do { \ + if (strcmp(a, b) != 0) { \ + SET_FAILURE(#a " != " #b); \ + return; \ + } \ + } while (0) + +#define TEST_CASE(_name) \ + static void __test_h_##_name(struct test_case_metadata *, \ + struct test_file_metadata *); \ + static struct test_file_metadata __test_h_file; \ + static struct test_case_metadata __test_h_meta_##_name = { \ + .name = #_name, \ + .fn = __test_h_##_name, \ + }; \ + static void __attribute__((constructor(101))) __test_h_##_name##_register(void) { \ + __test_h_meta_##_name.next = __test_h_file.tests; \ + __test_h_file.tests = &__test_h_meta_##_name; \ + if (!__test_h_file.registered) { \ + __test_h_file.name = __FILE__; \ + __test_h_file.next = test_file_head; \ + test_file_head = &__test_h_file; \ + __test_h_file.registered = true; \ + } \ + } \ + static void __test_h_##_name( \ + struct test_case_metadata *metadata __attribute__((unused)), \ + struct test_file_metadata *file_metadata __attribute__((unused))) + +extern void __attribute__((weak)) (*test_h_unittest_setup)(void); +/// Run defined tests, return true if all tests succeeds +/// @param[out] tests_run if not NULL, set to whether tests were run +static inline void __attribute__((constructor(102))) run_tests(void) { + bool should_run = false; +#ifdef USE_SYSCTL_FOR_ARGS + int mib[] = { + CTL_KERN, +#if defined(__NetBSD__) || defined(__OpenBSD__) + KERN_PROC_ARGS, + getpid(), + KERN_PROC_ARGV, +#else + KERN_PROC, + KERN_PROC_ARGS, + getpid(), +#endif + }; + char *arg = NULL; + size_t arglen; + sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &arglen, NULL, 0); + arg = malloc(arglen); + sysctl(mib, sizeof(mib) / sizeof(mib[0]), arg, &arglen, NULL, 0); +#else + FILE *cmdlinef = fopen("/proc/self/cmdline", "r"); + char *arg = NULL; + int arglen; + fscanf(cmdlinef, "%ms%n", &arg, &arglen); + fclose(cmdlinef); +#endif + for (char *pos = arg; pos < arg + arglen; pos += strlen(pos) + 1) { + if (strcmp(pos, "--unittest") == 0) { + should_run = true; + break; + } + } + free(arg); + + if (!should_run) { + return; + } + + if (&test_h_unittest_setup) { + test_h_unittest_setup(); + } + + struct test_file_metadata *i = test_file_head; + int failed = 0, success = 0; + while (i) { + fprintf(stderr, "Running tests from %s:\n", i->name); + struct test_case_metadata *j = i->tests; + while (j) { + fprintf(stderr, "\t%s ... ", j->name); + j->failure.present = false; + j->fn(j, i); + if (j->failure.present) { + fprintf(stderr, "failed (%s at %s:%d)\n", j->failure.message, + j->failure.file, j->failure.line); + failed++; + } else { + fprintf(stderr, "passed\n"); + success++; + } + j = j->next; + } + fprintf(stderr, "\n"); + i = i->next; + } + int total = failed + success; + fprintf(stderr, "Test results: passed %d/%d, failed %d/%d\n", success, total, + failed, total); + exit(failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE); +} + +#else + +#include + +#define TEST_CASE(name) static void __attribute__((unused)) __test_h_##name(void) + +#define TEST_EQUAL(a, b) \ + (void)(a); \ + (void)(b) +#define TEST_TRUE(a) (void)(a) +#define TEST_STREQUAL(a, b) \ + (void)(a); \ + (void)(b) + +#endif diff --git a/vm/test/vm_test.c b/vm/test/vm_test.c new file mode 100644 index 0000000..2d1a91f --- /dev/null +++ b/vm/test/vm_test.c @@ -0,0 +1,182 @@ +#include "vm.h" + +#include "test.h" + +#include + +/// Create and destroy a vm. +TEST_CASE(vm_create_destroy) { + Vm* vm = vm_new(); + TEST_TRUE(vm != 0); + vm_del(&vm); +} + +// Exit with an implicit 0 exit code. +TEST_CASE(vm_exit_implicit) { + // clang-format off + const Inst instructions[] = { + vmExit(), + }; + // clang-format on + + Vm* vm = vm_new(); + TEST_TRUE(vm != 0); + const int exit_code = + vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst)); + TEST_TRUE(exit_code == 0); + vm_del(&vm); +} + +// Exit with an explicit exit code. +TEST_CASE(vm_exit_explicit) { + const int32_t expected = 17; + + // clang-format off + const Inst instructions[] = { + vmPushI32(expected), + vmExit(), + }; + // clang-format on + + Vm* vm = vm_new(); + TEST_TRUE(vm != 0); + const int exit_code = + vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst)); + TEST_TRUE(exit_code == expected); + vm_del(&vm); +} + +/// Add two i32 numbers. +TEST_CASE(vm_add_i32) { + const int n1 = 2; + const int n2 = 3; + + // clang-format off + const Inst instructions[] = { + vmPushI32(n1), + vmPushI32(n2), + vmAdd(I32), + vmExit(), + }; + // clang-format on + + Vm* vm = vm_new(); + TEST_TRUE(vm != 0); + const int exit_code = + vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst)); + TEST_EQUAL(exit_code, n1 + n2); + vm_del(&vm); +} + +/// Sum an array of numbers with 4 add instructions. +TEST_CASE(vm_sum_array_i32_explicit) { + const int vals[5] = {1, 2, 3, 4, 5}; + + // clang-format off + const Inst instructions[] = { + vmPushI32(vals[0]), + vmPushI32(vals[1]), + vmPushI32(vals[2]), + vmPushI32(vals[3]), + vmPushI32(vals[4]), + vmAdd(I32), + vmAdd(I32), + vmAdd(I32), + vmAdd(I32), + vmExit(), + }; + // clang-format on + + int sum = 0; + for (size_t i = 0; i < sizeof(vals) / sizeof(vals[0]); ++i) { + sum += vals[i]; + } + + Vm* vm = vm_new(); + TEST_TRUE(vm != 0); + const int exit_code = + vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst)); + TEST_EQUAL(exit_code, sum); + vm_del(&vm); +} + +/// Sum an array of numbers with a loop. +TEST_CASE(vm_sum_array_i32_loop) { + const int vals[5] = {1, 2, 3, 4, 5}; + + const Label loop_label = 0; + const Label counter_index = 0; + + // clang-format off + const Inst instructions[] = { + vmPushI32(vals[0]), + vmPushI32(vals[1]), + vmPushI32(vals[2]), + vmPushI32(vals[3]), + vmPushI32(vals[4]), + vmLocal(I32, counter_index), + vmPushI32(sizeof(vals) / sizeof(vals[0]) - 1), + vmLocalWr(I32, counter_index), + vmLoop(loop_label), + vmAdd(I32), + vmLocalRd(I32, counter_index), + vmDec(I32), + // TODO: Could be useful to have a function that writes the local but + // leaves its value on the stack. + vmLocalWr(I32, counter_index), + vmLocalRd(I32, counter_index), + // TODO: Perhaps we should expect the comparison value to also be pushed + // to the stack. + vmCmpI32(0), + vmBr_if(false, loop_label), + vmEnd(), + vmExit(), + }; + // clang-format on + + int sum = 0; + for (size_t i = 0; i < sizeof(vals) / sizeof(vals[0]); ++i) { + sum += vals[i]; + } + + Vm* vm = vm_new(); + TEST_TRUE(vm != 0); + const int exit_code = + vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst)); + TEST_EQUAL(exit_code, sum); + vm_del(&vm); +} + +// Call a function to add two numbers. +TEST_CASE(vm_function_call) { + const Label func = 0; + const Label a = 0; + const Label b = 1; + const int32_t a_val = 3; + const int32_t b_val = 5; + const int32_t expected = a + b; + + // clang-format off + const Inst instructions[] = { + /* Function definition */ + vmFunc(func), + vmArg(I32, b), + vmArg(I32, a), + vmAdd(I32), + vmEnd(), + /* Main program */ + vmPushI32(a_val), + vmPushI32(b_val), + vmCall(func), + vmExit(), + }; + // clang-format on + + Vm* vm = vm_new(); + TEST_TRUE(vm != 0); + // const int exit_code = + // vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst)); + vm_del(&vm); +} + +int main() { return 0; } -- cgit v1.2.3