4 Commits
Author SHA1 Message Date
wiesendsi102436 f66f6179ce more stack tests and fix in stack 2025-12-11 20:18:13 +01:00
Fabrice 91f5c52077 clearTree hinzugefügt 2025-12-10 10:03:36 +01:00
Fabrice fc9031adbd In Main Z.74 geändert 2025-12-10 09:50:33 +01:00
Fabrice a7fd62eda4 numbers.c passiert alle Tests 2025-12-09 20:22:27 +01:00
4 changed files with 39 additions and 8 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ int main(int argc, char *argv[])
userInput = inputNumber("Welche Zahl kommt doppelt vor: ");
measuredSeconds = stopTimer();
duplicate = getDuplicate(numbers, numberOfElements+1);
duplicate = getDuplicate(numbers, numberOfElements);
// check result and update highscores
if(userInput == duplicate)
+15 -3
View File
@@ -32,9 +32,14 @@ unsigned int *createNumbers(unsigned int len)
indexDuplicate = rand() % len;
} while (numbers[indexDuplicate] == duplicate);
if (numbers[len-1] != duplicate) {
numbers[len-1] = numbers[indexDuplicate];
}
numbers[indexDuplicate] = duplicate;
clearTree(binTree);
return numbers;
}
@@ -60,12 +65,19 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
return 0;
}
qsort((void*)numbers, len, sizeof(unsigned int), compare);
unsigned int *copy = malloc (sizeof(unsigned int) * len);
memcpy (copy, numbers, sizeof(unsigned int) * len);
qsort(copy, len, sizeof(unsigned int), compare);
for (int i = 0; i < len-1; i++) {
if (numbers[i] == numbers [i+1]) {
return numbers[i];
if (copy[i] == copy [i+1]) {
unsigned int result = copy [i];
free (copy);
return result;
}
}
free (copy);
return 0;
}
+4 -2
View File
@@ -50,6 +50,8 @@ void *top(StackNode *stack)
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
while (pop(stack))
;
while (stack)
{
stack = pop(stack);
}
}
+17
View File
@@ -15,6 +15,7 @@ void setUp(void)
void tearDown(void)
{
clearStack(stack);
stack = NULL;
}
void test_push_and_pop(void)
@@ -31,17 +32,33 @@ void test_push_and_pop(void)
stack = pop(stack);
}
// pop and top should return NULL if called with NULL ptr
void test_handle_NULL(void)
{
TEST_ASSERT_NULL(pop(stack));
TEST_ASSERT_NULL(top(stack));
}
void test_top(void)
{
TEST_ASSERT_NULL(top(stack));
stack = push(stack, &data1);
TEST_ASSERT_EQUAL_PTR(top(stack), &data1);
TEST_ASSERT_EQUAL_INT(*(int *)top(stack), data1);
stack = push(stack, &data2);
TEST_ASSERT_EQUAL_PTR(top(stack), &data2);
TEST_ASSERT_EQUAL_INT(*(int *)top(stack), data2);
stack = push(stack, &data3);
TEST_ASSERT_EQUAL_PTR(top(stack), &data3);
TEST_ASSERT_EQUAL_INT(*(int *)top(stack), data3);
}
int main(void)
{
printf("============================\nStack tests\n============================\n");
UNITY_BEGIN();
RUN_TEST(test_push_and_pop);
RUN_TEST(test_handle_NULL);
RUN_TEST(test_top);
return UNITY_END();
}