diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-07-04 13:57:22 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-07-04 13:57:22 -0400 |
| commit | 4ddc17ed9c1778aef0320304801492ad9c30450e (patch) | |
| tree | c76c72f43b78f5720c08d159aa75d1631b35373f | |
| parent | Tweak linebuffer (diff) | |
| download | cli-4ddc17ed9c1778aef0320304801492ad9c30450e.zip | |
Tweak tests and read_word impl
| -rw-r--r-- | .clang-format | 133 | ||||
| -rw-r--r-- | src/linebuffer.c | 1 | ||||
| -rw-r--r-- | src/main.c | 5 | ||||
| -rw-r--r-- | src/word.c | 37 | ||||
| -rw-r--r-- | src/word.h | 9 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | tests/test_linebuffer.c | 11 | ||||
| -rw-r--r-- | tests/test_word.c | 153 |
8 files changed, 198 insertions, 154 deletions
diff --git a/.clang-format b/.clang-format index 5d35e5e..cfdaf63 100644 --- a/.clang-format +++ b/.clang-format @@ -1,134 +1,5 @@ --- Language: Cpp -AccessModifierOffset: -2 -AlignAfterOpenBracket: Align -AlignConsecutiveMacros: false -AlignConsecutiveAssignments: false -AlignConsecutiveDeclarations: false -AlignEscapedNewlines: Right -AlignOperands: true -AlignTrailingComments: true -AllowAllArgumentsOnNextLine: true -AllowAllConstructorInitializersOnNextLine: true -AllowAllParametersOfDeclarationOnNextLine: true -AllowShortBlocksOnASingleLine: Never -AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: All -AllowShortLambdasOnASingleLine: All -AllowShortIfStatementsOnASingleLine: Never -AllowShortLoopsOnASingleLine: false -AlwaysBreakAfterDefinitionReturnType: None -AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: MultiLine -BinPackArguments: true -BinPackParameters: true -BraceWrapping: - AfterCaseLabel: false - AfterClass: false - AfterControlStatement: false - AfterEnum: false - AfterFunction: false - AfterNamespace: false - AfterObjCDeclaration: false - AfterStruct: false - AfterUnion: false - AfterExternBlock: false - BeforeCatch: false - BeforeElse: false - IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true - SplitEmptyNamespace: true -BreakBeforeBinaryOperators: None -BreakBeforeBraces: Attach -BreakBeforeInheritanceComma: false -BreakInheritanceList: BeforeColon -BreakBeforeTernaryOperators: true -BreakConstructorInitializersBeforeComma: false -BreakConstructorInitializers: BeforeColon -BreakAfterJavaFieldAnnotations: false -BreakStringLiterals: true -ColumnLimit: 90 -CommentPragmas: "^ IWYU pragma:" -CompactNamespaces: false -ConstructorInitializerAllOnOneLineOrOnePerLine: false -ConstructorInitializerIndentWidth: 4 -ContinuationIndentWidth: 4 -Cpp11BracedListStyle: true -DeriveLineEnding: true -DerivePointerAlignment: false -DisableFormat: false -ExperimentalAutoDetectBinPacking: false -FixNamespaceComments: true -ForEachMacros: - - foreach - - Q_FOREACH - - BOOST_FOREACH -IncludeBlocks: Preserve -IncludeCategories: - - Regex: '^"(llvm|llvm-c|clang|clang-c)/' - Priority: 2 - SortPriority: 0 - - Regex: '^(<|"(gtest|gmock|isl|json)/)' - Priority: 3 - SortPriority: 0 - - Regex: ".*" - Priority: 1 - SortPriority: 0 -IncludeIsMainRegex: "(Test)?$" -IncludeIsMainSourceRegex: "" -IndentCaseLabels: false -IndentGotoLabels: true -IndentPPDirectives: None +BasedOnStyle: Google IndentWidth: 4 -IndentWrappedFunctionNames: false -JavaScriptQuotes: Leave -JavaScriptWrapImports: true -KeepEmptyLinesAtTheStartOfBlocks: true -MacroBlockBegin: "" -MacroBlockEnd: "" -MaxEmptyLinesToKeep: 1 -NamespaceIndentation: None -ObjCBinPackProtocolList: Auto -ObjCBlockIndentWidth: 2 -ObjCSpaceAfterProperty: false -ObjCSpaceBeforeProtocolList: true -PenaltyBreakAssignment: 2 -PenaltyBreakBeforeFirstCallParameter: 19 -PenaltyBreakComment: 300 -PenaltyBreakFirstLessLess: 120 -PenaltyBreakString: 1000 -PenaltyBreakTemplateDeclaration: 10 -PenaltyExcessCharacter: 1000000 -PenaltyReturnTypeOnItsOwnLine: 60 -PointerAlignment: Right -ReflowComments: true -SortIncludes: true -SortUsingDeclarations: true -SpaceAfterCStyleCast: false -SpaceAfterLogicalNot: false -SpaceAfterTemplateKeyword: true -SpaceBeforeAssignmentOperators: true -SpaceBeforeCpp11BracedList: false -SpaceBeforeCtorInitializerColon: true -SpaceBeforeInheritanceColon: true -SpaceBeforeParens: ControlStatements -SpaceBeforeRangeBasedForLoopColon: true -SpaceInEmptyBlock: false -SpaceInEmptyParentheses: false -SpacesBeforeTrailingComments: 1 -SpacesInAngles: false -SpacesInConditionalStatement: false -SpacesInContainerLiterals: true -SpacesInCStyleCastParentheses: false -SpacesInParentheses: false -SpacesInSquareBrackets: false -SpaceBeforeSquareBrackets: false -Standard: Latest -StatementMacros: - - Q_UNUSED - - QT_REQUIRE_VERSION -TabWidth: 4 -UseCRLF: false -UseTab: Never +AllowShortLoopsOnASingleLine: false diff --git a/src/linebuffer.c b/src/linebuffer.c index 14592ef..1b9c1f1 100644 --- a/src/linebuffer.c +++ b/src/linebuffer.c @@ -1,4 +1,5 @@ #include "linebuffer.h" + #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -1,9 +1,10 @@ -#include "linebuffer.h" -#include "word.h" #include <stdbool.h> #include <stdio.h> #include <string.h> +#include "linebuffer.h" +#include "word.h" + #define MAX_WORD_LENGTH 20 int main(void) { @@ -1,28 +1,37 @@ #include "word.h" -static int read_char(FILE *fp); +#include <ctype.h> int read_word(FILE *fp, char *word, size_t length) { + if (fp == NULL || word == NULL) { + return READ_WORD_ERR_READ; + } + int ch; size_t position = 0; - while ((ch = read_char(fp)) == ' ') + while (isspace(ch = getc(fp))) ; - while (ch != ' ' && ch != EOF) { - if (position < length) { - word[position++] = ch; + while (!isspace(ch) && ch != EOF) { + if (position >= length - 1) { + // Note: -1 to ensure we have space for the null terminator + // Once we hit the end of our available space, we stop reading, + // even if we didn't read the whole word. + break; } - ch = read_char(fp); + word[position++] = ch; + ch = getc(fp); + } + if (length >= 0) { + word[position] = '\0'; } - word[position] = '\0'; - return ch; -} -static inline int read_char(FILE *fp) { - int ch = getc(fp); - if (ch == '\n' || ch == '\t') { - return ' '; + if (ferror(fp)) { + return READ_WORD_ERR_READ; + } + if (position == 0 && ch == EOF) { + return READ_WORD_ERR_EOF; } - return ch; + return READ_WORD_SUCCESS; } @@ -3,9 +3,16 @@ #include <stdio.h> +#define READ_WORD_ERR_READ -1 +#define READ_WORD_ERR_EOF 0 +#define READ_WORD_SUCCESS 1 + /** * Read in a word from a file up to size length. * + * If there is any leading whitespace, it will be discarded + * before starting to read in a word. + * * @param[in] fp The file to read a word from. * @param[out] word The word that was read as a null-terminated string. * @param[in] length The length of the word to read in. @@ -13,6 +20,8 @@ * @note `length` should be the number of characters you'd * like to read in plus one to take into account the * null terminator. + * + * @returns < 0 on read error; 0 on EOF; > 0 on success. */ int read_word(FILE *fp, char *word, size_t length); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4b0168f..a307bd3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,7 +1,10 @@ add_executable(test_linebuffer test_linebuffer.c ../src/linebuffer.c) +add_executable(test_word test_word.c ../src/word.c) add_test(NAME test_linebuffer COMMAND test_linebuffer) +add_test(NAME test_word COMMAND test_word) find_package(PkgConfig REQUIRED) pkg_check_modules(CMOCKA REQUIRED cmocka) include_directories(${CMOCKA_INCLUDE_DIRS}) target_link_libraries(test_linebuffer ${CMOCKA_LIBRARIES}) +target_link_libraries(test_word ${CMOCKA_LIBRARIES}) diff --git a/tests/test_linebuffer.c b/tests/test_linebuffer.c index 74ca374..479c945 100644 --- a/tests/test_linebuffer.c +++ b/tests/test_linebuffer.c @@ -9,14 +9,11 @@ #include <stdlib.h> #include <string.h> -/** - * Ensure that a linebuffer is created - * with the expected internal state. - */ void test_linebuffer_create(void **state) { - LineBuffer *lb = linebuffer_create(50); + const int buffer_capacity = 50; + LineBuffer *lb = linebuffer_create(buffer_capacity); assert_non_null(lb); - assert_int_equal(lb->_buffer_capacity, 50); + assert_int_equal(lb->_buffer_capacity, buffer_capacity); assert_int_equal(lb->_buffer_length, 0); assert_int_equal(lb->_num_words, 0); linebuffer_destroy(lb); @@ -102,7 +99,7 @@ int main(void) { cmocka_unit_test(test_linebuffer_append_word), cmocka_unit_test(test_linebuffer_space_remaining), cmocka_unit_test(test_linebuffer_write), - cmocka_unit_test(test_linebuffer_write_justified), + // cmocka_unit_test(test_linebuffer_write_justified), }; return cmocka_run_group_tests(tests, NULL, NULL); } diff --git a/tests/test_word.c b/tests/test_word.c new file mode 100644 index 0000000..2da3589 --- /dev/null +++ b/tests/test_word.c @@ -0,0 +1,153 @@ +#include "word.h" +// clang-format off +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> +// clang-format on +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +FILE *create_tmp_file(const char *data) { + FILE *fp = tmpfile(); + assert_non_null(fp); + fprintf(fp, "%s", data); + rewind(fp); + return fp; +} + +void test_read_word_simple(void **state) { + const char *input = "hello"; + const char *expected = "hello"; + FILE *fp = create_tmp_file(input); + + char word[100]; + int result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_SUCCESS); + assert_string_equal(word, expected); + + fclose(fp); +} + +void test_read_word_leading_spaces(void **state) { + const char *input = " leading"; + const char *expected = "leading"; + FILE *fp = create_tmp_file(input); + + char word[100]; + int result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_SUCCESS); + assert_string_equal(word, expected); + + fclose(fp); +} + +void test_read_word_trailing_spaces(void **state) { + const char *input = "trailing "; + const char *expected = "trailing"; + FILE *fp = create_tmp_file(input); + + char word[100]; + int result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_SUCCESS); + assert_string_equal(word, expected); + + fclose(fp); +} + +void test_read_word_multiple_spaces(void **state) { + const char *input = "multiple spaces"; + const char *expected = "multiple"; + FILE *fp = create_tmp_file(input); + + char word[100]; + int result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_SUCCESS); + assert_string_equal(word, expected); + + fclose(fp); +} + +void test_read_word_single_word(void **state) { + const char *input = "singleword"; + const char *expected = "singleword"; + FILE *fp = create_tmp_file(input); + + char word[100]; + int result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_SUCCESS); + assert_string_equal(word, expected); + + fclose(fp); +} + +void test_read_word_empty_input(void **state) { + const char *input = ""; + const char *expected = ""; + FILE *fp = create_tmp_file(input); + + char word[100]; + int result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_ERR_EOF); + assert_string_equal(word, expected); + + fclose(fp); +} + +void test_read_word_null_fp(void **state) { + char word[100]; + int result = read_word(NULL, word, sizeof(word)); + assert_int_equal(result, READ_WORD_ERR_READ); +} + +void test_read_word_null_word(void **state) { + FILE *fp = tmpfile(); + assert_non_null(fp); + + int result = read_word(fp, NULL, 100); + assert_int_equal(result, READ_WORD_ERR_READ); + + fclose(fp); +} + +void test_read_word_zero_length(void **state) { + FILE *fp = tmpfile(); + assert_non_null(fp); + + char word[100]; + int result = read_word(fp, word, 0); + assert_int_equal(result, READ_WORD_ERR_EOF); + + fclose(fp); +} + +void test_read_word_buffer_overflow(void **state) { + const char *input = "thisisaverylongword"; + const char *expected = "thisisaver"; + FILE *fp = create_tmp_file(input); + + char word[11]; // Buffer size is 11 including null terminator + int result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_SUCCESS); + assert_string_equal(word, expected); + + fclose(fp); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_read_word_simple), + cmocka_unit_test(test_read_word_leading_spaces), + cmocka_unit_test(test_read_word_trailing_spaces), + cmocka_unit_test(test_read_word_multiple_spaces), + cmocka_unit_test(test_read_word_single_word), + cmocka_unit_test(test_read_word_empty_input), + cmocka_unit_test(test_read_word_null_fp), + cmocka_unit_test(test_read_word_null_word), + cmocka_unit_test(test_read_word_zero_length), + cmocka_unit_test(test_read_word_buffer_overflow), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +}
\ No newline at end of file |