mercredi 19 septembre 2018

c++ google test divsion by zero

I'm learning to write unit tests, and have started with an easy "Calculator"-class that I wanted to test. I figured out how to use the EXPECT/ASSERT functions, and what test cases etc. are, but I got a problem when I wanted to test the division by zero. Is there any possibility to test it? I mean, what should I write as test result? Is there anything like "ERROR"? Or do I have to use exceptions?

These are my tests so far:

TEST(TestCalc, TestPos) 
{
    Calc calculate;
    EXPECT_EQ(10.0, calculate.add(5.0, 5.0));
    EXPECT_EQ(9, calculate.mul(3, 3));
    EXPECT_EQ(9, calculate.div(27, 3));
    EXPECT_EQ(9, calculate.sub(12, 3));
}
TEST(TestCalc, TestNeg)
{
    Calc calculate;
    EXPECT_EQ(-1.0, calculate.add(5.0, -6.0));
    EXPECT_EQ(-9, calculate.mul(3, -3));
    EXPECT_EQ(-9, calculate.div(27, -3));
    EXPECT_EQ(15, calculate.sub(12, -3));
}

TEST(TestCalc, TestZero)
{
    Calc calculate;
    EXPECT_EQ(10.0, calculate.add(5.0, 0));
    EXPECT_EQ(9, calculate.mul(3, 0));
    EXPECT_EQ(, calculate.div(27,0));
    EXPECT_EQ(12, calculate.sub(12,0));
}

Aucun commentaire:

Enregistrer un commentaire