diff --git a/tests/test_internal.c b/tests/test_internal.c new file mode 100644 index 0000000..3ed8b66 --- /dev/null +++ b/tests/test_internal.c @@ -0,0 +1,48 @@ +#include "utest_lib.h" + +#include "../src/internal/byte_swap.h" +#include "../src/internal/byte_codec.h" + +const char* test_internal__byte_swap() +{ + UTEST_ASSERT(foug_uint16_bswap(0x1122) == 0x2211); + UTEST_ASSERT(foug_uint32_bswap(0x11223344) == 0x44332211); + return NULL; +} + +const char* test_internal__byte_codec() +{ + { /* decode */ + const uint8_t data[] = { 0x11, 0x22, 0x33, 0x44 }; + UTEST_ASSERT(foug_decode_uint16_le(data) == 0x2211); + UTEST_ASSERT(foug_decode_uint16_be(data) == 0x1122); + UTEST_ASSERT(foug_decode_uint32_le(data) == 0x44332211); + UTEST_ASSERT(foug_decode_uint32_be(data) == 0x11223344); + } + + { /* encode */ + uint8_t data[] = { 0, 0, 0, 0 }; + foug_encode_uint16_le(0x1122, data); + UTEST_ASSERT(data[0] == 0x22 && data[1] == 0x11); + foug_encode_uint32_le(0x11223344, data); + UTEST_ASSERT(data[0] == 0x44 && data[1] == 0x33 && data[2] == 0x22 && data[3] == 0x11); + foug_encode_uint32_be(0x11223344, data); + UTEST_ASSERT(data[3] == 0x44 && data[2] == 0x33 && data[1] == 0x22 && data[0] == 0x11); + } + + return NULL; +} + +const char* test_internal__ascii_parse() +{ +} + +const char* all_tests() +{ + UTEST_SUITE_START(); + UTEST_RUN(test_internal__byte_swap); + UTEST_RUN(test_internal__byte_codec); + return NULL; +} + +UTEST_MAIN(all_tests) \ No newline at end of file diff --git a/tests/utest_lib.h b/tests/utest_lib.h new file mode 100644 index 0000000..bccb412 --- /dev/null +++ b/tests/utest_lib.h @@ -0,0 +1,38 @@ +#ifndef UTEST_LIB_H +#define UTEST_LIB_H + +#include +#include + +#define UTEST_SUITE_START() const char *message = NULL + +#define UTEST_ASSERT(test) if (!(test))\ + { printf(" ERROR : %s (line = %i, file = %s)", #test, __LINE__, __FILE__);\ + return #test; } + +#define UTEST_ASSERT_MSG(test, message) if (!(test))\ + { printf(message); return message; } + +#define UTEST_RUN(test) printf("\n-----%s", " " #test); \ + message = test();\ + tests_run++;\ + if (message) return message; + +#define UTEST_MAIN(name) int main(int argc, char *argv[]) {\ + argc = 1;\ + printf("----\nRUNNING: %s\n", argv[0]);\ + const char *result = name();\ + if (result != 0) {\ + printf("\n\nFAILED: %s\n", result);\ + }\ + else {\ + printf("\n\nALL TESTS PASSED\n");\ + }\ + printf("Tests run: %d\n", tests_run);\ + exit(result != 0);\ + } + + +static int tests_run; + +#endif /* UTEST_LIB_H */