Add first unit tests

This commit is contained in:
Hugues Delorme 2014-01-29 18:33:49 +01:00
parent 58e19b0554
commit 0bef50a785
2 changed files with 86 additions and 0 deletions

48
tests/test_internal.c Normal file
View File

@ -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)

38
tests/utest_lib.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef UTEST_LIB_H
#define UTEST_LIB_H
#include <stdio.h>
#include <stdlib.h>
#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 */