From 311f5533903c3f35de6e0bdaee8f612f40820598 Mon Sep 17 00:00:00 2001 From: Simon Wiesend Date: Sun, 7 Dec 2025 17:26:16 +0100 Subject: [PATCH] add numbers.c test --- test_numbers.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test_numbers.c b/test_numbers.c index 630d239..1ffc34b 100644 --- a/test_numbers.c +++ b/test_numbers.c @@ -1,6 +1,7 @@ #include "unity.h" #include "numbers.h" #include "stdlib.h" +#include "string.h" static int compareInt(const void *ptr1, const void *ptr2); @@ -48,13 +49,29 @@ void test_for_triple(void) unsigned int *numbers = createNumbers(3); if (numbers[0] == numbers[1] && numbers[1] == numbers[2]) { - // fail the test - TEST_ASSERT(0); + TEST_FAIL_MESSAGE("triple generated"); } free(numbers); } } +// check if getDuplicate() modifies the original array (it should not) +void test_get_duplicate_does_modify() +{ + unsigned int arr1[] = {1, 2, 3, 4, 5, 4, 3, 2, 1}; // sorting would change this + size_t len1 = sizeof(arr1) / sizeof(arr1[0]); + unsigned int arr1Copy[5]; + memcpy(arr1Copy, arr1, len1); + + getDuplicate(arr1, len1); // return value does not matter + + // check if the arrays are still the same + if (memcmp(arr1, arr1Copy, len1)) + { + TEST_FAIL_MESSAGE("Arrays have diverged"); + } +} + // checks if there is exactly 1 duplicate number at varying array sizes void test_exactly_one_duplicate() { @@ -105,5 +122,6 @@ int main(void) RUN_TEST(test_for_triple); RUN_TEST(test_exactly_one_duplicate); RUN_TEST(test_get_duplicate); + RUN_TEST(test_get_duplicate_does_modify); return UNITY_END(); } \ No newline at end of file