Compare commits

...

2 Commits

Author SHA1 Message Date
9afecd8502 wip 2025-12-07 12:28:46 +01:00
28e76fdbb7 Überarbeitung von test_numbers.c und testen der Unit-Tests (3 mal PASS) 2025-12-03 20:50:52 +01:00
4 changed files with 90 additions and 34 deletions

22
stack.c
View File

@ -8,9 +8,29 @@
* `clearStack`: gibt den gesamten Speicher frei. */ * `clearStack`: gibt den gesamten Speicher frei. */
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
static StackNode *createEmptyElement()
{
return malloc(sizeof(StackNode));
}
StackNode *push(StackNode *stack, void *data) StackNode *push(StackNode *stack, void *data)
{ {
StackNode *newElement = createEmptyElement();
newElement->data = data;
newElement->next = NULL;
if (stack == NULL)
{
stack = newElement;
}
else
{
StackNode *currentElement = stack;
while(currentElement->next != NULL)
{
currentElement = currentElement->next;
}
currentElement->next = newElement;
return stack;
}
} }
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be // Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be

View File

@ -8,6 +8,11 @@ The latest element is taken from the stack. */
#include <stdlib.h> #include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen //TODO: passenden Datentyp als struct anlegen
typedef struct node
{
void *data;
struct node* next;
}StackNode;
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data); StackNode *push(StackNode *stack, void *data);
@ -22,4 +27,4 @@ void *top(StackNode *stack);
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack); void clearStack(StackNode *stack);
#endif #endif

View File

@ -1,67 +1,78 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include "unity/unity.h" #include "unity/unity.h"
#include "numbers.h" #include "numbers.h"
static unsigned int count_number_occurrence(const unsigned int *arr, unsigned int len, unsigned int value)
{
unsigned int count = 0;
for (unsigned int i = 0; i < len; i++)
{
if (arr[i] == value)
count++;
}
return count;
}
void test_length_and_duplicate() void test_length_and_duplicate()
{ {
setSeed(1); setSeed(1);
unsigned int len = 10; unsigned int len = 10;
unsigned int *test_numbers = createNumbers(len); unsigned int *test_numbers = createNumbers(len);
if (test_numbers == NULL) TEST_ASSERT_NOT_NULL(test_numbers);
{
return;
}
/*
setSeed(1);
unsigned int len = 10;
unsigned int *test_numbers = createNumbers(len);
if (test_numbers == NULL)
{
return;
}
unsigned int expected[10] = {20, 8, 13, 19, 17, 16, 12, 0, 15, 14};
//Bereich checken:
for (unsigned int i = 0; i < len; i++) for (unsigned int i = 0; i < len; i++)
{ {
if (test_numbers[i] != expected[i]) TEST_ASSERT_TRUE(test_numbers[i] >= 1);
{ TEST_ASSERT_TRUE(test_numbers[i] <= len * 2);
free(test_numbers); }
return;
} //Prüfen, ob genau 1 Wert doppelt vorkommt:
unsigned int duplicate = getDuplicate(test_numbers, len);
TEST_ASSERT_NOT_EQUAL(0, duplicate);
unsigned int occurrences = count_number_occurrence(test_numbers, len, duplicate);
TEST_ASSERT_EQUAL_UINT(2, occurrences);
//Prüfen, ob alle anderen Werte nur einmal vorkommen:
for (unsigned int i = 1; i <= len * 2; i++)
{
if (i == duplicate)
continue;
unsigned int c = count_number_occurrence(test_numbers, len, i);
TEST_ASSERT_TRUE(c <= 1); //Keine Zahl darf doppelt vorkommen.
} }
free(test_numbers); free(test_numbers);
*/
} }
void test_duplicate_value() void test_duplicate_value()
{ {
unsigned int test_numbers[6] = {1, 2, 3, 4, 5, 5}; unsigned int test_numbers[6] = {1, 2, 3, 4, 5, 5};
unsigned int dupli = 0; unsigned int duplicate = 0;
dupli = getDuplicate(test_numbers, 6); duplicate = getDuplicate(test_numbers, 6);
TEST_ASSERT_EQUAL(5, dupli);
TEST_ASSERT_EQUAL(5, duplicate);
} }
void test_duplicate_value_if_false() void test_duplicate_value_if_false()
{ {
unsigned int test_numbers[6] = {1, 2, 3, 4, 5, 6}; unsigned int test_numbers[6] = {1, 2, 3, 4, 5, 6};
unsigned int dupli = 0; unsigned int duplicate = 0;
dupli = getDuplicate(test_numbers, 6); duplicate = getDuplicate(test_numbers, 6);
TEST_ASSERT_EQUAL(0, dupli);
TEST_ASSERT_EQUAL(0, duplicate);
} }
void setUp(void) {}
void tearDown(void) {}
int main(void) int main(void)
{ {
UNITY_BEGIN(); UNITY_BEGIN();
@ -70,4 +81,4 @@ int main(void)
RUN_TEST(test_duplicate_value); RUN_TEST(test_duplicate_value);
RUN_TEST(test_duplicate_value_if_false); RUN_TEST(test_duplicate_value_if_false);
return UNITY_END(); return UNITY_END();
} }

20
test_stack.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
void testPushAddsElementCorrectly(void)
{
int a = 10;
StackNode *node = push(NULL, &a);
TEST_ASSERT_NOT_NULL(node);
TEST_ASSERT_EQUAL_INT(a, *((int*)node->data)); //void Zeiger in integer-Zeiger umwandeln
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(testPushAddsElementCorrectly);
return UNITY_END();
}