25 #ifndef WPOLYNOMIALEQUATIONSOLVERS_TEST_H
26 #define WPOLYNOMIALEQUATIONSOLVERS_TEST_H
33 #include <cxxtest/TestSuite.h>
35 #include "../WPolynomialEquationSolvers.h"
48 TS_ASSERT_THROWS_EQUALS( solveRealQuadraticEquation( 0.0, 0.0, 1.0 ),
const WEquationHasNoRoots &e,
49 std::string( e.
what() ),
"The equation: 0x^2 + 0x + 1 = 0.0 has no solutions!" );
57 typedef std::pair< std::complex< double >, std::complex< double > > ComplexPair;
58 ComplexPair actual = solveRealQuadraticEquation( 1.0, 0.0, 0.0 );
59 TS_ASSERT_EQUALS( actual.first, std::complex< double >( 0.0, 0.0 ) );
60 TS_ASSERT_EQUALS( actual.second, actual.first );
68 typedef std::pair< std::complex< double >, std::complex< double > > ComplexPair;
69 ComplexPair actual = solveRealQuadraticEquation( 1.0, 0.0, -1.0 );
70 ComplexPair expected = ComplexPair( std::complex< double >( -1.0, 0.0 ), std::complex< double >( 1.0, 0.0 ) );
71 if( actual.first != expected.first )
73 std::cout << std::endl <<
"We assume the order of the pair is swaped, but both solutions are right" << std::endl;
74 TS_ASSERT_EQUALS( actual.first, expected.second );
75 TS_ASSERT_EQUALS( actual.second, expected.first );
79 TS_ASSERT_EQUALS( actual.first, expected.first );
80 TS_ASSERT_EQUALS( actual.second, expected.second );
89 typedef std::pair< std::complex< double >, std::complex< double > > ComplexPair;
90 ComplexPair actual = solveRealQuadraticEquation( 1.0, 0.0, 1.0 );
91 ComplexPair expected = ComplexPair( std::complex< double >( 0.0, 1.0 ), std::complex< double >( 0.0, -1.0 ) );
92 if( actual.first != expected.first )
94 std::cout << std::endl <<
"We assume the order of the pair is swaped, but both solutions are right" << std::endl;
95 TS_ASSERT_EQUALS( actual.first, expected.second );
96 TS_ASSERT_EQUALS( actual.second, expected.first );
100 TS_ASSERT_EQUALS( actual.first, expected.first );
101 TS_ASSERT_EQUALS( actual.second, expected.second );
113 typedef std::pair< std::complex< double >, std::complex< double > > ComplexPair;
114 ComplexPair actual = solveRealQuadraticEquation( 1.0, 9999999999.0, -9.0e10 );
115 ComplexPair expected = ComplexPair( std::complex< double >( -1.00000000079999999928e10, 0.0 ), std::complex< double >( 8.99999999280000001224, 0.0 ) );
116 double delta = 0.00000001;
117 if( ( ( std::abs( actual.first - expected.first ) > delta ) && ( std::abs( actual.first - expected.second ) > delta ) ) ||
118 ( ( std::abs( actual.second - expected.second ) > delta ) && ( std::abs( actual.second - expected.first ) > delta ) ) )
120 std::stringstream ss;
121 ss << std::fixed << std::setprecision( 16 ) << std::endl;
122 ss <<
"Invalid solutions: " << std::endl;
123 ss << std::real( actual.first ) <<
" i" << std::imag( actual.first ) <<
" :::: ";
124 ss << std::real( actual.second ) <<
" i" << std::imag( actual.second ) << std::endl;
125 ss <<
" were invalid. I expected: " << std::endl;
126 ss << std::real( expected.first ) <<
" i" << std::imag( expected.first ) <<
" :::: ";
127 ss << std::real( expected.second ) <<
" i" << std::imag( expected.second ) << std::endl;
128 std::cout << ss.str();
129 TS_FAIL(
"Check your numerical stability and the order of the solutions found." );
Indicates invalid element access of a container.
virtual const char * what() const
Returns the message string set on throw.
Testsuite for the WPolynomialEquationSolvers functions.
void testRealQuadraticEquationToTestNumericalIssuesAndPrecisions(void)
This test numerical stability: x^2 + 9999999999x -9e10 = 0, has solutions according to wolfram alpha:...
void testRealQuadraticEquationWithTwoImaginarySolutions(void)
x^2 + 1 = 0 has two solutions: i and -i.
void testRealQuadraticEquationWithTwoRealSolutions(void)
x^2 - 1 = 0 has two solutions: 1.0 and -1.0.
void testRealQuadraticEquationWithNoSolution(void)
If there is no solution an exception should be thrown.
void testRealQuadraticEquationWithOnlyOneSolution(void)
x^2 = 0 has only one solution: 0.0.