From fda2346e11fc743ea99925097d220a988c13e75f Mon Sep 17 00:00:00 2001 From: John Kerl Date: Mon, 7 Sep 2015 14:43:09 -0400 Subject: [PATCH] [read-performance iterate] peek-file-reader UTs --- .gitignore | 1 + c/Makefile | 17 ++++++- c/input/peek_file_reader.c | 15 ++++++ c/input/peek_file_reader.h | 5 ++ c/input/test_peek_file_reader.c | 83 +++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 c/input/peek_file_reader.c create mode 100644 c/input/test_peek_file_reader.c diff --git a/.gitignore b/.gitignore index 09b414638..c562f97a7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ mlr-unit-tester test-mlrutil test-string-builder test-byte-readers +test-peek-file-reader test-parse-trie test-lrec test-join-bucket-keeper diff --git a/c/Makefile b/c/Makefile index 2a192e538..ead6d0db1 100644 --- a/c/Makefile +++ b/c/Makefile @@ -39,6 +39,14 @@ input/stdio_byte_reader.c \ input/mmap_byte_reader.c \ input/test_byte_readers.c +TEST_PEEK_FILE_READER_SRCS = \ +lib/mlrutil.c \ +lib/mlr_test_util.c \ +lib/mlr_globals.c \ +input/string_byte_reader.c \ +input/peek_file_reader.c \ +input/test_peek_file_reader.c + TEST_LREC_SRCS = lib/mlrutil.c lib/mlr_globals.c lib/string_builder.c \ containers/lrec.c containers/header_keeper.c containers/sllv.c \ containers/slls.c containers/lhmslv.c \ @@ -141,9 +149,10 @@ mlrp: .always dsls tests: unit-test reg-test -unit-test: test-mlrutil test-byte-readers test-parse-trie test-lrec test-string-builder test-join-bucket-keeper +unit-test: test-mlrutil test-byte-readers test-peek-file-reader test-parse-trie test-lrec test-string-builder test-join-bucket-keeper ./test-mlrutil ./test-byte-readers + ./test-peek-file-reader ./test-parse-trie ./test-lrec ./test-string-builder @@ -161,10 +170,11 @@ dev-tests: dev-unit-test reg-test # Unfortunately --error-exitcode=1 doesn't work well since there are # valgrind-detected errors in stdlibs. :( -dev-unit-test: test-mlrutil test-byte-readers test-parse-trie test-lrec test-string-builder test-join-bucket-keeper +dev-unit-test: test-mlrutil test-byte-readers test-peek-file-reader test-parse-trie test-lrec test-string-builder test-join-bucket-keeper #valgrind --leak-check=full --error-exitcode=1 ./a.out valgrind --leak-check=full ./test-mlrutil valgrind --leak-check=full ./test-byte-readers + valgrind --leak-check=full ./test-Peek-file-reader valgrind --leak-check=full ./test-parse-trie valgrind --leak-check=full ./test-lrec valgrind --leak-check=full ./test-string-builder @@ -183,6 +193,9 @@ regtest-copy: test-byte-readers: .always $(CCDEBUG) -D__TEST_BYTE_READERS_MAIN__ $(TEST_BYTE_READERS_SRCS) -o test-byte-readers +test-peek-file-reader: .always + $(CCDEBUG) -D__TEST_PEEK_FILE_READER_MAIN__ $(TEST_PEEK_FILE_READER_SRCS) -o test-peek-file-reader + test-lrec: .always $(CCDEBUG) -D__TEST_LREC_MAIN__ $(TEST_LREC_SRCS) -o test-lrec diff --git a/c/input/peek_file_reader.c b/c/input/peek_file_reader.c new file mode 100644 index 000000000..1cd959be6 --- /dev/null +++ b/c/input/peek_file_reader.c @@ -0,0 +1,15 @@ +#include +#include +#include "input/peek_file_reader.h" + +// ---------------------------------------------------------------- +void pfr_dump(peek_file_reader_t* pfr) { + printf("======================== pfr at %p\n", pfr); + printf(" peekbuflen = %d\n", pfr->peekbuflen); + printf(" npeeked = %d\n", pfr->npeeked); + for (int i = 0; i < pfr->npeeked; i++) { + char c = pfr->peekbuf[i]; + printf(" i=%d c=%c [%02x]\n", i, isprint((unsigned char)c) ? c : ' ', c); + } + printf("------------------------\n"); +} diff --git a/c/input/peek_file_reader.h b/c/input/peek_file_reader.h index a43b9bb24..b6e72ce6b 100644 --- a/c/input/peek_file_reader.h +++ b/c/input/peek_file_reader.h @@ -2,6 +2,8 @@ #define PEEK_FILE_READER_H #include +#include "lib/mlrutil.h" +#include "lib/mlr_globals.h" #include "input/byte_reader.h" typedef struct _peek_file_reader_t { @@ -83,4 +85,7 @@ static inline void pfr_advance_by(peek_file_reader_t* pfr, int len) { pfr->npeeked -= len; } +// ---------------------------------------------------------------- +void pfr_dump(peek_file_reader_t* pfr); + #endif // PEEK_FILE_READER_H diff --git a/c/input/test_peek_file_reader.c b/c/input/test_peek_file_reader.c new file mode 100644 index 000000000..e942e503b --- /dev/null +++ b/c/input/test_peek_file_reader.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include "lib/mlrutil.h" +#include "lib/minunit.h" +#include "lib/mlr_test_util.h" +#include "input/byte_readers.h" +#include "input/peek_file_reader.h" + +#ifdef __TEST_PEEK_FILE_READER_MAIN__ +int tests_run = 0; +int tests_failed = 0; +int assertions_run = 0; +int assertions_failed = 0; + +// ---------------------------------------------------------------- +static char* test_empty() { + byte_reader_t* pbr = string_byte_reader_alloc(); + int ok = pbr->popen_func(pbr, ""); + mu_assert_lf(ok == TRUE); + + peek_file_reader_t* pfr = pfr_alloc(pbr, 11); + + mu_assert_lf(pfr_peek_char(pfr) == EOF); + mu_assert_lf(pfr_read_char(pfr) == EOF); + + pbr->pclose_func(pbr); + pfr_free(pfr); + + return NULL; +} + +// ---------------------------------------------------------------- +static char* test_non_empty() { + byte_reader_t* pbr = string_byte_reader_alloc(); + int ok = pbr->popen_func(pbr, + "ab,cde\n" + "123,4567\n" + ); + mu_assert_lf(ok == TRUE); + + peek_file_reader_t* pfr = pfr_alloc(pbr, 11); + + pfr_dump(pfr); mu_assert_lf(pfr_peek_char(pfr) == 'a'); + pfr_dump(pfr); mu_assert_lf(pfr_read_char(pfr) == 'a'); + pfr_dump(pfr); mu_assert_lf(pfr_peek_char(pfr) == 'b'); + pfr_dump(pfr); mu_assert_lf(pfr_read_char(pfr) == 'b'); + + pfr_dump(pfr); mu_assert_lf(pfr_peek_char(pfr) == ','); + pfr_dump(pfr); mu_assert_lf(pfr_peek_char(pfr) == ','); + pfr_dump(pfr); mu_assert_lf(pfr_read_char(pfr) == ','); + pfr_dump(pfr); pfr_buffer_by(pfr, 5); + pfr_dump(pfr); pfr_advance_by(pfr, 5); + pfr_dump(pfr); mu_assert_lf(pfr_read_char(pfr) == '2'); + + pbr->pclose_func(pbr); + pfr_free(pfr); + + return NULL; +} + +// ================================================================ +static char * run_all_tests() { + mu_run_test(test_empty); + mu_run_test(test_non_empty); + return 0; +} + +int main(int argc, char **argv) { + char *result = run_all_tests(); + printf("\n"); + if (result != 0) { + printf("Not all unit tests passed\n"); + } + else { + printf("TEST_PEEK_FILE_READER: ALL UNIT TESTS PASSED\n"); + } + printf("Tests passed: %d of %d\n", tests_run - tests_failed, tests_run); + printf("Assertions passed: %d of %d\n", assertions_run - assertions_failed, assertions_run); + + return result != 0; +} +#endif // __TEST_PEEK_FILE_READER_MAIN__