generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c6f6b0370 |
@@ -12,22 +12,7 @@
|
|||||||
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
// 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){
|
|
||||||
TreeNode *node;
|
|
||||||
node = (TreeNode*)malloc(sizeof(TreeNode));
|
|
||||||
if(node == NULL){
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
node->data = malloc(dataSize);
|
|
||||||
if(node->data == NULL){
|
|
||||||
free(node);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
memcpy(node->data, data, dataSize);
|
|
||||||
node->left;
|
|
||||||
node->right;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// 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.
|
||||||
|
|||||||
@@ -41,15 +41,9 @@ unitTests: stacktest
|
|||||||
test_stack: stacktest.o stack.o
|
test_stack: stacktest.o stack.o
|
||||||
$(CC) $(FLAGS) $^ -o stacktest
|
$(CC) $(FLAGS) $^ -o stacktest
|
||||||
|
|
||||||
test_numbers: numberstest.o numbers.o
|
|
||||||
$(CC) $(FLAGS) $^ -o numberstest
|
|
||||||
|
|
||||||
test_stack.o: stacktest.c
|
test_stack.o: stacktest.c
|
||||||
$(CC) -c $(FLAGS) $< -o $@
|
$(CC) -c $(FLAGS) $< -o $@
|
||||||
|
|
||||||
test_numbers.o: numberstest.c
|
|
||||||
$(CC) -c $(FLAGS) $< -o $@
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
@@ -80,4 +79,4 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
|||||||
}
|
}
|
||||||
free(copy);
|
free(copy);
|
||||||
return duplicate;
|
return duplicate;
|
||||||
}
|
}
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "numbers.h"
|
|
||||||
|
|
||||||
//Funktion, um nachzuprüfen, ob Test bestanden wurde
|
|
||||||
void check(char *msg, int expected, int actual){
|
|
||||||
printf("%s: ", msg);
|
|
||||||
printf("Erwartet: %d, Tatsächlich: %d\n", expected, actual);
|
|
||||||
if(expected == actual){
|
|
||||||
printf("Test bestanden\n");
|
|
||||||
}else{
|
|
||||||
printf("Test nicht bestanden\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
//prüfen, ob len < 2 NULL zurückgibt
|
|
||||||
unsigned int *numbers = createNumbers(1);
|
|
||||||
check("createNumbers mit len < 2", 1, numbers == NULL);
|
|
||||||
if(numbers != NULL){
|
|
||||||
free(numbers);
|
|
||||||
}
|
|
||||||
//prüfen, ob len = 10 ein Array zurückgibt
|
|
||||||
int len = 10;
|
|
||||||
numbers = createNumbers(len);
|
|
||||||
check("createNumbers mit len = 10", 1, numbers != NULL);
|
|
||||||
//prüfen, ob alle Zahlen zwischen 1 und 2*len liegen
|
|
||||||
int OK = 1;
|
|
||||||
for(int i = 0; i < len; i++){
|
|
||||||
if(numbers[i] < 1 || numbers[i] > 2*len){
|
|
||||||
OK = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
check("Zahlenbereich bei createNumbers", 1, OK);
|
|
||||||
//prüfen, ob genau eine Zahl doppelt vorkommt
|
|
||||||
int duplikat = 0;
|
|
||||||
for(int i = 0; i < len; i++){
|
|
||||||
for(int j = i+1; j < len; j++){
|
|
||||||
if(numbers[i] == numbers[j]){
|
|
||||||
duplikat++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
check("Genau ein Duplikat bei createNumbers", 1, duplikat == 1);
|
|
||||||
free(numbers);
|
|
||||||
|
|
||||||
//Tests getDuplicate
|
|
||||||
int testArray1[] = {1,2,3,4,5,5,6,7,8,9};
|
|
||||||
int result = getDuplicate(testArray1, 10);
|
|
||||||
check("getDuplicate Test 1", 5, result);
|
|
||||||
int testArray2[] = {10,20,30,40,50,60,70,80,90,90};
|
|
||||||
result = getDuplicate(testArray2, 10);
|
|
||||||
check("getDuplicate Test 2", 90, result);
|
|
||||||
//prüfen bei ungültiger länge
|
|
||||||
result = getDuplicate(testArray2, 1);
|
|
||||||
check("getDuplicate mit len < 2", 0, result);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -8,18 +8,14 @@
|
|||||||
* `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.
|
||||||
|
|
||||||
|
|
||||||
StackNode *push(StackNode *stack, void *data)
|
StackNode *push(StackNode *stack, void *data)
|
||||||
{
|
{
|
||||||
//Neue Node wird erstellt, Speicher wird zugewiesen
|
|
||||||
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
|
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
|
||||||
if (newNode == NULL) {
|
if (newNode == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
//Data und der zeiger auf den nächsten Node werden zugewiesen
|
newNode ->data = data;
|
||||||
newNode->data = data;
|
newNode ->next = stack;
|
||||||
newNode->next = stack;
|
|
||||||
return newNode;
|
return newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,11 +23,9 @@ StackNode *push(StackNode *stack, void *data)
|
|||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
//Wenn Stack leer, wird NULL zurückgegeben
|
|
||||||
if(stack == NULL) {
|
if(stack == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
//Nächster Node wird gespeichert, aktuellerr Node wird freigegeben
|
|
||||||
StackNode *next = stack->next;
|
StackNode *next = stack->next;
|
||||||
free(stack);
|
free(stack);
|
||||||
return next;
|
return next;
|
||||||
@@ -49,7 +43,6 @@ if(stack == NULL) {
|
|||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
//Speichert den aktuellen Node, freed ihn dann und geht zum nächsten
|
|
||||||
StackNode *temp;
|
StackNode *temp;
|
||||||
while(stack != NULL) {
|
while(stack != NULL) {
|
||||||
temp = stack;
|
temp = stack;
|
||||||
|
|||||||
Reference in New Issue
Block a user