numbers + tests angepasst

This commit is contained in:
Simon May 2025-12-22 13:29:04 +01:00
parent 5900d759bd
commit e5c5f53ad4
7 changed files with 122 additions and 297 deletions

View File

@ -19,6 +19,10 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
{ {
TreeNode *newNode = malloc(sizeof(TreeNode)); TreeNode *newNode = malloc(sizeof(TreeNode));
newNode->data = malloc(dataSize); newNode->data = malloc(dataSize);
if (newNode->data == NULL)
{
return NULL; // Fehler
}
memcpy(newNode->data, data, dataSize); memcpy(newNode->data, data, dataSize);
newNode->left = NULL; newNode->left = NULL;
newNode->right = NULL; newNode->right = NULL;
@ -38,7 +42,12 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
{ {
if (isDuplicate != NULL) if (isDuplicate != NULL)
{ {
*isDuplicate = 1; // Mark as duplicate if needed. *isDuplicate = 1;
return root;
}
else
{
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
} }
} }
@ -55,17 +64,14 @@ void *nextTreeData(TreeNode *root)
clearStack(stack); clearStack(stack);
buildStack(root); buildStack(root);
} }
if(stack != NULL) if(stack != NULL)
{ {
void* data = top(stack); void* data = top(stack);
stack = pop(stack); stack = pop(stack);
return data; return data;
} }
return NULL; return NULL;
} }
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
@ -75,7 +81,6 @@ void clearTree(TreeNode *root)
{ {
return; return;
} }
if (root->left != NULL) if (root->left != NULL)
{ {
clearTree(root->left); clearTree(root->left);
@ -99,13 +104,11 @@ unsigned int treeSize(const TreeNode *root)
void buildStack(TreeNode *root) void buildStack(TreeNode *root)
{ {
if (root == NULL) if (root == NULL)
{ {
return; return;
} }
buildStack(root->left);
buildStack(root->left); // biggest first stack = push(stack, root->data);
stack = push(stack, root->data); // push
buildStack(root->right); buildStack(root->right);
} }

View File

@ -41,14 +41,14 @@ stackTests: stack.o test_stack.c $(unityfolder)/unity.c
# -------------------------- # --------------------------
# numbers.c Tests # numbers.c Tests
# -------------------------- # --------------------------
numbersTests: numbers.o test_numbers.c $(unityfolder)/unity.c numbersTests: numbers.o bintree.o stack.o test_numbers.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o $(unityfolder)/unity.c $(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o bintree.o stack.o $(unityfolder)/unity.c
# -------------------------- # --------------------------
# bintree.c Tests # bintree.c Tests
# -------------------------- # --------------------------
bintreeTests: bintree.o test_bintree.c $(unityfolder)/unity.c bintreeTests: bintree.o stack.o test_bintree.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) -o runBintreeTest test_bintree.c bintree.o $(unityfolder)/unity.c $(CC) $(FLAGS) -I$(unityfolder) -o runBintreeTest test_bintree.c bintree.o stack.o $(unityfolder)/unity.c
# -------------------------- # --------------------------
# Clean # Clean

184
numbers.c
View File

@ -14,157 +14,97 @@
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries. // Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while // Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers. // creating random numbers.
unsigned int checkArray(unsigned int *array, unsigned int len, unsigned int number) void duplicateNumber(unsigned int *numbers, unsigned int len)
{ {
int free = 1; if (!numbers || len < 2)
return;
for (int i = 0; i < len; i++) unsigned int numberPicked = rand() % len; // take random spot in array
{ unsigned int destination = rand() % len; // new spot for duplicated number
if (array[i] == number)
{
free = 0;
break;
} while (destination == numberPicked) // while same spot get a new one
} destination = rand() % len;
return free; numbers[destination] = numbers[numberPicked];
}
/* for qsort
-1 num1 should come before num2
0 num1 and num2 are equal
1 num1 should come after num2
*/
int compare(const void *num1, const void *num2)
{
unsigned int temp1 = *(const unsigned int *)num1;
unsigned int temp2 = *(const unsigned int *)num2;
return (temp1 > temp2) - (temp1 < temp2);
} }
unsigned int *createNumbers(unsigned int len) unsigned int *createNumbers(unsigned int len)
{ {
srand(time(NULL)); if (len < 2)
unsigned int *array = (unsigned int *)malloc(len * sizeof(unsigned int));
int randomNr, randomPos, filler;
if (array == NULL)
{ {
return NULL; // Fehler return NULL;
}
srand((unsigned)time(NULL));
TreeNode *root = NULL;
unsigned int i = 0;
unsigned int *numbers = malloc(sizeof(unsigned int) * len);
if (!numbers)
{
return NULL;
} }
for (int i = 0; i < len; i++) while (i < len)
{ {
array[i] = 0; unsigned int random = (rand() % (2 * len)) + 1;
} int duplicate = 0;
for (int i = 0; i < len; i++) root = addToTree(root, &random, sizeof(unsigned int), compare, &duplicate);
{ if (!root)
do
{ {
array[i] = (rand() % (2 * len))+ 1; free(numbers); // malloc-Fehler
} while (!checkArray(array, i, array[i])); return NULL;
}
randomPos = rand() % len;
randomNr = array[randomPos];
filler = randomPos;
while(filler == randomPos)
{
filler = rand() % len;
}
array[filler] = randomNr;
return array;
}
void merge(unsigned int arr[], unsigned int left, unsigned int mid, unsigned int right)
{
unsigned int i, j, k;
unsigned int n1 = mid - left + 1;
unsigned int n2 = right - mid;
// Create temporary arrays
unsigned int leftArr[n1], rightArr[n2];
// Copy data to temporary arrays
for (i = 0; i < n1; i++)
leftArr[i] = arr[left + i];
for (j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];
// Merge the temporary arrays back into arr[left..right]
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2)
{
if (leftArr[i] <= rightArr[j])
{
arr[k] = leftArr[i];
i++;
} }
else
{ if (!duplicate)
arr[k] = rightArr[j]; numbers[i++] = random;
j++;
}
k++;
} }
// Copy the remaining elements of leftArr[], if any duplicateNumber(numbers, len);
while (i < n1) clearTree(root);
{ return numbers;
arr[k] = leftArr[i];
i++;
k++;
}
// Copy the remaining elements of rightArr[], if any
while (j < n2)
{
arr[k] = rightArr[j];
j++;
k++;
}
} }
void mergeSort(unsigned int arr[], unsigned int left, unsigned int right) // Returns the only number in numbers which is present twice. Returns zero on errors.
{
if (left < right)
{
// Calculate the midpoint
unsigned int mid = left + (right - left) / 2;
// Sort first and second halves
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
// Merge the sorted halves
merge(arr, left, mid, right);
}
}
// Returns only the only number in numbers which is present twice. Returns zero on errors.
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len) unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
{ {
unsigned int temp[len]; if (!numbers || len < 2)
unsigned int duplicate = 0;
/*if(numbers == NULL || (sizeof(numbers) / sizeof(typeof(numbers)) != len))
{ {
return 0;S return 0;
}*/
for (int i = 0; i < len; i++)
{
temp[i] = numbers[i];
} }
// Sorting arr using mergesort unsigned int *copy = (unsigned int *)malloc(len * sizeof(unsigned int));
mergeSort(temp, 0, len - 1); if (!copy)
for (int i = 0; i < len - 1; i++)
{ {
duplicate = temp[i]; return 0;
if (duplicate == temp[i + 1]) }
{
memcpy(copy, numbers, len * sizeof(unsigned int));
qsort(copy, len, sizeof(unsigned int), compare);
unsigned int duplicate = 0;
for (unsigned int i = 1; i < len; i++)
{
if (copy[i] == copy[i - 1])
{
duplicate = copy[i];
break; break;
} }
} }
free(copy);
return duplicate; return duplicate;
} }

View File

@ -1,100 +0,0 @@
#include "unity.h"
#include "stack.h" // Stack-Header-Datei
#include <stdlib.h>
// Test Setup and Teardown Functions
void setUp(void) {
// Setup code (falls notwendig, wie Initialisierungen)
}
void tearDown(void) {
// Cleanup code (falls notwendig)
}
// Test for push operation
void test_push(void) {
StackNode *stack = NULL;
int data1 = 10, data2 = 20;
// Push elements to the stack
stack = push(stack, &data1);
stack = push(stack, &data2);
// Check if the stack is not empty
TEST_ASSERT_NOT_NULL(stack);
// Check if the top element is correct
int *topData = top(stack);
TEST_ASSERT_EQUAL_INT(20, *topData); // The last pushed element should be on top
}
// Test for pop operation
void test_pop(void) {
StackNode *stack = NULL;
int data1 = 10, data2 = 20;
// Push elements to the stack
stack = push(stack, &data1);
stack = push(stack, &data2);
// Pop the top element
stack = pop(stack);
// Check if the top element is now the first pushed element
int *topData = top(stack);
TEST_ASSERT_EQUAL_INT(10, *topData); // After popping, the first element should be on top
// Pop the last element
stack = pop(stack);
// Check if the stack is empty now
TEST_ASSERT_NULL(stack); // Stack should be NULL now
}
// Test for top operation
void test_top(void) {
StackNode *stack = NULL;
int data1 = 10, data2 = 20;
// Push elements to the stack
stack = push(stack, &data1);
stack = push(stack, &data2);
// Check the top element
int *topData = top(stack);
TEST_ASSERT_EQUAL_INT(20, *topData); // The top element should be 20 (last pushed)
// Pop the top element and check the new top
stack = pop(stack);
topData = top(stack);
TEST_ASSERT_EQUAL_INT(10, *topData); // Now the top element should be 10
}
// Test for clearStack operation
void test_clearStack(void) {
StackNode *stack = NULL;
int data1 = 10, data2 = 20;
// Push elements to the stack
stack = push(stack, &data1);
stack = push(stack, &data2);
// Clear the stack
clearStack(stack);
// The stack should be empty now
TEST_ASSERT_NULL(stack); // Stack should be NULL
}
// Run all tests
int main(void) {
UNITY_BEGIN();
// Run the individual test functions
RUN_TEST(test_push);
RUN_TEST(test_pop);
RUN_TEST(test_top);
RUN_TEST(test_clearStack);
return UNITY_END();
}

View File

@ -51,9 +51,9 @@ void clearTest()
node2->left = NULL; node2->left = NULL;
node2->right = NULL; node2->right = NULL;
TreeNode *ptr = root; clearTree(root);
root = NULL;
clearTree(ptr);
TEST_ASSERT_EQUAL_INT(0,treeSize(root)); TEST_ASSERT_EQUAL_INT(0,treeSize(root));
} }

View File

@ -3,26 +3,47 @@
#include "numbers.h" #include "numbers.h"
#include "unity.h" #include "unity.h"
void createNumbersTest() void test_createNumbers()
{ {
unsigned int *array;
unsigned int len = 6; unsigned int len = 6;
unsigned int *array = createNumbers(len);
array = createNumbers(len);
for (int i = 0; i < len; i++)
{
printf("%u ", array[i]);
}
printf("\n");
TEST_ASSERT_NOT_NULL(array); TEST_ASSERT_NOT_NULL(array);
unsigned int duplicate = getDuplicate(array, len);
TEST_ASSERT_NOT_EQUAL(0, duplicate);
int counter = 0;
for (unsigned int i = 0; i < len; i++)
{
if (array[i] == duplicate)
counter++;
}
TEST_ASSERT_EQUAL(2, counter);
free(array);
} }
void duplicateTest() void test_createNumbers_values()
{ {
unsigned int array[6] = {1, 4, 5, 2, 3, 1};
unsigned int len = 6; unsigned int len = 6;
unsigned int *array = createNumbers(len);
TEST_ASSERT_NOT_NULL(array);
TEST_ASSERT_EQUAL_INT(1, getDuplicate(array, len)); for (unsigned int i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(array[i] >= 1);
TEST_ASSERT_TRUE(array[i] <= 2 * len);
}
free(array);
}
void test_getDuplicate()
{
unsigned int array[6] = {1, 2, 4, 4, 6, 7};
unsigned int duplicate = getDuplicate(array, 6);
TEST_ASSERT_EQUAL_UINT(4, duplicate);
} }
void setUp(void) void setUp(void)
@ -39,9 +60,11 @@ int main()
{ {
UNITY_BEGIN(); UNITY_BEGIN();
printf("============================\nNumbers tests\n============================\n"); printf("\n============================\n Numbers tests\n============================\n");
RUN_TEST(createNumbersTest);
RUN_TEST(duplicateTest); RUN_TEST(test_createNumbers);
RUN_TEST(test_createNumbers_values);
RUN_TEST(test_getDuplicate);
return UNITY_END(); return UNITY_END();
} }

View File

@ -132,46 +132,6 @@ void test_top2(void)
TEST_ASSERT_EQUAL_INT(node2->data, data); TEST_ASSERT_EQUAL_INT(node2->data, data);
} }
void test_clearStack(void) {
StackNode *stack = NULL;
int data1 = 10, data2 = 20;
// Push elements to the stack
stack = push(stack, &data1);
stack = push(stack, &data2);
// Clear the stack
clearStack(stack);
// The stack should be empty now
TEST_ASSERT_NULL(stack); // Stack should be NULL
}
void test_clear()
{
int x,y,z;
x = 1;
y = 2;
z = 3;
StackNode* node2 = setup(&z, NULL);
StackNode* node1 = setup(&y, node2);
StackNode* header = setup(&x, node1);
StackNode* temp;
clearStack(header);
temp = header;
int after = 0;
while(temp)
{
after++;
temp = temp->next;
}
TEST_ASSERT_NULL(after);
}
void setUp(void) void setUp(void)
{ {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
@ -191,7 +151,6 @@ int main()
RUN_TEST(test_push); RUN_TEST(test_push);
RUN_TEST(test_pop); RUN_TEST(test_pop);
RUN_TEST(test_top); RUN_TEST(test_top);
RUN_TEST(test_clearStack);
return UNITY_END(); return UNITY_END();
} }