测试 CRC 逻辑的最佳方法?
如何验证两个 CRC 实现会生成相同的校验和?
我正在寻找一种针对 CRC 的详尽实施评估方法。
How can I verify two CRC implementations will generate the same checksums?
I'm looking for an exhaustive implementation evaluating methodology specific to CRC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将问题分为边缘情况和随机样本。
边缘情况。 CRC 输入有两个变量:字节数和每个字节的值。因此,创建 0、1 和 MAX_BYTES 数组,值范围为 0 到 MAX_BYTE_VALUE。边缘案例套件将是您最有可能希望保留在 JUnit 套件中的东西。
随机样本。使用上面的范围,在循环中对随机生成的字节数组运行 CRC。循环运行的时间越长,消耗的输入就越多。如果您的计算能力较低,请考虑将测试部署到 EC2。
You can separate the problem into edge cases and random samples.
Edge cases. There are two variables to the CRC input, number of bytes, and value of each byte. So create arrays of 0, 1, and MAX_BYTES, with values ranging from 0 to MAX_BYTE_VALUE. The edge case suite will be something you'll most likely want to keep within a JUnit suite.
Random samples. Using the ranges above, run CRC on randomly generated arrays of bytes in a loop. The longer you let the loop run, the more you exhaust the inputs. If you are low on computing power, consider deploying the test to EC2.
CRC 的一个很好的特性是,对于给定的一组参数(多项式、反射、初始状态等),当您在原始数据集 + 原始 CRC 上重新计算 CRC 时,您将获得一个常量值。这些常量记录了常见的 CRC,但您可以使用两个不同的随机数据集盲目地生成它们并检查它们是否相同:
您可以在实现中使用相同的方法来获得对其正确性的温暖模糊感觉。如果您的实现适用于任意多项式,您可以使用此方法让单元测试彻底检查每个可能的多项式,而无需知道常数是什么。
这种技术很强大,但明智的做法是添加一个独立的测试,根据病理情况下的已知输入来验证结果,在这种情况下,您的 CRC 实现都会产生通过恒定等价性检查碰巧得到的不良结果。
One nice property of CRCs is that for a given set of parameters (polynomial, reflection, initial state, etc.) you will get a constant value when you recompute the CRC over the original dataset + the original CRC. These constants are documented for common CRCs but you can just blindly generate them using two different random data sets and check that they are the same:
You can use the same method within an implementation to get a warm fuzzy feeling about its correctness. If your implementation works with arbitrary polynomials, you can have the unittest exhaustively check every possible polynomial using this method without needing to know what the constants are.
This technique is powerful but it would also be wise to add an independent test that verifies the result based on known input for the pathological case where your CRC implementations both produce bad results that happen to get by the constant equivalence check.
使用相同的输入创建多个单元测试,将两个实现的输出相互比较。
Create several unit tests with the same input that will compare the output of both implementations against each other.
首先,如果它是标准 CRC 实现,您应该能够在网络上的某个地方找到已知值。
其次,您可以生成一定数量的有效负载并对有效负载运行每个 CRC 并检查 CRC 值是否匹配。
First, if it is a standard CRC implementation, you should be able to find known values somewhere on the net.
Second, you could generate some number of payloads and run the each CRC on the payloads and check that the CRC values match.
通过为每个采用相同输入的单元编写单元测试,并根据预期输出进行验证。
By writing a unit test for each which takes the same input and verify against the expected output.