如何使用Google测试来测试一些代码?

发布于 2024-12-17 08:45:33 字数 420 浏览 0 评论 0原文

基本上我正在尝试在谷歌测试中启动一些单元测试,但不知道如何进行。我已经得到了一些代码来尝试和测试,但我不知道如何去做。这是我需要测试的一些代码?我应该从哪里开始?预先感谢您的任何帮助。

void CCRC32::FullCRC(const unsigned char *sData, unsigned long ulDataLength, unsigned long *ulOutCRC)
{
    *(unsigned long *)ulOutCRC = 0xffffffff; //Initilaize the CRC.
    this->PartialCRC(ulOutCRC, sData, ulDataLength);
    *(unsigned long *)ulOutCRC ^= 0xffffffff; //Finalize the CRC.
}

Basically I'm trying to start some unit tests in google test but not sure how to go about it. I have been given some code to try and test but I have no idea how to go about doing this. This is some of the code I need to test? Where should I start? Thanks in advance for any help.

void CCRC32::FullCRC(const unsigned char *sData, unsigned long ulDataLength, unsigned long *ulOutCRC)
{
    *(unsigned long *)ulOutCRC = 0xffffffff; //Initilaize the CRC.
    this->PartialCRC(ulOutCRC, sData, ulDataLength);
    *(unsigned long *)ulOutCRC ^= 0xffffffff; //Finalize the CRC.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-12-24 08:45:33

当您测试 CRC32::FullCRC 时,您应该有一个给出已知 CRC 的输入字符串,以便您可以根据已知值验证结果。
此外,您还应该使用小于或大于字符串大小的输入长度进行测试,以验证输入不正确时方法的行为。
您还可以尝试提供空指针而不是字符串来测试该方法不会使您的应用程序崩溃。

在 VC++ 中,测试可能如下所示:

TEST(CRC32, FullCRC)
{
    //Assuming this is correct CRC (example)
    unsigned long nCorrectCRC = 0xAA55BB77;
    //A string to build crc for
    CString sValue("What is the CRC32 for this string");
    //Pointer to string buffer
    LPCSTR pBuf = sValue.GetBuffer(0);
    //Length of string
    unsigned long nLength = sValue.GetLength();
    //Calculated crc
    unsigned long nCalculatedCRC = 0;
    //Get the CRC
    CRC32 MyCRC;
    MyCRC .FullCRC(pBuf,nLength,nCalculatedCRC);
    //Do the test, GooglTest returns "Passed" or "Failed"
    ASSERT_TRUE(nCalculatedCRC == nCorrectCRC);
}

When you are testing the CRC32::FullCRC you should have an input string giving a known CRC so you can verify the result against a known value.
Also you should test using an input length which is less or higher than the size of the string to verify the behaviour of the method when the input is not correct.
You could also try to give a null pointer instead of the string to test that the method does not crash your application.

In VC++ a test could look like this:

TEST(CRC32, FullCRC)
{
    //Assuming this is correct CRC (example)
    unsigned long nCorrectCRC = 0xAA55BB77;
    //A string to build crc for
    CString sValue("What is the CRC32 for this string");
    //Pointer to string buffer
    LPCSTR pBuf = sValue.GetBuffer(0);
    //Length of string
    unsigned long nLength = sValue.GetLength();
    //Calculated crc
    unsigned long nCalculatedCRC = 0;
    //Get the CRC
    CRC32 MyCRC;
    MyCRC .FullCRC(pBuf,nLength,nCalculatedCRC);
    //Do the test, GooglTest returns "Passed" or "Failed"
    ASSERT_TRUE(nCalculatedCRC == nCorrectCRC);
}
披肩女神 2024-12-24 08:45:33

我不确定你之前是否看过这个,但肯定有试一试吧。我确信您应该对这个简单的教程有基本的了解。此外,stackoverflow 上已经提供了很多答案,我认为最好的答案是: 设置 Googletest。无论如何,请仔细阅读 kjella 提供的答案并尝试在您的代码中实现它。希望它能按照您期望的方式工作。

注意:无论谁尝试使用 Microsoft Visual Studio .Net 2003 执行类似的实现,Google 测试框架库都会自动将运行时库默认为“单线程调试”' 用于调试模式,“单线程”用于发布模式,据我尝试,从代码中更改它的选项世代不是 可用的。因此,请确保选择单线程选项作为项目中的运行时库。除此之外,这个简单而简短的教程非常有效!

祝你好运!

I am not sure if you have seen this before or not, but surely give it a go. I am pretty sure you should get the basic understanding covered in this simple tutorial. Additionally, there are lots of answers already provided on stackoverflow and the best one in my opinion is: Setup Googletest. Regardless, do go through the answer provided by kjella and try to implement it in your code. Hopefully, it should work the way you expect it to.

Note: Whoever is trying to carry out this similar implementation with Microsoft Visual Studio .Net 2003, the Google Test Framework libraries automatically defaults the Runtime Library to 'Single-threaded Debug' for Debug mode and 'Single-threaded' for Release Mode and as far as I have tried out, the option for changing it from the Code Generation isn't available. So, please make sure you choose Single-threaded option as your runtime library in your project. Other than that, this simple and short tutorial works perfect!

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文