- 可能对 c 和 c 线程中的一般打印如何发生的深入解释将解决该问题
- 在任何时候都没有使用循环和 scanf
- 因为我的代码使用检查框架,所以我会假设正在进行一些分叉。
- 很可能它与缓冲区有关,所以我使用 fflush(stdout),如果是因为框架内部的某些东西,我会很感激知道框架的人会解释发生了什么。
- 检查框架页面 check.sourceforge.net
- 如果这可能会有所帮助,请使用 Git 链接https://github.com/batousik/Practical-C2
- 链接到构建/测试日志,在第 438 行打印开始https://travis-ci.org/batousik/Practical-C2/builds/53513707
代码示例
START_TEST(test_START_EMPTY_TREE_TREEBASE_PRINT_FREETREE_TEST) {
printf("_________START_EMPTY_TREE/TREEBASE_PRINT/FREETREE_TEST__________\n");
fflush(stdout);
int *ptr;
for (int i = 0; i < arr_size; i++) {
ptr = malloc(sizeof(int));
memcpy(ptr, (int_arr_ptr + i), sizeof(int));
insert(ptr_tree_base_int_1, ptr);
}
// should print tree
printf("!!!Next lines has to be tree printed out\n");
fflush(stdout);
ck_assert_int_eq(printTree(ptr_tree_base_int_1), true);
printf("_____________________\n");
fflush(stdout);
// No output
ck_assert_int_eq(freeTree(ptr_tree_base_int_1), true);
// should print cannot free empty tree
printf("!!!Next line has to be:\"cannot free empty tree\"\n");
fflush(stdout);
ck_assert_int_eq(freeTree(ptr_tree_base_int_1), true);
// should print cannot print empty tree
printf("!!!Next line has to be:\"cannot print empty tree\"\n");
fflush(stdout);
ck_assert_int_eq(printTree(ptr_tree_base_int_1), false);
printf("_____________________\n");
fflush(stdout);
ptr_tree_base_int_1 = NULL;
free(ptr_tree_base_int_1);
// should print cannot free empty tree base
printf("!!!Next line has to be:\"cannot free empty tree base\"\n");
fflush(stdout);
ck_assert_int_eq(freeTree(ptr_tree_base_int_1), true);
// should print cannot print empty tree base
printf("!!!Next line has to be:\"cannot print empty tree base\"\n");
fflush(stdout);
ck_assert_int_eq(printTree(ptr_tree_base_int_1), false);
printf("_____________________\n");
fflush(stdout);
free(int_arr_ptr);
int_arr_ptr = NULL;
printf("freeing the array...\n");
fflush(stdout);
printf("________END_____________\n");
fflush(stdout);
ck_assert_int_eq(ptr_tree_base_int_1->size, 0);
} END_TEST
打印树函数
bool printTree(TreeBase *tree){
void *previous = NULL;
int cnt_tasks = 0;
if (!tree) {
printf("PRINT: Cannot print empty tree base\n");
fflush(stdout);
return false;
}
/* set current to root of binary tree */
TreeNode *current_node = tree->base;
if (!(tree->base)) {
printf("PRINT: Cannot print empty tree\n");
fflush(stdout);
return false;
}
StackNode *stack = NULL;
while (true) {
if(current_node) {
push(&stack, current_node);
current_node = current_node->left;
} else {
if (stack) {
current_node = pop(&stack);
if (cnt_tasks > 1) {
if (tree->comp(previous, current_node->value) != -1) {
printf("PRINTTREE: Invalid BST\n");
fflush(stdout);
assert(NULL);
}
}
previous = current_node->value;
tree->print(current_node->value);
cnt_tasks++;
current_node = current_node->right;
} else {
return (cnt_tasks == tree->size);
}
}
}
}
树->打印功能
void print_ints(void *p){
printf("%d\n", *(int*)p);
fflush(stdout);
}
样本输出
FREETREE: Cant free empty tree
!!!Next line has to be:"cannot free empty tree"
FREETREE: Cant free empty tree
FREETREE: Cant free empty tree
!!!Next line has to be:"cannot print empty tree"
PRINT: Cannot print empty tree
PRINT: Cannot print empty tree
_____________________
代码很多,重点在后面!!!应该只有一行,但要打印两行
printf("PRINT: Cannot print empty tree\n");
fflush(stdout);
return false;
^^ 此行打印两次
评论:什么是“宏”?这是来自 API
#define ck_assert_int_eq(X, Y) _ck_assert_int(X, ==, Y)