0

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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • @πάντα-ῥεῖ How is that a duplicate? Can you explain the logic I don't see it. – Shafik Yaghmour Feb 11 '15 at 18:25
  • _@OP_ You're actually hitting undefined behavior when doing `return temp.c_str(); `. That's why the little kittens meow. – πάντα ῥεῖ Feb 11 '15 at 18:25
  • @ShafikYaghmour I've just added a comment for the OP. – πάντα ῥεῖ Feb 11 '15 at 18:26
  • @πάνταῥεῖ The bahavior of static_cast on an out of range enum is unspecified, there may be other issues but that is the issue the OP is asking about – Shafik Yaghmour Feb 11 '15 at 18:26
  • @ShafikYaghmour Yup, even more UB. – πάντα ῥεῖ Feb 11 '15 at 18:27
  • IT is not UB is is unspecified – Shafik Yaghmour Feb 11 '15 at 18:27
  • @ShafikYaghmour OK, but anyway the `ASSERT_EQ("Unknown value: -1", printSomething(-1));` can't pass reliably, because of the UB in the `printSomething()` function. – πάντα ῥεῖ Feb 11 '15 at 18:29
  • You are completely right that this example exhibits undefined behaviour, as it uses a pointer to memory formerly owned by a destructed local object. Nevertheless, this example has a high likelyhood to "accidentally work", if the string comparison in Google Test was not messed up. `ASSERT_EQ` compares pointers, so it would even fail if the buffer were still life. To compare ASCII strings represented by pointers to the first character, you need to use `ASSERT_STREQ` – Michael Karcher Feb 11 '15 at 20:34

0 Answers0