196 lines
No EOL
5.9 KiB
C++
196 lines
No EOL
5.9 KiB
C++
//
|
|
// Created by trotfunky on 09/05/19.
|
|
//
|
|
|
|
#include <gtest/gtest.h>
|
|
#include "Polynomial.tpp"
|
|
|
|
class PolynomialOperationTest : public ::testing::Test
|
|
{
|
|
protected:
|
|
void SetUp() override
|
|
{
|
|
pInt = Polynomial<int>({1,2,3});
|
|
pFloat = Polynomial<float>({-1.0,0,2.0});
|
|
pLarge = Polynomial<int>({{10,1},{0,5}});
|
|
}
|
|
|
|
Polynomial<int> pInt;
|
|
Polynomial<float> pFloat;
|
|
Polynomial<int> pLarge;
|
|
};
|
|
|
|
TEST(PolynomialTest,polynomialCreation)
|
|
{
|
|
Polynomial<int> pTestInt = Polynomial<int>({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<float> pTestFloat = Polynomial<float>({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<float> pTestCopy = Polynomial<float>(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<int> pTestMapInit = Polynomial<int>({{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<int> 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<float> 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<int> 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<int>() == Polynomial<int>());
|
|
EXPECT_TRUE(Polynomial<float>() == Polynomial<int>());
|
|
|
|
Polynomial<int> pCompInt = Polynomial<int>({1,2,3});
|
|
EXPECT_TRUE(pCompInt == pInt);
|
|
EXPECT_TRUE(pInt == pCompInt);
|
|
|
|
Polynomial<float> pCompFloat = Polynomial<float>({-1.0,0,2.0});
|
|
EXPECT_TRUE(pCompFloat == pFloat);
|
|
EXPECT_TRUE(pFloat == pCompFloat);
|
|
|
|
EXPECT_TRUE(Polynomial<float>(pFloat) == pFloat);
|
|
EXPECT_TRUE(Polynomial<float>(pInt) == pInt);
|
|
|
|
EXPECT_FALSE(pInt == pFloat);
|
|
EXPECT_FALSE(pInt == Polynomial<int>());
|
|
EXPECT_FALSE(pInt == Polynomial<float>());
|
|
|
|
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<int> ..." << std::endl;
|
|
|
|
Polynomial<int> expectedIntDerivative = Polynomial<int>(std::vector<int>({2,6}));
|
|
EXPECT_TRUE(expectedIntDerivative == pInt.getNthDerivative(1));
|
|
|
|
std::cout << "First order derivative : " << pInt.getNthDerivative(1) << std::endl;
|
|
|
|
expectedIntDerivative = Polynomial<int>({6});
|
|
EXPECT_TRUE(expectedIntDerivative == pInt.getNthDerivative(2));
|
|
|
|
std::cout << "Second order derivative : " << pInt.getNthDerivative(2) << std::endl;
|
|
|
|
expectedIntDerivative = Polynomial<int>();
|
|
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<float> ..." << std::endl;
|
|
|
|
Polynomial<float> expectedFloatDerivative = Polynomial<float>(std::vector<float>({0,4}));
|
|
EXPECT_TRUE(expectedFloatDerivative == pFloat.getNthDerivative(1));
|
|
|
|
std::cout << "First order derivative : " << pFloat.getNthDerivative(1) << std::endl;
|
|
|
|
expectedFloatDerivative = Polynomial<float>({4});
|
|
EXPECT_TRUE(expectedFloatDerivative == pFloat.getNthDerivative(2));
|
|
|
|
std::cout << "Second order derivative : " << pFloat.getNthDerivative(2) << std::endl;
|
|
|
|
expectedFloatDerivative = Polynomial<float>();
|
|
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<bool>()(100),0);
|
|
EXPECT_EQ(Polynomial<bool>()(0),0);
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
::testing::InitGoogleTest(&argc,argv);
|
|
return RUN_ALL_TESTS();
|
|
} |