DobleSpiel/test_bintree.c

52 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity/unity.h"
#include "bintree.h"
#include "stack.h"
int cmpInt(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
void test_binary_tree_functions(void)
{
TreeNode *root = NULL;
int dup;
int values[] = {5, 3, 8, 2, 6};
for (int i = 0; i < 5; i++) {
root = addToTree(root, &values[i], sizeof(int), cmpInt, &dup);
TEST_ASSERT_EQUAL_INT(0, dup);
}
int testDup = 5;
root = addToTree(root, &testDup, sizeof(int), cmpInt, &dup);
TEST_ASSERT_EQUAL_INT(1, dup);
TEST_ASSERT_EQUAL_UINT(5, treeSize(root));
int inorderExpected[] = {2, 3, 5, 6, 8};
int idx = 0;
void *data;
for (data = nextTreeData(root); data != NULL; data = nextTreeData(NULL)) {
TEST_ASSERT_EQUAL_INT(inorderExpected[idx], *(int *)data);
idx++;
}
TEST_ASSERT_EQUAL_INT(5, idx);
clearTree(root);
root = NULL;
TEST_ASSERT_NULL(root);
}
void setUp(void) {}
void tearDown(void) {}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_binary_tree_functions);
return UNITY_END();
}