C++ C++/错误测试

C++ C++/错误测试,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

我在一次采访中被问到这个问题。我完全不知道如何解决这个问题

/* 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
对浮点数使用的精度。他们想知道您是否可以看到…

打印出表示浮点值的字节,而不是打印值。然后可以将精确值作为十六进制值插入

char value[4] = { 0x12, 0x34, 0x56, 0x78 };
float *a = (float*)value;

// test using *a

那个代码不起作用吗?我完全不知道实际的问题是什么。问题是如何编写注入随机数的循环?或者能够打印数字?在
for
循环中调用代码段1000000次,同时在每次迭代中生成随机数字,是否有问题?学究式的,这是不允许的。使用
字符值[4]
来避免未定义的行为。@Ben我之所以更改它,是因为它不是我希望以任何方式投入生产的真正代码,但我很好奇,为什么使用32位整数与将其指定为字符[4]有什么不同(除了在字节排序上提供更明显的控制)?我真的很好奇,因为它特别让我想起了32位int。这里解释道:@benahha!非常感谢你。信息量很大。哦,我明白了,该死,这是个很简单的问题。我完全可以得到它!