Compare commits

..

4 Commits
Lena ... main

11 changed files with 200 additions and 156 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"files.associations": {
"stdio.h": "c",
"unity.h": "c"
}
}

View File

@ -17,11 +17,20 @@ 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 top node and push all its left nodes. // push the right 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

View File

@ -35,9 +35,12 @@ $(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
# -------------------------- # --------------------------

View File

@ -5,7 +5,9 @@
#include "numbers.h" #include "numbers.h"
#include "bintree.h" #include "bintree.h"
unsigned int RANDOM_SEED = 0; unsigned int 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.
@ -17,13 +19,13 @@ unsigned int RANDOM_SEED = 0;
// 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) unsigned int setSeed(unsigned int seed_test)
{ {
RANDOM_SEED = seed; seed = seed_test;
return RANDOM_SEED; return seed;
} }
void createchecknumber(unsigned int len, unsigned int numbers[]) void create_unique_random_numbers(unsigned int len, unsigned int numbers[])
{ {
for (unsigned int i = 0; i < len; i++) for (unsigned int i = 0; i < len; i++)
{ {
@ -56,43 +58,48 @@ void createchecknumber(unsigned int len, unsigned int numbers[])
unsigned int *createNumbers(unsigned int len) unsigned int *createNumbers(unsigned int len)
{ {
if (RANDOM_SEED == 0)
if (len <= 2)
{
return numbers;
}
if (seed == 0)
{ {
srand(time(NULL)); srand(time(NULL));
} }
else else
{ {
srand(RANDOM_SEED); srand(seed);
} }
unsigned int *numbers = malloc(sizeof(unsigned int) * len); numbers = malloc(sizeof(unsigned int) * len);
if (numbers == NULL)
if (numbers != NULL)
{ {
return 0; create_unique_random_numbers(len, numbers);
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 = 0;
j = 0;
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++)
{ {
@ -108,11 +115,26 @@ void sortnumbers(unsigned int numbers[], unsigned int len)
} }
} }
} }
*/
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. // 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)
{
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;
@ -123,19 +145,21 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
copynumbers[i] = numbers[i]; copynumbers[i] = numbers[i];
} }
sortnumbers(copynumbers, len); qsort(copynumbers, len, sizeof(unsigned int), compare);
// sortnumbers(copynumbers, len);
for (unsigned i = 0; i < len - 1; i++) for (unsigned i = 0; i < len - 1; i++)
{ {
if (copynumbers[i] == copynumbers[i+1]) if (copynumbers[i] == copynumbers[i + 1])
{ {
unsigned int a = copynumbers[i]; unsigned int a = copynumbers[i];
free (copynumbers); free(copynumbers);
return a; return a;
} }
} }
free (copynumbers); free(copynumbers);
return 0; return 0;
} }

View File

@ -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);

58
stack.c
View File

@ -1,53 +1,65 @@
#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. */
// Pushes data as pointer onto the stack. static StackNode *createNewElement()
static StackNode *createEmptyElement()
{ {
return malloc(sizeof(StackNode)); return malloc(sizeof(StackNode));
} }
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data) StackNode *push(StackNode *stack, void *data)
{ {
StackNode *newElement = createEmptyElement(); StackNode *node = createNewElement();
newElement->data = data; if (node != NULL)
newElement->next = NULL;
if (stack == NULL)
{ {
stack = newElement; node->data = data;
node->next = stack;
return node;
} }
else
{
StackNode *currentElement = stack;
while(currentElement->next != NULL)
{
currentElement = currentElement->next;
}
currentElement->next = newElement;
return stack; 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;
}
}

View File

@ -8,11 +8,11 @@ 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 node typedef struct stackNode
{ {
void *data; void *data;
struct node* next; struct stackNode *next;
}StackNode; } 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);
@ -27,4 +27,4 @@ void *top(StackNode *stack);
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack); void clearStack(StackNode *stack);
#endif #endif

View File

@ -1,84 +0,0 @@
#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();
}

0
test_bintree.c Normal file
View File

56
test_numbers.c Normal file
View File

@ -0,0 +1,56 @@
#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();
}

View File

@ -1,20 +1,38 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "unity/unity.h"
#include "stack.h" #include "stack.h"
void testPushAddsElementCorrectly(void) void test_stackOrderIsCorrect()
{ {
int a = 10; int expectedValues[] = {1, 2, 3, 4};
StackNode *node = push(NULL, &a); const unsigned int exprectedLen = sizeof(expectedValues) / sizeof(expectedValues[0]);
unsigned int observedLen = 0;
StackNode *stack = NULL;
TEST_ASSERT_NOT_NULL(node); for(int i = 0; i < exprectedLen; i++)
TEST_ASSERT_EQUAL_INT(a, *((int*)node->data)); //void Zeiger in integer-Zeiger umwandeln {
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) int main(void)
{ {
UNITY_BEGIN(); UNITY_BEGIN();
RUN_TEST(testPushAddsElementCorrectly); RUN_TEST(test_stackOrderIsCorrect);
return UNITY_END(); return UNITY_END();
} }