aboutsummaryrefslogtreecommitdiff
path: root/tests/test_opcodes.c
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-11-15 21:22:20 +0800
committerMistivia <i@mistivia.com>2024-11-15 21:22:20 +0800
commit789ebeb7d097d2824ff62c73438af4cb77f882bc (patch)
tree86de650556666795641016e33517f8d22714815c /tests/test_opcodes.c
parent12f7b236531d839fe6790c71e2e51e014ae84e3d (diff)
finish project framework
Diffstat (limited to 'tests/test_opcodes.c')
-rw-r--r--tests/test_opcodes.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/test_opcodes.c b/tests/test_opcodes.c
new file mode 100644
index 0000000..27bfbf3
--- /dev/null
+++ b/tests/test_opcodes.c
@@ -0,0 +1,45 @@
+#include <assert.h>
+#include <stdio.h>
+
+#include "fvm.h"
+
+char code[4096];
+char stack[4096];
+int code_cursor = 0;
+struct fvm vm;
+
+static void reset() {
+ vm.pc = (int64_t)code;
+ vm.sp = (int64_t)stack + 4095;
+ vm.bp = (int64_t)stack + 4095;
+ code_cursor = 0;
+}
+
+static void emit_op(enum fvm_op op) {
+ code[code_cursor] = (char)op;
+ code_cursor++;
+}
+static void emit_num(int64_t x) {
+ *(int64_t*)(code + code_cursor) = x;
+ code_cursor += 8;
+}
+
+void test_imm() {
+ printf("[TEST] imm\n");
+
+ reset();
+
+ emit_op(FVM_OP_IMM);
+ emit_num(3);
+ emit_op(FVM_OP_EXIT);
+
+ assert(fvm_execute(&vm) == 3);
+
+ printf("[PASS] imm\n");
+}
+
+int main()
+{
+ test_imm();
+ return 0;
+}