From 08a5f3441675c446482b473777cbba53d5191f20 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 21 Jun 2026 17:20:13 +0200 Subject: rework: add testing framework --- .gdb_history | 6 + .gitignore | 1 + lib/.clang-format | 45 ----- lib/.clang-tidy | 19 -- lib/compile_flags.txt | 8 - lib/config.mk | 4 - lib/include/csv.h | 71 ------- lib/makefile | 20 -- lib/src/csv.c | 464 --------------------------------------------- libcsv/.clang-format | 45 +++++ libcsv/.clang-tidy | 19 ++ libcsv/compile_flags.txt | 8 + libcsv/config.mk | 3 + libcsv/include/csv.h | 71 +++++++ libcsv/makefile | 19 ++ libcsv/src/csv.c | 464 +++++++++++++++++++++++++++++++++++++++++++++ makefile | 11 +- tests/compile_flags.txt | 4 +- tests/config.mk | 5 +- tests/makefile | 20 +- tests/src/csv-test.c | 2 + tests/src/tester.cpp | 476 +++++++++++++++++++++++++++++++++++++++++++++++ 22 files changed, 1133 insertions(+), 652 deletions(-) create mode 100644 .gdb_history delete mode 100644 lib/.clang-format delete mode 100644 lib/.clang-tidy delete mode 100644 lib/compile_flags.txt delete mode 100644 lib/config.mk delete mode 100644 lib/include/csv.h delete mode 100644 lib/makefile delete mode 100644 lib/src/csv.c create mode 100644 libcsv/.clang-format create mode 100644 libcsv/.clang-tidy create mode 100644 libcsv/compile_flags.txt create mode 100644 libcsv/config.mk create mode 100644 libcsv/include/csv.h create mode 100644 libcsv/makefile create mode 100644 libcsv/src/csv.c create mode 100644 tests/src/tester.cpp diff --git a/.gdb_history b/.gdb_history new file mode 100644 index 0000000..cf67eb5 --- /dev/null +++ b/.gdb_history @@ -0,0 +1,6 @@ +start +b tester.cpp:45 +b tests/src/tester.cpp:45 +run +bt +quit diff --git a/.gitignore b/.gitignore index 98d5016..40460e0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.a *.csv build/ +tests/bin csv-test csv-perf diff --git a/lib/.clang-format b/lib/.clang-format deleted file mode 100644 index b32bad6..0000000 --- a/lib/.clang-format +++ /dev/null @@ -1,45 +0,0 @@ ---- -AccessModifierOffset: -4 -AlignConsecutiveAssignments: 'true' -AlignConsecutiveDeclarations: 'true' -AlignEscapedNewlines: Left -AlignTrailingComments: 'true' -AlwaysBreakAfterReturnType: TopLevelDefinitions -BreakBeforeBraces: Stroustrup -BreakConstructorInitializers: BeforeComma -BreakInheritanceList: BeforeComma -ColumnLimit: '0' -CompactNamespaces: 'false' -Cpp11BracedListStyle: 'false' -FixNamespaceComments: 'true' -IncludeBlocks: Regroup -IncludeCategories: - - Regex: '^.*(precomp|pch|stdafx)' - Priority: -1 - - Regex: '^<.*>' - Priority: 1 - - Regex: '^".*"' - Priority: 2 - - Regex: '.*' - Priority: 3 -IndentCaseLabels: 'false' -IndentPPDirectives: AfterHash -IndentWidth: '4' -IndentWrappedFunctionNames: 'false' -KeepEmptyLinesAtTheStartOfBlocks: 'false' -PointerAlignment: Right -SortIncludes: 'true' -SpaceAfterCStyleCast: 'true' -SpaceAfterTemplateKeyword: 'false' -SpaceBeforeAssignmentOperators: 'true' -SpaceBeforeParens: ControlStatements -SpaceBeforeRangeBasedForLoopColon: 'false' -SpaceInEmptyParentheses: 'false' -SpacesInAngles: 'false' -SpacesInCStyleCastParentheses: 'false' -SpacesInConditionalStatement: 'true' -SpacesInParentheses: 'false' -Standard: Auto -TabWidth: '4' -UseTab: ForIndentation -... diff --git a/lib/.clang-tidy b/lib/.clang-tidy deleted file mode 100644 index 0bceb5b..0000000 --- a/lib/.clang-tidy +++ /dev/null @@ -1,19 +0,0 @@ ---- -Checks: "*, - -abseil-*, - -altera-*, - -android-*, - -fuchsia-*, - -google-*, - -llvm*, - -modernize-use-trailing-return-type, - -zircon-*, - -readability-else-after-return, - -readability-static-accessed-through-instance, - -readability-avoid-const-params-in-decls, - -cppcoreguidelines-non-private-member-variables-in-classes, - -misc-non-private-member-variables-in-classes, -" -WarningsAsErrors: '' -HeaderFilterRegex: '' -FormatStyle: none diff --git a/lib/compile_flags.txt b/lib/compile_flags.txt deleted file mode 100644 index 8b85c07..0000000 --- a/lib/compile_flags.txt +++ /dev/null @@ -1,8 +0,0 @@ --Wall --Wextra --Wsign-compare --Wsign-conversion --pedantic --std=c99 --O2 --Iinclude diff --git a/lib/config.mk b/lib/config.mk deleted file mode 100644 index 6b4ff86..0000000 --- a/lib/config.mk +++ /dev/null @@ -1,4 +0,0 @@ -BUILD_DIR=../build -ARFLAGS=rcs -CFLAGS=-Wall -Wextra -Wsign-compare -Wsign-conversion -pedantic -std=c99 -O2 -Iinclude -DNDEBUG - diff --git a/lib/include/csv.h b/lib/include/csv.h deleted file mode 100644 index 9ac7c05..0000000 --- a/lib/include/csv.h +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -#include /* size_t */ -#include /* FILE */ - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum { - CSV_ERR_OK = 0, - CSV_ERR_OUT_OF_MEMORY = -1, - CSV_ERR_OUT_OF_RANGE = -2, - CSV_ERR_IO_READ = -3, - CSV_ERR_IO_WRITE = -4 -} csv_err_t; - -typedef struct { - int field_delimiter; - int field_separator; - - void (*cb_error)(csv_err_t, void *); - void *cb_error_arg; - - void *(*cb_allocate)(size_t, size_t, void *); - void *(*cb_reallocate)(void *, size_t, size_t, void *); - void (*cb_free)(void *, size_t, size_t, void *); - void *cb_memory_arg; -} csv_options_t; - -extern const csv_options_t csv_default_options; - -typedef struct { - char * str; - size_t cap, pos; -} csv_string_t; - -typedef struct { - size_t *fields; - size_t cap, pos; -} csv_field_t; - -typedef struct { - const csv_options_t *csv_options; - csv_string_t csv_string; - csv_field_t csv_field; -} csv_t; - -/* version */ -extern const char csv_version[]; - -/* initialization */ -void csv_init(csv_t *csv); -void csv_init_opt(csv_t *csv, const csv_options_t *const csv_options); - -/* cleanup after error */ -void csv_cleanup(csv_t *csv); - -/* read */ -size_t csv_read(csv_t *csv, FILE *in); - -/* field access */ -size_t csv_nfields(const csv_t *const csv); -const char *csv_field(const csv_t *const csv, size_t idx); - -/* error handling */ -const char *csv_err_str(csv_err_t csv_err); - -#ifdef __cplusplus -} -#endif diff --git a/lib/makefile b/lib/makefile deleted file mode 100644 index 5c832ba..0000000 --- a/lib/makefile +++ /dev/null @@ -1,20 +0,0 @@ -include config.mk - -LIBRARY=$(BUILD_DIR)/libcsv.a -OBJS=$(BUILD_DIR)/csv.o - -$(LIBRARY): $(OBJS) - ar $(ARFLAGS) $@ $(OBJS) - -$(BUILD_DIR)/%.o: src/%.c | $(BUILD_DIR) - cc $(CFLAGS) -c $< -o $@ - -$(BUILD_DIR): - mkdir -p $@ - -compile_flags.txt: - echo "$(CFLAGS)" | tr ' ' '\n' > $@ - -.PHONY: clean -clean: - rm -f $(LIBRARY) $(OBJS) diff --git a/lib/src/csv.c b/lib/src/csv.c deleted file mode 100644 index ba78dbd..0000000 --- a/lib/src/csv.c +++ /dev/null @@ -1,464 +0,0 @@ -#include "csv.h" - -#include -#include -#include -#include -#include - -// === Compile time options === - -#ifndef CSV_DEFAULT_DELIMITER -# define CSV_DEFAULT_DELIMITER '"' -#endif - -#ifndef CSV_DEFAULT_SEPARATOR -# define CSV_DEFAULT_SEPARATOR ',' -#endif - -// === some useful Makros === - -#define UNUSED(x) (void) (x) // mark a parameter as 'unused' -#define STR(s) #s // Stringify a Makro -#define XSTR(s) STR(s) - -// === Semantic Version Information === - -#define CSV_VER_MAJOR 1 -#define CSV_VER_MINOR 0 -#define CSV_VER_PATCH 0 -#define CSV_VER_APPENDIX "-dev" - -const char csv_version[] = XSTR(CSV_VER_MAJOR) "." XSTR(CSV_VER_MINOR) "." XSTR(CSV_VER_PATCH) CSV_VER_APPENDIX "\0" __DATE__ "\0" __TIME__; - -// === CSV-MEMORY Interface === - -static void * -csv_mem_reallocate(void *ptr, size_t num, size_t size, void *cb_arg) -{ - UNUSED(cb_arg); - - return reallocarray(ptr, num, size); -} - -static void -csv_mem_free(void *ptr, size_t num, size_t size, void *cb_arg) -{ - UNUSED(num); - UNUSED(size); - UNUSED(cb_arg); - - free(ptr); -} - -// === CSV-OPTIONS Interface === - -const csv_options_t csv_default_options = { - .field_delimiter = CSV_DEFAULT_DELIMITER, - .field_separator = CSV_DEFAULT_SEPARATOR, - .cb_error = NULL, - .cb_reallocate = csv_mem_reallocate, - .cb_free = csv_mem_free -}; - -// === CSV-ERROR Interface === - -static void -csv_fatal_error(csv_err_t csv_err, const csv_options_t *const csv_options) -{ - if ( csv_options->cb_error != NULL ) { - (*csv_options->cb_error)(csv_err, csv_options->cb_error_arg); - } - (void) fprintf(stderr, "fatal error: %s\n", csv_err_str(csv_err)); - abort(); -} - -// === CSV-STRING Interface === - -static inline void -csv_string_init(csv_string_t *csv_string) -{ - assert(csv_string != NULL); - - csv_string->str = NULL; - csv_string->cap = 0; - csv_string->pos = 0; -} - -static inline void -csv_string_reset(csv_string_t *csv_string) -{ - assert(csv_string != NULL); - - csv_string->pos = 0; -} - -static inline int -csv_string_isempty(csv_string_t *csv_string) -{ - assert(csv_string != NULL); - - return (csv_string->pos == 0) ? 1 : 0; -} - -static inline size_t -growth_strategy(size_t current_cap) -{ - static const size_t INITIAL_CAP = 16; - - return (current_cap == 0) ? INITIAL_CAP : (current_cap * 3) / 2; -} - -static inline void -csv_string_grow_if_needed(csv_string_t *csv_string, const csv_options_t *const csv_options) -{ - assert(csv_string != NULL); - assert(csv_options != NULL); - - if ( csv_string->pos == csv_string->cap ) { - size_t cap = growth_strategy(csv_string->cap); - char *str = csv_options->cb_reallocate(csv_string->str, cap, 1, csv_options->cb_memory_arg); - if ( str == NULL ) { - csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); - return; - } - csv_string->str = str; - csv_string->cap = cap; - } -} - -static inline void -csv_string_append(csv_string_t *csv_string, int ch, const csv_options_t *const csv_options) -{ - assert(csv_string != NULL); - assert(csv_options != NULL); - - csv_string_grow_if_needed(csv_string, csv_options); - - csv_string->str[csv_string->pos++] = (char) ch; // append char -} - -static inline void -csv_string_free(csv_string_t *csv_string, const csv_options_t *const csv_options) -{ - assert(csv_string != NULL); - assert(csv_options != NULL); - - csv_options->cb_free(csv_string->str, csv_string->cap, 1, csv_options->cb_memory_arg); - - // call *_init() for sane default values; prevent possible double-free - csv_string_init(csv_string); -} - -// === CSV-FIELD Interface === - -static inline void -csv_field_init(csv_field_t *csv_field) -{ - assert(csv_field != NULL); - - csv_field->fields = NULL; - csv_field->cap = 0; - csv_field->pos = 0; -} - -static inline void -csv_field_reset(csv_field_t *csv_field) -{ - assert(csv_field != NULL); - - csv_field->pos = 0; -} - -static inline void -csv_field_grow_if_needed(csv_field_t *csv_field, const csv_options_t *const csv_options) -{ - assert(csv_field != NULL); - assert(csv_options != NULL); - - if ( csv_field->pos == csv_field->cap ) { - size_t cap = growth_strategy(csv_field->cap); - size_t *fields = csv_options->cb_reallocate(csv_field->fields, cap, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg); - if ( fields == NULL ) { - csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); - return; - } - csv_field->fields = fields; - csv_field->cap = cap; - } -} - -static inline void -csv_field_append(csv_field_t *csv_field, size_t idx, const csv_options_t *const csv_options) -{ - assert(csv_field != NULL); - assert(csv_options != NULL); - - csv_field_grow_if_needed(csv_field, csv_options); - - csv_field->fields[csv_field->pos++] = idx; // append index -} - -static inline void -csv_field_free(csv_field_t *csv_field, const csv_options_t *const csv_options) -{ - assert(csv_field != NULL); - assert(csv_options != NULL); - - csv_options->cb_free(csv_field->fields, csv_field->cap, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg); - - csv_field_init(csv_field); -} - -// === CSV Interface === - -void -csv_init(csv_t *csv) -{ - assert(csv != NULL); - - csv_init_opt(csv, NULL); -} - -void -csv_init_opt(csv_t *csv, const csv_options_t *const csv_options) -{ - assert(csv != NULL); - - if ( csv_options != NULL ) { - csv->csv_options = csv_options; - } - else { - csv->csv_options = &csv_default_options; - } - - csv_string_init(&csv->csv_string); - csv_field_init(&csv->csv_field); -} - -void -csv_cleanup(csv_t *csv) -{ - assert(csv != NULL); - assert(csv->csv_options != NULL); - - csv_string_free(&csv->csv_string, csv->csv_options); - csv_field_free(&csv->csv_field, csv->csv_options); -} - -size_t -csv_nfields(const csv_t *const csv) -{ - assert(csv != NULL); - - return csv->csv_field.pos; -} - -const char * -csv_field(const csv_t *const csv, size_t idx) -{ - assert(csv != NULL); - assert(idx >= 0 && idx < csv->csv_field.pos); - - if ( idx >= csv->csv_field.pos ) { - csv_fatal_error(CSV_ERR_OUT_OF_RANGE, csv->csv_options); - return NULL; - } - return &csv->csv_string.str[csv->csv_field.fields[idx]]; -} - -size_t -csv_read(csv_t *csv, FILE *in) -{ - assert(csv != NULL); - assert(in != NULL); - - // initialize if needed... - if ( csv->csv_options == NULL ) { - csv_init(csv); - } - - if ( ferror(in) ) { - csv_fatal_error(CSV_ERR_IO_READ, csv->csv_options); - return 0; - } - - // do not try to read if EOF has already been seen - if ( feof(in) ) { - csv_cleanup(csv); - return 0; - } - - enum { - STATE_START_FIELD, - STATE_QUOTED_FIELD, - STATE_SIMPLE_FIELD, - STATE_END_FIELD, - STATE_END_LINE, - STATE_END_FILE, - }; - - register const int DELIM = csv->csv_options->field_delimiter; - register const int SEP = csv->csv_options->field_separator; - - csv_string_reset(&csv->csv_string); - csv_field_reset(&csv->csv_field); - - for ( int state = STATE_START_FIELD;; ) { - int chr; - - switch ( state ) { - case STATE_START_FIELD: - csv_field_append(&csv->csv_field, csv->csv_string.pos, csv->csv_options); - - chr = getc(in); - if ( chr == EOF ) { - state = STATE_END_FILE; - } - else if ( chr == '\r' ) { // test for CR.. - chr = getc(in); - if ( chr != '\n' ) { // ..LF - (void) ungetc(chr, in); - } - state = STATE_END_LINE; - } - else if ( chr == '\n' ) { - state = STATE_END_LINE; - } - else if ( chr == SEP ) { - state = STATE_END_FIELD; - } - else if ( chr == DELIM ) { - state = STATE_QUOTED_FIELD; - } - else { - if ( chr != '\0' ) { - csv_string_append(&csv->csv_string, chr, csv->csv_options); - } - state = STATE_SIMPLE_FIELD; - } - break; - - case STATE_QUOTED_FIELD: - do { - chr = getc(in); - if ( chr == EOF ) { - state = STATE_END_FILE; - } - else if ( chr == DELIM ) { - chr = getc(in); - if ( chr == EOF ) { - state = STATE_END_FILE; - } - else if ( chr == DELIM ) { - csv_string_append(&csv->csv_string, DELIM, csv->csv_options); - } - else if ( chr == SEP ) { - state = STATE_END_FIELD; - } - else if ( chr == '\r' ) { - chr = getc(in); - if ( chr != '\n' ) { - (void) ungetc(chr, in); - } - state = STATE_END_LINE; - } - else if ( chr == '\n' ) { - state = STATE_END_LINE; - } - else { - csv_string_append(&csv->csv_string, DELIM, csv->csv_options); - (void) ungetc(chr, in); // we have read too far... Put the character back! - } - } - else { - if ( chr != '\0' ) { - csv_string_append(&csv->csv_string, chr, csv->csv_options); - } - } - } while ( state == STATE_QUOTED_FIELD ); - break; - - case STATE_SIMPLE_FIELD: - do { - chr = getc(in); - if ( chr == EOF ) { - state = STATE_END_FILE; - } - else if ( chr == SEP ) { - state = STATE_END_FIELD; - } - else if ( chr == '\r' ) { - chr = getc(in); - if ( chr != '\n' ) { - (void) ungetc(chr, in); - } - state = STATE_END_LINE; - } - else if ( chr == '\n' ) { - state = STATE_END_LINE; - } - else { - if ( chr != '\0' ) { - csv_string_append(&csv->csv_string, chr, csv->csv_options); - } - } - } while ( state == STATE_SIMPLE_FIELD ); - break; - - case STATE_END_FIELD: - csv_string_append(&csv->csv_string, '\0', csv->csv_options); - state = STATE_START_FIELD; - break; - - case STATE_END_LINE: - csv_string_append(&csv->csv_string, '\0', csv->csv_options); - return csv->csv_field.pos; - - case STATE_END_FILE: - if ( ferror(in) ) { - csv_fatal_error(CSV_ERR_IO_READ, csv->csv_options); - return 0; - } - - if ( csv_string_isempty(&csv->csv_string) ) { - csv_cleanup(csv); - return 0; // EOF reached - } - - /* - * The last data record was not terminated with a NEWLINE-Symbol. - * So we can't signal End-Of-File for now. Terminate the current - * field and return the number of fields processed so far. - */ - csv_string_append(&csv->csv_string, '\0', csv->csv_options); - - return csv->csv_field.pos; - - default: - assert(!"this should never be happen..."); - break; - } - } - // NOT REACHED -} - -const char * -csv_err_str(csv_err_t csv_err) -{ - switch ( csv_err ) { - case CSV_ERR_OK: - return "no error"; - case CSV_ERR_OUT_OF_MEMORY: - return "out of memory"; - case CSV_ERR_OUT_OF_RANGE: - return "index out of range"; - case CSV_ERR_IO_READ: - return "read error"; - case CSV_ERR_IO_WRITE: - return "write error"; - default: - return "unknown error"; - } - // NOT REACHED -} diff --git a/libcsv/.clang-format b/libcsv/.clang-format new file mode 100644 index 0000000..b32bad6 --- /dev/null +++ b/libcsv/.clang-format @@ -0,0 +1,45 @@ +--- +AccessModifierOffset: -4 +AlignConsecutiveAssignments: 'true' +AlignConsecutiveDeclarations: 'true' +AlignEscapedNewlines: Left +AlignTrailingComments: 'true' +AlwaysBreakAfterReturnType: TopLevelDefinitions +BreakBeforeBraces: Stroustrup +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +ColumnLimit: '0' +CompactNamespaces: 'false' +Cpp11BracedListStyle: 'false' +FixNamespaceComments: 'true' +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^.*(precomp|pch|stdafx)' + Priority: -1 + - Regex: '^<.*>' + Priority: 1 + - Regex: '^".*"' + Priority: 2 + - Regex: '.*' + Priority: 3 +IndentCaseLabels: 'false' +IndentPPDirectives: AfterHash +IndentWidth: '4' +IndentWrappedFunctionNames: 'false' +KeepEmptyLinesAtTheStartOfBlocks: 'false' +PointerAlignment: Right +SortIncludes: 'true' +SpaceAfterCStyleCast: 'true' +SpaceAfterTemplateKeyword: 'false' +SpaceBeforeAssignmentOperators: 'true' +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: 'false' +SpaceInEmptyParentheses: 'false' +SpacesInAngles: 'false' +SpacesInCStyleCastParentheses: 'false' +SpacesInConditionalStatement: 'true' +SpacesInParentheses: 'false' +Standard: Auto +TabWidth: '4' +UseTab: ForIndentation +... diff --git a/libcsv/.clang-tidy b/libcsv/.clang-tidy new file mode 100644 index 0000000..0bceb5b --- /dev/null +++ b/libcsv/.clang-tidy @@ -0,0 +1,19 @@ +--- +Checks: "*, + -abseil-*, + -altera-*, + -android-*, + -fuchsia-*, + -google-*, + -llvm*, + -modernize-use-trailing-return-type, + -zircon-*, + -readability-else-after-return, + -readability-static-accessed-through-instance, + -readability-avoid-const-params-in-decls, + -cppcoreguidelines-non-private-member-variables-in-classes, + -misc-non-private-member-variables-in-classes, +" +WarningsAsErrors: '' +HeaderFilterRegex: '' +FormatStyle: none diff --git a/libcsv/compile_flags.txt b/libcsv/compile_flags.txt new file mode 100644 index 0000000..8b85c07 --- /dev/null +++ b/libcsv/compile_flags.txt @@ -0,0 +1,8 @@ +-Wall +-Wextra +-Wsign-compare +-Wsign-conversion +-pedantic +-std=c99 +-O2 +-Iinclude diff --git a/libcsv/config.mk b/libcsv/config.mk new file mode 100644 index 0000000..9aa2270 --- /dev/null +++ b/libcsv/config.mk @@ -0,0 +1,3 @@ +ARFLAGS=rcs +CFLAGS=-Wall -Wextra -Wsign-compare -Wsign-conversion -pedantic -std=c99 -O2 -Iinclude -DNDEBUG + diff --git a/libcsv/include/csv.h b/libcsv/include/csv.h new file mode 100644 index 0000000..9ac7c05 --- /dev/null +++ b/libcsv/include/csv.h @@ -0,0 +1,71 @@ +#pragma once + +#include /* size_t */ +#include /* FILE */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + CSV_ERR_OK = 0, + CSV_ERR_OUT_OF_MEMORY = -1, + CSV_ERR_OUT_OF_RANGE = -2, + CSV_ERR_IO_READ = -3, + CSV_ERR_IO_WRITE = -4 +} csv_err_t; + +typedef struct { + int field_delimiter; + int field_separator; + + void (*cb_error)(csv_err_t, void *); + void *cb_error_arg; + + void *(*cb_allocate)(size_t, size_t, void *); + void *(*cb_reallocate)(void *, size_t, size_t, void *); + void (*cb_free)(void *, size_t, size_t, void *); + void *cb_memory_arg; +} csv_options_t; + +extern const csv_options_t csv_default_options; + +typedef struct { + char * str; + size_t cap, pos; +} csv_string_t; + +typedef struct { + size_t *fields; + size_t cap, pos; +} csv_field_t; + +typedef struct { + const csv_options_t *csv_options; + csv_string_t csv_string; + csv_field_t csv_field; +} csv_t; + +/* version */ +extern const char csv_version[]; + +/* initialization */ +void csv_init(csv_t *csv); +void csv_init_opt(csv_t *csv, const csv_options_t *const csv_options); + +/* cleanup after error */ +void csv_cleanup(csv_t *csv); + +/* read */ +size_t csv_read(csv_t *csv, FILE *in); + +/* field access */ +size_t csv_nfields(const csv_t *const csv); +const char *csv_field(const csv_t *const csv, size_t idx); + +/* error handling */ +const char *csv_err_str(csv_err_t csv_err); + +#ifdef __cplusplus +} +#endif diff --git a/libcsv/makefile b/libcsv/makefile new file mode 100644 index 0000000..0ace84a --- /dev/null +++ b/libcsv/makefile @@ -0,0 +1,19 @@ +include config.mk + +OBJS=obj/csv.o + +lib/libcsv.a: $(OBJS) | lib + ar $(ARFLAGS) $@ $(OBJS) + +obj/%.o: src/%.c | obj + cc $(CFLAGS) -c $< -o $@ + +lib obj: + mkdir -p $@ + +compile_flags.txt: + echo "$(CFLAGS)" | tr ' ' '\n' > $@ + +.PHONY: clean +clean: + rm -rf lib obj diff --git a/libcsv/src/csv.c b/libcsv/src/csv.c new file mode 100644 index 0000000..ba78dbd --- /dev/null +++ b/libcsv/src/csv.c @@ -0,0 +1,464 @@ +#include "csv.h" + +#include +#include +#include +#include +#include + +// === Compile time options === + +#ifndef CSV_DEFAULT_DELIMITER +# define CSV_DEFAULT_DELIMITER '"' +#endif + +#ifndef CSV_DEFAULT_SEPARATOR +# define CSV_DEFAULT_SEPARATOR ',' +#endif + +// === some useful Makros === + +#define UNUSED(x) (void) (x) // mark a parameter as 'unused' +#define STR(s) #s // Stringify a Makro +#define XSTR(s) STR(s) + +// === Semantic Version Information === + +#define CSV_VER_MAJOR 1 +#define CSV_VER_MINOR 0 +#define CSV_VER_PATCH 0 +#define CSV_VER_APPENDIX "-dev" + +const char csv_version[] = XSTR(CSV_VER_MAJOR) "." XSTR(CSV_VER_MINOR) "." XSTR(CSV_VER_PATCH) CSV_VER_APPENDIX "\0" __DATE__ "\0" __TIME__; + +// === CSV-MEMORY Interface === + +static void * +csv_mem_reallocate(void *ptr, size_t num, size_t size, void *cb_arg) +{ + UNUSED(cb_arg); + + return reallocarray(ptr, num, size); +} + +static void +csv_mem_free(void *ptr, size_t num, size_t size, void *cb_arg) +{ + UNUSED(num); + UNUSED(size); + UNUSED(cb_arg); + + free(ptr); +} + +// === CSV-OPTIONS Interface === + +const csv_options_t csv_default_options = { + .field_delimiter = CSV_DEFAULT_DELIMITER, + .field_separator = CSV_DEFAULT_SEPARATOR, + .cb_error = NULL, + .cb_reallocate = csv_mem_reallocate, + .cb_free = csv_mem_free +}; + +// === CSV-ERROR Interface === + +static void +csv_fatal_error(csv_err_t csv_err, const csv_options_t *const csv_options) +{ + if ( csv_options->cb_error != NULL ) { + (*csv_options->cb_error)(csv_err, csv_options->cb_error_arg); + } + (void) fprintf(stderr, "fatal error: %s\n", csv_err_str(csv_err)); + abort(); +} + +// === CSV-STRING Interface === + +static inline void +csv_string_init(csv_string_t *csv_string) +{ + assert(csv_string != NULL); + + csv_string->str = NULL; + csv_string->cap = 0; + csv_string->pos = 0; +} + +static inline void +csv_string_reset(csv_string_t *csv_string) +{ + assert(csv_string != NULL); + + csv_string->pos = 0; +} + +static inline int +csv_string_isempty(csv_string_t *csv_string) +{ + assert(csv_string != NULL); + + return (csv_string->pos == 0) ? 1 : 0; +} + +static inline size_t +growth_strategy(size_t current_cap) +{ + static const size_t INITIAL_CAP = 16; + + return (current_cap == 0) ? INITIAL_CAP : (current_cap * 3) / 2; +} + +static inline void +csv_string_grow_if_needed(csv_string_t *csv_string, const csv_options_t *const csv_options) +{ + assert(csv_string != NULL); + assert(csv_options != NULL); + + if ( csv_string->pos == csv_string->cap ) { + size_t cap = growth_strategy(csv_string->cap); + char *str = csv_options->cb_reallocate(csv_string->str, cap, 1, csv_options->cb_memory_arg); + if ( str == NULL ) { + csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); + return; + } + csv_string->str = str; + csv_string->cap = cap; + } +} + +static inline void +csv_string_append(csv_string_t *csv_string, int ch, const csv_options_t *const csv_options) +{ + assert(csv_string != NULL); + assert(csv_options != NULL); + + csv_string_grow_if_needed(csv_string, csv_options); + + csv_string->str[csv_string->pos++] = (char) ch; // append char +} + +static inline void +csv_string_free(csv_string_t *csv_string, const csv_options_t *const csv_options) +{ + assert(csv_string != NULL); + assert(csv_options != NULL); + + csv_options->cb_free(csv_string->str, csv_string->cap, 1, csv_options->cb_memory_arg); + + // call *_init() for sane default values; prevent possible double-free + csv_string_init(csv_string); +} + +// === CSV-FIELD Interface === + +static inline void +csv_field_init(csv_field_t *csv_field) +{ + assert(csv_field != NULL); + + csv_field->fields = NULL; + csv_field->cap = 0; + csv_field->pos = 0; +} + +static inline void +csv_field_reset(csv_field_t *csv_field) +{ + assert(csv_field != NULL); + + csv_field->pos = 0; +} + +static inline void +csv_field_grow_if_needed(csv_field_t *csv_field, const csv_options_t *const csv_options) +{ + assert(csv_field != NULL); + assert(csv_options != NULL); + + if ( csv_field->pos == csv_field->cap ) { + size_t cap = growth_strategy(csv_field->cap); + size_t *fields = csv_options->cb_reallocate(csv_field->fields, cap, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg); + if ( fields == NULL ) { + csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); + return; + } + csv_field->fields = fields; + csv_field->cap = cap; + } +} + +static inline void +csv_field_append(csv_field_t *csv_field, size_t idx, const csv_options_t *const csv_options) +{ + assert(csv_field != NULL); + assert(csv_options != NULL); + + csv_field_grow_if_needed(csv_field, csv_options); + + csv_field->fields[csv_field->pos++] = idx; // append index +} + +static inline void +csv_field_free(csv_field_t *csv_field, const csv_options_t *const csv_options) +{ + assert(csv_field != NULL); + assert(csv_options != NULL); + + csv_options->cb_free(csv_field->fields, csv_field->cap, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg); + + csv_field_init(csv_field); +} + +// === CSV Interface === + +void +csv_init(csv_t *csv) +{ + assert(csv != NULL); + + csv_init_opt(csv, NULL); +} + +void +csv_init_opt(csv_t *csv, const csv_options_t *const csv_options) +{ + assert(csv != NULL); + + if ( csv_options != NULL ) { + csv->csv_options = csv_options; + } + else { + csv->csv_options = &csv_default_options; + } + + csv_string_init(&csv->csv_string); + csv_field_init(&csv->csv_field); +} + +void +csv_cleanup(csv_t *csv) +{ + assert(csv != NULL); + assert(csv->csv_options != NULL); + + csv_string_free(&csv->csv_string, csv->csv_options); + csv_field_free(&csv->csv_field, csv->csv_options); +} + +size_t +csv_nfields(const csv_t *const csv) +{ + assert(csv != NULL); + + return csv->csv_field.pos; +} + +const char * +csv_field(const csv_t *const csv, size_t idx) +{ + assert(csv != NULL); + assert(idx >= 0 && idx < csv->csv_field.pos); + + if ( idx >= csv->csv_field.pos ) { + csv_fatal_error(CSV_ERR_OUT_OF_RANGE, csv->csv_options); + return NULL; + } + return &csv->csv_string.str[csv->csv_field.fields[idx]]; +} + +size_t +csv_read(csv_t *csv, FILE *in) +{ + assert(csv != NULL); + assert(in != NULL); + + // initialize if needed... + if ( csv->csv_options == NULL ) { + csv_init(csv); + } + + if ( ferror(in) ) { + csv_fatal_error(CSV_ERR_IO_READ, csv->csv_options); + return 0; + } + + // do not try to read if EOF has already been seen + if ( feof(in) ) { + csv_cleanup(csv); + return 0; + } + + enum { + STATE_START_FIELD, + STATE_QUOTED_FIELD, + STATE_SIMPLE_FIELD, + STATE_END_FIELD, + STATE_END_LINE, + STATE_END_FILE, + }; + + register const int DELIM = csv->csv_options->field_delimiter; + register const int SEP = csv->csv_options->field_separator; + + csv_string_reset(&csv->csv_string); + csv_field_reset(&csv->csv_field); + + for ( int state = STATE_START_FIELD;; ) { + int chr; + + switch ( state ) { + case STATE_START_FIELD: + csv_field_append(&csv->csv_field, csv->csv_string.pos, csv->csv_options); + + chr = getc(in); + if ( chr == EOF ) { + state = STATE_END_FILE; + } + else if ( chr == '\r' ) { // test for CR.. + chr = getc(in); + if ( chr != '\n' ) { // ..LF + (void) ungetc(chr, in); + } + state = STATE_END_LINE; + } + else if ( chr == '\n' ) { + state = STATE_END_LINE; + } + else if ( chr == SEP ) { + state = STATE_END_FIELD; + } + else if ( chr == DELIM ) { + state = STATE_QUOTED_FIELD; + } + else { + if ( chr != '\0' ) { + csv_string_append(&csv->csv_string, chr, csv->csv_options); + } + state = STATE_SIMPLE_FIELD; + } + break; + + case STATE_QUOTED_FIELD: + do { + chr = getc(in); + if ( chr == EOF ) { + state = STATE_END_FILE; + } + else if ( chr == DELIM ) { + chr = getc(in); + if ( chr == EOF ) { + state = STATE_END_FILE; + } + else if ( chr == DELIM ) { + csv_string_append(&csv->csv_string, DELIM, csv->csv_options); + } + else if ( chr == SEP ) { + state = STATE_END_FIELD; + } + else if ( chr == '\r' ) { + chr = getc(in); + if ( chr != '\n' ) { + (void) ungetc(chr, in); + } + state = STATE_END_LINE; + } + else if ( chr == '\n' ) { + state = STATE_END_LINE; + } + else { + csv_string_append(&csv->csv_string, DELIM, csv->csv_options); + (void) ungetc(chr, in); // we have read too far... Put the character back! + } + } + else { + if ( chr != '\0' ) { + csv_string_append(&csv->csv_string, chr, csv->csv_options); + } + } + } while ( state == STATE_QUOTED_FIELD ); + break; + + case STATE_SIMPLE_FIELD: + do { + chr = getc(in); + if ( chr == EOF ) { + state = STATE_END_FILE; + } + else if ( chr == SEP ) { + state = STATE_END_FIELD; + } + else if ( chr == '\r' ) { + chr = getc(in); + if ( chr != '\n' ) { + (void) ungetc(chr, in); + } + state = STATE_END_LINE; + } + else if ( chr == '\n' ) { + state = STATE_END_LINE; + } + else { + if ( chr != '\0' ) { + csv_string_append(&csv->csv_string, chr, csv->csv_options); + } + } + } while ( state == STATE_SIMPLE_FIELD ); + break; + + case STATE_END_FIELD: + csv_string_append(&csv->csv_string, '\0', csv->csv_options); + state = STATE_START_FIELD; + break; + + case STATE_END_LINE: + csv_string_append(&csv->csv_string, '\0', csv->csv_options); + return csv->csv_field.pos; + + case STATE_END_FILE: + if ( ferror(in) ) { + csv_fatal_error(CSV_ERR_IO_READ, csv->csv_options); + return 0; + } + + if ( csv_string_isempty(&csv->csv_string) ) { + csv_cleanup(csv); + return 0; // EOF reached + } + + /* + * The last data record was not terminated with a NEWLINE-Symbol. + * So we can't signal End-Of-File for now. Terminate the current + * field and return the number of fields processed so far. + */ + csv_string_append(&csv->csv_string, '\0', csv->csv_options); + + return csv->csv_field.pos; + + default: + assert(!"this should never be happen..."); + break; + } + } + // NOT REACHED +} + +const char * +csv_err_str(csv_err_t csv_err) +{ + switch ( csv_err ) { + case CSV_ERR_OK: + return "no error"; + case CSV_ERR_OUT_OF_MEMORY: + return "out of memory"; + case CSV_ERR_OUT_OF_RANGE: + return "index out of range"; + case CSV_ERR_IO_READ: + return "read error"; + case CSV_ERR_IO_WRITE: + return "write error"; + default: + return "unknown error"; + } + // NOT REACHED +} diff --git a/makefile b/makefile index db36c5c..1bda58d 100644 --- a/makefile +++ b/makefile @@ -1,16 +1,15 @@ -.PHONY: all lib test tests clean +.PHONY: all libcsv test tests clean -all: lib +all: libcsv -lib tests: +libcsv tests: $(MAKE) -C $@ test: tests - build/tester + tests/bin/tester --gtest_shuffle clean: - $(MAKE) -C lib clean + $(MAKE) -C libcsv clean $(MAKE) -C tests clean - rm -rf build # Testdaten: https://excelbianalytics.com/wp/downloads-18-sample-csv-files-data-sets-for-testing-sales/ diff --git a/tests/compile_flags.txt b/tests/compile_flags.txt index 9352ecc..f77f48c 100644 --- a/tests/compile_flags.txt +++ b/tests/compile_flags.txt @@ -3,6 +3,6 @@ -Wsign-compare -Wsign-conversion -pedantic --std=c99 +-std=c++20 -O2 --I../lib/include +-I../libcsv/include diff --git a/tests/config.mk b/tests/config.mk index 4b51d37..72527d1 100644 --- a/tests/config.mk +++ b/tests/config.mk @@ -1,3 +1,2 @@ -BUILD_DIR=../build -CFLAGS=-Wall -Wextra -Wsign-compare -Wsign-conversion -pedantic -std=c99 -O2 -I../lib/include -LDFLAGS=-L$(BUILD_DIR) -lcsv +CXXFLAGS=-Wall -Wextra -Wsign-compare -Wsign-conversion -pedantic -std=c++20 -O2 -I../libcsv/include +LDFLAGS=-L../libcsv/lib -lcsv -lgtest -lgtest_main diff --git a/tests/makefile b/tests/makefile index 2300a19..979dbd2 100644 --- a/tests/makefile +++ b/tests/makefile @@ -1,24 +1,24 @@ include config.mk -TEST_RUNNER=$(BUILD_DIR)/tester -OBJS=$(BUILD_DIR)/csv-test.o +SRCS=$(wildcard src/*.cpp) +OBJS=$(patsubst src/%.cpp,obj/%.o,$(SRCS)) -$(TEST_RUNNER): $(OBJS) | libcsv - cc -o $@ $(LDFLAGS) $(OBJS) +bin/tester: $(OBJS) | bin libcsv + c++ -o $@ $(LDFLAGS) $(OBJS) -$(BUILD_DIR)/%.o: src/%.c | $(BUILD_DIR) - cc $(CFLAGS) -c $< -o $@ +obj/%.o: src/%.cpp | obj + c++ $(CXXFLAGS) -c $< -o $@ -$(BUILD_DIR): +bin obj: mkdir -p $@ .PHONY: libcsv libcsv: - $(MAKE) -C ../lib + $(MAKE) -C ../libcsv compile_flags.txt: - echo "$(CFLAGS)" | tr ' ' '\n' > $@ + echo "$(CXXFLAGS)" | tr ' ' '\n' > $@ .PHONY: clean clean: - rm -f $(TEST_RUNNER) $(OBJS) + rm -rf bin obj diff --git a/tests/src/csv-test.c b/tests/src/csv-test.c index c99c049..4035f70 100644 --- a/tests/src/csv-test.c +++ b/tests/src/csv-test.c @@ -1,3 +1,4 @@ +// NOLINTBEGIN #undef NDEBUG #include @@ -625,3 +626,4 @@ main(void) return EXIT_SUCCESS; } +// NOLINTEND diff --git a/tests/src/tester.cpp b/tests/src/tester.cpp new file mode 100644 index 0000000..75f1366 --- /dev/null +++ b/tests/src/tester.cpp @@ -0,0 +1,476 @@ +#include +#include +#include + +#include "csv.h" + +using namespace std; +using namespace testing; + +TEST(TestEmptyObject, StandardInitialization) +{ + csv_t csv{}; + ASSERT_THAT(csv_nfields(&csv), Eq(0)); +} + +TEST(TestEmptyObject, ExplicitInitialization) +{ + csv_t csv; + csv_init(&csv); + + ASSERT_THAT(csv_nfields(&csv), Eq(0)); +} + +class TestLineEndingsBase : public Test { + using file_ptr = unique_ptr; + +public: + explicit TestLineEndingsBase(string_view data) + : data_{ data } + , file_{ fmemopen(data_.data(), data_.size(), "r"), &fclose } + { + csv_init(&csv_); + } + + TestLineEndingsBase(string_view data, csv_options_t &csv_options) + : data_{ data } + , file_{ fmemopen(data_.data(), data_.size(), "r"), &fclose } + { + csv_init_opt(&csv_, &csv_options); + } + + ~TestLineEndingsBase() override + { + csv_cleanup(&csv_); + } + + TestLineEndingsBase(const TestLineEndingsBase &) = delete; + TestLineEndingsBase(TestLineEndingsBase &&) = delete; + void operator=(const TestLineEndingsBase &) = delete; + void operator=(TestLineEndingsBase &&) = delete; + + csv_t csv_{}; + string data_; + file_ptr file_; +}; + +class TestLineEndings : public TestLineEndingsBase { +protected: + TestLineEndings() + : TestLineEndingsBase("A,B,C\r\nD,E,F\n") + { + } +}; + +TEST_F(TestLineEndings, Part1) +{ + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + EXPECT_STREQ(csv_field(&csv_, 2), "C"); + + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), "D"); + EXPECT_STREQ(csv_field(&csv_, 1), "E"); + EXPECT_STREQ(csv_field(&csv_, 2), "F"); + + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(0)); +} + +class TestEmptyLinesWithDifferentLineEndings : public TestLineEndingsBase { +protected: + TestEmptyLinesWithDifferentLineEndings() + : TestLineEndingsBase("\r\n\n") + { + } +}; + +TEST_F(TestEmptyLinesWithDifferentLineEndings, Part1) +{ + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(0)); +} + +class TestEmptyFieldsWithDifferentLineEndings : public TestLineEndingsBase { + // NOLINTBEGIN + constexpr static const char *data = "\n" // L1 + "\r" // L2 + "\r\n" // L3 + "\"\"\n" // L4 + "\"\"\r" // L5 + "\"\"\r\n" // L6 + ",\n" // L7 + ",\r" // L8 + ",\r\n" // L9 + "\"\",\"\"\n" // L10 + "\"\",\"\"\r" // L11 + "\"\",\"\"\r\n" // L12 + ",,\n" // L13 + ",,\r" // L14 + ",,\r\n" // L15 + "\"\",\"\",\n" // L16 + "\"\",\"\",\r" // L17 + "\"\",\"\",\r\n" // L18 + ",,"; // L19 + + // NOLINTEND +protected: + TestEmptyFieldsWithDifferentLineEndings() + : TestLineEndingsBase(data) + { + } +}; + +TEST_F(TestEmptyFieldsWithDifferentLineEndings, Part1) +{ + // Line 1 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + + // Line 2 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + + // Line 3 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + + // Line 4 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + + // Line 5 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + + // Line 6 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + + // Line 7 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + + // Line 8 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + + // Line 9 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + + // Line 10 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + + // Line 11 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + + // Line 12 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + + // Line 13 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + EXPECT_STREQ(csv_field(&csv_, 2), ""); + + // Line 14 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + EXPECT_STREQ(csv_field(&csv_, 2), ""); + + // Line 15 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + EXPECT_STREQ(csv_field(&csv_, 2), ""); + + // Line 16 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + EXPECT_STREQ(csv_field(&csv_, 2), ""); + + // Line 17 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + EXPECT_STREQ(csv_field(&csv_, 2), ""); + + // Line 18 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + EXPECT_STREQ(csv_field(&csv_, 2), ""); + + // Line 19 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), ""); + EXPECT_STREQ(csv_field(&csv_, 1), ""); + EXPECT_STREQ(csv_field(&csv_, 2), ""); + + // EOF + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(0)); + EXPECT_THAT(csv_nfields(&csv_), Eq(0)); +} + +class TestFieldsWithDifferentLineEndings : public TestLineEndingsBase { + // NOLINTBEGIN + static constexpr const char *data = "A\n" // L1 + "A\r" // L2 + "A\r\n" // L3 + "\"A\"\n" // L4 + "\"A\"\r" // L5 + "\"A\"\r\n" // L6 + "A,B\n" // L7 + "A,B\r" // L8 + "A,B\r\n" // L9 + "\"A\",\"B\"\n" // L10 + "\"A\",\"B\"\r" // L11 + "\"A\",\"B\"\r\n" // L12 + "A,B,C\n" // L13 + "A,B,C\r" // L14 + "A,B,C\r\n" // L15 + "\"A\",\"B\",C\n" // L16 + "\"A\",\"B\",C\r" // L17 + "\"A\",\"B\",C\r\n" // L18 + "A,B,C"; // L19 + + // NOLINTEND +protected: + TestFieldsWithDifferentLineEndings() + : TestLineEndingsBase(data) + { + } +}; + +TEST_F(TestFieldsWithDifferentLineEndings, Part1) +{ + // Line 1 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + + // Line 2 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + + // Line 3 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + + // Line 4 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + + // Line 5 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + + // Line 6 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + + // Line 7 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + + // Line 8 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + + // Line 9 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + + // Line 10 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + + // Line 11 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + + // Line 12 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + + // Line 13 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + EXPECT_STREQ(csv_field(&csv_, 2), "C"); + + // Line 14 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + EXPECT_STREQ(csv_field(&csv_, 2), "C"); + + // Line 15 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + EXPECT_STREQ(csv_field(&csv_, 2), "C"); + + // Line 16 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + EXPECT_STREQ(csv_field(&csv_, 2), "C"); + + // Line 17 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + EXPECT_STREQ(csv_field(&csv_, 2), "C"); + + // Line 18 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + EXPECT_STREQ(csv_field(&csv_, 2), "C"); + + // Line 19 + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(3)); + EXPECT_THAT(csv_nfields(&csv_), Eq(3)); + EXPECT_STREQ(csv_field(&csv_, 0), "A"); + EXPECT_STREQ(csv_field(&csv_, 1), "B"); + EXPECT_STREQ(csv_field(&csv_, 2), "C"); + + // EOF + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(0)); + EXPECT_THAT(csv_nfields(&csv_), Eq(0)); +} + +class TestQuotedFields : public TestLineEndingsBase { + // NOLINTBEGIN + static constexpr const char *data = "foo \"baz\" bar,foo \"\"baz\"\" bar,\"foo \"\"baz\"\" bar\",\"foo \"baz\" bar\",\"foo \"\"baz\"\", bar\""; + // NOLINTEND + +protected: + TestQuotedFields() + : TestLineEndingsBase(data) + { + } +}; + +TEST_F(TestQuotedFields, Part1) +{ + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(5)); + EXPECT_THAT(csv_nfields(&csv_), Eq(5)); + + EXPECT_STREQ(csv_field(&csv_, 0), "foo \"baz\" bar"); + EXPECT_STREQ(csv_field(&csv_, 1), "foo \"\"baz\"\" bar"); + EXPECT_STREQ(csv_field(&csv_, 2), "foo \"baz\" bar"); + EXPECT_STREQ(csv_field(&csv_, 3), "foo \"baz\" bar"); + EXPECT_STREQ(csv_field(&csv_, 4), "foo \"baz\", bar"); + + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(0)); + EXPECT_THAT(csv_nfields(&csv_), Eq(0)); +} + +class TestWrongQuotedFields : public TestLineEndingsBase { +protected: + TestWrongQuotedFields() + : TestLineEndingsBase("\"foo") + { + } +}; + +TEST_F(TestWrongQuotedFields, Part1) +{ + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(1)); + EXPECT_THAT(csv_nfields(&csv_), Eq(1)); + + EXPECT_STREQ(csv_field(&csv_, 0), "foo"); + + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(0)); + EXPECT_THAT(csv_nfields(&csv_), Eq(0)); +} + +class TestOutOfRangeError : public TestLineEndingsBase { +protected: + TestOutOfRangeError() + : TestLineEndingsBase(",") + { + csv_options.cb_error = &cb_error; + csv_init_opt(&csv_, &csv_options); + } + + static void cb_error(csv_err_t /*unused*/, void * /*unused*/) + { + throw out_of_range("out of range"); + } + + csv_options_t csv_options{ csv_default_options }; +}; + +TEST_F(TestOutOfRangeError, Part1) +{ + EXPECT_THAT(csv_read(&csv_, file_.get()), Eq(2)); + EXPECT_THAT(csv_nfields(&csv_), Eq(2)); + + EXPECT_THROW({ csv_field(&csv_, 2); }, out_of_range); +} -- cgit v1.3