From 698a6b2bf76437ab79e791fc03cae0ca94292421 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 4 Jul 2024 11:29:19 -0400 Subject: Tweak clang format and tests --- .clang-format | 132 +++++++++++++++++++++++++++++++++++++++++++++++- CMakeLists.txt | 5 ++ tests/CMakeLists.txt | 7 +++ tests/test_linebuffer.c | 108 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/test_linebuffer.c diff --git a/.clang-format b/.clang-format index 93e1d33..5d35e5e 100644 --- a/.clang-format +++ b/.clang-format @@ -1,4 +1,134 @@ --- Language: Cpp -BasedOnStyle: LLVM +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 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 diff --git a/CMakeLists.txt b/CMakeLists.txt index f5724e8..27230b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,3 +17,8 @@ set(SRC_FILES ) include_directories(src) add_executable(justify ${SRC_FILES}) + +include(CTest) +if(BUILD_TESTING) + add_subdirectory(tests) +endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..4b0168f --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,7 @@ +add_executable(test_linebuffer test_linebuffer.c ../src/linebuffer.c) +add_test(NAME test_linebuffer COMMAND test_linebuffer) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(CMOCKA REQUIRED cmocka) +include_directories(${CMOCKA_INCLUDE_DIRS}) +target_link_libraries(test_linebuffer ${CMOCKA_LIBRARIES}) diff --git a/tests/test_linebuffer.c b/tests/test_linebuffer.c new file mode 100644 index 0000000..74ca374 --- /dev/null +++ b/tests/test_linebuffer.c @@ -0,0 +1,108 @@ +#include "linebuffer.h" +// clang-format off +#include +#include +#include +#include +// clang-format on +#include +#include +#include + +/** + * Ensure that a linebuffer is created + * with the expected internal state. + */ +void test_linebuffer_create(void **state) { + LineBuffer *lb = linebuffer_create(50); + assert_non_null(lb); + assert_int_equal(lb->_buffer_capacity, 50); + assert_int_equal(lb->_buffer_length, 0); + assert_int_equal(lb->_num_words, 0); + linebuffer_destroy(lb); +} + +void test_linebuffer_append_word(void **state) { + LineBuffer *lb = linebuffer_create(50); + assert_non_null(lb); + + linebuffer_append_word(lb, "hello"); + assert_string_equal(lb->_buffer, "hello"); + assert_int_equal(lb->_buffer_length, 5); + assert_int_equal(lb->_num_words, 1); + + linebuffer_append_word(lb, "world"); + assert_string_equal(lb->_buffer, "hello world"); + assert_int_equal(lb->_buffer_length, 11); + assert_int_equal(lb->_num_words, 2); + + linebuffer_destroy(lb); +} + +void test_linebuffer_space_remaining(void **state) { + LineBuffer *lb = linebuffer_create(50); + assert_non_null(lb); + + linebuffer_append_word(lb, "hello"); + assert_int_equal(linebuffer_space_remaining(lb), 45); + + linebuffer_append_word(lb, "world"); + assert_int_equal(linebuffer_space_remaining(lb), 39); + + linebuffer_append_word(lb, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + printf("%ld\n", linebuffer_space_remaining(lb)); + assert_int_equal(linebuffer_space_remaining(lb), 0); + + linebuffer_destroy(lb); +} + +void test_linebuffer_write(void **state) { + LineBuffer *lb = linebuffer_create(50); + assert_non_null(lb); + + linebuffer_append_word(lb, "hello"); + linebuffer_append_word(lb, "world"); + + FILE *fp = tmpfile(); + assert_non_null(fp); + linebuffer_write(lb, fp); + + rewind(fp); + char buffer[50]; + fgets(buffer, sizeof(buffer), fp); + assert_string_equal(buffer, "hello world"); + + fclose(fp); + linebuffer_destroy(lb); +} + +void test_linebuffer_write_justified(void **state) { + LineBuffer *lb = linebuffer_create(20); + assert_non_null(lb); + + linebuffer_append_word(lb, "hello"); + linebuffer_append_word(lb, "world"); + + FILE *fp = tmpfile(); + assert_non_null(fp); + linebuffer_write_justified(lb, fp); + + rewind(fp); + char buffer[50]; + fgets(buffer, sizeof(buffer), fp); + assert_string_equal(buffer, "hello world\n"); + + fclose(fp); + linebuffer_destroy(lb); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_linebuffer_create), + 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), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} -- cgit v1.2.3