/**************************************************************** **** **** This file belongs with the course **** Introduction to Scientific Programming in C++/Fortran2003 **** copyright 2017-2023 Victor Eijkhout eijkhout@tacc.utexas.edu **** **** testzero.cxx : unittests for root finding **** ****************************************************************/ #include using std::cin; using std::cout; #include using std::setw; #include using std::vector; #include "findzeroclass.hpp" #define CATCH_CONFIG_MAIN #include "catch2/catch_all.hpp" TEST_CASE( "proper test","[2]" ) { vector coefficients{3.,2.,0.,1.1}; polynomial p(coefficients); REQUIRE( p.is_proper() ); coefficients.at(0) = 0.; polynomial q(coefficients); REQUIRE( not q.is_proper() ); } TEST_CASE( "polynomial degree","[3]" ) { polynomial second( {2,0,1} ); // 2x^2 + 1 REQUIRE( not second.is_odd() ); polynomial third( {3,2,0,1} ); // 3x^3 + 2x^2 + 1 REQUIRE( third.is_odd() ); } TEST_CASE( "polynomial evaluation","[4]" ) { //codesnippet rootcatcheval polynomial second( {2,0,1} ); // correct interpretation: 2x^2 + 1 REQUIRE( second.is_proper() ); REQUIRE( second.evaluate_at(2) == Catch::Approx(9) ); // wrong interpretation: 1x^2 + 2 REQUIRE( second.evaluate_at(2) != Catch::Approx(6) ); //codesnippet end REQUIRE( second.evaluate_at(2) == Catch::Approx(9) ); polynomial third( {3,2,0,1} ); // 3x^3 + 2x^2 + 1 REQUIRE( third.is_proper() ); REQUIRE( third.evaluate_at(0) == Catch::Approx(1) ); } TEST_CASE( "outer bounds","[5]" ) { double left{10},right{11}; right = left+1; polynomial second( {2,0,1} ); // 2x^2 + 1 REQUIRE_THROWS( second.find_initial_bounds(left,right) ); polynomial third( {3,2,0,1} ); // 3x^3 + 2x^2 + 1 REQUIRE_NOTHROW( third.find_initial_bounds(left,right) ); REQUIRE( left>{ {1., 0., 0., 0.}, {.01, 0., 0., 0., 0., 100000.} } ) { polynomial oddpoly( c ); double left{10},right{11}; REQUIRE_NOTHROW( oddpoly.find_initial_bounds(left,right) ); REQUIRE( leftold_left ) ) ); } TEST_CASE( "test zero finder", "[7]" ) { vector> coefficients{ {1., 0., 0., 0.01}, // x^3, 3x^2 {.01, 0., 0., 0., 0., 100000.} // 1/100 x^5 + 10,000 , 1/20 x^4 }; for ( int ipoly=0; ipoly