generated from freudenreichan/info2Praktikum-DobleSpiel
Finished Working on numbers and made some unittests + addition to makefile. Task Sortierung should be finished with that
This commit is contained in:
parent
a1e43ebd3b
commit
79c8928225
2
makefile
2
makefile
@ -37,6 +37,8 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests:
|
unitTests:
|
||||||
echo "needs to be implemented"
|
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
|
# Clean
|
||||||
|
|||||||
32
numbers.c
32
numbers.c
@ -14,8 +14,8 @@
|
|||||||
// 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.
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len){
|
||||||
{
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
unsigned int *numbers = malloc(len * sizeof(unsigned int)); //reserving memory for array
|
unsigned int *numbers = malloc(len * sizeof(unsigned int)); //reserving memory for array
|
||||||
if(!numbers){
|
if(!numbers){
|
||||||
@ -64,7 +64,9 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
|||||||
|
|
||||||
unsigned int duplicate = 0;
|
unsigned int duplicate = 0;
|
||||||
unsigned int temp = 0;
|
unsigned int temp = 0;
|
||||||
|
unsigned int duplicateCount = 0;
|
||||||
|
|
||||||
|
//Sorting sortedNumbers
|
||||||
for (int i = 0; i < len; i++){
|
for (int i = 0; i < len; i++){
|
||||||
for(int j = 0; j< len - 1; j++){
|
for(int j = 0; j< len - 1; j++){
|
||||||
if(sortedNumbers[j]> sortedNumbers[j+1]){ //Switching Elements
|
if(sortedNumbers[j]> sortedNumbers[j+1]){ //Switching Elements
|
||||||
@ -72,12 +74,28 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
|||||||
sortedNumbers[j] = sortedNumbers[j+1];
|
sortedNumbers[j] = sortedNumbers[j+1];
|
||||||
sortedNumbers[j+1] = temp;
|
sortedNumbers[j+1] = temp;
|
||||||
}
|
}
|
||||||
if(sortedNumbers[j] == sortedNumbers[j+1]){
|
|
||||||
duplicate = sortedNumbers[j];
|
|
||||||
free(sortedNumbers);
|
|
||||||
return duplicate;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
//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;
|
||||||
|
|
||||||
}
|
}
|
||||||
55
numbersTests.c
Normal file
55
numbersTests.c
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#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 setUp(void){}
|
||||||
|
void tearDown(void){}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("\n============================\nNumbers tests\n============================\n");
|
||||||
|
|
||||||
|
RUN_TEST(test_ArrayIsCreatedAndNotNull);
|
||||||
|
RUN_TEST(test_getDouplicateReturnsDouplicateNumber);
|
||||||
|
RUN_TEST(test_getDouplicateReturnsZeroWhenThereIsNoDuplicate);
|
||||||
|
RUN_TEST(test_getDouplicateReturnsZeroWhenThereAreMultipleDuplicates);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user