diff options
| author | physick <mynameisgennadiy@vk.com> | 2026-08-29 15:01:27 +0500 |
|---|---|---|
| committer | physick <mynameisgennadiy@vk.com> | 2026-08-29 15:01:27 +0500 |
| commit | 935ed0235d0454cd18559fb0f6ee58e0647723fd (patch) | |
| tree | d9ffad6961feadd17664057fd6bae7002d684b35 /src | |
| parent | a6ce29f64683c63c5a42c000d60bc467aee3e84d (diff) | |
difficulty calcualtion fix + unit test
Diffstat (limited to 'src')
| -rw-r--r-- | src/puzzles/distractions.c | 20 | ||||
| -rw-r--r-- | src/tests/difficulty_calc.c | 57 | ||||
| -rw-r--r-- | src/tests/runner.c | 1 | ||||
| -rw-r--r-- | src/tests/test.h | 1 |
4 files changed, 72 insertions, 7 deletions
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 <raylib.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -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 <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#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 |
