Commit 77d5b88c authored by Jim Newton's avatar Jim Newton

added test cases for lecture 2

parent c2fb2ce0
// Copyright (c) 2020 EPITA Research and Development Laboratory
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import homework.BinarySearch._
import lecture.BinarySearch._
import org.scalatest.FunSuite
import scala.math._
class BinarySearchTestSuite extends FunSuite {
def almostEqual(tolerance:Double)(x:Double, y:Double):Boolean = {
abs(x - y ) <= tolerance
}
test("tolerance") {
assert(almostEqual(0.0)(1.0, 1.0))
assert(almostEqual(0.5)(1.1, 1.2))
assert(almostEqual(0.5)(100.1, 100.2))
assert(almostEqual(0.1)(1000.01, 1000.05))
assert(!almostEqual(0.01)(0.001, 0.101))
}
test("bin search") {
assert(None != binSearch(-1.5, 1.0, sin, 0.0001, 20))
assert(None != binSearch(-1.0, 1.0, sin, 0.000001, 30))
// given function does not equal 0.0 in the given interval
assert(None == binSearch(1.0, 2.0, (x: Double) => 3.0, 0.001, 20))
assert(None != binSearch(-1.0, 1.0, sin, 0.1, 0.0001, 20))
// interval reversed
assert(None != binSearch(1.0, -1.0, sin, 0.1, 0.0001, 20))
// given function does not equal 20.3 in the given interval
assert(None == binSearch(1.0, -1.0, sin, 20.3, 0.001, 20))
// not enough iterations allowed
assert(None == binSearch(-0.9, 0.72, sin, 0.000001, 2))
}
test("boolean search") {
def optionAlmostEqual(test: (Double, Double) => Boolean): (Option[Double], Option[Double]) => Boolean = {
(op1, op2) => {
(op1, op2) match {
case (None,None) => true
case (Some(a), Some(b)) => test(a, b)
case _ => false
}
}
}
def ae(delta: Double): (Option[Double], Option[Double]) => Boolean = {
optionAlmostEqual(almostEqual(delta))
}
// check for maxDepth exceeded
assert(None == binSearchByBoolean(0.0, 100.0, _>= 5.0, 0.01, 3))
assert(ae(0.1)(None,binSearchByBoolean(0.0, 100.0, _>= 5.0, 0.01, 2)))
// check for successfully finding the switching point
assert(ae(0.1)(Some(3.4), binSearchByBoolean(0.3, 52.1, _ > 3.4, 0.01, 1000)))
// check for no such result in range
assert(None == binSearchByBoolean(10.0, 20.0, _ > 3.4, 0.01, 100))
}
}
// Copyright (c) 2020 EPITA Research and Development Laboratory
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import homework.DoubleIntegral._
import lecture.Calculus.almostEqual
import org.scalatest.FunSuite
import scala.math._
class DoubleIntegralTestSuite extends FunSuite {
test("slow double integral 1") {
assert(0.0 == slowDoubleIntegral((x, y) => 0.0,
-1.0, -1.0, 1.0, 1.0,
_ == _))
}
test("slow double integral 2") {
assert(1.0 == slowDoubleIntegral((x, y) => 1.0,
0.0, 0.0, 1.0, 1.0,
almostEqual(0.01)))
}
test("slow double integral 3") {
// taken from http://www.stankova.net/statistics_2012/double_integration.pdf
assert(almostEqual(0.01)(57.0, slowDoubleIntegral((x: Double, y: Double) => 1 + 8 * x * y,
0.0, 1.0, 3.0, 2.0,
almostEqual(0.001))))
}
test("slow double integral 4") {
assert(almostEqual(0.1)(1.0, slowDoubleIntegral((x, y) => x * y,
0.0, 0.0, 1.0, 2.0,
almostEqual(0.001))))
}
test("slow double integral 5") {
assert(almostEqual(0.01)(0.5, slowDoubleIntegral((x, y) => y * sin(x),
0.0, 0.0, (Pi / 2), 1.0,
almostEqual(0.001))))
}
test("slow double integral 6") {
// example from https://mathinsight.org/double_integral_examples
assert(almostEqual(0.01)(2.0 / 3, slowDoubleIntegral((x, y) => x * y * y,
0.0, 0.0, 2.0, 1.0,
almostEqual(0.0001))))
}
}
// Copyright (c) 2020 EPITA Research and Development Laboratory
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import homework.FixedPoint._
import lecture.Calculus.almostEqual
import org.scalatest.FunSuite
class FixedPointTestSuite extends FunSuite {
test("fixed point") {
assert(almostEqual(0.01)(5.0, squareRoot(25.0)))
}
}
// Copyright (c) 2020 EPITA Research and Development Laboratory
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import homework.Gradient._
import lecture.Calculus.almostEqual
import org.scalatest.FunSuite
import scala.math._
class GradientTestSuite extends FunSuite {
def ae(z1:(Double,Double),z2:(Double,Double)):Boolean = {
val (x1,y1) = z1
val (x2,y2) = z2
almostEqual(0.001)(x1, x2) && almostEqual(0.001)(y1,y2)
}
test("gradient"){
assert((0.0,0.0) == gradient((x:Double,y:Double)=>0.0, 0.1, almostEqual(0.001), 3.0, 4.0))
assert(ae((1.0,1.0),gradient((x:Double,y:Double)=>x+y, 0.1, almostEqual(0.001), 3.0, 4.0)))
def f1(x:Double,y:Double) = x*x+y
assert(ae((4.0,1.0), gradient((x:Double,y:Double)=>(x*x+y), 0.01, almostEqual(0.0001), 2.0, 0.0)))
def f2(x:Double,y:Double):Double = x*x + y*y*y
assert(ae((2.0,12.0), gradient(f2, 0.1, almostEqual(0.001), 1.0,2.0)))
def f3(x:Double,y:Double):Double = x*cos(y) + y*sin(x)
assert(ae((1.0,0.0),gradient(f3,0.1,almostEqual(0.0001),0.0,0.0)))
}
}
// Copyright (c) 2020 EPITA Research and Development Laboratory
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import homework.LimitAtDiscontinuity._
import lecture.Calculus.almostEqual
import org.scalatest._
class LimitAtDiscontinuityTestSuite extends FunSuite {
test("limit at discontinuity"){
assert(almostEqual(0.0015)(4.0, evalLimitAtDiscontinutiy()))
}
}
// Copyright (c) 2020 EPITA Research and Development Laboratory
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import org.scalatest.FunSuite
import lecture.Polynomial._
import scala.math._
class PolynomialTestSuite extends FunSuite {
import lecture.Polynomial._
val polynomials: Array[POLY] = Array(one,
zero,
Map(0 -> 3, 1 -> 0.1, 2 -> -0.5),
Map(1 -> 1.0, 2 -> 2.0),
Map(2 -> -2.0, 3 -> 0.125, 1 -> 1.0),
Map(2 -> -2.1, 3 -> 0.125, 1 -> 1.0, 0 -> -0.5),
Map(2 -> -2.2, 3 -> 0.125, 1 -> 1.0),
Map(2 -> -2.3, 3 -> 0.125, 1 -> 1.0, 0 -> -0.25),
Map(2 -> -2.4, 3 -> 0.125, 1 -> 1.0),
Map(2 -> -2.3, 3 -> 0.125, 1 -> -0.5, 0 -> -0.25))
val xs: Array[Double] = Array(1.0, 0.5, 0.25, -0.8, 0.75)
test("evaluate") {
for {p <- polynomials
x <- xs}
evaluate(p, x)
for {x <- xs}
assert(abs(evaluate(Map(2 -> 1), x) - x * x) < 0.001, "x=$x n=$n")
for {n <- 0 to 10
x <- xs}
assert(abs(evaluate(Map(n -> 1), x) - pow(x, n)) < 0.001, "x=$x n=$n")
for {x <- xs
c1 <- List(0.5, 0.25)
c2 <- List(0.5, 0.25)
y = c1 * pow(x, 3) + c2 * pow(x, 2) - 1.0
p = Map(3 -> c1, 2 -> c2, 0 -> -1.0)}
assert(abs(y - evaluate(p, x)) < 0.0001)
for {x <- xs} assert(1.0 == evaluate(one, x))
for {x <- xs} assert(0.0 == evaluate(zero, x))
}
test("plus") {
for {p1 <- polynomials
p2 <- polynomials
} plus(p1, p2)
// check commutativity
for {p1 <- polynomials
p2 <- polynomials
} assert(almostEqual(0.001)(plus(p1, p2), plus(p2, p1)))
// check associativity
for {p1 <- polynomials
p2 <- polynomials
p3 <- polynomials
} assert(almostEqual(0.001)(plus(plus(p1, p2),p3), plus(p1,plus(p2, p3))))
for {x <- xs
p1 <- polynomials
p2 <- polynomials
p12 = plus(p1, p2)
p1x = evaluate(p1, x)
p2x = evaluate(p2, x)
p12x = evaluate(p12, x)
} assert(abs(p1x + p2x - p12x) < 0.0001, s"p1=$p1 p2=$p2 p12=$p12 x=$x p1x=$p1x p2x=$p2x p12x=$p12x")
}
test("scale") {
for {p <- polynomials
s <- Array(0.0, 1.0, -1.0, 0.5, 0.25, 0.125)
} scale(s, p)
for {p <- polynomials
s <- Array(0.0, 1.0, -1.0, 0.5, 0.25, 0.125)
x <- xs
spx = evaluate(scale(s,p),x)
px = evaluate(p,x)
} assert(abs(spx - s*px) < 0.0001)
}
test("subtract") {
for {p1 <- polynomials
p2 <- polynomials
} subtract(p1, p2)
for {x <- xs
p1 <- polynomials
p2 <- polynomials
p12 = subtract(p1, p2)
p1x = evaluate(p1, x)
p2x = evaluate(p2, x)
p12x = evaluate(p12, x)
} assert(abs(p1x - p2x - p12x) < 0.0001, s"p1=$p1 p2=$p2 p12=$p12 x=$x p1x=$p1x p2x=$p2x p12x=$p12x")
for {p1 <- polynomials
p2 <- polynomials
p12a = subtract(p1, p2)
p12b = plus(p1,scale(-1.0,p2))
} assert(almostEqual(0.0001)(p12a,p12b))
}
test("times") {
for {p1 <- polynomials
p2 <- polynomials
} times(p1, p2)
// check commutativity
for {p1 <- polynomials
p2 <- polynomials
} assert(almostEqual(0.001)(times(p1, p2), times(p2, p1)))
// check associativity
for {p1 <- polynomials
p2 <- polynomials
p3 <- polynomials
} assert(almostEqual(0.001)(times(times(p1, p2),p3), times(p1,times(p2, p3))))
for {x <- xs
p1 <- polynomials
p2 <- polynomials
p12 = times(p1, p2)
p1x = evaluate(p1, x)
p2x = evaluate(p2, x)
p12x = evaluate(p12, x)
} assert(abs(p1x * p2x - p12x) < 0.0001, s"p1=$p1 p2=$p2 p12=$p12 x=$x p1x=$p1x p2x=$p2x p12x=$p12x")
}
test("one") {
for {p <- polynomials
} assert(almostEqual(0.0001)(p, times(one, p)))
for {p <- polynomials
} assert(almostEqual(0.0001)(p, times(p,one)))
}
test("zero") {
for {p <- polynomials
} assert(almostEqual(0.0001)(p, plus(zero, p)))
for {p <- polynomials
} assert(almostEqual(0.0001)(p, plus(p,zero)))
for {p <- polynomials
} assert(almostEqual(0.0001)(zero, times(zero, p)))
for {p <- polynomials
} assert(almostEqual(0.0001)(zero, times(p,zero)))
}
test("power") {
for {n <- 1 to 20}
assert(almostEqual(0.0001)(zero, power(zero, n)), s"zero to any positive power ($n) should be zero")
for {n <- 1 to 20}
assert(almostEqual(0.0001)(one, power(one, n)), "one to any positive power should be 1")
for {p <- polynomials}
assert(almostEqual(0.0001)(one, power(p, 0)), "one raised to zero should be one")
for {p <- polynomials}
assert(almostEqual(0.0001)(p, power(p, 1)), "p raised to one should be p")
for {x <- xs
poly <- polynomials
n <- 0 to 4
p1 = power(poly, n)
polyx = evaluate(poly, x) // Double
p1x = evaluate(p1, x) // Double
polyxn = pow(polyx, n) // Double
} assert(abs(p1x - polyxn) < 0.0001, s"poly=$poly n=$n x=$x")
}
}
// Copyright (c) 2020 EPITA Research and Development Laboratory
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import homework.TrapezoidIntegral._
import lecture.Calculus.almostEqual
import org.scalatest.FunSuite
import scala.math._
class TrapezoidIntegralTestSuite extends FunSuite {
test("faster double integral 1") {
assert(0.0 == fasterDoubleIntegral((x, y) => 0.0,
-1.0, -1.0, 1.0, 1.0,
_ == _))
}
test("faster double integral 2") {
assert(1.0 == fasterDoubleIntegral((x, y) => 1.0,
0.0, 0.0, 1.0, 1.0,
almostEqual(0.01)))
}
test("faster double integral 3") {
// taken from http://www.stankova.net/statistics_2012/double_integration.pdf
assert(almostEqual(0.01)(57.0, fasterDoubleIntegral((x: Double, y: Double) => 1 + 8 * x * y,
0.0, 1.0, 3.0, 2.0,
almostEqual(0.001))))
}
test("faster double integral 4") {
assert(almostEqual(0.1)(1.0, fasterDoubleIntegral((x, y) => x * y,
0.0, 0.0, 1.0, 2.0,
almostEqual(0.001))))
}
test("faster double integral 5") {
assert(almostEqual(0.01)(0.5, fasterDoubleIntegral((x, y) => y * sin(x),
0.0, 0.0, (Pi / 2), 1.0,
almostEqual(0.001))))
}
test("faster double integral 6") {
// example from https://mathinsight.org/double_integral_examples
assert(almostEqual(0.01)(2.0 / 3, fasterDoubleIntegral((x, y) => x * y * y,
0.0, 0.0, 2.0, 1.0,
almostEqual(0.0001))))
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment