10 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
wiesendsi102436 c6fd4b300e Merge remote-tracking branch 'origin/bintree' 2025-12-08 18:50:19 +01:00
wiesendsi102436 93a818088c Merge branch 'main' into Fabrice 2025-12-08 18:45:17 +01:00
Fabrice 2e805ba8e6 3. Test (vorerst) gelöscht 2025-12-08 17:55:44 +01:00
Fabrice 2d3ffcf3f8 reset bintree 2025-12-08 17:50:40 +01:00
Fabrice aa3538c14b 3. Test hinzugefügt 2025-12-08 17:20:01 +01:00
Fabrice a4a60a3d95 Kleiner Fix 2025-12-08 16:27:24 +01:00
4 changed files with 48 additions and 11 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)
+24 -6
View File
@@ -19,16 +19,27 @@ unsigned int *createNumbers(unsigned int len)
for (unsigned int i = 0; i < len; i++) {
do
{
isDuplicate = 0;
numbers[i] = rand () % upperLimit + 1;
binTree = addToTree(binTree, &numbers[i], sizeof(unsigned int), compare, &isDuplicate);
} while (isDuplicate);
}
unsigned int duplicate = numbers[rand () % len];
int indexDuplicate = rand () % len;
numbers[len] = numbers[indexDuplicate];
int indexDuplicate;
do {
indexDuplicate = rand() % len;
} while (numbers[indexDuplicate] == duplicate);
if (numbers[len-1] != duplicate) {
numbers[len-1] = numbers[indexDuplicate];
}
numbers[indexDuplicate] = duplicate;
clearTree(binTree);
return numbers;
}
@@ -54,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;
}
}
return 0;
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();
}