-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46682fc
commit 7d27759
Showing
5 changed files
with
165 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 17 additions & 2 deletions
19
...numbers/src/main/kotlin/xyz/lbres/exactnumbers/expressions/expression/SimpleExpression.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,31 @@ | ||
package xyz.lbres.exactnumbers.expressions.expression | ||
|
||
import xyz.lbres.exactnumbers.expressions.term.Term | ||
import xyz.lbres.exactnumbers.utils.castToByte | ||
import xyz.lbres.exactnumbers.utils.castToChar | ||
import xyz.lbres.exactnumbers.utils.castToDouble | ||
import xyz.lbres.exactnumbers.utils.castToFloat | ||
import xyz.lbres.exactnumbers.utils.castToInt | ||
import xyz.lbres.exactnumbers.utils.castToLong | ||
import xyz.lbres.exactnumbers.utils.castToShort | ||
import xyz.lbres.exactnumbers.utils.createHashCode | ||
|
||
/** | ||
* Expression consisting of a single term | ||
*/ | ||
internal class SimpleExpression(private val term: Term) : Expression() { | ||
override fun unaryMinus(): Expression = SimpleExpression(-term) | ||
|
||
override fun unaryPlus(): Expression = this | ||
|
||
override fun inverse(): Expression = SimpleExpression(term.inverse()) | ||
|
||
override fun equals(other: Any?): Boolean = other is SimpleExpression && term == other.term | ||
override fun hashCode(): Int = createHashCode(listOf(term, "SimpleExpression")) | ||
|
||
override fun toByte(): Byte = castToByte(term.getValue(), term, "SimpleExpression") | ||
override fun toChar(): Char = castToChar(term.getValue(), term, "SimpleExpression") | ||
override fun toShort(): Short = castToShort(term.getValue(), term, "SimpleExpression") | ||
override fun toInt(): Int = castToInt(term.getValue(), term, "SimpleExpression") | ||
override fun toLong(): Long = castToLong(term.getValue(), term, "SimpleExpression") | ||
override fun toFloat(): Float = castToFloat(term.getValue(), term, "SimpleExpression") | ||
override fun toDouble(): Double = castToDouble(term.getValue(), term, "SimpleExpression") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...bers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/expression/simple/castingTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package xyz.lbres.exactnumbers.expressions.expression.simple | ||
|
||
fun runToByteTests() { | ||
} | ||
|
||
fun runToCharTests() { | ||
} | ||
|
||
fun runToShortTests() { | ||
} | ||
|
||
fun runToIntTests() { | ||
} | ||
|
||
fun runToLongTests() { | ||
} | ||
|
||
fun runToFloatTests() { | ||
} | ||
|
||
fun runToDoubleTests() { | ||
} |
111 changes: 111 additions & 0 deletions
111
...umbers/src/test/kotlin/xyz/lbres/exactnumbers/expressions/expression/simple/unaryTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package xyz.lbres.exactnumbers.expressions.expression.simple | ||
|
||
import xyz.lbres.exactnumbers.exactfraction.ExactFraction | ||
import xyz.lbres.exactnumbers.expressions.expression.* // ktlint-disable no-wildcard-imports no-unused-imports | ||
import xyz.lbres.exactnumbers.expressions.term.Term | ||
import xyz.lbres.exactnumbers.irrationals.log.Log | ||
import xyz.lbres.exactnumbers.irrationals.sqrt.Sqrt | ||
import xyz.lbres.exactnumbers.testutils.assertDivByZero | ||
import kotlin.test.assertEquals | ||
|
||
fun runUnaryMinusTests() { | ||
var expr = SimpleExpression(Term.ZERO) | ||
var expected = SimpleExpression(Term.ZERO) | ||
assertEquals(expected, -expr) | ||
|
||
expr = SimpleExpression(Term.fromValues(one, listOf(Log.ONE))) | ||
expected = SimpleExpression(Term.fromValues(-one, listOf(Log.ONE))) | ||
assertEquals(expected, -expr) | ||
|
||
expr = SimpleExpression(Term.fromValues(one, listOf(pi))) | ||
expected = SimpleExpression(Term.fromValues(-one, listOf(pi))) | ||
assertEquals(expected, -expr) | ||
|
||
expr = SimpleExpression(Term.fromValues(ExactFraction(44, 15), emptyList())) | ||
expected = SimpleExpression(Term.fromValues(ExactFraction(-44, 15), emptyList())) | ||
assertEquals(expected, -expr) | ||
|
||
expr = SimpleExpression( | ||
Term.fromValues( | ||
ExactFraction(-15, 44), | ||
listOf(log2, log3, log4, Sqrt(ExactFraction(64, 9)), pi, piInverse) | ||
) | ||
) | ||
expected = SimpleExpression( | ||
Term.fromValues( | ||
ExactFraction(15, 44), | ||
listOf(log2, log3, log4, Sqrt(ExactFraction(64, 9)), pi, piInverse) | ||
) | ||
) | ||
assertEquals(expected, -expr) | ||
} | ||
|
||
fun runUnaryPlusTests() { | ||
var expr = SimpleExpression(Term.ZERO) | ||
assertEquals(expr, +expr) | ||
|
||
expr = SimpleExpression(Term.fromValues(one, listOf(Log.ONE))) | ||
assertEquals(expr, +expr) | ||
|
||
expr = SimpleExpression(Term.fromValues(one, listOf(pi))) | ||
assertEquals(expr, +expr) | ||
|
||
expr = SimpleExpression(Term.fromValues(ExactFraction(44, 15), emptyList())) | ||
assertEquals(expr, +expr) | ||
|
||
expr = SimpleExpression( | ||
Term.fromValues( | ||
ExactFraction(-15, 44), | ||
listOf(log2, log3, log4, Sqrt(ExactFraction(64, 9)), pi, piInverse) | ||
) | ||
) | ||
assertEquals(expr, +expr) | ||
} | ||
|
||
fun runInverseTests() { | ||
assertDivByZero { SimpleExpression(Term.ZERO).inverse() } | ||
|
||
var expr1 = SimpleExpression(Term.ONE) | ||
var expr2 = SimpleExpression(Term.ONE) | ||
assertEquals(expr1, expr2.inverse()) | ||
|
||
expr1 = SimpleExpression(Term.fromValues(ExactFraction(17, 12), emptyList())) | ||
expr2 = SimpleExpression(Term.fromValues(ExactFraction(12, 17), emptyList())) | ||
assertEquals(expr1, expr2.inverse()) | ||
assertEquals(expr2, expr1.inverse()) | ||
|
||
expr1 = SimpleExpression(Term.fromValues(one, listOf(log3))) | ||
expr2 = SimpleExpression(Term.fromValues(one, listOf(log3.inverse()))) | ||
assertEquals(expr1, expr2.inverse()) | ||
assertEquals(expr2, expr1.inverse()) | ||
|
||
expr1 = SimpleExpression(Term.fromValues(one, listOf(pi, piInverse, sqrt2))) | ||
expr2 = SimpleExpression(Term.fromValues(one, listOf(pi, piInverse, sqrt2.inverse()))) | ||
assertEquals(expr1, expr2.inverse()) | ||
assertEquals(expr2, expr1.inverse()) | ||
|
||
expr1 = SimpleExpression(Term.fromValues(ExactFraction(-1, 9), listOf(testNumber2, testNumber2, pi, log4, pi, sqrt1))) | ||
expr2 = SimpleExpression( | ||
Term.fromValues( | ||
-ExactFraction.NINE, | ||
listOf(testNumber2.inverse(), testNumber2.inverse(), piInverse, piInverse, log4.inverse(), sqrt1.inverse()) | ||
) | ||
) | ||
assertEquals(expr1, expr2.inverse()) | ||
assertEquals(expr2, expr1.inverse()) | ||
|
||
expr1 = SimpleExpression( | ||
Term.fromValues( | ||
ExactFraction(100, 99999999999), | ||
listOf(Log(4), pi, pi, Log(14, 3)) | ||
) | ||
) | ||
expr2 = SimpleExpression( | ||
Term.fromValues( | ||
ExactFraction(99999999999, 100), | ||
listOf(Log(4).inverse(), piInverse, piInverse, Log(14, 3).inverse()) | ||
) | ||
) | ||
assertEquals(expr1, expr2.inverse()) | ||
assertEquals(expr2, expr1.inverse()) | ||
} |