From 689935ed399b778841903ea8107ad13345f920f8 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 19 Jun 2016 12:48:20 -0400 Subject: [PATCH] Add readline tests --- common/readline.c | 4 +- test/common/CMakeLists.txt | 1 + test/common/list/list.c | 2 - test/common/readline/CMakeLists.txt | 9 +++++ test/common/readline/readline.c | 61 +++++++++++++++++++++++++++++ test/runner | 23 ++++++----- 6 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 test/common/readline/CMakeLists.txt create mode 100644 test/common/readline/readline.c diff --git a/common/readline.c b/common/readline.c index 5106172c..5815c3f1 100644 --- a/common/readline.c +++ b/common/readline.c @@ -10,7 +10,7 @@ char *read_line(FILE *file) { return NULL; } while (1) { - int c = getc(file); + int c = fgetc(file); if (c == '\n' && lastChar == '\\'){ --length; // Ignore last character. lastChar = '\0'; @@ -51,7 +51,7 @@ char *read_line_buffer(FILE *file, char *string, size_t string_len) { return NULL; } while (1) { - int c = getc(file); + int c = fgetc(file); if (c == EOF || c == '\n' || c == '\0') { break; } diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt index 1487548d..8226b207 100644 --- a/test/common/CMakeLists.txt +++ b/test/common/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(list) +add_subdirectory(readline) diff --git a/test/common/list/list.c b/test/common/list/list.c index 9d3077bc..105e2e81 100644 --- a/test/common/list/list.c +++ b/test/common/list/list.c @@ -1,6 +1,4 @@ #define _POSIX_C_SOURCE 200809L -#include -#include #include #include "tests.h" #include "list.h" diff --git a/test/common/readline/CMakeLists.txt b/test/common/readline/CMakeLists.txt new file mode 100644 index 00000000..0327c426 --- /dev/null +++ b/test/common/readline/CMakeLists.txt @@ -0,0 +1,9 @@ +configure_test( + SUBPROJECT common + NAME readline + SOURCES + ${PROJECT_SOURCE_DIR}/common/readline.c + readline.c + WRAPPERS + fgetc +) diff --git a/test/common/readline/readline.c b/test/common/readline/readline.c new file mode 100644 index 00000000..ba182fbd --- /dev/null +++ b/test/common/readline/readline.c @@ -0,0 +1,61 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include "tests.h" +#include "readline.h" + +int __wrap_fgetc(FILE *stream) { + return mock_type(int); +} + +static void prep_string(const char *str) { + while (*str) { + will_return(__wrap_fgetc, *str++); + } +} + +static void test_eof_line_ending(void **state) { + prep_string("hello"); + will_return(__wrap_fgetc, EOF); + char *line = read_line(NULL); + assert_string_equal(line, "hello"); + free(line); +} + +static void test_newline(void **state) { + prep_string("hello\n"); + char *line = read_line(NULL); + assert_string_equal(line, "hello"); + free(line); +} + +static void test_continuation(void **state) { + prep_string("hello \\\nworld"); + will_return(__wrap_fgetc, EOF); + char *line = read_line(NULL); + assert_string_equal(line, "hello world"); + free(line); +} + +static void test_expand_buffer(void **state) { + const char *test = "This is a very very long string. " + "This string is so long that it may in fact be greater " + "than 128 bytes (or octets) in length, which is suitable " + "for triggering a realloc"; + prep_string(test); + will_return(__wrap_fgetc, EOF); + char *line = read_line(NULL); + assert_string_equal(line, test); + free(line); + assert_int_equal(realloc_calls(), 1); +} + +int main(int argc, char **argv) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_eof_line_ending), + cmocka_unit_test(test_newline), + cmocka_unit_test(test_continuation), + cmocka_unit_test(test_expand_buffer), + }; + return cmocka_run_group_tests(tests, reset_mem_wrappers, NULL); +} diff --git a/test/runner b/test/runner index 43ab45e5..35004f5a 100755 --- a/test/runner +++ b/test/runner @@ -8,17 +8,20 @@ do ret+=$? done -if grep 'enable-coverage:BOOL=YES' "$1/CMakeCache.txt" +if (( $ret == 0 )) then - echo "Generating coverage reports" - rm -rf "$1/coverage" - mkdir "$1/coverage" - lcov --directory "$1" \ - --capture \ - --output-file "$1/coverage/lcov.info" - lcov --remove "$1/coverage/lcov.info" 'test/*' '/usr/*' \ - --output-file "$1/coverage/lcov.info.clean" - genhtml -o "$1/coverage/" "$1/coverage/lcov.info.clean" + if grep 'enable-coverage:BOOL=YES' "$1/CMakeCache.txt" + then + echo "Generating coverage reports" + rm -rf "$1/coverage" + mkdir "$1/coverage" + lcov --directory "$1" \ + --capture \ + --output-file "$1/coverage/lcov.info" + lcov --remove "$1/coverage/lcov.info" 'test/*' '/usr/*' \ + --output-file "$1/coverage/lcov.info.clean" + genhtml -o "$1/coverage/" "$1/coverage/lcov.info.clean" + fi fi exit $ret