generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
16
Commits
5532cec3ad
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b43402cc86 | ||
|
|
bbf0460d1a | ||
|
|
4b81d627f5 | ||
|
|
326ad66959 | ||
|
|
a118f4e920 | ||
|
|
5941913b6c | ||
|
|
b8f80512ba | ||
|
|
e9fa5b72a5 | ||
|
|
0a75187497 | ||
|
|
ce3de3b8f8 | ||
|
|
353c4cfe6f | ||
|
|
da4ea358a3 | ||
|
|
0dd1564ba5 | ||
|
|
7d5caa5ce7 | ||
|
|
0ebd521d61 | ||
|
|
cb6a9feab8 |
No files matched your search
@@ -1,121 +1,125 @@
|
|||||||
#include <string.h>
|
#include <stdlib.h>
|
||||||
#include "stack.h"
|
#include <stdio.h>
|
||||||
#include "bintree.h"
|
#include <string.h>
|
||||||
|
|
||||||
static StackNode *push_left(TreeNode *node, StackNode *stack);
|
#include "stack.h"
|
||||||
|
#include "bintree.h"
|
||||||
|
|
||||||
|
static StackNode *push_left(TreeNode *node, StackNode *stack);
|
||||||
|
|
||||||
|
// Vergleichsfunktion
|
||||||
int compareFct(const void *argument1, const void *argument2)
|
int compareFct(const void *argument1, const void *argument2)
|
||||||
{
|
{
|
||||||
int x = *((int *)argument1);
|
int x = *((int *)argument1);
|
||||||
int y = *((int *)argument2);
|
int y = *((int *)argument2);
|
||||||
if (x < y)
|
|
||||||
return -1;
|
if (x < y) return -1;
|
||||||
if (x > y)
|
if (x > y) return 1;
|
||||||
return 1;
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Erzeugt einen neuen Baumknoten mit Datenkopie
|
||||||
static TreeNode *create_node(const void *data, size_t data_size)
|
static TreeNode *create_node(const void *data, size_t data_size)
|
||||||
{
|
{
|
||||||
TreeNode *node = malloc(sizeof(TreeNode));
|
TreeNode *node = malloc(sizeof(TreeNode)); // Speicher für neuen Knoten reservieren
|
||||||
if (!node)
|
if (!node)
|
||||||
{
|
{
|
||||||
perror("Speicher wurde NICHT reserviert!\n");
|
perror("Speicher wurde NICHT reserviert!");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
node->data = malloc(data_size);
|
node->data = malloc(data_size); // Speicher für Knotendaten reservieren
|
||||||
if (!node->data)
|
if (!node->data)
|
||||||
{
|
{
|
||||||
perror("Speicher wurde NICHT reserviert!\n");
|
perror("Speicher wurde NICHT reserviert!");
|
||||||
return NULL;
|
free(node);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(node->data, data, data_size);
|
memcpy(node->data, data, data_size); // Daten in neuen Speicher kopieren
|
||||||
node->left = NULL;
|
node->left = NULL; // Linken Kindzeiger initialisieren
|
||||||
node ->right = NULL;
|
node->right = NULL; // Rechten Kindzeiger initialisieren
|
||||||
|
|
||||||
return node;
|
return node; // Zeiger auf neuen Knoten zurückgeben
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
//Fügt Daten in den Binärbaum ein
|
||||||
// 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 *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||||
{
|
{
|
||||||
if (root == NULL)
|
if (root == NULL) // Wenn Baum leer ist (Ende erreicht)
|
||||||
{
|
{
|
||||||
if (isDuplicate)
|
if (isDuplicate)
|
||||||
{
|
*isDuplicate = 0; // Kein Duplikat, da neu eingefügt
|
||||||
*isDuplicate = 0;
|
return create_node(data, dataSize);
|
||||||
}
|
|
||||||
return create_node(data, dataSize);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmpFCT = compareFCT(data, root->data);
|
int cmp = compareFct(data, root->data); // Daten mit aktuellem Knoten vergleichen
|
||||||
if (cmpFCT < 0)
|
|
||||||
|
if (cmp < 0)
|
||||||
{
|
{
|
||||||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
}
|
}
|
||||||
else if (cmpFCT > 0)
|
else if (cmp > 0)
|
||||||
{
|
{
|
||||||
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (isDuplicate)
|
if (isDuplicate)
|
||||||
*isDuplicate = 1;
|
*isDuplicate = 1;
|
||||||
}
|
}
|
||||||
return root;
|
|
||||||
|
return root; // Wurzel unverändert zurückgeben
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// Inorder-Iterator über den Baum
|
||||||
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
|
||||||
// push the top node and push all its left nodes.
|
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *root)
|
||||||
{
|
{
|
||||||
static StackNode *stack = NULL;
|
static StackNode *stack = NULL;
|
||||||
|
|
||||||
|
/* Linken Pfad auf Stack legen */
|
||||||
if (root != NULL)
|
if (root != NULL)
|
||||||
stack = push_left(root, stack);
|
stack = push_left(root, stack); // Linken Pfad ab root auf Stack legen
|
||||||
|
|
||||||
if (stack == NULL)
|
if (stack == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
TreeNode *node;
|
TreeNode *node = (TreeNode *)top(stack); // Oberstes Element vom Stack holen
|
||||||
node = pop(&stack);
|
stack = pop(stack);
|
||||||
|
|
||||||
if (node->right != NULL)
|
if (node->right != NULL) // Wenn rechter Teilbaum existiert
|
||||||
stack = push_left(node->right, stack);
|
stack = push_left(node->right, stack); // Linken Pfad vom rechten Kind auf Stack legen
|
||||||
|
|
||||||
return node->data;
|
return node->data; // Daten des Knotens zurückgeben
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hilfsfunktion: linken Pfad auf Stack legen
|
||||||
static StackNode *push_left(TreeNode *node, StackNode *stack)
|
static StackNode *push_left(TreeNode *node, StackNode *stack)
|
||||||
{
|
{
|
||||||
while (node != NULL)
|
while (node != NULL) // Solange linkes Kind existiert
|
||||||
{
|
{
|
||||||
stack = push(stack, node);
|
stack = push(stack, node); // Knoten auf Stack legen
|
||||||
node = node->left;
|
node = node->left;
|
||||||
}
|
}
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
// Releases all memory resources (including data copies).
|
|
||||||
|
// Gibt gesamten Baum frei
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
if (root = NULL) return;
|
if (root == NULL)
|
||||||
clearTree(root->left);
|
return;
|
||||||
clearTree(root->right);
|
|
||||||
free(root->data);
|
|
||||||
free(root);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the number of entries in the tree given by root.
|
clearTree(root->left);
|
||||||
|
clearTree(root->right);
|
||||||
|
free(root->data);
|
||||||
|
free(root);
|
||||||
|
}
|
||||||
|
// Liefert Anzahl der Knoten
|
||||||
unsigned int treeSize(const TreeNode *root)
|
unsigned int treeSize(const TreeNode *root)
|
||||||
{
|
{
|
||||||
if (root == NULL)
|
if (root == NULL)
|
||||||
{
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
return 1 + treeSize(root->left) + treeSize(root->right);
|
|
||||||
|
|
||||||
|
return 1 + treeSize(root->left) + treeSize(root->right);
|
||||||
}
|
}
|
||||||
@@ -1,2 +1,10 @@
|
|||||||
|
thilo;19753
|
||||||
|
thilo;14880
|
||||||
|
max;14849
|
||||||
|
thilo;14733
|
||||||
|
player1;9956
|
||||||
|
player2;4988
|
||||||
|
player1;4980
|
||||||
player_name;4980
|
player_name;4980
|
||||||
|
player2;4885
|
||||||
player1;3999
|
player1;3999
|
||||||
@@ -35,19 +35,33 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
test_sources = test_stack.c stack.c
|
stack_test_sources = test_stack.c stack.c
|
||||||
test_objects = $(test_sources:.c=.o)
|
stack_test_objects = $(stack_test_sources:.c=.o)
|
||||||
|
|
||||||
test_runner: $(test_objects)
|
numbers_test_sources = test_numbers.c numbers.c
|
||||||
|
numbers_test_objects = $(numbers_test_sources:.c=.o)
|
||||||
|
|
||||||
|
bintree_test_sources = test_bintree.c bintree.c
|
||||||
|
bintree_test_objects = $(bintree_test_sources:.c=.o)
|
||||||
|
|
||||||
|
stack_test_runner: $(stack_test_objects)
|
||||||
$(CC) $(FLAGS) $^ -o $@
|
$(CC) $(FLAGS) $^ -o $@
|
||||||
|
|
||||||
unitTests: test_runner
|
numbers_test_runner: $(numbers_test_objects)
|
||||||
|
$(CC) $(FLAGS) $^ -o $@
|
||||||
|
|
||||||
|
bintree_test_runner: $(bintree_test_objects)
|
||||||
|
$(CC) $(FLAGS) $^ -o $@
|
||||||
|
|
||||||
|
unitTests: stack_test_runner numbers_test_runner bintree_test_runner
|
||||||
@echo "============================="
|
@echo "============================="
|
||||||
@echo "Running Stack Unit Tests..."
|
@echo "Running Unit Tests..."
|
||||||
@echo "============================="
|
@echo "============================="
|
||||||
./test_runner
|
./stack_test_runner
|
||||||
|
./numbers_test_runner
|
||||||
|
./bintree_test_runner
|
||||||
@echo "============================="
|
@echo "============================="
|
||||||
@echo "Stack Unit Tests Finished."
|
@echo "Unit Tests Finished."
|
||||||
@echo "============================="
|
@echo "============================="
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -73,12 +73,37 @@ unsigned int *createNumbers(unsigned int len)
|
|||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
for (unsigned int i = 0; i < len; i++)
|
/*for (unsigned int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
array[i] = rand() % (2*len) + 1;
|
array[i] = rand() % (2*len) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: sicherstellen, dass im Array keine doppelten Zahlen vorhanden sind (Max)
|
*/
|
||||||
|
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
int isDuplicate;
|
||||||
|
unsigned int value;
|
||||||
|
unsigned int i = 0;
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
while (i < len)
|
||||||
|
{
|
||||||
|
value = rand() % (2 * len) + 1;
|
||||||
|
|
||||||
|
root = addToTree(root, &value, sizeof(unsigned int), compareFct, &isDuplicate);
|
||||||
|
|
||||||
|
if (isDuplicate == 0)
|
||||||
|
{
|
||||||
|
array[i] = value;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
// Bei Duplikat: nochmal versuchen, kein Inkrement
|
||||||
|
}
|
||||||
|
|
||||||
|
clearTree(root);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
position = rand() % len;
|
position = rand() % len;
|
||||||
duplicate_value = array[position];
|
duplicate_value = array[position];
|
||||||
@@ -86,7 +111,7 @@ unsigned int *createNumbers(unsigned int len)
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
pos_dup = rand() % len;
|
pos_dup = rand() % len;
|
||||||
} while (pos_dup = duplicate_value);
|
} while (pos_dup == position);
|
||||||
|
|
||||||
array[pos_dup] = array[position];
|
array[pos_dup] = array[position];
|
||||||
|
|
||||||
@@ -100,7 +125,7 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
|||||||
|
|
||||||
if (numbers == NULL)
|
if (numbers == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Fehler! Das übergebene Array ist leer!");
|
fprintf(stderr, "Fehler! Das übergebene Array ist leer!\n");
|
||||||
return duplicate;
|
return duplicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "numbers.h"
|
||||||
|
|
||||||
|
void test_empty_function(void)
|
||||||
|
{
|
||||||
|
unsigned int len1 = 1;
|
||||||
|
unsigned int len2 = 5;
|
||||||
|
unsigned int *null_array = NULL;
|
||||||
|
unsigned int array[] = {1, 2, 3, 4, 5};
|
||||||
|
unsigned int *array_ptr = array;
|
||||||
|
|
||||||
|
unsigned int *create_num = createNumbers(len1);
|
||||||
|
if (create_num == NULL)
|
||||||
|
printf("Test on invalid lenght of array: SUCCESS!\n");
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Test on invalid lenght of array: FAILED! Expected: NULL, Was: %p\n", create_num);
|
||||||
|
free(create_num);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int duplicate = getDuplicate(null_array, len2);
|
||||||
|
|
||||||
|
if (duplicate == 0)
|
||||||
|
printf("Test on invalid array: SUCESS!\n");
|
||||||
|
|
||||||
|
else
|
||||||
|
printf("Test on invalid array: FAILED! Expected: 0, WAS: %u\n", duplicate);
|
||||||
|
|
||||||
|
unsigned int duplicate2 = getDuplicate(array_ptr, len1);
|
||||||
|
|
||||||
|
if (duplicate2 == 0)
|
||||||
|
printf("Test on invalid lenght of array: SUCESS!\n");
|
||||||
|
|
||||||
|
else
|
||||||
|
printf("Test on invalid lenght of array: FAILED, Expected: 0, WAS: %u\n", duplicate2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_array(void)
|
||||||
|
{
|
||||||
|
unsigned int len = 10;
|
||||||
|
unsigned int *array = NULL;
|
||||||
|
|
||||||
|
array = createNumbers(len);
|
||||||
|
|
||||||
|
if (array == NULL)
|
||||||
|
printf("Test on valid lenght of array: FAILED! Expected array greater than 0, Was: NULL\n");
|
||||||
|
|
||||||
|
else printf("Test on valid lenght of array: SUCESS!\n");
|
||||||
|
|
||||||
|
unsigned int test_array_dup[] = {1, 2, 3, 4, 5, 2, 6, 7, 8, 9};
|
||||||
|
unsigned int test_len = 10;
|
||||||
|
unsigned int duplicate = 2;
|
||||||
|
|
||||||
|
unsigned int duplicate_found = getDuplicate(test_array_dup, test_len);
|
||||||
|
|
||||||
|
if (duplicate_found != duplicate)
|
||||||
|
printf("Test on valid lenght of array: FAILED! Expected %u, Was: %u\n", duplicate, duplicate_found);
|
||||||
|
|
||||||
|
else printf("Test on valid lenght of array: SUCESS!, Duplicate value: %d\n", duplicate);
|
||||||
|
|
||||||
|
free(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
test_empty_function();
|
||||||
|
printf("\n");
|
||||||
|
test_array();
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user