From 935ed0235d0454cd18559fb0f6ee58e0647723fd Mon Sep 17 00:00:00 2001 From: physick Date: Sat, 29 Aug 2026 15:01:27 +0500 Subject: difficulty calcualtion fix + unit test --- build.zig | 3 ++- src/puzzles/distractions.c | 20 ++++++++++------ src/tests/difficulty_calc.c | 57 +++++++++++++++++++++++++++++++++++++++++++++ src/tests/runner.c | 1 + src/tests/test.h | 1 + 5 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 src/tests/difficulty_calc.c diff --git a/build.zig b/build.zig index 2d7c1ce..5e4b696 100644 --- a/build.zig +++ b/build.zig @@ -21,7 +21,7 @@ const common_c_files = &[_][]const u8 { "src/scenes/collectionScene.c", "src/puzzles/puzzle.c", "src/puzzles/parser.c", - "src/puzzles//distractions.c", + "src/puzzles/distractions.c", }; pub fn build(b: *std.Build) void { @@ -70,6 +70,7 @@ pub fn build(b: *std.Build) void { .files = &[_][]const u8 { "src/tests/runner.c", "src/tests/arena_tests.c", + "src/tests/difficulty_calc.c", } }); diff --git a/src/puzzles/distractions.c b/src/puzzles/distractions.c index bd5db87..c2c57fb 100644 --- a/src/puzzles/distractions.c +++ b/src/puzzles/distractions.c @@ -1,5 +1,6 @@ #include "puzzle.h" #include +#include #include #include @@ -18,12 +19,16 @@ bool isInSignatiureStack(signatureDescriptor *signatures_stack, size_t stack_len size_t signature_len = strlen(signature); for (size_t i = 0; i < stack_len; ++i) { signatureDescriptor *desc = &signatures_stack[i]; - if (!strncmp(reciever, desc->reciever_name, reciever_len)) - return false; - if (!strncmp(signature, desc->method_signature, signature_len)) - return false; + // printf("comparing %s.%s and %s.%s\n", desc->reciever_name, desc->method_signature, + // reciever, signature); + if ( strlen(desc->reciever_name) == reciever_len + && strncmp(reciever, desc->reciever_name, reciever_len) == 0 + && strlen(desc->method_signature) == signature_len + && strncmp(signature, desc->method_signature, signature_len) == 0) + return true; + // printf("Not equal \n"); } - return true; + return false; } // The naive implementation relies on the amount of tests and function overloads @@ -40,7 +45,7 @@ size_t deriveDifficuty(Puzzle *puzzle) { size_t stack_len = 0; for (size_t i = 0; i < puzzle->TestC; ++i) { Test *test = &puzzle->Tests[i]; - if (isInSignatiureStack(signatures_stack, stack_len, + if (!isInSignatiureStack(signatures_stack, stack_len, test->RecieverName, test->MainMethodSignature)) { signatures_stack[stack_len] = (signatureDescriptor) { .reciever_name = test->RecieverName, @@ -49,8 +54,9 @@ size_t deriveDifficuty(Puzzle *puzzle) { stack_len += 1; } } + // printf("stack len: %zu\n", stack_len); free(signatures_stack); - return puzzle->TestC * stack_len + 1; + return puzzle->TestC * stack_len; } DistractionPuzzle *GetRandomDistraction() { diff --git a/src/tests/difficulty_calc.c b/src/tests/difficulty_calc.c new file mode 100644 index 0000000..24796e9 --- /dev/null +++ b/src/tests/difficulty_calc.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include "../puzzles/puzzle.h" + +extern size_t deriveDifficuty(Puzzle *puzzle); + +// we can't use the global version since we don't strdup here +void free_puzzle(Puzzle *p) { + if (p == NULL) return; + if (p->Tests != NULL) free(p->Tests); + free(p); +} + +char *difficultyCalculator() { + Puzzle *simple_puzzle = malloc(sizeof(Puzzle)); + if (simple_puzzle == NULL) return "Puzzle allocation failed"; + simple_puzzle->TestC = 2; + simple_puzzle->Tests = malloc(sizeof(Test) * 2); + if (simple_puzzle->Tests == NULL) return "Test array allocation failed"; + + simple_puzzle->Tests[0].MainMethodSignature = "sig"; + simple_puzzle->Tests[0].RecieverName = "rec"; + + simple_puzzle->Tests[1].MainMethodSignature = "sig"; + simple_puzzle->Tests[1].RecieverName = "rec"; + + size_t calcualted_diff = deriveDifficuty(simple_puzzle); + if (calcualted_diff != 2) { + free_puzzle(simple_puzzle); + printf("Got: %zu, expected: %d\n", calcualted_diff, 2); + return "The difficulty calculation for puzzles with one method is incorrect"; + } + + simple_puzzle->Tests[1].MainMethodSignature = "sig1"; + + calcualted_diff = deriveDifficuty(simple_puzzle); + if (calcualted_diff != 4) { + free_puzzle(simple_puzzle); + printf("Got: %zu, expected: %d\n", calcualted_diff, 4); + return "The difficulty calculation for puzzles with two methods (different signature) is incorrect"; + } + + simple_puzzle->Tests[1].MainMethodSignature = "sig"; + simple_puzzle->Tests[1].RecieverName = "re1"; + + calcualted_diff = deriveDifficuty(simple_puzzle); + if (calcualted_diff != 4) { + free_puzzle(simple_puzzle); + printf("Got: %zu, expected: %d\n", calcualted_diff, 4); + return "The difficulty calculation for puzzles with two methods (different recievers) is incorrect"; + } + // FreePuzzle(simple_puzzle); + free_puzzle(simple_puzzle); + + return NULL; +} diff --git a/src/tests/runner.c b/src/tests/runner.c index 68c17bf..bdc4031 100644 --- a/src/tests/runner.c +++ b/src/tests/runner.c @@ -4,6 +4,7 @@ const UnitTest TESTS[] = { (UnitTest) { .Name = "Arena out of mem", .Function = ArenaOutOfMemory }, (UnitTest) { .Name = "Arena allocation", .Function = ArenaAllocation }, + (UnitTest) { .Name = "Test difficulty calculation", .Function = difficultyCalculator}, }; void SetGreenColor() { diff --git a/src/tests/test.h b/src/tests/test.h index d2c4aeb..5994ecb 100644 --- a/src/tests/test.h +++ b/src/tests/test.h @@ -12,5 +12,6 @@ typedef struct { char *ArenaAllocation(); char *ArenaOutOfMemory(); +char *difficultyCalculator(); #endif // TESTS_H -- cgit v1.3