Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test some code using Google test?

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.
}
like image 721
bigbaz34 Avatar asked Apr 15 '26 00:04

bigbaz34


1 Answers

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);
}
like image 179
kjella Avatar answered Apr 17 '26 13:04

kjella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!