Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f66f6179ce
|
||
|
|
91f5c52077 | ||
|
|
fc9031adbd | ||
|
|
a7fd62eda4 | ||
|
|
c6fd4b300e
|
||
|
|
93a818088c
|
||
|
|
2e805ba8e6 | ||
|
|
2d3ffcf3f8 | ||
|
|
aa3538c14b | ||
|
|
a4a60a3d95 |
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user