Skip to content

Commit

Permalink
Update ExpressionUnitTest.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkempire78 committed Jul 3, 2023
1 parent a7bd256 commit d5c3c8a
Showing 1 changed file with 51 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,113 +16,131 @@ class ExpressionUnitTest {
private val groupingSeparatorSymbol = DecimalFormatSymbols.getInstance().groupingSeparator.toString()
@Test
fun percentage_isCorrect() {
var result = calculate("100*95%", false)
var result = calculate("100*95%", false).toDouble()
assertEquals(95.0, result, 0.0)

result = calculate("(10*10)*95%", false)
result = calculate("(10*10)*95%", false).toDouble()
assertEquals(95.0, result, 0.0)

result = calculate("100*100%", false)
result = calculate("100*100%", false).toDouble()
assertEquals(100.0, result, 0.0)

result = calculate("100*100/100*100%", false)
result = calculate("100*100/100*100%", false).toDouble()
assertEquals(100.0, result, 0.0)

result = calculate("100%10", false)
result = calculate("100%10", false).toDouble()
assertEquals(10.0, result, 0.0)

result = calculate("10%10%", false)
result = calculate("10%10%", false).toDouble()
assertEquals(0.01, result, 0.0)

result = calculate("900/10%", false)
result = calculate("900/10%", false).toDouble()
assertEquals(9000.0, result, 0.0)
}

@Test
fun addition_isCorrect() {
var result = calculate("1+1", false)
var result = calculate("1+1", false).toDouble()
assertEquals(2.0, result, 0.0)

result = calculate("(1+1)+1", false)
result = calculate("(1+1)+1", false).toDouble()
assertEquals(3.0, result, 0.0)
}

@Test
fun pow_isCorrect() {
var result = calculate("4^3", false).toDouble()
assertEquals(64.0, result, 0.0)

result = calculate("5^-5", false).toDouble()
assertEquals(0.00032, result, 0.0)
}

@Test
fun subtraction_isCorrect() {
var result = calculate("1-1", false)
var result = calculate("1-1", false).toDouble()
assertEquals(0.0, result, 0.0)

result = calculate("1-1-1", false)
result = calculate("1-1-1", false).toDouble()
assertEquals(-1.0, result, 0.0)
}

@Test
fun division_isCorrect() {
var result = calculate("0.5/0.01", false).toDouble()
assertEquals(50.0, result, 0.0)

result = calculate("7/2", false).toDouble()
assertEquals(3.5, result, 0.0)
}

@Test
fun factorial_isCorrect() {
var result = calculate("0!", false)
var result = calculate("0!", false).toDouble()
assertEquals(1.0, result, 0.0)

result = calculate("5!", false)
result = calculate("5!", false).toDouble()
assertEquals(120.0, result, 0.0)

result = calculate("10!", false)
result = calculate("10!", false).toDouble()
assertEquals(3628800.0, result, 0.0)

result = calculate("5!+5!", false)
result = calculate("5!+5!", false).toDouble()
assertEquals(240.0, result, 0.0)

result = calculate("(3!)!+(3!)!", false)
result = calculate("(3!)!+(3!)!", false).toDouble()
assertEquals(1440.0, result, 0.0)

result = calculate("(3!)!/(3!)!", false)
result = calculate("(3!)!/(3!)!", false).toDouble()
assertEquals(1.0, result, 0.0)
}

@Test
fun sqrt_isCorrect() {
var result = calculate("√2^2", false)
var result = calculate("√2^2", false).toDouble()
assertEquals(2.0, result, 0.0)

result = calculate("√9", false)
result = calculate("√9", false).toDouble()
assertEquals(3.0, result, 0.0)
}

@Test
fun trigonometric_functions_isCorrect() {
// In radians
var result = calculate("cos(0)", false)
var result = calculate("cos(0)", false).toDouble()
assertEquals(1.0, result, 0.0)

result = calculate("1*cos(0)", false)
result = calculate("1*cos(0)", false).toDouble()
assertEquals(1.0, result, 0.0)

result = calculate("1+cos(0)", false)
result = calculate("1+cos(0)", false).toDouble()
assertEquals(2.0, result, 0.0)

result = calculate("1-cos(0)", false)
result = calculate("1-cos(0)", false).toDouble()
assertEquals(0.0, result, 0.0)

result = calculate("2^cos(0)", false)
result = calculate("2^cos(0)", false).toDouble()
assertEquals(2.0, result, 0.0)

result = calculate("(cos(0))", false)
result = calculate("(cos(0))", false).toDouble()
assertEquals(1.0, result, 0.0)

result = calculate("cos(2)", false)
result = calculate("cos(2)", false).toDouble()
assertEquals(-0.4161468365471424, result, 0.0)

result = calculate("tan(pi/2)", false)
assertEquals(Double.NaN, result, 0.0)
result = calculate("tan(pi/2)", false).toDouble()
assertEquals(0.0, result, 0.0) // 0.0 means that it is impossible

result = calculate("tan(45)", true)
result = calculate("tan(45)", true).toDouble()
assertEquals(0.9999999999999999, result, 0.0)

result = calculate("sin(220)", true)
result = calculate("sin(220)", true).toDouble()
assertEquals(-0.6427876096865393, result, 0.0)

result = calculate("sin(5!)", true)
result = calculate("sin(5!)", true).toDouble()
assertEquals(0.8660254037844387, result, 0.0)

result = calculate("sin(1+1)", true)
result = calculate("sin(1+1)", true).toDouble()
assertEquals(0.03489949670250097, result, 0.0)
}

Expand All @@ -135,7 +153,7 @@ class ExpressionUnitTest {
@BeforeClass
@JvmStatic fun setup() {
expression = Expression()
calculator = Calculator()
calculator = Calculator(10)
}
}
}

0 comments on commit d5c3c8a

Please sign in to comment.