Skip to content

Commit

Permalink
Test framework changes (#11)
Browse files Browse the repository at this point in the history
* Modified test macros

- Added new `instr_test` macro to encapsulate logic for testing
instruction generation with a set and uniform instruction expected
values and generation values.

- Used `len` member instead of providing a general size of a buf-
fer object

* 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
  • Loading branch information
cheng-alvin authored Dec 16, 2024
1 parent d80c456 commit 63f8455
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,26 @@ static void test_error_handler(const char *msg) {
fail(msg);
}

#define assert_eq_buf(a, b) assert_eq_buf_arr(a, b.data, b.len)
#define assert_eq_buf_arr(a, b, arr_len) \
\
if (a.len != arr_len) test_printf("\nAssertion failed: %s is not the same as %s\n", #a, #b); \
for (size_t i = 0; i < a.len; i++) { \
if (a.data[i] != b[i]) { \
test_printf("\nAssertion failed: `%s` is not the same as `%s`\n", #a, #b); \
exit(1); \
} \
}

#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 63f8455

Please sign in to comment.