Help this is my first time asking so please be gentle to me
Lets say I have an enum
typedef enum Fruits
{
Apple = 1,
Banana,
Cat
};
and has a function
const char* printSomething(const Fruits myFruityFruit)
{
switch (myFruityFruit)
{
case Apple:
return "This is Apple!";
case Banana:
return "This is Banana!";
case Cat:
return "This is Cat ... ?";
default:
std::stringstream ss;
ss << "Unknown value: " << myFruityFruit;
auto temp = ss.str();
return temp.c_str();
}
}
and a test case that looks like this
TEST(myTest, myFruityTest)
{
ASSERT_EQ("This is Apple!", printSomething(Apple));
ASSERT_EQ("This is Banana!", printSomething(Banana));
ASSERT_EQ("This is Cat ... ?", printSomething(Cat));
Fruits unknownFruit = static_cast<Fruits>(-1);
std::stringstream ss;
ss << "Unknown value: " << unknownFruit;
auto temp = ss.str();
ASSERT_EQ(temp.c_str(), printSomething(unknownFruit));
}
The last ASSERT_EQ
is always failing, I want to see what is the value of myFruit
if it goes to default
in the switch case
.
I am assuming that in the
ASSERT_EQ(temp.c_str(), printSomething(unknownFruit));
it will look like this
ASSERT_EQ("Unknown value: -1", printSomething(-1));
But Fruits
doesn't have a -1 so myFruityFruit
will be null
? and that is why the last ASSERT_EQ
is failing?
What really happen in this scenario? What can I do to make it pass?
Also I'm trying to avoid any valgrind errors like memory leak or others.