diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..bd61ea1 --- /dev/null +++ b/.clang-format @@ -0,0 +1,273 @@ +--- +BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveShortCaseStatements: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCaseArrows: false + AlignCaseColons: false +AlignConsecutiveTableGenBreakingDAGArgColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveTableGenCondOperatorColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveTableGenDefinitionColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowBreakBeforeNoexceptSpecifier: Never +AllowShortBlocksOnASingleLine: Never +AllowShortCaseExpressionOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAdjacentStringLiterals: true +BreakAfterAttributes: Always +BreakAfterJavaFieldAnnotations: false +BreakAfterReturnType: None +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Allman +BreakBeforeConceptDeclarations: Always +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakFunctionDefinitionParameters: false +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +BreakTemplateDeclarations: MultiLine +ColumnLimit: 80 +CommentPragmas: "^ IWYU pragma:" +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: ^"(llvm|llvm-c|clang|clang-c)/ + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: ^(<|"(gtest|gmock|isl|json)/) + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: .* + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: (Test)?$ +IncludeIsMainSourceRegex: "" +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLines: + AtEndOfFile: false + AtStartOfBlock: true + AtStartOfFile: true +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: "" +MacroBlockEnd: "" +MainIncludeChar: Quote +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PPIndentWidth: -1 +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakScopeResolution: 500 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +RemoveParentheses: Leave +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SkipMacroDefinitionBody: false +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeJsonColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDeclarationName: false + AfterFunctionDefinitionName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterPlacementOperator: true + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParens: Never +SpacesInParensOptions: + ExceptDoubleParentheses: false + InConditionalStatements: false + InCStyleCasts: false + InEmptyParentheses: false + Other: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +TableGenBreakInsideDAGArg: DontBreak +UseTab: Never +VerilogBreakBetweenInstancePorts: true +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b010b19 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.cache/ +compile_commands.json diff --git a/set.c b/set.c new file mode 100644 index 0000000..6464991 --- /dev/null +++ b/set.c @@ -0,0 +1,126 @@ +#include "set.h" +#include +#include +#include +#include + +#include "set_asserts.h" + +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_BLUE "\x1b[34m" +#define ANSI_COLOR_MAGENTA "\x1b[35m" +#define ANSI_COLOR_CYAN "\x1b[36m" +#define ANSI_COLOR_RESET "\x1b[0m" + +// clang-format off +#define BANNER \ +"" \ +" _____ _ _ _____ _ _____ _ \n"\ +" / ___| | | | | ___| | | |_ _| | | \n"\ +" \\ `--. _ __ ___ __ _| | | | |__ _ __ ___ _ _ __ _| |__ | | ___ ___| |_ ___ _ __ \n"\ +" `--. | '_ ` _ \\ / _` | | | | __| '_ \\ / _ \\| | | |/ _` | '_ \\ | |/ _ / __| __/ _ | '__|\n"\ +" /\\__/ | | | | | | (_| | | | | |__| | | | (_) | |_| | (_| | | | | | | __\\__ | || __| | \n"\ +" \\____/|_| |_| |_|\\__,_|_|_| \\____|_| |_|\\___/ \\__,_|\\__, |_| |_| \\_/\\___|___/\\__\\___|_| \n"\ +" __/ | \n"\ +" |___/ \n"\ +"" +//clang-format on + +char* format_string(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + size_t size = vsnprintf(NULL, 0, fmt, args); + char* out = (char*)malloc(size + 1); + + if (!out) { + fprintf(stderr, "%s", "Failed to allocate for format string.\n"); + } + + va_end(args); + + va_start(args, fmt); + vsnprintf(out, size + 1, fmt, args); + va_end(args); + + return out; +} + +static void log_test_start(const char* name) { printf("Running test: %s", name); } + +static void log_test_summary(struct SETest* test) +{ + if (test->passed) { +#ifdef COLORIZED + printf(ANSI_COLOR_GREEN " - passed.\n" ANSI_COLOR_RESET); +#else + printf(" - passed.\n"); +#endif + } else { +#ifdef COLORIZED + printf(ANSI_COLOR_RED " - failed.\n" ANSI_COLOR_RESET " Reason: \n %s\n\n", + test->error_msg); +#else + printf(" - failed.\n Reason: \n %s\n\n", test->error_msg); +#endif + } +} + +static void dispatch_tests(struct SETSuit* suit) +{ + printf("\n\n==== Running suit: %s ====\n\n", suit->name); + for (int i = 0; i < suit->len; i++) { + struct SETest* test = suit->tests[i]; + log_test_start(test->name); + test->function(test); + log_test_summary(test); + fflush(stdout); + } +} + + +static void free_suit(struct SETSuit* suit) +{ + for (int i = 0; i < suit->len; i++) { + struct SETest* test = suit->tests[i]; + free((void*)test->error_msg); + free((void*)test); + } + + free(suit->tests); + free(suit); +} + + +static void dispatch_test_suits(struct SETSuit** suits, int counter) +{ + for (int i = 0; i < counter; i++) + { + dispatch_tests(suits[i]); + free_suit(suits[i]); + } +} + +int main(int argc, char** argv) +{ + #ifndef NO_BANNER + + printf("\n\n%s", BANNER); + #endif /* ifndef NO_BANNER */ + + int counter = 0; + + set_bundle_suits(NULL, &counter, true); + + struct SETSuit ** suits = (struct SETSuit**)malloc(counter * sizeof(struct SETSuit*)); + + counter = 0; + + set_bundle_suits(suits, &counter, false); + + dispatch_test_suits(suits, counter); + + free(suits); +} diff --git a/set.h b/set.h new file mode 100644 index 0000000..b72aa09 --- /dev/null +++ b/set.h @@ -0,0 +1,76 @@ +#include +#include +#include + +#ifndef INCLUDE_SET_H +#define INCLUDE_SET_H + +struct SETest +{ + char *name; + void (*function)(struct SETest *test); + bool passed; + char *error_msg; +}; + +struct SETSuit +{ + char *name; + struct SETest **tests; + bool passed; + int len; +}; + +char *format_string(const char *fmt, ...); + +// To be implemented by user. +void set_bundle_suits(struct SETSuit **suits, int *counter, bool count); + +#define BUNDLE() \ + void set_bundle_suits(struct SETSuit **suits, int *counter, bool count) + +#define ADD_SUIT(suit_name) \ + if (!count) \ + { \ + suits[(*counter)] = suit_name##_suit_contructor(); \ + } \ + (*counter)++; + +#define SUIT(suit_name) \ + void suit_name##_suit(struct SETSuit *suit, bool count); \ + struct SETSuit *suit_name##_suit_contructor() \ + { \ + struct SETSuit *suit = \ + (struct SETSuit *)malloc(sizeof(struct SETSuit)); \ + suit->name = #suit_name; \ + suit_name##_suit(suit, true); \ + suit->tests = \ + (struct SETest **)malloc(suit->len * sizeof(struct SETest *)); \ + suit->len = 0; \ + suit_name##_suit(suit, false); \ + return suit; \ + } \ + void suit_name##_suit(struct SETSuit *suit, bool count) + +#define ADD_TEST(test_name) \ + if (!count) \ + { \ + suit->tests[suit->len] = test_name##_test_constructor(); \ + } \ + suit->len++; + +// TEST MACRO +#define TEST(test_name) \ + void test_name##_test(struct SETest *test); \ + struct SETest *test_name##_test_constructor() \ + { \ + struct SETest *test = (struct SETest *)malloc(sizeof(struct SETest)); \ + test->name = #test_name; \ + test->function = &test_name##_test; \ + test->passed = true; \ + test->error_msg = NULL; \ + return test; \ + } \ + void test_name##_test(struct SETest *test) + +#endif // !INCLUDE_SET_H diff --git a/set_asserts.h b/set_asserts.h new file mode 100644 index 0000000..06cd0a3 --- /dev/null +++ b/set_asserts.h @@ -0,0 +1,88 @@ +#include +#include + +#include "set.h" + +#ifndef ASSERT_H +#define ASSERT_H + +#define STATIC_ASSERT(condition) (void)sizeof(char[1 - 2 * (!(condition))]) + +#define ASSERT_EQ(exp, act) \ + if (exp != act) \ + { \ + test->passed = false; \ + test->error_msg = format_string("Expected %d to be %d\n.", act, exp); \ + return; \ + } + +#define ASSERT_NEQ(exp, act) \ + if (exp == act) \ + { \ + test->passed = false; \ + test->error_msg = \ + format_string("Expect %d to be %d\n", act, exp) return; \ + } + +#define ASSERT_EQ_MSG(exp, act, msg) \ + if (exp != act) \ + { \ + test->passed = false; \ + test->error_msg = format_string( \ + "Expected %d to be %d\n Failed with message: " msg, act, exp); \ + return; \ + } + +#define ASSERT_TRUE(x) \ + if (!x) \ + { \ + test->passed = false; \ + test->error_msg = format_string("Expected true got false."); \ + return; \ + } + +#define ASSERT_FALSE(x) \ + if (x) \ + { \ + test->passed = false; \ + test->error_msg = format_string("Expected false got true."); \ + return; \ + } + +#define ASSERT_EQ_FLOAT(exp, act, epsilon) \ + if (fabs(exp - act) <= epsilon) \ + { \ + test->passed = false; \ + test->error_msg = \ + format_string("Expected %f to be %f (e: %f)", act, exp, epsilon); \ + return; \ + } + +#define ASSERT_NEQ_FLOAT(exp, act, epsilon) \ + if (fabs(exp - act) > epsilon) \ + { \ + test->passed = false; \ + test->error_msg = \ + format_string("Expected %f to be %f (e: %f)", act, exp, epsilon); \ + return; \ + } + +#define ASSERT_EQ_STR(exp, act) \ + if (strcmp(exp, act) != 0) \ + { \ + test->passed = false; \ + type->error_msg = \ + format_string("Expected: %d\nBut got: %d", act, exp); \ + return; \ + } + +#define ASSERT_NEQ_STR(exp, act) \ + if (strcmp(exp, act) == 0) \ + { \ + test->passed = false; \ + type->error_msg = \ + format_string("Expected: %d\nBut got: %d", act, exp); \ + return; \ + } + +#endif diff --git a/testtest.c b/testtest.c new file mode 100644 index 0000000..6852684 --- /dev/null +++ b/testtest.c @@ -0,0 +1,42 @@ +#include "set.h" +#include "set_asserts.h" + +int fac(int n) +{ + if (n <= 1) + return 1; + return n * fac(n - 1); +} + +TEST(Faculty_Basic) +{ + ASSERT_EQ(fac(5), 120); + ASSERT_EQ(fac(4), 24); + ASSERT_EQ(fac(11), 39916800); +} + +TEST(Faculty_Negative) +{ + ASSERT_EQ(fac(-2), 1); + ASSERT_EQ(fac(0), 1); +} + +SUIT(Basic) +{ + ADD_TEST(Faculty_Basic); + ADD_TEST(Faculty_Negative); +} + +TEST(Other_Basic) +{ + ASSERT_TRUE(true); + ASSERT_FALSE(false); +} + +SUIT(Other) { ADD_TEST(Other_Basic); } + +BUNDLE() +{ + ADD_SUIT(Basic); + ADD_SUIT(Other); +}