generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
7
Commits
Sortierung
..
stack
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
288385a220 | ||
|
|
bf4c41b897 | ||
|
|
c663833b6e | ||
|
|
036158fc59 | ||
|
|
0bc9263b60 | ||
|
|
94e489cfae | ||
|
|
6671a3aacc |
+1
-1
@@ -1,2 +1,2 @@
|
||||
player_name;18787
|
||||
Lukas;4985
|
||||
player1;3999
|
||||
|
||||
@@ -35,10 +35,10 @@ $(program_obj_filesobj_files): %.o: %.c
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
unitTests:
|
||||
stackTests: stack.o test_stack.c $(unityfolder)/unity.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests test_stack.c stack.o $(unityfolder)/unity.c
|
||||
|
||||
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,92 +14,13 @@
|
||||
// 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){
|
||||
|
||||
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;
|
||||
unsigned int *createNumbers(unsigned int len)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
#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();
|
||||
}
|
||||
@@ -10,6 +10,22 @@
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data)
|
||||
{
|
||||
if(!(stack)) { // check if stack is empty
|
||||
stack = malloc(sizeof(StackNode));
|
||||
if(!stack)
|
||||
return NULL;
|
||||
stack->stackData = data;
|
||||
stack->below = NULL;
|
||||
return stack;
|
||||
}
|
||||
|
||||
StackNode* newStack = malloc(sizeof(StackNode));
|
||||
if(!newStack)
|
||||
return stack;
|
||||
newStack->below = stack;
|
||||
newStack->stackData = data;
|
||||
stack = newStack;
|
||||
return stack;
|
||||
|
||||
}
|
||||
|
||||
@@ -17,17 +33,32 @@ StackNode *push(StackNode *stack, void *data)
|
||||
// freed by caller.)
|
||||
StackNode *pop(StackNode *stack)
|
||||
{
|
||||
|
||||
if(stack) {
|
||||
StackNode* temp = stack;
|
||||
stack = stack->below;
|
||||
temp->stackData = NULL;
|
||||
free(temp);
|
||||
temp = NULL;
|
||||
return stack;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the data of the top element.
|
||||
void *top(StackNode *stack)
|
||||
{
|
||||
if(stack)
|
||||
return stack->stackData;
|
||||
else
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
// Clears stack and releases all memory.
|
||||
void clearStack(StackNode *stack)
|
||||
{
|
||||
while(stack)
|
||||
stack = pop(stack);
|
||||
|
||||
}
|
||||
@@ -9,6 +9,11 @@ The latest element is taken from the stack. */
|
||||
|
||||
//TODO: passenden Datentyp als struct anlegen
|
||||
|
||||
typedef struct StackNode {
|
||||
struct StackNode* below;
|
||||
void* stackData;
|
||||
} StackNode;
|
||||
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data);
|
||||
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
#include "stack.h"
|
||||
#include "unity.h"
|
||||
|
||||
|
||||
void test_firstNodeAddedCorrectly(void) {
|
||||
printf("\nStarting first test...\n");
|
||||
StackNode* modelStack = malloc(sizeof(StackNode));
|
||||
int modelData = 5;
|
||||
modelStack->below = NULL;
|
||||
modelStack->stackData = &modelData;
|
||||
|
||||
StackNode* testStack = NULL;
|
||||
int testData = 5;
|
||||
testStack = push(testStack, &testData);
|
||||
TEST_ASSERT_EQUAL_INT(*(int*)modelStack->stackData, *(int*)testStack->stackData);
|
||||
TEST_ASSERT_NULL(testStack->below);
|
||||
clearStack(modelStack);
|
||||
clearStack(testStack);
|
||||
}
|
||||
|
||||
void test_topReturnsCorrectValues(void) {
|
||||
printf("Starting second test...\n");
|
||||
StackNode* testStack = NULL;
|
||||
int data1 = 1;
|
||||
testStack = push(testStack, &data1);
|
||||
int* returnData1 = (int*) top(testStack);
|
||||
int data2 = 0;
|
||||
testStack = push(testStack, &data2);
|
||||
int* returnData2 = (int*) top(testStack);
|
||||
int data3 = 3;
|
||||
testStack = push(testStack, &data3);
|
||||
int* returnData3 = (int*) top(testStack);
|
||||
|
||||
size_t counter = 0;
|
||||
if(*returnData1 == 1)
|
||||
counter++;
|
||||
if(*returnData2 == 0)
|
||||
counter++;
|
||||
if(*returnData3 == 3)
|
||||
counter++;
|
||||
TEST_ASSERT_EQUAL_INT(3, counter);
|
||||
clearStack(testStack);
|
||||
}
|
||||
|
||||
void test_popRemovesCorrectly(void) {
|
||||
printf("Starting third test...\n");
|
||||
StackNode* testStack = NULL;
|
||||
int data1 = 1;
|
||||
testStack = push(testStack, &data1);
|
||||
int data2 = 0;
|
||||
testStack = push(testStack, &data2);
|
||||
int data3 = 3;
|
||||
testStack = push(testStack, &data3);
|
||||
int data4 = 9;
|
||||
testStack = push(testStack, &data4);
|
||||
|
||||
int* returnData1 = (int*) top(testStack);
|
||||
testStack = pop(testStack);
|
||||
int* returnData2 = (int*) top(testStack);
|
||||
testStack = pop(testStack);
|
||||
int* returnData3 = (int*) top(testStack);
|
||||
testStack = pop(testStack);
|
||||
int* returnData4 = (int*) top(testStack);
|
||||
testStack = pop(testStack);
|
||||
|
||||
size_t counter = 0;
|
||||
if(*returnData1 == 9)
|
||||
counter++;
|
||||
if(*returnData2 == 3)
|
||||
counter++;
|
||||
if(*returnData3 == 0)
|
||||
counter++;
|
||||
if(*returnData4 == 1)
|
||||
counter++;
|
||||
TEST_ASSERT_EQUAL_INT(4, counter);
|
||||
clearStack(testStack);
|
||||
}
|
||||
|
||||
void setUp(void) {
|
||||
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
printf("\n----------------------------Stack-Tests----------------------------\n");
|
||||
RUN_TEST(test_firstNodeAddedCorrectly);
|
||||
RUN_TEST(test_topReturnsCorrectValues);
|
||||
RUN_TEST(test_popRemovesCorrectly);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user