// // Created by trotfunky on 09/05/19. // #include #include "Polynomial.tpp" class PolynomialOperationTest : public ::testing::Test { protected: void SetUp() override { pInt = Polynomial({1,2,3}); pFloat = Polynomial({-1.0,0,2.0}); pLarge = Polynomial({{10,1},{0,5}}); } Polynomial pInt; Polynomial pFloat; Polynomial pLarge; }; TEST(PolynomialTest,polynomialCreation) { Polynomial pTestInt = Polynomial({1,2,3}); EXPECT_EQ(pTestInt[0],1); EXPECT_EQ(pTestInt[1],2); EXPECT_EQ(pTestInt[2],3); EXPECT_EQ(2,pTestInt.getDegree()); std::cout << "Int polynomial : " << pTestInt << std::endl; Polynomial pTestFloat = Polynomial({0,0,1,0}); EXPECT_EQ(pTestFloat[0],0); EXPECT_EQ(pTestFloat[1],0); EXPECT_EQ(pTestFloat[2],1); EXPECT_EQ(pTestFloat[3],0); EXPECT_EQ(2,pTestFloat.getDegree()); std::cout << "Float polynomial : " << pTestFloat << std::endl; Polynomial pTestCopy = Polynomial(pTestInt); EXPECT_EQ(pTestCopy[0],1); EXPECT_EQ(pTestCopy[1],2); EXPECT_EQ(pTestCopy[2],3); std::cout << pTestCopy << " is a copy of " << pTestInt << std::endl; Polynomial pTestMapInit = Polynomial({{0,1},{2,3},{10,10}}); EXPECT_EQ(pTestMapInit[0],1); EXPECT_EQ(pTestMapInit[2],3); EXPECT_EQ(pTestMapInit[10],10); std::cout << "Polynomial created via a map : " << pTestMapInit << std::endl; std::cout << std::endl; } TEST_F(PolynomialOperationTest,polynomialSum) { Polynomial summedIntP = pInt + pInt; EXPECT_EQ(summedIntP[0],2); EXPECT_EQ(summedIntP[1],4); EXPECT_EQ(summedIntP[2],6); std::cout << "Summed int polynomial : " << summedIntP << std::endl; Polynomial summedFloatP = pFloat + pFloat; EXPECT_EQ(summedFloatP[0],-2.0f); EXPECT_EQ(summedFloatP[1],0); EXPECT_EQ(summedFloatP[2],4.0f); std::cout << "Summed float polynomial : " << summedFloatP << std::endl; auto summedAuto = pInt + pFloat; EXPECT_EQ(summedAuto[0],0); EXPECT_EQ(summedAuto[1],2); EXPECT_EQ(summedAuto[2],5); summedAuto = pFloat + pInt; EXPECT_EQ(summedAuto[0],0); EXPECT_EQ(summedAuto[1],2); EXPECT_EQ(summedAuto[2],5); std::cout << "Summed auto polynomial : " << summedAuto << std::endl; Polynomial summedLarge = pInt + pLarge; EXPECT_EQ(summedLarge[0],6); EXPECT_EQ(summedLarge[1],2); EXPECT_EQ(summedLarge[2],3); for(int i = 3;i<10;i++) EXPECT_EQ(summedLarge[i],0); EXPECT_EQ(summedLarge[10],1); std::cout << "Summed small and large polynomials : " << summedLarge << std::endl; std::cout << std::endl; } TEST_F(PolynomialOperationTest,polynomialComparison) { EXPECT_TRUE(Polynomial() == Polynomial()); EXPECT_TRUE(Polynomial() == Polynomial()); Polynomial pCompInt = Polynomial({1,2,3}); EXPECT_TRUE(pCompInt == pInt); EXPECT_TRUE(pInt == pCompInt); Polynomial pCompFloat = Polynomial({-1.0,0,2.0}); EXPECT_TRUE(pCompFloat == pFloat); EXPECT_TRUE(pFloat == pCompFloat); EXPECT_TRUE(Polynomial(pFloat) == pFloat); EXPECT_TRUE(Polynomial(pInt) == pInt); EXPECT_FALSE(pInt == pFloat); EXPECT_FALSE(pInt == Polynomial()); EXPECT_FALSE(pInt == Polynomial()); std::cout << std::endl; } TEST_F(PolynomialOperationTest,polynomialDerivation) { EXPECT_TRUE(pInt.getNthDerivative(0) == pInt); EXPECT_TRUE(pFloat.getNthDerivative(0) == pFloat); std::cout << "Testing with Polynomial ..." << std::endl; Polynomial expectedIntDerivative = Polynomial(std::vector({2,6})); EXPECT_TRUE(expectedIntDerivative == pInt.getNthDerivative(1)); std::cout << "First order derivative : " << pInt.getNthDerivative(1) << std::endl; expectedIntDerivative = Polynomial({6}); EXPECT_TRUE(expectedIntDerivative == pInt.getNthDerivative(2)); std::cout << "Second order derivative : " << pInt.getNthDerivative(2) << std::endl; expectedIntDerivative = Polynomial(); EXPECT_TRUE(expectedIntDerivative == pInt.getNthDerivative(3)); EXPECT_TRUE(expectedIntDerivative == pInt.getNthDerivative(10)); std::cout << "Third order derivative : " << pInt.getNthDerivative(3) << std::endl; std::cout << "Tenth order derivative : " << pInt.getNthDerivative(10) << std::endl; std::cout << std::endl; std::cout << "Testing with Polynomial ..." << std::endl; Polynomial expectedFloatDerivative = Polynomial(std::vector({0,4})); EXPECT_TRUE(expectedFloatDerivative == pFloat.getNthDerivative(1)); std::cout << "First order derivative : " << pFloat.getNthDerivative(1) << std::endl; expectedFloatDerivative = Polynomial({4}); EXPECT_TRUE(expectedFloatDerivative == pFloat.getNthDerivative(2)); std::cout << "Second order derivative : " << pFloat.getNthDerivative(2) << std::endl; expectedFloatDerivative = Polynomial(); EXPECT_TRUE(expectedFloatDerivative == pFloat.getNthDerivative(3)); EXPECT_TRUE(expectedFloatDerivative == pFloat.getNthDerivative(10)); std::cout << "Third order derivative : " << pFloat.getNthDerivative(3) << std::endl; std::cout << "Tenth order derivative : " << pFloat.getNthDerivative(10) << std::endl; std::cout << std::endl; } TEST_F(PolynomialOperationTest,polynomialAssertion) { EXPECT_EQ(pInt(0),1); EXPECT_EQ(pInt(0.0f),1); EXPECT_EQ(pInt(1),6); EXPECT_EQ(pInt(1.0),6); EXPECT_EQ(pInt(-1),2); EXPECT_EQ(pFloat(0),-1); EXPECT_EQ(pFloat(0.0),-1); EXPECT_EQ(pFloat(1),1); EXPECT_EQ(pFloat(-1),1); EXPECT_EQ(pLarge(0),5); EXPECT_EQ(pLarge(1),6); EXPECT_EQ(pLarge(2),1029); EXPECT_EQ(Polynomial()(100),0); EXPECT_EQ(Polynomial()(0),0); } int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc,argv); return RUN_ALL_TESTS(); }