目前我有一个程序将二进制数据加载到字符串流中,然后将数据压缩到fstream,如下所示:
stringstream ss(stringstream::binary | stringstream::in | stringstream::out);
ss.write(data, 512); // Loads data into stream
// Uses a memory block to pass the data between the streams
char* memBlock = new char[512];
ss.read(memBlock, 512);
ofstream fout("someFile.bin", ios::binary);
fout.write(memBlock, 512); // Writes the data to a file
fout.close();
delete[] memBlock;
Run Code Online (Sandbox Code Playgroud)
我的问题是:有没有更好的方法在流之间传递二进制数据?
我有一个算法可以在摄氏度和华氏度之间转换值。为了测试它是否适用于各种值,我使用 NUnit 的测试用例,如下所示:
[TestCase( 0, Result = -17.778 )]
[TestCase( 50, Result = 10 )]
public double FahrenheitToCelsius(double val) {
return (val - 32) / 1.8;
}
Run Code Online (Sandbox Code Playgroud)
问题是第一个测试用例失败,因为它测试完全匹配。
我发现的一种解决方案是执行以下操作:
[TestCase( 0, -17.778 )]
[TestCase( 50, 10 )]
public void FahrenheitToCelsius2(double val, double expected) {
double result = (val - 32) / 1.8;
Assert.AreEqual( expected, result, 0.005 );
}
Run Code Online (Sandbox Code Playgroud)
但我对此不太满意。我的问题是:
可以在测试用例中定义结果的容差吗?
更新:
为了澄清,我正在寻找类似的内容:
[TestCase( 0, Result = 1.1, Tolerance = 0.05 )]
Run Code Online (Sandbox Code Playgroud)