Skip to content

Commit

Permalink
Unit test - mov (#13)
Browse files Browse the repository at this point in the history
* Added macro to testing framework - `assert_eq_buf`

Added the new `assert_eq_buf` macro to the testing framework in
`test.h` this macro is used in unit tests and can check if two
buffers, `a` and `b` have the same length and values data pairs.

This macro is especially useful when unit tests are rolled out for
indivdual instructons to compare buffers and expected outcomes.

* Added `assert_eq_buf_arr`

A suppliment to previosuly added macro - `assert_eq_buf` in commit
number c9fb1ef, this checks for
the same values in two arrays instead of two value buffers as such
of the ones defined in `buffer.c`, giving greater flexability

* Updated `test.h` testing library

- Abstracted the `assert_eq_buf` macro to leverage the behavior
of the new `assert_eq_buf_arr` macro by providing `.data` member
to the `arr_len` argument.

- Adding error exiting function call when the assertion statement
does not turn true to be consisten with other macros

* Added test case

Added a test case specifically aimed at the `mr` instruction iden-
ity, this unit test and the whole instruction testing in general
will spilt out each unit as instruction encoder identities as
described in `encoder.h`, allowing the modular integration and
testing of instructions in an organised and clean way

* Reverted change in previous commit in #11

The `instr_test` macro for the testing framework introduced into
this header of commit no 63f8455
(as part of Pull request #11) Has been proved useless in further
development and being a pain the ass to deal with, therefore, this
macro will be removed for now, we can go back and revert this comm-
it in the future if this functionality is needed again. (I mean,
we can always revise a new and better solution if needed)

* Finished filling out the `mov` test

This commit describes the changes that has completed the develop-
ment of the `mov` test which tests the `mov` instruction, (*clap*
*clap* wow!)

This test works by leveraging the power from the standard C pre-
processor, this test first defines the set of expected values
in hex, then feeds it into a macro that runs the `assemble_instr`
function and compares the bytes returned.
  • Loading branch information
cheng-alvin authored Dec 17, 2024
1 parent 63f8455 commit ec74b1a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
42 changes: 42 additions & 0 deletions tests/mov.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "jas.h"
#include "test.h"

#define mr_bytes {0x48, 0x89, 0x80, 0xff, 0x00, 0x00, 0x00};
#define rm_bytes {0x48, 0x8B, 0x80, 0xff, 0x00, 0x00, 0x00};
#define oi_bytes {0x48, 0xB8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

#define mi_bytes \
{ 0x48, 0xC7, 0x80, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF }

// TODO: Encapsulate this into a global macro for all tests
#define MOV_TEST(name, op1, op2, expected_bytes) \
Test(mov, name) { \
err_add_callback(test_error_handler); \
\
const operand_t operands[] = {op1, op2, OP_NONE, OP_NONE}; \
const instruction_t instr = { \
.instr = INSTR_MOV, \
.operands = &(operand_t[]){operands[0], operands[1], operands[2], operands[3]}, \
}; \
\
const unsigned char expected[] = expected_bytes; \
const buffer_t buf = assemble_instr(MODE_LONG, instr); \
\
assert_eq_buf_arr(buf, expected, sizeof(expected)); \
free(buf.data); \
}

MOV_TEST(mr, m64, r64, mr_bytes);
MOV_TEST(rm, r64, m64, rm_bytes);
MOV_TEST(oi, r64, imm64, oi_bytes);
MOV_TEST(mi, m64, imm64, oi_bytes);

int main(void) {
TestSuite(mov);
RunTest(mov, mr);
RunTest(mov, rm);
RunTest(mov, oi);
RunTest(mov, mi);

return 0;
}
11 changes: 0 additions & 11 deletions tests/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,4 @@ static void test_error_handler(const char *msg) {
} \
}

#define instr_test(operands, expected) \
\
err_add_callback(test_error_handler); \
const instruction_t instr = { \
.instr = INSTR_MOV, \
.operands = (operand_t[]) #operands, \
}; \
const buffer_t buf = assemble_instr(MODE_LONG, instr); \
assert_eq_buf_arr(buf, #expected, sizeof(#expected)); \
free(buf.data);

#endif

0 comments on commit ec74b1a

Please sign in to comment.