generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3cc67f0343 | ||
|
|
ea36ddeec2 | ||
|
|
492a101160 |
No files matched your search
@@ -77,12 +77,14 @@ void *nextTreeData(TreeNode *root)
|
|||||||
current = root;
|
current = root;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (current != NULL)
|
while (current != NULL){
|
||||||
{
|
|
||||||
stack = push(stack, current);
|
stack = push(stack, current);
|
||||||
current = current->left;
|
current = current->left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(stack == NULL){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
TreeNode *node = (TreeNode *)top(stack);
|
TreeNode *node = (TreeNode *)top(stack);
|
||||||
stack = pop(stack);
|
stack = pop(stack);
|
||||||
|
|
||||||
|
|||||||
+285
-3
@@ -8,8 +8,8 @@ int compareInts(const void *arg1, const void *arg2){
|
|||||||
int val1 = *(int *)arg1;
|
int val1 = *(int *)arg1;
|
||||||
int val2 = *(int *)arg2;
|
int val2 = *(int *)arg2;
|
||||||
|
|
||||||
if(val1 < val2) return 1;
|
if(val1 < val2) return -1;
|
||||||
if(val1 > val2) return -1;
|
if(val1 > val2) return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,6 +25,7 @@ void tearDown(void){
|
|||||||
//Use if needed
|
//Use if needed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//addToTree()
|
||||||
void test_addToTree_singleElement(void){
|
void test_addToTree_singleElement(void){
|
||||||
int value = 42;
|
int value = 42;
|
||||||
TreeNode *tree = NULL;
|
TreeNode *tree = NULL;
|
||||||
@@ -36,12 +37,293 @@ void test_addToTree_singleElement(void){
|
|||||||
TEST_ASSERT_NULL(tree->left);
|
TEST_ASSERT_NULL(tree->left);
|
||||||
TEST_ASSERT_NULL(tree->right);
|
TEST_ASSERT_NULL(tree->right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_addToTree_multipleElements(void){
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
int values[] = {20, 30, 40, 50, 60, 70, 80};
|
||||||
|
|
||||||
|
for(int i = 0; i < 7; i++){
|
||||||
|
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(tree);
|
||||||
|
TEST_ASSERT_EQUAL_INT(20, *(int *)tree->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(30, *(int *)tree->right->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(40, *(int *)tree->right->right->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(50, *(int *)tree->right->right->right->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->right->right->right->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_addToTree_multipleElementsOptimised(void){
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
int values[] = {50, 30, 70, 20, 40, 60, 80};
|
||||||
|
|
||||||
|
for(int i = 0; i < 7; i++){
|
||||||
|
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(tree);
|
||||||
|
TEST_ASSERT_EQUAL_INT(50, *(int *)tree->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(30, *(int *)tree->left->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(70, *(int *)tree->right->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(20, *(int *)tree->left->left->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(40, *(int *)tree->left->right->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_addToTree_withDuplicatesAccept(void){
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
int values[] = {50, 30, 70, 20, 20, 60, 60};
|
||||||
|
|
||||||
|
for(int i = 0; i < 7; i++){
|
||||||
|
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(tree);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(20, *(int *)tree->left->left->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(20, *(int *)tree->left->left->right->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->left->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->left->right->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_addToTree_withoutDuplicatesAccept(void){
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
int values[] = {50, 30, 70, 20, 20, 60, 60};
|
||||||
|
int isDuplicate = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < 7; i++){
|
||||||
|
tree = addToTree(tree, &values[i], sizeof(int), compareInts, &isDuplicate);
|
||||||
|
if(i == 4 || i == 6){
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, isDuplicate);
|
||||||
|
} else{
|
||||||
|
TEST_ASSERT_EQUAL_INT(0, isDuplicate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(tree);
|
||||||
|
TEST_ASSERT_EQUAL_INT(20, *(int *)tree->left->left->data);
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_treeSize_singleNode(void){
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
int value = 42;
|
||||||
|
tree = addToTree(tree, &value, sizeof(int), compareInts, NULL);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(tree);
|
||||||
|
TEST_ASSERT_EQUAL_UINT(1, treeSize(tree));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_treeSize_multipleNodes(void){
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
int values[] = {50, 30, 70, 20, 40, 60, 80};
|
||||||
|
|
||||||
|
for(int i = 0; i < 7; i++){
|
||||||
|
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(tree);
|
||||||
|
TEST_ASSERT_EQUAL_UINT(7, treeSize(tree));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_clearTree_singleElement(void){
|
||||||
|
int value = 42;
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
|
||||||
|
tree = addToTree(tree, &value, sizeof(int), compareInts, NULL);
|
||||||
|
|
||||||
|
clearTree(tree);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_clearTree_multipleElements(void){
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
int values[] = {50, 30, 70, 20, 40, 60, 80};
|
||||||
|
|
||||||
|
for(int i = 0; i < 7; i++){
|
||||||
|
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearTree(tree);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(1);
|
||||||
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
|
|
||||||
printf("\n============================\nBintree tests\n============================\n");
|
printf("\n============================\nBintree tests\n============================\n");
|
||||||
|
|
||||||
|
//addToTree()
|
||||||
RUN_TEST(test_addToTree_singleElement);
|
RUN_TEST(test_addToTree_singleElement);
|
||||||
|
RUN_TEST(test_addToTree_multipleElements);
|
||||||
|
RUN_TEST(test_addToTree_multipleElementsOptimised);
|
||||||
|
RUN_TEST(test_addToTree_withDuplicatesAccept);
|
||||||
|
RUN_TEST(test_addToTree_withoutDuplicatesAccept);
|
||||||
|
|
||||||
|
//treeSize()
|
||||||
|
RUN_TEST(test_treeSize_emptyTree);
|
||||||
|
RUN_TEST(test_treeSize_singleNode);
|
||||||
|
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);
|
||||||
|
RUN_TEST(test_clearTree_singleElement);
|
||||||
|
RUN_TEST(test_clearTree_multipleElements);
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user