C++/错误测试

标签 c++

<分区>

我在面试中被问到这个问题。我完全不知道如何解决这个问题......

/* You are writing a unit test to confirm the correctness of a function which
takes 3 float values as arguments. You decide to stress test it by testing
1000000 'random' inputs.
You find that the function will fail, but very rarely, so you include code
to print out all failure cases, hoping to grab a simple repro case which you
can debug into.
Note: All code here is run in a single-threaded environment. */

//...
// Some code sets a,b,c
//...
if ( testPasses(a,b,c) )
{
    printf("Pass\n");
}
else // someFunc fails, print the values so we can reproduce
{
    printf("Fail: %f %f %f\n", a, b, c);
}

你能帮帮我吗?谢谢!

更新:很抱歉,我不小心删除了问题!!

在这里

// where testPasses has the following signature and executes deterministically 

// with no side effects:

    bool testPasses(float f1, float f2, float f3);

void testRepro()
{
    float a = // fill in from value printed by above code
    float b = // fill in from value printed by above code
    float c = // fill in from value printed by above code
    bool result = testPasses(a,b,c);
};

// Surprisingly, when you type in the 3 values printed out in testRepro()
// the test does not fail!
// Modify the original code and test bed to reproduce the problem reliably. 

最佳答案

问题是 printf 在 float 上使用的精度。他们想知道您是否能看到...

关于C++/错误测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6170284/

相关文章:

c++ - 确保 4 个随机值完全不相等的更好方法?

c++ - C++ 实现代码中的字符串不应出现在输出二进制文件中。怎么修

c++ - Mac : Terminal Compiler Error on Server

c++ - 使用 Visual Studio 2013 的 OpenMP 性能下降

c++ - 当结构具有默认构造函数时,为什么我的 C++ 程序在 Release模式下崩溃?

c++ - 为什么该程序无法按排序顺序合并2个链表?

c++ - 抽象类类型指针编译成功?

c++ - 在 linux 上安装 nsis 失败 exitcode1

C++ 函数指针语法

安卓 NDK 和 C++ STL