remedyvm

A toy RISC virtual machine inspired by Bell Lab's `dis' and Tsoding's `bm'
git clone git://git.ethandl.dev/remedyvm
Log | Files | Refs

commit 4ace8a25056cb8c2d39ee34033fa415c7ea3718d
parent 8c54354b4a11a01b6a9b6078bdbaa39317452feb
Author: Ethan Long <edl@disroot.org>
Date:   Mon, 26 Jun 2023 02:02:37 -0600

Added tests and cleaned up a bit

Diffstat:
Aimplementations/C/tests/remcc_tests.c | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 106 insertions(+), 0 deletions(-)

diff --git a/implementations/C/tests/remcc_tests.c b/implementations/C/tests/remcc_tests.c @@ -0,0 +1,106 @@ +// Test if the assembler lexer is functioning as expected +#import "../src/remcc.h" + +typedef enum { + PASS, + FAIL +} RESULT; + +typedef struct { + RESULT state; + union { + char *result; + char *error; + } val; +} result_t; + +result_t test_lex(char *tok, token_t expect); +result_t test_lexer(FILE *stream, token_t *expect); + +int main(int argc, char **argv) { + result_t result; + + char *lex_tests[] = { + // Instructions tests + "nop", + "add", + "sub", + "mul", + "div", + "and", + "or", + "xor", + "not", + "shiftl", + "shiftr(l)", + "shiftr(a)", + "move", + "swap", + "push", + "pop", + "peek", + "load", + "store", + "jump", + "call", + "return", + NULL + }; + + opcode_t lex_expects[] = { + NOP, + ADD, + SUB, + MUL, + DIV, + AND, + OR, + XOR, + NOT, + SHIFTL, + SHIFTR_L, + SHIFTR_A, + MOVE, + SWAP, + PUSH, + POP, + PEEK, + LOAD, + STORE, + JUMP, + CALL, + RETURN + }; + + //FILE *stream_tests[] = {0}; + + for (int i = 0; lex_tests[i] != NULL; i++) { + switch ((result = test_lex(lex_tests[i], lex_expects[i])).state) { + case PASS: + printf("We have a success!\n"); + printf("Result: %s\n", result.val.result); + break; + case FAIL: + fprintf(stderr, "Dumbledore dies\n"); + fprintf(stderr, "Error: %s\n", result.val.error); + break; + } + } + //test_lexer(); + return 0; +} + +result_t test_lex(char *tok, token_t expect) { + token_t res = lex(tok); + if (res.type == OPCODE && res.val.opcode == expect) { + return (result_t) { + .state = PASS, + .val.result = "Success!" + }; + } else { + return (result_t) { + .state = FAIL, + .val.error = "Didn't get what we expected!" + }; + } +}