Compare commits
10
Commits
31a76162ee
...
temp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a3b951d0a9 | ||
|
|
f8c297f84d | ||
|
|
5b576589e3 | ||
|
|
bf0a0b3f40 | ||
|
|
d54dd3eb6f | ||
|
|
8051686a37 | ||
|
|
39965a95c4 | ||
|
|
8b0fa4601a | ||
|
|
f59489779b | ||
|
|
b76ffa054a |
@@ -2,3 +2,10 @@ doble_initial.exe
|
||||
highscores.txt
|
||||
runStackTest.exe
|
||||
stack.o
|
||||
runNumbersTest.exe
|
||||
numbers.o
|
||||
.vscode/launch.json
|
||||
.vscode/settings.json
|
||||
*.o
|
||||
*.exe
|
||||
runBintreeTest
|
||||
|
||||
@@ -12,7 +12,26 @@
|
||||
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
TreeNode newNode = {data, NULL, NULL};
|
||||
|
||||
|
||||
if(root == NULL)
|
||||
{
|
||||
return &newNode;
|
||||
}
|
||||
|
||||
if (data < root->data)
|
||||
{
|
||||
root->left = addToTree(root->left, data, dataSize,compareFct, isDuplicate);
|
||||
}
|
||||
else if(data > root->data)
|
||||
{
|
||||
root->right = addToTree(root->right, data, dataSize,compareFct, isDuplicate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
|
||||
@@ -26,11 +45,36 @@ void *nextTreeData(TreeNode *root)
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (root->left != NULL)
|
||||
{
|
||||
clearTree(root->left);
|
||||
free(root->left);
|
||||
}
|
||||
else if (root->right != NULL)
|
||||
{
|
||||
clearTree(root->right);
|
||||
free(root->right);
|
||||
}
|
||||
root->data = NULL;
|
||||
}
|
||||
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root)
|
||||
{
|
||||
int counterL, counterR = 0;
|
||||
if (root->left != NULL)
|
||||
{
|
||||
counterL = treeSize(root->left) + 1;
|
||||
}
|
||||
else if (root->right != NULL)
|
||||
{
|
||||
counterR = treeSize(root->right) + 1;
|
||||
}
|
||||
|
||||
return counterL + counterR;
|
||||
}
|
||||
@@ -38,6 +38,18 @@ $(program_obj_filesobj_files): %.o: %.c
|
||||
unitTests: stack.o test_stack.c $(unityfolder)/unity.c
|
||||
$(CC) $(FLAGS) -I$(unityfolder) -o runStackTest test_stack.c stack.o $(unityfolder)/unity.c
|
||||
|
||||
# --------------------------
|
||||
# numbers.c Tests
|
||||
# --------------------------
|
||||
numbersTests: numbers.o test_numbers.c $(unityfolder)/unity.c
|
||||
$(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o $(unityfolder)/unity.c
|
||||
|
||||
# --------------------------
|
||||
# bintree.c Tests
|
||||
# --------------------------
|
||||
bintreeTests: bintree.o test_bintree.c $(unityfolder)/unity.c
|
||||
$(CC) $(FLAGS) -I$(unityfolder) -o runBintreeTest test_bintree.c bintree.o $(unityfolder)/unity.c
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
# --------------------------
|
||||
|
||||
@@ -14,13 +14,150 @@
|
||||
// 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
|
||||
// creating random numbers.
|
||||
unsigned int checkArray(unsigned int *array, unsigned int len, unsigned int number)
|
||||
{
|
||||
int free = 1;
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
if (array[i] == number)
|
||||
{
|
||||
free = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return free;
|
||||
}
|
||||
|
||||
unsigned int *createNumbers(unsigned int len)
|
||||
{
|
||||
srand(time(NULL));
|
||||
unsigned int *array = (unsigned int*)malloc(len * sizeof(unsigned int));
|
||||
int randomNr, counter;
|
||||
|
||||
if(array == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
counter = 0;
|
||||
do
|
||||
{
|
||||
if (counter == 9)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
randomNr = rand() % (2 * len + 1);
|
||||
counter++;
|
||||
} while (!checkArray(array, i, randomNr));
|
||||
|
||||
array[i] = randomNr;
|
||||
printf("%u ", array[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
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
|
||||
{
|
||||
arr[k] = rightArr[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of leftArr[], if any
|
||||
while (i < n1)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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 temp[len];
|
||||
unsigned int duplicate = 0;
|
||||
|
||||
/*if(numbers == NULL || (sizeof(numbers) / sizeof(typeof(numbers)) != len))
|
||||
{
|
||||
return 0;S
|
||||
}*/
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
temp[i] = numbers[i];
|
||||
}
|
||||
|
||||
// Sorting arr using mergesort
|
||||
mergeSort(temp, 0, len - 1);
|
||||
|
||||
for (int i = 0; i < len - 1; i++)
|
||||
{
|
||||
duplicate = temp[i];
|
||||
if (duplicate == temp[i + 1])
|
||||
{
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return duplicate;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "bintree.h"
|
||||
#include "unity.h"
|
||||
|
||||
|
||||
void treeTest()
|
||||
{
|
||||
TreeNode root;
|
||||
TreeNode node1;
|
||||
TreeNode node2;
|
||||
|
||||
int dataRoot = 2;
|
||||
int dataNode1 = 1;
|
||||
int dataNode2 = 3;
|
||||
|
||||
|
||||
root.data = &dataRoot;
|
||||
root.left = &node1;
|
||||
root.right = &node2;
|
||||
|
||||
node1.data = &dataNode1;
|
||||
node1.left = NULL;
|
||||
node1.right = NULL;
|
||||
|
||||
node2.data = &dataNode2;
|
||||
node2.left = NULL;
|
||||
node2.right = NULL;
|
||||
|
||||
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(3,treeSize(&root));
|
||||
|
||||
|
||||
clearTree(&root);
|
||||
TEST_ASSERT_EQUAL_INT(0,treeSize(&root));
|
||||
|
||||
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
{
|
||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
printf("============================\nNumbers tests\n============================\n");
|
||||
|
||||
RUN_TEST(treeTest);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "numbers.h"
|
||||
#include "unity.h"
|
||||
|
||||
void createNumbersTest()
|
||||
{
|
||||
unsigned int *array;
|
||||
unsigned int len = 6;
|
||||
|
||||
array = createNumbers(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
printf("%u ", array[i]);
|
||||
}
|
||||
printf("\n");
|
||||
TEST_ASSERT_NOT_NULL(array);
|
||||
}
|
||||
|
||||
void duplicateTest()
|
||||
{
|
||||
unsigned int array[6] = {1, 4, 5, 2, 3, 1};
|
||||
unsigned int len = 6;
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(1, getDuplicate(array, len));
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
{
|
||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
printf("============================\nNumbers tests\n============================\n");
|
||||
RUN_TEST(createNumbersTest);
|
||||
RUN_TEST(duplicateTest);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
+51
-6
@@ -25,30 +25,75 @@ void test_push(void)
|
||||
TEST_ASSERT_EQUAL_INT(2, testNode->next->value);
|
||||
}
|
||||
|
||||
StackNode* setup(int value, StackNode* next) {
|
||||
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
|
||||
if (node == NULL) {
|
||||
perror("malloc failed");
|
||||
exit(EXIT_FAILURE); // or handle the error differently
|
||||
}
|
||||
node->value = value;
|
||||
node->next = next;
|
||||
return node;
|
||||
}
|
||||
|
||||
void test_pop(void)
|
||||
{
|
||||
StackNode* node2 = setup(3, NULL);
|
||||
StackNode* node1 = setup(2, node2);
|
||||
StackNode* header = setup(1, node1);
|
||||
StackNode* temp;
|
||||
|
||||
TEST_ASSERT_NULL(pop(NULL));
|
||||
temp = pop(header);
|
||||
int after = 0;
|
||||
while(temp)
|
||||
{
|
||||
after++;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(2, after);
|
||||
TEST_ASSERT_NULL(node1->next);
|
||||
}
|
||||
|
||||
void test_top(void)
|
||||
{
|
||||
StackNode* node2 = setup(3, NULL);
|
||||
StackNode* node1 = setup(2, node2);
|
||||
StackNode* header = setup(1, node1);
|
||||
|
||||
TEST_ASSERT_NULL(top(NULL));
|
||||
int data = *(int *)top(header);
|
||||
TEST_ASSERT_EQUAL_INT(node2->value, data);
|
||||
}
|
||||
|
||||
void test_clear(void)
|
||||
void test_clear()
|
||||
{
|
||||
StackNode* node2 = setup(3, NULL);
|
||||
StackNode* node1 = setup(2, node2);
|
||||
StackNode* header = setup(1, node1);
|
||||
StackNode* temp;
|
||||
|
||||
// TEST_ASSERT_NULL(clearStack(NULL));
|
||||
clearStack(header);
|
||||
temp = header;
|
||||
|
||||
int after = 0;
|
||||
while(temp)
|
||||
{
|
||||
after++;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
void setUp(void) {
|
||||
|
||||
TEST_ASSERT_NULL(after);
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
void tearDown(void)
|
||||
{
|
||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user