Skip to content

Commit

Permalink
test: term test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lbressler13 committed Jan 23, 2024
1 parent 44279d3 commit f34541e
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 297 deletions.
Original file line number Diff line number Diff line change
@@ -1,211 +1,27 @@
package xyz.lbres.exactnumbers.expressions.term

import xyz.lbres.exactnumbers.exactfraction.ExactFraction
import xyz.lbres.exactnumbers.irrationals.log.Log
import xyz.lbres.exactnumbers.irrationals.pi.Pi
import xyz.lbres.exactnumbers.irrationals.sqrt.Sqrt
import xyz.lbres.exactnumbers.testutils.TestNumber
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

class TermTest {
private val logNum1 = Log(ExactFraction(15, 4))
private val logNum2 = Log(8, 7)
private val logNum3 = Log(ExactFraction(19, 33)).inverse()
private val logNum4 = Log(ExactFraction(25, 121))
private val testNumber1 = TestNumber(ExactFraction(5, 6))
private val testNumber2 = TestNumber(ExactFraction.SEVEN)
private val one = ExactFraction.ONE

@Test fun testConstructor() = runConstructorTests()

@Test fun testTimes() = runTimesTests()
@Test fun testDiv() = runDivTests()
@Test fun testEquals() = runEqualsTests()

@Test fun testGetSimplified() = runCommonSimplifyTests(Term::getSimplified)
@Test fun testGetValue() = runGetValueTests()

@Test fun testGetFactorsByType() = runGetFactorsByTypeTests()

@Test
fun testEquals() {
// equal
var term1 = Term.ZERO
assertEquals(term1, term1)

term1 = Term.fromValues(ExactFraction(-17, 4), emptyList())
assertEquals(term1, term1)

term1 = Term.fromValues(one, listOf(logNum1, logNum2))
assertEquals(term1, term1)

term1 = Term.fromValues(one, listOf(Pi(), Pi()))
assertEquals(term1, term1)

term1 = Term.fromValues(ExactFraction.EIGHT, listOf(logNum4, logNum3, logNum1, Sqrt(15), Pi().inverse(), Pi()))
assertEquals(term1, term1)

term1 = Term.fromValues(one, listOf(Pi(), TestNumber(ExactFraction(5))))
assertEquals(term1, term1)

term1 = Term.fromValues(ExactFraction.EIGHT, listOf(logNum4, logNum3, logNum1, Sqrt(5), Sqrt(7), Pi().inverse(), Pi()))
var term2 = Term.fromValues(ExactFraction.EIGHT, listOf(logNum4, logNum3, logNum1, Sqrt(35)))
assertEquals(term1, term2)
assertEquals(term2, term1)

term1 = Term.fromValues(ExactFraction(-4, 7), listOf(Log.ZERO, Sqrt.ONE))
term2 = Term.ZERO
assertEquals(term1, term2)
assertEquals(term2, term1)

term1 = Term.fromValues(ExactFraction(-4, 7), listOf(testNumber1, Sqrt(ExactFraction(7, 9)), Pi(), logNum1, logNum1.inverse()))
term2 = Term.fromValues(ExactFraction(-4, 3), listOf(Sqrt(7), Pi()))
assertEquals(term1, term2)
assertEquals(term2, term1)

// not equal
term1 = Term.ONE
term2 = -Term.ONE
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(ExactFraction.TWO, emptyList())
term2 = Term.fromValues(ExactFraction.HALF, emptyList())
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(one, listOf(logNum1))
term2 = Term.ONE
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(one, listOf(logNum1))
term2 = Term.fromValues(one, listOf(logNum1.inverse()))
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(one, listOf(logNum1))
term2 = Term.fromValues(one, listOf(logNum1, logNum2))
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(one, listOf(Pi()))
term2 = Term.ONE
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(one, listOf(Pi()))
term2 = Term.fromValues(one, listOf(Pi().inverse()))
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(one, listOf(Pi(), Pi().inverse()))
term2 = Term.fromValues(one, listOf(Pi().inverse()))
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(one, listOf(Sqrt(12)))
term2 = Term.ONE
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(one, listOf(Sqrt(12)))
term2 = Term.fromValues(one, listOf(Sqrt(ExactFraction(1, 12))))
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(one, listOf(Sqrt(12), Sqrt(1000)))
term2 = Term.fromValues(one, listOf(Sqrt(ExactFraction(1, 12))))
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(ExactFraction.EIGHT, listOf(Log(15)))
term2 = Term.fromValues(ExactFraction.EIGHT, listOf(Sqrt(15)))
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(ExactFraction(5, 7), listOf(logNum1, logNum1, Pi(), Pi().inverse()))
term2 = Term.fromValues(ExactFraction.FIVE, listOf(logNum1, logNum1, Pi(), Pi().inverse()))
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(ExactFraction.EIGHT, listOf(logNum3, logNum4, Pi().inverse()))
term2 = Term.fromValues(ExactFraction(-17, 15), listOf(logNum1, logNum2, logNum3))
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)

term1 = Term.fromValues(ExactFraction.FOUR, listOf(Pi(), testNumber1))
term2 = Term.fromValues(ExactFraction.FOUR, listOf(Pi(), testNumber1.inverse()))
assertNotEquals(term1, term2)
assertNotEquals(term2, term1)
}

@Test fun testUnaryMinus() = runUnaryMinusTests()
@Test fun testUnaryPlus() = runUnaryPlusTests()
@Test fun testIsZero() = runIsZeroTests()

@Test fun testGetFactorsByType() = runGetFactorsByTypeTests()
@Test fun testGetLogs() = runGetLogsTests()
@Test fun testGetPiCount() = runGetPiCountTests()
@Test fun testGetSquareRoots() = runGetSquareRootsTests()

@Test
fun testToString() {
// zero
var term = Term.ZERO
var expected = "<0>"
assertEquals(expected, term.toString())

// just coefficient
term = Term.fromValues(ExactFraction(-25), emptyList())
expected = "<-25>"
assertEquals(expected, term.toString())

term = Term.fromValues(ExactFraction(44, 7), emptyList())
expected = "<[44/7]>"
assertEquals(expected, term.toString())

// just logs
term = Term.fromValues(one, listOf(Log.ONE))
expected = "<1x${Log.ONE}>"
assertEquals(expected, term.toString())

term = Term.fromValues(one, listOf(logNum2, logNum4, logNum1))
expected = "<1x${logNum2}x${logNum4}x$logNum1>"
assertEquals(expected, term.toString())

// just pi
term = Term.fromValues(one, listOf(Pi()))
expected = "<1x${Pi()}>"
assertEquals(expected, term.toString())

term = Term.fromValues(one, listOf(Pi(), Pi().inverse(), Pi()))
expected = "<1x${Pi()}x${Pi().inverse()}x${Pi()}>"
assertEquals(expected, term.toString())

// just sqrt
term = Term.fromValues(one, listOf(Sqrt.ONE))
expected = "<1x${Sqrt.ONE}>"
assertEquals(expected, term.toString())

term = Term.fromValues(one, listOf(Sqrt(32), Sqrt(127), Sqrt(ExactFraction(2, 9))))
expected = "<1x${Sqrt(32)}x${Sqrt(127)}x${Sqrt(ExactFraction(2, 9))}>"
assertEquals(expected, term.toString())

// mix
term = Term.fromValues(ExactFraction.EIGHT, listOf(logNum3, Sqrt(12), testNumber2, Pi()))
expected = "<8x${logNum3}x${Sqrt(12)}x${testNumber2}x${Pi()}>"
assertEquals(expected, term.toString())

val sqrt1 = Sqrt(ExactFraction(1000, 109))
val sqrt2 = Sqrt(5096)
term = Term.fromValues(
ExactFraction(-100, 333),
listOf(logNum2, logNum2, logNum4, logNum1, sqrt1, sqrt2, Pi().inverse(), Pi())
)
expected = "<[-100/333]x${logNum2}x${logNum2}x${logNum4}x${logNum1}x${sqrt1}x${sqrt2}x${Pi().inverse()}x${Pi()}>"
assertEquals(expected, term.toString())
}
@Test fun testToString() = runToStringTests()

@Test fun testToByte() = runToByteTests()
@Test fun testToChar() = runToCharTests()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import xyz.lbres.exactnumbers.exactfraction.ExactFraction
import xyz.lbres.exactnumbers.irrationals.log.Log
import xyz.lbres.exactnumbers.irrationals.pi.Pi
import xyz.lbres.exactnumbers.irrationals.sqrt.Sqrt
import xyz.lbres.exactnumbers.testutils.TestNumber
import xyz.lbres.exactnumbers.testutils.assertSucceeds
import xyz.lbres.exactnumbers.testutils.getCastingOverflowAssertion
import kotlin.test.assertEquals
Expand All @@ -30,8 +31,8 @@ fun runToCharTests() {
expected = 116.toChar()
assertEquals(expected, term.toChar())

factors = listOf(Sqrt(17), Pi(), Pi(), Log(1245, 12))
term = Term.fromValues(ExactFraction(7, 17), factors)
factors = listOf(Sqrt(17), Pi(), Pi(), TestNumber(ExactFraction(3)), Log(1245, 12))
term = Term.fromValues(ExactFraction(7, 51), factors)
expected = 48.toChar()
assertEquals(expected, term.toChar())

Expand Down Expand Up @@ -104,8 +105,8 @@ private fun <T : Number> runWholeNumberCastingTests(castLong: (Long) -> T, castT
expected = castLong(116)
assertEquals(expected, castTerm(term))

factors = listOf(Sqrt(17), Pi(), Pi(), Log(1245, 12))
term = Term.fromValues(ExactFraction(7, 17), factors)
factors = listOf(Sqrt(17), Pi(), Pi(), TestNumber(ExactFraction(3)), Log(1245, 12))
term = Term.fromValues(ExactFraction(7, 51), factors)
expected = castLong(48)
assertEquals(expected, castTerm(term))

Expand Down Expand Up @@ -160,7 +161,7 @@ private fun <T : Number> runDecimalNumberCastingTests(castDouble: (Double) -> T,
expected = castDouble(-8.377580409572781)
assertEquals(expected, castTerm(term))

term = Term.fromValues(ExactFraction.HALF, listOf(Log(4, 2).inverse(), Log(123456789), Pi().inverse()))
term = Term.fromValues(ExactFraction.EIGHT, listOf(TestNumber(ExactFraction(1, 16)), Log(4, 2).inverse(), Log(123456789), Pi().inverse()))
expected = castDouble(0.6439023028592971)
assertEquals(expected, castTerm(term))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ private val testNumber2 = TestNumber(ExactFraction.SEVEN)
private val one = ExactFraction.ONE

fun runConstructorTests() {
runFullListConstructorTests()
runFactorsConstructorTests()
runComponentConstructorTests()
}

private fun runFullListConstructorTests() {
private fun runFactorsConstructorTests() {
// zero
var term = Term.fromValues(ExactFraction.ZERO, emptyList())
checkTerm(term, ExactFraction.ZERO)
Expand Down Expand Up @@ -86,8 +86,8 @@ private fun runFullListConstructorTests() {
term = Term.fromValues(ExactFraction(-1, 5), sqrts + pis)
checkTerm(term, ExactFraction(-1, 5), sqrts + pis, emptyList(), sqrts, pis, 1)

term = Term.fromValues(one, sqrts + pis + logs + listOf(testNumber1, testNumber2))
checkTerm(term, one, sqrts + pis + logs + listOf(testNumber1, testNumber2), logs, sqrts, pis, 1)
term = Term.fromValues(one, pis + logs + listOf(testNumber1, testNumber2))
checkTerm(term, one, pis + logs + listOf(testNumber1, testNumber2), logs, sqrts = emptyList(), pis, 1)
}

private fun runComponentConstructorTests() {
Expand Down
Loading

0 comments on commit f34541e

Please sign in to comment.