SHARE
TWEET

Tests

a guest Oct 9th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.scalatest.FunSuite
  2. class C_Polynomial_Tests extends FunSuite {
  3.   import polynomials.Polynomial.x
  4.   import polynomials.{ C, S, X }
  5.   val p1 = C(2)
  6.   val p2 = C(3)
  7.   val p3 = C(-5)
  8.   val p4 = X(C(5))
  9.   val p5 = S(1, S(2, C(3)))
  10.   test("degree") {
  11.     assert(p1.degree === 0)
  12.     assert(p2.degree === 0)
  13.     assert(p3.degree === 0)
  14.     assert(p4.degree === 1)
  15.     assert(p5.degree === 2)
  16.   }
  17.   test("eval") {
  18.     assert(p1.eval(1) === 2.0)
  19.     assert(p2.eval(0) === 3.0)
  20.     assert(p3.eval(10) === -5.0)
  21.     assert(p4.eval(2) === 10.0)
  22.     assert(p5.eval(2) === 17.0)
  23.   }
  24.   test("+: Polynomial") {
  25.     assert(p1 + p2 === C(5))
  26.     assert(p1 + p3 === C(-3))
  27.     assert(p2 + p3 === C(-2))
  28.     assert(p1 + p2 + p3 === C(0))
  29.     assert(p1 + p4 === S(2, C(5)))
  30.   }
  31.   test("-: Polynomial") {
  32.     assert(p2 - p1 === C(1))
  33.     assert(p3 - p1 === C(-7))
  34.     assert(p5 - p3 === S(6, S(2, C(3))))
  35.     assert(p4 - p5 ===  S(-1, S(3, C(-3))))
  36.     assert(p4 - p1 === S(-2, C(5)))
  37.   }
  38.   test("*: Polynomial") {
  39.     val p = X(S(6, X(S(12, X(C(9))))))
  40.     assert(p.eval(1) === 27.0)
  41.     assert(p1 * p2 === C(6))
  42.     assert(p1 * p4 === X(C(10)))
  43.     assert(p1 * p5 === S(2, S(4, C(6))))
  44.     assert(p4 * p4 === X(X(C(25))))
  45.     assert((p4*p4).degree === 2)
  46.     assert(p5 * C(0) === C(0))
  47.     assert(p5 * p5 === S(1,S(4,S(10,S(12,C(9))))))
  48.     assert(p5 * p4 === X(S(5, S(10, C(15)))))
  49.     assert(p4 * p5 === X(S(5, S(10, C(15)))))
  50.     assert(p5 * C(0) === C(0))
  51.     assert(p4 * C(0) === C(0))
  52.     assert(p1 * C(0) === C(0))
  53.   }
  54.   test("Basic subtraction and simplification") {
  55.     val f = X(S(2, C(3)))
  56.     assert(p5 - f === C(1))
  57.     val f2 = S(1, C(5))
  58.     assert(p4 - f2 === C(-1))
  59.     assert((p5 - C(1)).degree === 2)
  60.     assert(p5 - C(1) === X(S(2, C(3))))
  61.   }
  62.   test("-: Unary Operator") {
  63.     assert(-p1 === C(-2))
  64.     assert(-p4 === X(C(-5)))
  65.     assert(-p5 === S(-1, S(-2, C(-3))))
  66.   }
  67.   test("Derivative") {
  68.     assert(p1.derivative === C(0))
  69.     assert(p4.derivative === C(5))
  70.     assert(p5.derivative === S(2, C(6)))
  71.   }
  72.   test("@@: Polynomial") {
  73.     assert(p1 @@ 0 === C(1))
  74.     assert(p4 @@ 0 === C(1))
  75.     assert(p5 @@ 0 === C(1))
  76.     assert(p1 @@ 3 === C(8))
  77.     assert(p4 @@ 2 === X(X(C(25))))
  78.     assert(p5 @@ 2 === S(1, S(4, S(10, S(12, C(9))))))
  79.   }
  80.   test("+, -, *: Integer") {
  81.     assert(p1 + 10 === C(12))
  82.     assert(p4 + 10 === S(10, C(5)))
  83.     assert(p5 + 10 === S(11, S(2, C(3))))
  84.     assert(p1 - 10 === C(-8))
  85.     assert(p4 - 10 === S(-10, C(5)))
  86.     assert(p5 - 10 === S(-9, S(2, C(3))))
  87.     assert(p1 * 10 === C(20))
  88.     assert(p4 * 10 === X(C(50)))
  89.     assert(p5 * 10 === S(10, S(20, C(30))))
  90.   }
  91.   test("exceptions") {
  92.     assertThrows[IllegalArgumentException](p1 @@ -1)
  93.     assertThrows[IllegalArgumentException](p4 @@ -1)
  94.     assertThrows[IllegalArgumentException](p5 @@ -1)
  95.   }
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top