2 Commits
Author SHA1 Message Date
D2A62006 38e5ff6a0a first running version 2025-12-08 20:00:38 +01:00
D2A62006 3cc67f0343 Add tests for nextTreeData functionality 2025-12-08 17:49:18 +01:00
5 changed files with 216 additions and 3 deletions

No files matched your search

+1
View File
@@ -1,5 +1,6 @@
doble_initial
stackTests
bintreeTests
doble
*.o
*.exe
+138 -3
View File
@@ -25,6 +25,7 @@ void tearDown(void){
//Use if needed
}
//addToTree()
void test_addToTree_singleElement(void){
int value = 42;
TreeNode *tree = NULL;
@@ -53,7 +54,7 @@ void test_addToTree_multipleElements(void){
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->right->right->right->data);
}
void test_addToTree_multipleElementsOptimised(void){
void test_addToTree_multipleElements_optimized(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 40, 60, 80};
@@ -104,6 +105,8 @@ void test_addToTree_withoutDuplicatesAccept(void){
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->left->data);
TEST_ASSERT_EQUAL_INT(5, treeSize(tree));
}
//treeSize()
void test_treeSize_emptyTree(void){
TEST_ASSERT_EQUAL_UINT(0, treeSize(NULL));
@@ -132,10 +135,137 @@ void test_treeSize_multipleNodes(void){
}
//nextTreeData()
void test_nextTreeData_emptyTree(void){
TEST_ASSERT_NULL(nextTreeData(NULL));
}
void test_nextTreeData_singleElement(void){
TreeNode *tree = NULL;
int value = 42;
tree = addToTree(tree, &value, sizeof(int), compareInts, NULL);
int *result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(value, *result);
TEST_ASSERT_NULL(nextTreeData(NULL));
clearTree(tree);
}
void test_nextTreeData_multipleElements(void){
TreeNode *tree = NULL;
int values[] = {20, 30, 40, 50, 60, 70, 80};
int expected[] = {20, 30, 40, 50, 60, 70, 80};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
int *result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[0], *result);
for(int i = 1; i < 7; i++){
result = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[i], *result);
}
TEST_ASSERT_NULL(nextTreeData(NULL));
clearTree(tree);
}
void test_nextTreeData_multipleElements_optimized(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 40, 60, 80};
int expected[] = {20, 30, 40, 50, 60, 70, 80};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
int *result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[0], *result);
for(int i = 1; i < 7; i++){
result = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[i], *result);
}
TEST_ASSERT_NULL(nextTreeData(NULL));
clearTree(tree);
}
void test_nextTreeData_withDuplicates(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 20, 60, 60};
int expected[] = {20, 20, 30, 50, 60, 60, 70};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
int *result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[0], *result);
for(int i = 1; i < 7; i++){
result = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[i], *result);
}
TEST_ASSERT_NULL(nextTreeData(NULL));
}
void test_nextTreeData_restart(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 40, 60, 80};
int expected[] = {20, 30, 40, 50, 60, 70, 80};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
int *result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[0], *result);
for(int i = 1; i < 4; i++){
result = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[i], *result);
}
result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[0], *result);
for(int i = 1; i < 7; i++){
result = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[i], *result);
}
}
//clearTree()
void test_clearTree_emptyTree(void){
clearTree(NULL);
TEST_ASSERT_TRUE(1);
@@ -164,6 +294,7 @@ void test_clearTree_multipleElements(void){
TEST_ASSERT_TRUE(1);
}
int main(){
UNITY_BEGIN();
@@ -172,7 +303,7 @@ int main(){
//addToTree()
RUN_TEST(test_addToTree_singleElement);
RUN_TEST(test_addToTree_multipleElements);
RUN_TEST(test_addToTree_multipleElementsOptimised);
RUN_TEST(test_addToTree_multipleElements_optimized);
RUN_TEST(test_addToTree_withDuplicatesAccept);
RUN_TEST(test_addToTree_withoutDuplicatesAccept);
@@ -182,8 +313,12 @@ int main(){
RUN_TEST(test_treeSize_multipleNodes);
//nextTreeData()
RUN_TEST(test_nextTreeData_emptyTree);
RUN_TEST(test_nextTreeData_singleElement);
RUN_TEST(test_nextTreeData_multipleElements);
RUN_TEST(test_nextTreeData_multipleElements_optimized);
RUN_TEST(test_nextTreeData_withDuplicates);
RUN_TEST(test_nextTreeData_restart);
//clearTree()
RUN_TEST(test_clearTree_emptyTree);
+1
View File
@@ -1,2 +1,3 @@
test;24999
test;9999
player1;3999
+3
View File
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "numbers.h"
#include "timer.h"
#include "highscore.h"
@@ -37,6 +38,8 @@ void showNumbers(const unsigned int *numbers, unsigned int len)
// Main game loop: generate numbers, ask user for duplicate, measure time, update highscores.
int main(int argc, char *argv[])
{
srand(time(NULL));
int exitCode = EXIT_FAILURE;
if(argc != 2)
+73
View File
@@ -14,13 +14,86 @@
// 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.
int compareUInts(const void *arg1, const void *arg2){
unsigned int val1 = *(unsigned int *)arg1;
unsigned int val2 = *(unsigned int *)arg2;
if(val1 < val2) return -1;
if(val1 > val2) return 1;
return 0;
}
unsigned int* createNumbers(unsigned int len)
{
if(len == 0){
return NULL;
}
unsigned int* numbers = (unsigned int*)malloc(len * sizeof(unsigned int));
if(numbers == NULL){
return NULL;
}
TreeNode *tree = NULL;
unsigned int i = 0;
while(i < len){
unsigned int tmp = (rand() % (2*len)) + 1;
int isDuplicate = 0;
tree = addToTree(tree, &tmp, sizeof(unsigned int), compareUInts, &isDuplicate);
if(!isDuplicate){
numbers[i] = tmp;
i++;
}
}
clearTree(tree);
unsigned int rIdx = rand() % len;
unsigned int duplicate = numbers[rIdx];
unsigned int tIdx = rand() % len;
while(tIdx == rIdx){
tIdx = rand() % len;
}
numbers[tIdx] = duplicate;
return numbers;
}
// 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)
{
if(numbers == NULL || len < 2){
return 0;
}
unsigned int* sorted = (unsigned int*)malloc(len * sizeof(unsigned int));
if(sorted == NULL){
return 0;
}
TreeNode *tree = NULL;
for(unsigned int i = 0; i < len; i++){
tree = addToTree(tree, &numbers[i], sizeof(unsigned int), compareUInts, NULL);
}
sorted[0] = *(unsigned int *)nextTreeData(tree);
for(unsigned int j = 1; j < len; j++){
sorted[j] = *(unsigned int*)nextTreeData(NULL);
}
unsigned int duplicate = 0;
for(unsigned int k = 0; k < len - 1; k++){
if(sorted[k] == sorted[k+1]){
duplicate = sorted[k];
break;
}
}
free(sorted);
return duplicate;
}