generated from freudenreichan/info2Praktikum-DobleSpiel
numbers + Test
This commit is contained in:
parent
50079dbfca
commit
e7358b2c6f
@ -32,6 +32,8 @@ stackTests: stack.o stackTests.c $(unityfolder)/unity.c
|
||||
bintreeTests: stack.o bintree.o bintreeTests.c $(unityfolder)/unity.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runBintreeTests bintreeTests.c stack.o bintree.o $(unityfolder)/unity.c
|
||||
|
||||
numbersTests: stack.o bintree.o numbers.o numbersTests.c $(unityfolder)/unity.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runNumbersTests numbersTests.c stack.o bintree.o numbers.o $(unityfolder)/unity.c
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
|
||||
@ -10,13 +10,49 @@
|
||||
* Sicherstellen, dass beim Befüllen keine Duplikate entstehen.
|
||||
* Duplizieren eines zufälligen Eintrags im Array.
|
||||
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
||||
|
||||
int compareInt(const void *arg1, const void *arg2) {
|
||||
int a = *(const int *)arg1;
|
||||
int b = *(const int *)arg2;
|
||||
return a - b;
|
||||
}
|
||||
// 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 *numbers = malloc(sizeof(int)*len);
|
||||
unsigned int *arrayNoDup = malloc(sizeof(int)*len);
|
||||
unsigned int dupNumber;
|
||||
int dupLocation;
|
||||
if(!numbers){
|
||||
return NULL;
|
||||
}
|
||||
TreeNode *tree = NULL;
|
||||
int isDuplicate = 0;
|
||||
int gespeichert = 0;
|
||||
|
||||
srand(time(NULL));
|
||||
while(gespeichert <len-1){
|
||||
arrayNoDup[gespeichert] = rand()%(2*len);
|
||||
tree = addToTree(tree,&arrayNoDup[gespeichert],sizeof(int),compareInt,&isDuplicate);
|
||||
if(!isDuplicate){
|
||||
gespeichert++;
|
||||
}
|
||||
}
|
||||
dupNumber = arrayNoDup[rand()%(len-1)];
|
||||
dupLocation = rand()%len;
|
||||
for(int i=0;i<len;i++){
|
||||
if(i<dupLocation){
|
||||
numbers[i]=arrayNoDup[i];
|
||||
}else if(i==dupLocation){
|
||||
numbers[i] = dupNumber;
|
||||
}else{
|
||||
numbers[i]=arrayNoDup[i-1];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return numbers;
|
||||
}
|
||||
|
||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
||||
|
||||
BIN
Start_Windows/numbers.o
Normal file
BIN
Start_Windows/numbers.o
Normal file
Binary file not shown.
77
Start_Windows/numbersTests.c
Normal file
77
Start_Windows/numbersTests.c
Normal file
@ -0,0 +1,77 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include "unity.h"
|
||||
#include "numbers.h"
|
||||
/*
|
||||
int compareInt(const void *arg1, const void *arg2) {
|
||||
int a = *(const int *)arg1;
|
||||
int b = *(const int *)arg2;
|
||||
return a - b;
|
||||
}
|
||||
int compareString(const void *arg1, const void *arg2) {
|
||||
const char *str1 = (const char *)arg1;
|
||||
const char *str2 = (const char *)arg2;
|
||||
return strcmp(str1, str2);
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
|
||||
static void testCreateNumbersNotNull() {
|
||||
unsigned int len = 10;
|
||||
unsigned int *nums = createNumbers(len);
|
||||
TEST_ASSERT_NOT_NULL(nums);
|
||||
free(nums);
|
||||
}
|
||||
|
||||
static void testCreateNumbersLength() {
|
||||
unsigned int len = 20;
|
||||
unsigned int *nums = createNumbers(len);
|
||||
TEST_ASSERT_NOT_NULL(nums);
|
||||
for (unsigned int i = 0; i < len; i++) {
|
||||
TEST_ASSERT_INT_WITHIN(len * 2, 0, nums[i]); // Überprüft, ob werte zwischen 0 und len*2 liegen
|
||||
}
|
||||
free(nums);
|
||||
}
|
||||
|
||||
static void testCreateNumbersContainsDuplicate() {
|
||||
unsigned int len = 30;
|
||||
unsigned int *nums = createNumbers(len);
|
||||
|
||||
int duplicate = 0;
|
||||
for (unsigned int i = 0; i < len; i++) {
|
||||
for (unsigned int ii = i + 1; ii < len; ii++) {
|
||||
if (nums[i] == nums[ii]) {
|
||||
duplicate ++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
TEST_ASSERT_EQUAL_INT(1,duplicate);
|
||||
free(nums);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void setUp(void){
|
||||
|
||||
}
|
||||
void tearDown(void){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
UNITY_BEGIN();
|
||||
printf("\n============================\nNumber tests\n============================\n");
|
||||
|
||||
RUN_TEST(testCreateNumbersNotNull);
|
||||
RUN_TEST(testCreateNumbersLength);
|
||||
RUN_TEST(testCreateNumbersContainsDuplicate);
|
||||
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
BIN
Start_Windows/runNumbersTests.exe
Normal file
BIN
Start_Windows/runNumbersTests.exe
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user