Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Polynomial_t.cpp
- Unit tests for the Polynomial class
- Evan M. Purkhiser
- evanpurkhiser@gmail.com
- */
- #include "Polynomial.hpp"
- #include <cassert>
- int main() {
- Polynomial poly1(5);
- Polynomial poly2(5);
- Polynomial poly3(2);
- Polynomial poly4(3);
- // Test for object equality
- assert(poly1 == poly2);
- assert(! (poly1 == poly3));
- assert(poly2 != poly3);
- // Test for object immediate add
- poly3 += poly4;
- assert(poly3 == poly1);
- poly1 += poly2;
- assert(poly1 != poly2);
- // Test the coefficent method
- Polynomial poly5(10);
- assert(poly4.coefficient(0) == 3);
- assert(poly5.coefficient(0) == 10);
- Polynomial poly6(2, 10);
- assert(poly6.coefficient(0) == 2);
- assert(poly6.coefficient(1) == 10);
- // Test immediate add polynomials with 2 coefficients
- Polynomial poly7(4, 2);
- poly6 += poly7;
- assert(poly6.coefficient(0) == 6);
- assert(poly6.coefficient(1) == 12);
- // Test the quadratic polynomial
- Polynomial poly8(4, 2, -5);
- assert(poly8.coefficient(0) == 4);
- assert(poly8.coefficient(1) == 2);
- assert(poly8.coefficient(2) == -5);
- // Test construction of a polynomial from a vector
- std::vector<double> coeffs(4);
- coeffs[0] = 1; coeffs[1] = 0; coeffs[2] = -1;
- Polynomial poly9(coeffs);
- assert(poly9.coefficient(0) == 1);
- assert(poly9.coefficient(1) == 0);
- assert(poly9.coefficient(2) == -1);
- assert(poly9.coefficient(3) == 0);
- double acoeffs[] = { 1, 0, -1 };
- Polynomial polya(acoeffs, 3);
- assert(polya.coefficient(0) == 1);
- assert(polya.coefficient(1) == 0);
- assert(polya.coefficient(2) == -1);
- // Test case for different size polynomial addition
- polya += poly7;
- // Test the factoring method
- assert(poly2.factor()[0] == 5);
- return 0;
- }
Add Comment
Please, Sign In to add comment