add numbers.c test
This commit is contained in:
parent
0e408e5ec9
commit
311f553390
@ -1,6 +1,7 @@
|
|||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
#include "stdlib.h"
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
static int compareInt(const void *ptr1, const void *ptr2);
|
static int compareInt(const void *ptr1, const void *ptr2);
|
||||||
|
|
||||||
@ -48,13 +49,29 @@ void test_for_triple(void)
|
|||||||
unsigned int *numbers = createNumbers(3);
|
unsigned int *numbers = createNumbers(3);
|
||||||
if (numbers[0] == numbers[1] && numbers[1] == numbers[2])
|
if (numbers[0] == numbers[1] && numbers[1] == numbers[2])
|
||||||
{
|
{
|
||||||
// fail the test
|
TEST_FAIL_MESSAGE("triple generated");
|
||||||
TEST_ASSERT(0);
|
|
||||||
}
|
}
|
||||||
free(numbers);
|
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
|
// checks if there is exactly 1 duplicate number at varying array sizes
|
||||||
void test_exactly_one_duplicate()
|
void test_exactly_one_duplicate()
|
||||||
{
|
{
|
||||||
@ -105,5 +122,6 @@ int main(void)
|
|||||||
RUN_TEST(test_for_triple);
|
RUN_TEST(test_for_triple);
|
||||||
RUN_TEST(test_exactly_one_duplicate);
|
RUN_TEST(test_exactly_one_duplicate);
|
||||||
RUN_TEST(test_get_duplicate);
|
RUN_TEST(test_get_duplicate);
|
||||||
|
RUN_TEST(test_get_duplicate_does_modify);
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user