diff --git a/CMakeLists.txt b/CMakeLists.txt index b869e63..530a883 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,3 +156,18 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # cmake ../.. -DCMAKE_INSTALL_PREFIX=../../gcc-linux64 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_DEBUG_POSTFIX=.debug # cmake ../.. -DCMAKE_INSTALL_PREFIX=../../gcc-linux64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_RELEASE_POSTFIX=.release # make VERBOSE=1 or cmake -DCMAKE_VERBOSE_MAKEFILE=TRUE + + +# Tests + +enable_testing() +set(CMAKE_CTEST_COMMAND ctest -V) +add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}) + +add_executable(test_internal EXCLUDE_FROM_ALL tests/test_internal.c + tests/stream_buffer.c + src/stream.c + src/internal/ascii_parse.c) +add_test(test_internal test_internal) +add_dependencies(check test_internal) + diff --git a/tests/test_internal.c b/tests/test_internal.c index 3ed8b66..a24bcb6 100644 --- a/tests/test_internal.c +++ b/tests/test_internal.c @@ -1,8 +1,14 @@ #include "utest_lib.h" +#include "../src/internal/ascii_parse.h" #include "../src/internal/byte_swap.h" #include "../src/internal/byte_codec.h" +#include "stream_buffer.h" + +#include +#include + const char* test_internal__byte_swap() { UTEST_ASSERT(foug_uint16_bswap(0x1122) == 0x2211); @@ -35,6 +41,69 @@ const char* test_internal__byte_codec() const char* test_internal__ascii_parse() { + char text[] = + "Une citation,\to je crois qu'elle est de moi :" + "Parfois le chemin est rude.\n" + "pi : 3.1415926535897932384626433832795"; + + { + foug_buffer_t buff; + foug_stream_t stream; + + char small_fwd_it_str[4]; + char fwd_it_str[32]; + foug_ascii_stream_fwd_iterator_t fwd_it; + + char copy_str[128]; + foug_ascii_string_buffer_t copy_strbuff; + + buff.ptr = text; + buff.len = strlen(text); + buff.pos = 0; + foug_stream_set_buffer(&stream, &buff); + + memset(&fwd_it, 0, sizeof(foug_ascii_stream_fwd_iterator_t)); + fwd_it.stream = &stream; + fwd_it.buffer.ptr = fwd_it_str; + fwd_it.buffer.max_len = sizeof(fwd_it_str); + foug_ascii_stream_fwd_iterator_init(&fwd_it); + + copy_strbuff.ptr = copy_str; + copy_strbuff.max_len = sizeof(copy_str); + + UTEST_ASSERT(foug_current_char(&fwd_it) != NULL); + UTEST_ASSERT(*foug_current_char(&fwd_it) == 'U'); + + UTEST_ASSERT(foug_eat_word(&fwd_it, ©_strbuff) == 0); + /* printf("\ncopy_strbuff.ptr = \"%s\"\n", copy_strbuff.ptr); */ + UTEST_ASSERT(strcmp(copy_strbuff.ptr, "Une") == 0); + + UTEST_ASSERT(foug_eat_word(&fwd_it, ©_strbuff) == 0); + UTEST_ASSERT(strcmp(copy_strbuff.ptr, "citation,") == 0); + + UTEST_ASSERT(foug_eat_word(&fwd_it, ©_strbuff) == 0); + UTEST_ASSERT(strcmp(copy_strbuff.ptr, "o") == 0); + + UTEST_ASSERT(foug_eat_word(&fwd_it, ©_strbuff) == 0); + UTEST_ASSERT(strcmp(copy_strbuff.ptr, "je") == 0); + + foug_skip_spaces(&fwd_it); + UTEST_ASSERT(foug_next_char(&fwd_it) != NULL); + UTEST_ASSERT(*foug_current_char(&fwd_it) == 'r'); + + /* Test with very small string buffer */ + buff.pos = 0; + fwd_it.buffer.ptr = small_fwd_it_str; + fwd_it.buffer.max_len = sizeof(small_fwd_it_str); + foug_ascii_stream_fwd_iterator_init(&fwd_it); + + UTEST_ASSERT(*foug_current_char(&fwd_it) == 'U'); + UTEST_ASSERT(foug_eat_word(&fwd_it, ©_strbuff) == 0); + UTEST_ASSERT(foug_eat_word(&fwd_it, ©_strbuff) == 0); + UTEST_ASSERT(strcmp(copy_strbuff.ptr, "citation,") == 0); + } + + return NULL; } const char* all_tests() @@ -42,7 +111,8 @@ const char* all_tests() UTEST_SUITE_START(); UTEST_RUN(test_internal__byte_swap); UTEST_RUN(test_internal__byte_codec); + UTEST_RUN(test_internal__ascii_parse); return NULL; } -UTEST_MAIN(all_tests) \ No newline at end of file +UTEST_MAIN(all_tests) diff --git a/tests/utest_lib.h b/tests/utest_lib.h index d36c58d..ae87746 100644 --- a/tests/utest_lib.h +++ b/tests/utest_lib.h @@ -23,19 +23,21 @@ 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);\ - } +#define UTEST_MAIN(name) \ + int main(int argc, char *argv[]) {\ + const char *result = NULL; \ + \ + printf("----\nRUNNING: %s\n", argv[0]);\ + result = name();\ + if (result != NULL) {\ + printf("\n\nFAILED: %s\n", result);\ + }\ + else {\ + printf("\n\nALL TESTS PASSED\n");\ + }\ + printf("Tests run: %d\n", tests_run);\ + exit(result != NULL);\ + } static int tests_run;