gpt4 book ai didi

c - 在 C 中停止 printf 打印两次运行检查框架?

转载 作者:太空宇宙 更新时间:2023-11-04 03:40:42 24 4
gpt4 key购买 nike

  1. 可能对 c 和 c 线程中的打印一般如何发生进行深入解释可以解决问题
  2. 在任何时候都没有使用循环和 scanf
  3. 因为我的代码使用了检查框架,所以我假设正在进行一些 fork 。
  4. 这很可能与缓冲区有关,所以我使用 fflush(stdout),以防万一是因为框架内部的原因,我希望知道框架的人能解释发生了什么。
  5. 检查框架页面check.sourceforge.net
  6. Git 链接,如果这可能有所帮助 https://github.com/batousik/Practical-C2
  7. 构建/测试日志链接,第 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)

最佳答案

ck_assert_int_eq macroCheck unit testing framework 的 0.9.6 版中引入被定义为:

#define _ck_assert_int(X, O, Y) ck_assert_msg((X) O (Y), \
"Assertion '"#X#O#Y"' failed: "#X"==%d, "#Y"==%d", X, Y)
#define ck_assert_int_eq(X, Y) _ck_assert_int(X, ==, Y)

可以看出,参数在宏定义中出现了两次:一次用于实际的相等性检查((X) O (Y) 部分),另一次作为最后一个参数ck_assert_msg(..., X, Y) 用于在失败时记录消息。查看代码的特定部分:

// 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);

C 预处理器会相应地将宏扩展为等同于以下代码的代码:

// should print cannot free empty tree
printf("!!!Next line has to be:\"cannot free empty tree\"\n");
fflush(stdout);
ck_assert_msg((printTree(ptr_tree_base_int_1)) == (false),
"Assertion '"#X#O#Y"' failed: "#X"==%d, "#Y"==%d",
printTree(ptr_tree_base_int_1), false);

printTree 函数因此被调用两次并产生所有相关的副作用,包括打印行 "PRINT: Cannot print empty tree" 两次。保护自己免受此类错误的一种方法是确保宏的参数没有可能的副作用。在您的情况下,这可以通过将调用移到宏外的 printTree 来完成:

bool printTreeResult;
printTreeResult = printTree(ptr_tree_base_int_1);
ck_assert_int_eq(printTreeResult, false);

请注意,由于这种宏通常容易出错(正如您刚才所经历的),ck_assert_int_eq 的定义在 0.9.9 版中得到了改进:

#define _ck_assert_int(X, OP, Y) do { \
int _ck_x = (X); \
int _ck_y = (Y); \
ck_assert_msg(_ck_x OP _ck_y, \
"Assertion '"#X#OP#Y"' failed: "#X"==%d, "#Y"==%d", _ck_x, _ck_y); \
} while (0)
#define ck_assert_int_eq(X, Y) _ck_assert_int(X, ==, Y)

相应地,解决重复输出行问题的另一种方法是将您安装的 Check 单元测试框架升级到更新的版本(即至少 0.9.9),如果可以的话。

关于c - 在 C 中停止 printf 打印两次运行检查框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28922965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com