I’ve developed a small helper function for a unit test class, which takes my vector<unsigned char> and converts it back to a const char *. I wrote this so I can pass it to gtest’s ASSERT_STREQ macro for simple comparisons. Here it is:
const char * convertVecToChar(std::vector<unsigned char>& source)
{
std::vector<unsigned char>::size_type size = source.size();
char* data = (char*)malloc(sizeof(char) * (size + 1));
memcpy(data, &source[0], size);
data[size] = 0;
return data;
}
And here’s an example of it being called:
ASSERT_STREQ("de", convertVecToChar(somevector));
I presume this is leaky however as I’m calling malloc, but without calling delete further down the road?
Is there a more elegant way to do this, which does not involve creating a separate const char * variable for every time I call ASSERT_STREQ within a test method?
Big thanks in advance for all responses.
Chris
Return a
std::stringinstead of achar*(malloc(), ornew, unrequired):and use: