numbers done/ test missing
This commit is contained in:
parent
8b0fa4601a
commit
39965a95c4
6
makefile
6
makefile
@ -38,6 +38,12 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
unitTests: stack.o test_stack.c $(unityfolder)/unity.c
|
unitTests: stack.o test_stack.c $(unityfolder)/unity.c
|
||||||
$(CC) $(FLAGS) -I$(unityfolder) -o runStackTest test_stack.c stack.o $(unityfolder)/unity.c
|
$(CC) $(FLAGS) -I$(unityfolder) -o runStackTest test_stack.c stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# numbers.c Tests
|
||||||
|
# --------------------------
|
||||||
|
numbersTests: numbers.o test_numbers.c $(unityfolder)/unity.c
|
||||||
|
$(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
118
numbers.c
118
numbers.c
@ -14,13 +14,131 @@
|
|||||||
// 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 checkArray(unsigned int *array, unsigned int len, unsigned int number)
|
||||||
|
{
|
||||||
|
int free = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (array[i] == number)
|
||||||
|
{
|
||||||
|
free = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return free;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
srand(time(NULL));
|
||||||
|
unsigned int array[len];
|
||||||
|
unsigned int randomNr;
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
randomNr = rand() % 2 * len + 1;
|
||||||
|
} while (!checkArray(array, i, randomNr));
|
||||||
|
|
||||||
|
array[i] = randomNr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
void merge(unsigned int arr[], unsigned int left, unsigned int mid, unsigned int right)
|
||||||
|
{
|
||||||
|
unsigned int i, j, k;
|
||||||
|
unsigned int n1 = mid - left + 1;
|
||||||
|
unsigned int n2 = right - mid;
|
||||||
|
|
||||||
|
// Create temporary arrays
|
||||||
|
unsigned int leftArr[n1], rightArr[n2];
|
||||||
|
|
||||||
|
// Copy data to temporary arrays
|
||||||
|
for (i = 0; i < n1; i++)
|
||||||
|
leftArr[i] = arr[left + i];
|
||||||
|
for (j = 0; j < n2; j++)
|
||||||
|
rightArr[j] = arr[mid + 1 + j];
|
||||||
|
|
||||||
|
// Merge the temporary arrays back into arr[left..right]
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
k = left;
|
||||||
|
while (i < n1 && j < n2)
|
||||||
|
{
|
||||||
|
if (leftArr[i] <= rightArr[j])
|
||||||
|
{
|
||||||
|
arr[k] = leftArr[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
arr[k] = rightArr[j];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the remaining elements of leftArr[], if any
|
||||||
|
while (i < n1)
|
||||||
|
{
|
||||||
|
arr[k] = leftArr[i];
|
||||||
|
i++;
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the remaining elements of rightArr[], if any
|
||||||
|
while (j < n2)
|
||||||
|
{
|
||||||
|
arr[k] = rightArr[j];
|
||||||
|
j++;
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void mergeSort(unsigned int arr[], unsigned int left, unsigned int right)
|
||||||
|
{
|
||||||
|
if (left < right)
|
||||||
|
{
|
||||||
|
|
||||||
|
// Calculate the midpoint
|
||||||
|
unsigned int mid = left + (right - left) / 2;
|
||||||
|
|
||||||
|
// Sort first and second halves
|
||||||
|
mergeSort(arr, left, mid);
|
||||||
|
mergeSort(arr, mid + 1, right);
|
||||||
|
|
||||||
|
// Merge the sorted halves
|
||||||
|
merge(arr, left, mid, right);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 temp[len];
|
||||||
|
unsigned int duplicate = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
temp[i] = numbers[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sorting arr using mergesort
|
||||||
|
mergeSort(temp, 0, len - 1);
|
||||||
|
|
||||||
|
for (int i = 0; i < len - 1; i++)
|
||||||
|
{
|
||||||
|
duplicate = temp[i];
|
||||||
|
if (duplicate == temp[i + 1])
|
||||||
|
{
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return duplicate;
|
||||||
}
|
}
|
||||||
BIN
runNumbersTest.exe
Normal file
BIN
runNumbersTest.exe
Normal file
Binary file not shown.
33
test_numbers.c
Normal file
33
test_numbers.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "numbers.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
void duplicateTest()
|
||||||
|
{
|
||||||
|
unsigned int array[6] = {1, 4, 5, 2, 3, 1};
|
||||||
|
unsigned int len = 6;
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, getDuplicate(array, len));
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp(void)
|
||||||
|
{
|
||||||
|
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void)
|
||||||
|
{
|
||||||
|
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("============================\nNumbers tests\n============================\n");
|
||||||
|
|
||||||
|
RUN_TEST(duplicateTest);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user