Fertig
This commit is contained in:
parent
6ccb495beb
commit
87c19b6b25
17
bintree.c
17
bintree.c
@ -73,7 +73,24 @@ void *nextTreeData(TreeNode *root)
|
|||||||
if (root != NULL) {
|
if (root != NULL) {
|
||||||
clearStack(stack);
|
clearStack(stack);
|
||||||
stack = NULL;
|
stack = NULL;
|
||||||
|
TreeNode *current = root;
|
||||||
|
while (current != NULL) {
|
||||||
|
stack = push(stack,current);
|
||||||
|
current = current->left;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (stack == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
TreeNode *node = (TreeNode*) top(stack);
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
TreeNode *right = node->right;
|
||||||
|
while (right != NULL) {
|
||||||
|
stack = push(stack,right);
|
||||||
|
right = right->left;
|
||||||
|
}
|
||||||
|
return node->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
|
|||||||
1
bintree_tests.c
Normal file
1
bintree_tests.c
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
||||||
@ -1,5 +1 @@
|
|||||||
player_name;18989
|
|
||||||
test;9905
|
|
||||||
player_name;4983
|
|
||||||
highscores.txt;4979
|
|
||||||
player1;3999
|
|
||||||
|
|||||||
6
makefile
6
makefile
@ -37,12 +37,14 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
stackTests: stack.o test_stack.c $(unity_strc)
|
stackTests: stack.o test_stack.c $(unity_strc)
|
||||||
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTests
|
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTests
|
||||||
|
numbersTests: numbers.o bintree.o stack.o test_numbers.c $(unity_strc)
|
||||||
|
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runNumbersTests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
clean:
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
del /f *.o doble doble_initial runStackTests
|
del /f *.o doble doble_initial runStackTests runNumbersTests
|
||||||
else
|
else
|
||||||
rm -f *.o doble doble_initial runStackTests
|
rm -f *.o doble doble_initial runStackTests runNumbersTests
|
||||||
endif
|
endif
|
||||||
24
numbers.c
24
numbers.c
@ -20,19 +20,23 @@ int compareFct(const void *a, const void *b)
|
|||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
if (len <= 2)
|
||||||
|
return NULL;
|
||||||
TreeNode *root = NULL;
|
TreeNode *root = NULL;
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
while (treeSize(root) < len-1) {
|
unsigned int *numbers = malloc(sizeof(unsigned int)*len);
|
||||||
unsigned int zahl = rand()%(2*len)+1;
|
int counter = 0;
|
||||||
root = addToTree(root, &zahl,sizeof(zahl), compareFct, 0);
|
while (treeSize(root) < len) {
|
||||||
|
unsigned int num = rand()%(2*len)+1;
|
||||||
|
int isDuplicate = 0;
|
||||||
|
root = addToTree(root, &num,sizeof(num), compareFct, &isDuplicate);
|
||||||
|
if (isDuplicate == 0) {
|
||||||
|
numbers[counter] = num;
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
unsigned int *numbers = malloc(sizeof(unsigned int)*(len-1));
|
unsigned int duplicate = rand()%(len);
|
||||||
for (int i = 0; i<len - 1; i++)
|
unsigned int duplicateIdx = rand()%(len);
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
unsigned int duplicate = rand()%(len-1);
|
|
||||||
unsigned int duplicateIdx = rand()%(len-1);
|
|
||||||
numbers[duplicate] = numbers[duplicateIdx];
|
numbers[duplicate] = numbers[duplicateIdx];
|
||||||
return numbers;
|
return numbers;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,5 +8,5 @@ unsigned int *createNumbers(unsigned int len);
|
|||||||
|
|
||||||
// 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);
|
||||||
|
int compareFct(const void *a, const void *b);
|
||||||
#endif
|
#endif
|
||||||
58
test_numbers.c
Normal file
58
test_numbers.c
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#include "numbers.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
void generateNumbersFail()
|
||||||
|
{
|
||||||
|
unsigned int* result = createNumbers(2);
|
||||||
|
if(result == NULL)
|
||||||
|
printf("PASS ");
|
||||||
|
else
|
||||||
|
printf("FAIL ");
|
||||||
|
printf("generateNumbersFailsOnTooSmallLength\n");
|
||||||
|
}
|
||||||
|
void generateNumbersSuccess()
|
||||||
|
{
|
||||||
|
unsigned int* result = createNumbers(10);
|
||||||
|
int duplicates = 0;
|
||||||
|
qsort(result, 10, sizeof(unsigned int), compareFct);
|
||||||
|
for(int i = 0; i < 9; i++)
|
||||||
|
{
|
||||||
|
if(result[i]==result[i+1])
|
||||||
|
{
|
||||||
|
duplicates++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(duplicates == 1)
|
||||||
|
printf("PASS ");
|
||||||
|
else
|
||||||
|
printf("FAIL ");
|
||||||
|
printf("generateNumbersSuccessful\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
void duplicateErrorTest(){
|
||||||
|
unsigned int result = getDuplicate(NULL,10);
|
||||||
|
if(result == 0)
|
||||||
|
printf("PASS ");
|
||||||
|
else
|
||||||
|
printf("FAIL ");
|
||||||
|
printf("duplicateReturnsZeroOnErrors\n");
|
||||||
|
}
|
||||||
|
void duplicateNormalTest()
|
||||||
|
{
|
||||||
|
unsigned int numbers[10]={2,3,4,5,6,7,2,9,10};
|
||||||
|
unsigned int result = getDuplicate(numbers,10);
|
||||||
|
if(result == 2)
|
||||||
|
printf("PASS ");
|
||||||
|
else
|
||||||
|
printf("FAIL ");
|
||||||
|
printf("duplicateReturnsCorrectDuplicate\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
generateNumbersFail();
|
||||||
|
generateNumbersSuccess();
|
||||||
|
duplicateNormalTest();
|
||||||
|
duplicateErrorTest();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user