[read-performance iterate] peek-file-reader UTs

This commit is contained in:
John Kerl 2015-09-07 14:43:09 -04:00
parent 7eba9b7c78
commit fda2346e11
5 changed files with 119 additions and 2 deletions

1
.gitignore vendored
View file

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

View file

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

View file

@ -0,0 +1,15 @@
#include <stdio.h>
#include <ctype.h>
#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");
}

View file

@ -2,6 +2,8 @@
#define PEEK_FILE_READER_H
#include <stdio.h>
#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

View file

@ -0,0 +1,83 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#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__