generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 63a9126e4e | |||
| c66935ad26 |
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"files.associations": {
|
|
||||||
"stdio.h": "c",
|
|
||||||
"unity.h": "c"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
13
bintree.h
13
bintree.h
@ -17,20 +17,11 @@ typedef struct node
|
|||||||
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);
|
||||||
// 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.
|
||||||
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
// 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 right top node and push all its left nodes.
|
// push the top node and push all its left nodes.
|
||||||
// Hilfsfunktion pushAllLeft (node) auf rechten nachfolger
|
|
||||||
// wenn neuer Baum dann pushallleft auf wurzel
|
|
||||||
// immer aufräumen!!!
|
|
||||||
// kein vorsortiertes array, sonst entarteter Baum
|
|
||||||
|
|
||||||
void *nextTreeData(TreeNode *root);
|
void *nextTreeData(TreeNode *root);
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
// sortierte Ausgabe
|
|
||||||
// aufrufen wie mit strtok
|
|
||||||
// welcher Baum durchlaufen
|
|
||||||
// immer null aufrufen bis fertig durchlaufen weil null zurückgegeben wird
|
|
||||||
void clearTree(TreeNode *root);
|
void clearTree(TreeNode *root);
|
||||||
// Returns the number of entries in the tree given by root.
|
// Returns the number of entries in the tree given by root.
|
||||||
unsigned int treeSize(const TreeNode *root);
|
unsigned int treeSize(const TreeNode *root);
|
||||||
//rekursiv die Anzahl der gesetzten Knoten ermitteln
|
|
||||||
#endif
|
#endif
|
||||||
7
makefile
7
makefile
@ -35,12 +35,9 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
test_numbers:
|
|
||||||
$(CC) -o test_numbers test_numbers.c numbers.c $(unityfolder)/unity.c $(FLAGS)
|
|
||||||
|
|
||||||
test_stack:
|
|
||||||
$(CC) -o test_stack test_stack.c stack.c $(unityfolder)/unity.c $(FLAGS)
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
82
numbers.c
82
numbers.c
@ -5,9 +5,7 @@
|
|||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
unsigned int seed = 0;
|
unsigned int RANDOM_SEED = 0;
|
||||||
|
|
||||||
unsigned int *numbers;
|
|
||||||
|
|
||||||
// TODO: getDuplicate und createNumbers implementieren
|
// TODO: getDuplicate und createNumbers implementieren
|
||||||
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
|
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
|
||||||
@ -19,13 +17,13 @@ unsigned int *numbers;
|
|||||||
// 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 setSeed(unsigned int seed_test)
|
unsigned int setSeed(unsigned int seed)
|
||||||
{
|
{
|
||||||
seed = seed_test;
|
RANDOM_SEED = seed;
|
||||||
return seed;
|
return RANDOM_SEED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void create_unique_random_numbers(unsigned int len, unsigned int numbers[])
|
void createchecknumber(unsigned int len, unsigned int numbers[])
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < len; i++)
|
for (unsigned int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
@ -33,9 +31,7 @@ void create_unique_random_numbers(unsigned int len, unsigned int numbers[])
|
|||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
unsigned int value = 0;
|
unsigned int value = (rand() % (len * 2) + 1);
|
||||||
value = (rand() % (len * 2) + 1);
|
|
||||||
|
|
||||||
same = 0;
|
same = 0;
|
||||||
|
|
||||||
for (unsigned int j = 0; j < i; j++)
|
for (unsigned int j = 0; j < i; j++)
|
||||||
@ -58,48 +54,44 @@ void create_unique_random_numbers(unsigned int len, unsigned int numbers[])
|
|||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
if (len <= 2) //sonst Index-Auswahl-Dauerschleife
|
||||||
if (len <= 2)
|
|
||||||
{
|
{
|
||||||
return numbers;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (seed == 0)
|
if (RANDOM_SEED == 0)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
srand(seed);
|
srand(RANDOM_SEED);
|
||||||
}
|
}
|
||||||
|
|
||||||
numbers = malloc(sizeof(unsigned int) * len);
|
unsigned int *numbers = malloc(sizeof(unsigned int) * len);
|
||||||
|
if (numbers == NULL)
|
||||||
if (numbers != NULL)
|
|
||||||
{
|
{
|
||||||
create_unique_random_numbers(len, numbers);
|
return NULL;
|
||||||
|
|
||||||
unsigned int i = 0;
|
|
||||||
unsigned int j = 0;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
i = 0;
|
|
||||||
j = 0;
|
|
||||||
|
|
||||||
i = rand() % len;
|
|
||||||
j = rand() % len;
|
|
||||||
|
|
||||||
} while (i == j || i == j + 1 || i == j - 1);
|
|
||||||
|
|
||||||
numbers[i] = numbers[j];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createchecknumber(len, numbers);
|
||||||
|
|
||||||
|
unsigned int i = 0;
|
||||||
|
unsigned int j = 0;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
i = rand() % len;
|
||||||
|
j = rand() % len;
|
||||||
|
|
||||||
|
} while (i == j);
|
||||||
|
|
||||||
|
numbers[i] = numbers[j];
|
||||||
|
|
||||||
return numbers;
|
return numbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
void sortnumbers(unsigned int numbers[], unsigned int len)
|
||||||
void sortnumbers(unsigned int numbers[], unsigned int len) //-> qsort
|
|
||||||
{
|
{
|
||||||
for (unsigned int j = 0; j < len - 1; j++)
|
for (unsigned int j = 0; j < len - 1; j++)
|
||||||
{
|
{
|
||||||
@ -115,39 +107,27 @@ void sortnumbers(unsigned int numbers[], unsigned int len) //-> qsort
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
int compare(const void *arg1, const void *arg2)
|
|
||||||
{
|
|
||||||
unsigned int a = *(const unsigned int *)arg1;
|
|
||||||
unsigned int b = *(const unsigned int *)arg2;
|
|
||||||
|
|
||||||
return ((a > b) - (a < b));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
if (len <= 2)
|
if (numbers == NULL || len < 2) // sonst Index-Auswahl-Dauerschleife
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int *copynumbers = malloc(sizeof(unsigned int) * len);
|
unsigned int *copynumbers = malloc(sizeof(unsigned int) * len);
|
||||||
|
|
||||||
if (copynumbers == NULL)
|
if (copynumbers == NULL)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < len; i++)
|
for (unsigned int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
copynumbers[i] = numbers[i];
|
copynumbers[i] = numbers[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
qsort(copynumbers, len, sizeof(unsigned int), compare);
|
sortnumbers(copynumbers, len);
|
||||||
|
|
||||||
// sortnumbers(copynumbers, len);
|
|
||||||
|
|
||||||
for (unsigned i = 0; i < len - 1; i++)
|
for (unsigned i = 0; i < len - 1; i++)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
#ifndef NUMBERS_H
|
#ifndef NUMBERS_H
|
||||||
#define NUMBERS_H
|
#define NUMBERS_H
|
||||||
|
|
||||||
|
extern unsigned int RANDOM_SEED;
|
||||||
|
|
||||||
// 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.
|
||||||
|
|
||||||
int compare(const void *arg1, const void *arg2);
|
|
||||||
|
|
||||||
unsigned int setSeed(unsigned int seed);
|
unsigned int setSeed(unsigned int seed);
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len);
|
unsigned int *createNumbers(unsigned int len);
|
||||||
|
|||||||
48
stack.c
48
stack.c
@ -1,65 +1,33 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
// TODO: grundlegende Stackfunktionen implementieren:
|
//TODO: grundlegende Stackfunktionen implementieren:
|
||||||
/* * `push`: legt ein Element oben auf den Stack,
|
/* * `push`: legt ein Element oben auf den Stack,
|
||||||
* `pop`: entfernt das oberste Element,
|
* `pop`: entfernt das oberste Element,
|
||||||
* `top`: liefert das oberste Element zurück,
|
* `top`: liefert das oberste Element zurück,
|
||||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
* `clearStack`: gibt den gesamten Speicher frei. */
|
||||||
|
|
||||||
static StackNode *createNewElement()
|
|
||||||
{
|
|
||||||
return malloc(sizeof(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)
|
||||||
{
|
{
|
||||||
StackNode *node = createNewElement();
|
|
||||||
if (node != NULL)
|
|
||||||
{
|
|
||||||
node->data = data;
|
|
||||||
node->next = stack;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
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
|
||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
if (stack != NULL)
|
|
||||||
{
|
|
||||||
StackNode *currentElement = stack;
|
|
||||||
stack = stack->next;
|
|
||||||
|
|
||||||
free(currentElement);
|
|
||||||
}
|
|
||||||
return stack;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
if (stack != NULL)
|
|
||||||
{
|
|
||||||
return stack->data;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
StackNode *nextElement = stack;
|
|
||||||
|
|
||||||
while (stack != NULL)
|
}
|
||||||
{
|
|
||||||
nextElement = stack->next;
|
|
||||||
|
|
||||||
free(stack);
|
|
||||||
|
|
||||||
stack = nextElement;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
5
stack.h
5
stack.h
@ -8,11 +8,6 @@ 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 stackNode
|
|
||||||
{
|
|
||||||
void *data;
|
|
||||||
struct stackNode *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);
|
||||||
|
|||||||
84
test__numbers.c
Normal file
84
test__numbers.c
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "unity/unity.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()
|
||||||
|
{
|
||||||
|
setSeed(1);
|
||||||
|
unsigned int len = 10;
|
||||||
|
unsigned int *test_numbers = createNumbers(len);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(test_numbers);
|
||||||
|
|
||||||
|
//Bereich checken:
|
||||||
|
for (unsigned int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(test_numbers[i] >= 1);
|
||||||
|
TEST_ASSERT_TRUE(test_numbers[i] <= len * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
//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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_duplicate_value()
|
||||||
|
{
|
||||||
|
unsigned int test_numbers[6] = {1, 2, 3, 4, 5, 5};
|
||||||
|
unsigned int duplicate = 0;
|
||||||
|
|
||||||
|
duplicate = getDuplicate(test_numbers, 6);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL(5, duplicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_duplicate_value_if_false()
|
||||||
|
{
|
||||||
|
unsigned int test_numbers[6] = {1, 2, 3, 4, 5, 6};
|
||||||
|
unsigned int duplicate = 0;
|
||||||
|
|
||||||
|
duplicate = getDuplicate(test_numbers, 6);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL(0, duplicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp(void) {}
|
||||||
|
|
||||||
|
void tearDown(void) {}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
RUN_TEST(test_length_and_duplicate);
|
||||||
|
RUN_TEST(test_duplicate_value);
|
||||||
|
RUN_TEST(test_duplicate_value_if_false);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
@ -1,56 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "unity/unity.h"
|
|
||||||
#include "numbers.h"
|
|
||||||
|
|
||||||
void test_createNumbers_create_one_double_number()
|
|
||||||
{
|
|
||||||
setSeed(1);
|
|
||||||
unsigned int len = 20;
|
|
||||||
unsigned int *numbers = createNumbers(len);
|
|
||||||
|
|
||||||
qsort(numbers, len, sizeof(unsigned int), compare);
|
|
||||||
|
|
||||||
unsigned int count = 0;
|
|
||||||
for (unsigned int i = 1; i < len; i++)
|
|
||||||
{
|
|
||||||
if (numbers[i] == numbers[i - 1])
|
|
||||||
{
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TEST_ASSERT_EQUAL_UINT(1, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_getduplicate_returns_corecct_duplicate()
|
|
||||||
{
|
|
||||||
unsigned int test_numbers[6] = {1, 2, 3, 4, 5, 5};
|
|
||||||
unsigned int duplicate = 0;
|
|
||||||
|
|
||||||
duplicate = getDuplicate(test_numbers, 6);
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL(5, duplicate);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_getduplicate_know_if_no_duplicate()
|
|
||||||
{
|
|
||||||
unsigned int test_numbers[6] = {1, 2, 3, 4, 5, 6};
|
|
||||||
unsigned int duplicate = 0;
|
|
||||||
|
|
||||||
duplicate = getDuplicate(test_numbers, 6);
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL(0, duplicate);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setUp(void) {}
|
|
||||||
void tearDown(void) {}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
UNITY_BEGIN();
|
|
||||||
|
|
||||||
RUN_TEST(test_createNumbers_create_one_double_number);
|
|
||||||
RUN_TEST(test_getduplicate_returns_corecct_duplicate);
|
|
||||||
RUN_TEST(test_getduplicate_know_if_no_duplicate);
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
38
test_stack.c
38
test_stack.c
@ -1,38 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "unity/unity.h"
|
|
||||||
#include "stack.h"
|
|
||||||
|
|
||||||
void test_stackOrderIsCorrect()
|
|
||||||
{
|
|
||||||
int expectedValues[] = {1, 2, 3, 4};
|
|
||||||
const unsigned int exprectedLen = sizeof(expectedValues) / sizeof(expectedValues[0]);
|
|
||||||
unsigned int observedLen = 0;
|
|
||||||
StackNode *stack = NULL;
|
|
||||||
|
|
||||||
for(int i = 0; i < exprectedLen; i++)
|
|
||||||
{
|
|
||||||
stack = push(stack, &expectedValues[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int i = exprectedLen-1; i >= 0 && stack != NULL; i--)
|
|
||||||
{
|
|
||||||
TEST_ASSERT_EQUAL(expectedValues[i], *(int *)top(stack));
|
|
||||||
observedLen++;
|
|
||||||
stack = pop(stack);
|
|
||||||
}
|
|
||||||
TEST_ASSERT_EQUAL_UINT32(exprectedLen, observedLen);
|
|
||||||
|
|
||||||
clearStack(stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setUp(void) {}
|
|
||||||
void tearDown(void) {}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
UNITY_BEGIN();
|
|
||||||
RUN_TEST(test_stackOrderIsCorrect);
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user