Skip to content

Commit

Permalink
test: add some tests for Evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed May 5, 2024
1 parent e5c8625 commit 0697b3c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions app/src/test/java/net/youapps/calcyou/EvaluatorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.youapps.calcyou

import net.youapps.calcyou.data.graphing.CompiledExpression
import net.youapps.calcyou.data.graphing.Evaluator
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Test
import kotlin.math.abs
import kotlin.math.cos
import kotlin.math.exp
import kotlin.math.ln
import kotlin.math.log
import kotlin.math.log10
import kotlin.math.pow
import kotlin.math.round
import kotlin.math.sin
import kotlin.math.sqrt
import kotlin.math.tan
import kotlin.random.Random

internal class EvaluatorTest {

val testCases = mapOf(
"sin(x)" to { x: Double -> sin(x) },
"cos(x)" to { x: Double -> cos(x) },
"tan(x)" to { x: Double -> tan(x) },
"sqrt(x)" to { x: Double -> sqrt(x) },
"abs(x)" to { x: Double -> abs(x) },
"log(x)" to { x: Double -> ln(x) },
"exp(x)" to { x: Double -> exp(x) },
"log10(x)" to { x: Double -> log10(x) },
"log(x,2)" to { x: Double -> log(x, 2.0) },
"sin(x) + cos(x)" to { x: Double -> sin(x) + cos(x) },
"sin(x) * cos(x)" to { x: Double -> sin(x) * cos(x) },
"sin(x) / cos(x)" to { x: Double -> sin(x) / cos(x) },
"sin(x) - cos(x)" to { x: Double -> sin(x) - cos(x) },
"sin(x) ** 2" to { x: Double -> sin(x) * sin(x) },
"x * x" to { x: Double -> x * x },
"x ** 2" to { x: Double -> x.pow(2) },
"pow(x,3)" to { x: Double -> x.pow(3) },
"2" to { _: Double -> 2.0 },
"sqrt(4)" to { _: Double -> sqrt(4.0) },
"round(x)" to { x: Double -> round(x) },
"sin(2*PI)" to { _: Double -> sin(2 * Math.PI) },
)
val random = Random(System.currentTimeMillis())

@Test
fun `execute() should return correct answer for compiled expressions`() {
testCases.forEach { expression, func ->
val argument = random.nextDouble()
val compiled: CompiledExpression = Evaluator.compile(expression)

val answer = compiled.execute("x" to argument)

assertNotNull(answer)

val expected = func(argument)
assertEquals(expected, answer!!, 1e-6)
}
}
}

0 comments on commit 0697b3c

Please sign in to comment.