generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
3 Commits
main
...
Sortierung
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d9cd9dd84 | |||
| f5fbcee1ea | |||
| d30c0b29c5 |
@ -1 +1,2 @@
|
|||||||
|
player_name;18787
|
||||||
player1;3999
|
player1;3999
|
||||||
|
|||||||
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
|
||||||
|
|||||||
83
numbers.c
83
numbers.c
@ -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 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){
|
||||||
{
|
|
||||||
|
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.
|
// 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)
|
||||||
{
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
70
numbersTests.c
Normal file
70
numbersTests.c
Normal file
@ -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();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user