Reputation: 395
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.
}
Upvotes: 3
Views: 4475
Reputation: 5860
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!
Upvotes: 5
Reputation: 211
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);
}
Upvotes: 6