generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6d9cd9dd84 | ||
|
|
f5fbcee1ea | ||
|
|
d30c0b29c5 |
No files matched your search
@@ -12,36 +12,7 @@
|
||||
// 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)
|
||||
{
|
||||
if(!root)
|
||||
{
|
||||
root = malloc(sizeof(node));
|
||||
*node newNode;
|
||||
*newNode.data = malloc(dataSize);
|
||||
strcpy(*newNode.data, data);
|
||||
*newNode.left = NULL;
|
||||
*newNode.right = NULL;
|
||||
if(isDuplicate)
|
||||
*isDuplicate = 0;
|
||||
return newNode;
|
||||
}
|
||||
if(compareFct(*data, *root->data) > 0)
|
||||
{
|
||||
*right = addToTree(right, data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
else if(compareFct(*data, *root->data) < 0)
|
||||
{
|
||||
*left = addToTree(right, data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!isDuplicate)
|
||||
*left = addToTree(right, data, dataSize, compareFct, isDuplicate);
|
||||
else
|
||||
{
|
||||
*isDuplicate = 1;
|
||||
}
|
||||
}
|
||||
return root;
|
||||
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -49,50 +20,17 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
||||
// push the top node and push all its left nodes.
|
||||
void *nextTreeData(TreeNode *root)
|
||||
{
|
||||
StackNode *stackRoot = NULL;//Generates a new, empty stack
|
||||
stackRoot = iterateThroughTree(root, stackRoot); //Fills the stack via the helping function iterateThroughTree
|
||||
stackNode * tempBuffer = pop(stackRoot);
|
||||
clearStack(stackRoot);
|
||||
return tempBuffer;
|
||||
}
|
||||
|
||||
//Function to aid nextTreeData
|
||||
StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot)
|
||||
{
|
||||
if(!root)
|
||||
{
|
||||
return stackRoot;
|
||||
}
|
||||
stackRoot = iterateThroughTree(root->left, stackRoot);
|
||||
stackNode = push(stackNode, root->data);
|
||||
stackRoot = iterateThroughTree(root->right, stackRoot);
|
||||
return stackRoot;
|
||||
}
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
if(left)
|
||||
clearTree(left);
|
||||
if(right)
|
||||
clearTree(right);
|
||||
free(data);
|
||||
data = NULL;
|
||||
free(root);
|
||||
root = NULL;
|
||||
|
||||
}
|
||||
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root)
|
||||
{
|
||||
if(root)
|
||||
{
|
||||
unsigned int tempSize = 0;
|
||||
if(left)
|
||||
tempSize += treeSize(left);
|
||||
if(right)
|
||||
tempSize += treeSize(right);
|
||||
return tempSize + 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -23,6 +23,5 @@ void *nextTreeData(TreeNode *root);
|
||||
void clearTree(TreeNode *root);
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root);
|
||||
//Function to aid nextTreeData
|
||||
StackNode iterateThroughTree(TreeNode *root, StackNode stackRoot);
|
||||
|
||||
#endif
|
||||
@@ -1 +1,2 @@
|
||||
player_name;18787
|
||||
player1;3999
|
||||
@@ -37,6 +37,8 @@ $(program_obj_filesobj_files): %.o: %.c
|
||||
# --------------------------
|
||||
unitTests:
|
||||
echo "needs to be implemented"
|
||||
numbersTests: numbers.o numbersTests.c $(unityfolder)/unity.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runNumbersTests numbersTests.c numbers.o $(unityfolder)/unity.c
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
|
||||
@@ -14,13 +14,92 @@
|
||||
// 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
|
||||
// creating random numbers.
|
||||
unsigned int *createNumbers(unsigned int len)
|
||||
{
|
||||
unsigned int *createNumbers(unsigned int len){
|
||||
|
||||
if(len == 1){
|
||||
printf("Minimum length is 2");
|
||||
return NULL;
|
||||
}
|
||||
srand(time(NULL));
|
||||
unsigned int *numbers = malloc(len * sizeof(unsigned int)); //reserving memory for array
|
||||
if(!numbers){
|
||||
printf("Error allocating Memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < len; i++){
|
||||
numbers[i] = rand() % (2 * len) + 1; //creates randorm number between 1 and 2x len
|
||||
if(getDuplicate(numbers, i+1)){ //We use i+1 to refer to current initialised array length
|
||||
i--; //If there a douplicate has been created, i gets 1 lower therefore overwriting it in next iteration
|
||||
}
|
||||
}
|
||||
//Creating Duplicate
|
||||
unsigned int duplicationSuccess = 0;
|
||||
while (duplicationSuccess == 0){
|
||||
unsigned int duplicateInitialIndex = rand()%len; //Getting random index for creating duplicate
|
||||
unsigned int duplicateReplaceIndex = rand ()%len; //Index of Number replaced by duplicateInitialIndex
|
||||
|
||||
if(duplicateInitialIndex == duplicateReplaceIndex)
|
||||
continue; //returning process if chosen duplicate is replacing itsself
|
||||
|
||||
duplicationSuccess = 1;
|
||||
numbers[duplicateReplaceIndex] = numbers [duplicateInitialIndex];
|
||||
}
|
||||
|
||||
|
||||
|
||||
return numbers;
|
||||
|
||||
}
|
||||
|
||||
// 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 *sortedNumbers = malloc(len * sizeof(unsigned int));
|
||||
if(!sortedNumbers){
|
||||
printf("Error allocating memory");
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Creating Copy of Numbers Array
|
||||
for (int i = 0; i < len; i++){
|
||||
sortedNumbers[i] = numbers[i];
|
||||
}
|
||||
|
||||
unsigned int duplicate = 0;
|
||||
unsigned int temp = 0;
|
||||
unsigned int duplicateCount = 0;
|
||||
|
||||
//Sorting sortedNumbers
|
||||
for (int i = 0; i < len; i++){
|
||||
for(int j = 0; j< len - 1; j++){
|
||||
if(sortedNumbers[j]> sortedNumbers[j+1]){ //Switching Elements
|
||||
temp = sortedNumbers[j];
|
||||
sortedNumbers[j] = sortedNumbers[j+1];
|
||||
sortedNumbers[j+1] = temp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
//identifying duplicates
|
||||
|
||||
for(int i = 0; i < len; i++){
|
||||
if(sortedNumbers[i] == sortedNumbers[i+1]){
|
||||
duplicate = sortedNumbers[i];
|
||||
duplicateCount++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
free(sortedNumbers);
|
||||
if(duplicateCount != 1){
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
return duplicate;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include "unity.h"
|
||||
#include "numbers.h"
|
||||
|
||||
void test_ArrayIsCreatedAndNotNull(void){
|
||||
size_t len = 15;
|
||||
int *testArray = createNumbers(len);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(testArray); //If array hasnt been created at all
|
||||
|
||||
free(testArray);
|
||||
}
|
||||
|
||||
|
||||
void test_getDouplicateReturnsDouplicateNumber(void){
|
||||
|
||||
unsigned int testArray[8] = {1, 2, 3, 3, 4, 5, 6, 7};
|
||||
unsigned int testValue = getDuplicate(testArray, 8);
|
||||
|
||||
TEST_ASSERT_EQUAL(3, testValue);
|
||||
}
|
||||
|
||||
|
||||
void test_getDouplicateReturnsZeroWhenThereIsNoDuplicate(void){
|
||||
unsigned int testArray[8] = {1, 2, 3, 4, 5, 6, 7, 8};
|
||||
unsigned int testValue = getDuplicate(testArray, 8);
|
||||
|
||||
TEST_ASSERT_EQUAL(0, testValue);
|
||||
}
|
||||
|
||||
void test_getDouplicateReturnsZeroWhenThereAreMultipleDuplicates(void){
|
||||
|
||||
unsigned int testArray[8] = {1, 3, 3, 4, 5, 6, 7, 7};
|
||||
unsigned int testValue = getDuplicate(testArray, 8);
|
||||
|
||||
TEST_ASSERT_EQUAL(0, testValue);
|
||||
}
|
||||
|
||||
void test_CreateNumbersCreatesDuplicatein2LenArray(void){
|
||||
unsigned int *testArray = createNumbers(2);
|
||||
unsigned int testValue = getDuplicate(testArray, 2);
|
||||
|
||||
if(!testValue)
|
||||
testValue = 0;
|
||||
|
||||
else
|
||||
testValue = 1;
|
||||
|
||||
TEST_ASSERT_EQUAL(1, testValue);
|
||||
|
||||
}
|
||||
void setUp(void){}
|
||||
void tearDown(void){}
|
||||
|
||||
int main(){
|
||||
UNITY_BEGIN();
|
||||
|
||||
printf("\n============================\nNumbers tests\n============================\n");
|
||||
|
||||
RUN_TEST(test_ArrayIsCreatedAndNotNull);
|
||||
RUN_TEST(test_CreateNumbersCreatesDuplicatein2LenArray);
|
||||
RUN_TEST(test_getDouplicateReturnsDouplicateNumber);
|
||||
RUN_TEST(test_getDouplicateReturnsZeroWhenThereIsNoDuplicate);
|
||||
RUN_TEST(test_getDouplicateReturnsZeroWhenThereAreMultipleDuplicates);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in new issue
Block a user