Guest User

Untitled

a guest
Jan 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. /*
  2. Polynomial_t.cpp
  3.  
  4. Unit tests for the Polynomial class
  5.  
  6. Evan M. Purkhiser
  7. evanpurkhiser@gmail.com
  8. */
  9.  
  10. #include "Polynomial.hpp"
  11. #include <cassert>
  12.  
  13. int main() {
  14.  
  15. Polynomial poly1(5);
  16. Polynomial poly2(5);
  17. Polynomial poly3(2);
  18. Polynomial poly4(3);
  19.  
  20. // Test for object equality
  21. assert(poly1 == poly2);
  22. assert(! (poly1 == poly3));
  23.  
  24. assert(poly2 != poly3);
  25.  
  26. // Test for object immediate add
  27. poly3 += poly4;
  28. assert(poly3 == poly1);
  29.  
  30. poly1 += poly2;
  31. assert(poly1 != poly2);
  32.  
  33. // Test the coefficent method
  34. Polynomial poly5(10);
  35. assert(poly4.coefficient(0) == 3);
  36. assert(poly5.coefficient(0) == 10);
  37.  
  38. Polynomial poly6(2, 10);
  39. assert(poly6.coefficient(0) == 2);
  40. assert(poly6.coefficient(1) == 10);
  41.  
  42. // Test immediate add polynomials with 2 coefficients
  43. Polynomial poly7(4, 2);
  44.  
  45. poly6 += poly7;
  46. assert(poly6.coefficient(0) == 6);
  47. assert(poly6.coefficient(1) == 12);
  48.  
  49. // Test the quadratic polynomial
  50. Polynomial poly8(4, 2, -5);
  51. assert(poly8.coefficient(0) == 4);
  52. assert(poly8.coefficient(1) == 2);
  53. assert(poly8.coefficient(2) == -5);
  54.  
  55. // Test construction of a polynomial from a vector
  56. std::vector<double> coeffs(4);
  57. coeffs[0] = 1; coeffs[1] = 0; coeffs[2] = -1;
  58.  
  59. Polynomial poly9(coeffs);
  60. assert(poly9.coefficient(0) == 1);
  61. assert(poly9.coefficient(1) == 0);
  62. assert(poly9.coefficient(2) == -1);
  63. assert(poly9.coefficient(3) == 0);
  64.  
  65. double acoeffs[] = { 1, 0, -1 };
  66. Polynomial polya(acoeffs, 3);
  67.  
  68. assert(polya.coefficient(0) == 1);
  69. assert(polya.coefficient(1) == 0);
  70. assert(polya.coefficient(2) == -1);
  71.  
  72. // Test case for different size polynomial addition
  73. polya += poly7;
  74.  
  75. // Test the factoring method
  76. assert(poly2.factor()[0] == 5);
  77.  
  78. return 0;
  79. }
Add Comment
Please, Sign In to add comment