From 26f9719fee2adfe84d15e7d8d09a1515e1a9a89e Mon Sep 17 00:00:00 2001 From: NathanFallet Date: Sat, 4 Nov 2023 23:58:45 +0100 Subject: [PATCH 1/2] Refactoring to compile for JS --- build.gradle.kts | 7 +- docs/actions/action_set.md | 2 +- docs/actions/introduction.md | 13 +- docs/getstarted/install.md | 20 +- docs/numbers/instantiate.md | 8 +- docs/numbers/lexer.md | 2 +- .../nathanfallet/makth/actions/ForAction.kt | 15 +- .../me/nathanfallet/makth/actions/IfAction.kt | 16 +- .../nathanfallet/makth/actions/PrintAction.kt | 10 +- .../nathanfallet/makth/actions/SetAction.kt | 12 +- .../nathanfallet/makth/actions/WhileAction.kt | 13 +- .../makth/exceptions/ExecutionException.kt | 18 + .../makth/exceptions/NotABooleanException.kt | 22 ++ .../makth/exceptions/NotIterableException.kt | 22 ++ .../exceptions/UnknownVariablesException.kt | 22 ++ .../{BooleanExtension.kt => BooleanValue.kt} | 4 +- .../{StringExtension.kt => StringValue.kt} | 4 +- .../nathanfallet/makth/interfaces/Action.kt | 65 +--- .../nathanfallet/makth/interfaces/Iterable.kt | 3 + .../nathanfallet/makth/interfaces/Output.kt | 3 + .../me/nathanfallet/makth/interfaces/Value.kt | 6 +- .../makth/lexers/AlgorithmLexer.kt | 2 + .../me/nathanfallet/makth/lexers/MathLexer.kt | 19 +- .../me/nathanfallet/makth/numbers/Natural.kt | 29 -- .../makth/numbers/{ => integers}/Integer.kt | 78 +++-- .../makth/numbers/integers/IntegerFactory.kt | 18 + .../numbers/{ => integers}/IntegerImpl.kt | 2 +- .../makth/numbers/naturals/Natural.kt | 19 ++ .../makth/numbers/naturals/NaturalFactory.kt | 17 + .../numbers/{ => naturals}/NaturalImpl.kt | 2 +- .../makth/numbers/{ => rationals}/Rational.kt | 105 ++---- .../numbers/rationals/RationalFactory.kt | 77 +++++ .../numbers/{ => rationals}/RationalImpl.kt | 5 +- .../makth/numbers/{ => reals}/Real.kt | 63 +--- .../makth/numbers/reals/RealFactory.kt | 36 ++ .../makth/numbers/{ => reals}/RealImpl.kt | 2 +- .../makth/numbers/{ => reals}/RealImplPi.kt | 2 +- .../nathanfallet/makth/operations/Equality.kt | 6 +- .../makth/operations/Exponentiation.kt | 6 +- .../makth/operations/Operation.kt | 84 +---- .../makth/operations/OperationFactory.kt | 90 +++++ .../nathanfallet/makth/operations/Product.kt | 6 +- .../nathanfallet/makth/operations/Quotient.kt | 6 +- .../makth/operations/Remainder.kt | 6 +- .../me/nathanfallet/makth/operations/Sum.kt | 6 +- .../nathanfallet/makth/resolvables/Context.kt | 10 +- .../makth/resolvables/Variable.kt | 59 ---- .../makth/resolvables/variables/Variable.kt | 42 +++ .../resolvables/variables/VariableFactory.kt | 30 ++ .../resolvables/variables/VariableImpl.kt | 5 + .../makth/sets/{ => matrixes}/Matrix.kt | 42 +-- .../makth/sets/matrixes/MatrixFactory.kt | 30 ++ .../makth/sets/{ => matrixes}/MatrixImpl.kt | 2 +- .../makth/sets/{ => vectors}/Vector.kt | 33 +- .../makth/sets/vectors/VectorFactory.kt | 21 ++ .../makth/sets/{ => vectors}/VectorImpl.kt | 2 +- .../makth/actions/ForActionTest.kt | 69 ++-- .../makth/actions/IfActionTest.kt | 42 +-- .../makth/actions/PrintActionTest.kt | 20 +- .../makth/actions/SetActionTest.kt | 32 +- .../makth/actions/WhileActionTest.kt | 39 ++- ...anExtensionTest.kt => BooleanValueTest.kt} | 2 +- ...ingExtensionTest.kt => StringValueTest.kt} | 2 +- .../makth/lexers/AlgorithmLexerTest.kt | 46 +-- .../makth/lexers/MathLexerTest.kt | 116 ++++--- .../nathanfallet/makth/numbers/IntegerTest.kt | 218 ------------ .../nathanfallet/makth/numbers/NaturalTest.kt | 220 ------------- .../makth/numbers/RationalTest.kt | 252 -------------- .../me/nathanfallet/makth/numbers/RealTest.kt | 300 ----------------- .../makth/numbers/integers/IntegerTest.kt | 224 +++++++++++++ .../makth/numbers/naturals/NaturalTest.kt | 227 +++++++++++++ .../makth/numbers/rationals/RationalTest.kt | 258 +++++++++++++++ .../makth/numbers/reals/RealTest.kt | 311 ++++++++++++++++++ .../makth/operations/EqualityTest.kt | 190 +++++++---- .../makth/operations/ExponentiationTest.kt | 26 +- .../makth/operations/OperationsTest.kt | 26 +- .../makth/operations/ProductTest.kt | 30 +- .../makth/operations/QuotientTest.kt | 26 +- .../makth/operations/RemainderTest.kt | 26 +- .../nathanfallet/makth/operations/SumTest.kt | 50 +-- .../makth/resolvables/VariableTest.kt | 42 --- .../resolvables/variables/VariableTest.kt | 45 +++ .../me/nathanfallet/makth/sets/MatrixTest.kt | 187 ----------- .../me/nathanfallet/makth/sets/VectorTest.kt | 107 ------ .../makth/sets/matrixes/MatrixTest.kt | 243 ++++++++++++++ .../makth/sets/vectors/VectorTest.kt | 194 +++++++++++ 86 files changed, 2669 insertions(+), 2150 deletions(-) create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/exceptions/ExecutionException.kt create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/exceptions/NotABooleanException.kt create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/exceptions/NotIterableException.kt create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/exceptions/UnknownVariablesException.kt rename src/commonMain/kotlin/me/nathanfallet/makth/extensions/{BooleanExtension.kt => BooleanValue.kt} (89%) rename src/commonMain/kotlin/me/nathanfallet/makth/extensions/{StringExtension.kt => StringValue.kt} (92%) delete mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/numbers/Natural.kt rename src/commonMain/kotlin/me/nathanfallet/makth/numbers/{ => integers}/Integer.kt (62%) create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/numbers/integers/IntegerFactory.kt rename src/commonMain/kotlin/me/nathanfallet/makth/numbers/{ => integers}/IntegerImpl.kt (62%) create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/Natural.kt create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalFactory.kt rename src/commonMain/kotlin/me/nathanfallet/makth/numbers/{ => naturals}/NaturalImpl.kt (82%) rename src/commonMain/kotlin/me/nathanfallet/makth/numbers/{ => rationals}/Rational.kt (56%) create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/numbers/rationals/RationalFactory.kt rename src/commonMain/kotlin/me/nathanfallet/makth/numbers/{ => rationals}/RationalImpl.kt (63%) rename src/commonMain/kotlin/me/nathanfallet/makth/numbers/{ => reals}/Real.kt (56%) create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/RealFactory.kt rename src/commonMain/kotlin/me/nathanfallet/makth/numbers/{ => reals}/RealImpl.kt (63%) rename src/commonMain/kotlin/me/nathanfallet/makth/numbers/{ => reals}/RealImplPi.kt (86%) create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/operations/OperationFactory.kt delete mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/resolvables/Variable.kt create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/Variable.kt create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/VariableFactory.kt create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/VariableImpl.kt rename src/commonMain/kotlin/me/nathanfallet/makth/sets/{ => matrixes}/Matrix.kt (69%) create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixFactory.kt rename src/commonMain/kotlin/me/nathanfallet/makth/sets/{ => matrixes}/MatrixImpl.kt (86%) rename src/commonMain/kotlin/me/nathanfallet/makth/sets/{ => vectors}/Vector.kt (67%) create mode 100644 src/commonMain/kotlin/me/nathanfallet/makth/sets/vectors/VectorFactory.kt rename src/commonMain/kotlin/me/nathanfallet/makth/sets/{ => vectors}/VectorImpl.kt (75%) rename src/commonTest/kotlin/me/nathanfallet/makth/extensions/{BooleanExtensionTest.kt => BooleanValueTest.kt} (97%) rename src/commonTest/kotlin/me/nathanfallet/makth/extensions/{StringExtensionTest.kt => StringValueTest.kt} (98%) delete mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/numbers/IntegerTest.kt delete mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/numbers/NaturalTest.kt delete mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/numbers/RationalTest.kt delete mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/numbers/RealTest.kt create mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/numbers/integers/IntegerTest.kt create mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalTest.kt create mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/numbers/rationals/RationalTest.kt create mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/numbers/reals/RealTest.kt delete mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/resolvables/VariableTest.kt create mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/resolvables/variables/VariableTest.kt delete mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/sets/MatrixTest.kt delete mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/sets/VectorTest.kt create mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixTest.kt create mode 100644 src/commonTest/kotlin/me/nathanfallet/makth/sets/vectors/VectorTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 8a2dccd..66db781 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "me.nathanfallet.makth" -version = "1.2.1" +version = "1.2.2" repositories { mavenCentral() @@ -41,6 +41,11 @@ kotlin { sourceSets { + all { + languageSettings.apply { + optIn("kotlin.js.ExperimentalJsExport") + } + } val commonMain by getting val commonTest by getting { dependencies { diff --git a/docs/actions/action_set.md b/docs/actions/action_set.md index 7b7eddc..fefbf00 100644 --- a/docs/actions/action_set.md +++ b/docs/actions/action_set.md @@ -9,7 +9,7 @@ SetAction("name", value) Or ```kotlin -SetAction(Variable.instantiate("name"), value) +SetAction(VariableFactory.instantiate("name"), value) ``` ## Makth lexer syntax diff --git a/docs/actions/introduction.md b/docs/actions/introduction.md index 1770fd8..e3f70a5 100644 --- a/docs/actions/introduction.md +++ b/docs/actions/introduction.md @@ -4,11 +4,12 @@ You can execute actions to play with a context: ```kotlin val result = Context().execute(listOf( - SetAction("x", Integer.instantiate(2)), - WhileAction(Equality(Variable.instantiate("x"), Integer.instantiate(10), Equality.Operator.LessThan), listOf( - SetAction("x", Sum(Variable.instantiate("x"), Integer.instantiate(1))) + SetAction("x", IntegerFactory.instantiate(2)), + WhileAction( + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(10), Equality.Operator.LessThan), listOf( + SetAction("x", Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(1))) )), - PrintAction(listOf(StringValue("x = "), Variable.instantiate("x"))) + PrintAction(listOf(StringValue("x = "), VariableFactory.instantiate("x"))) )) ``` @@ -17,10 +18,10 @@ val result = Context().execute(listOf( ```kotlin Context( mapOf( - "x" to Integer.instantiate(10) // The value of x + "x" to IntegerFactory.instantiate(10) // The value of x ), listOf( - StringValue("x = "), Integer.instantiate(10), StringValue("\n") // What we printed + StringValue("x = "), IntegerFactory.instantiate(10), StringValue("\n") // What we printed ) ) ``` \ No newline at end of file diff --git a/docs/getstarted/install.md b/docs/getstarted/install.md index 3848ae0..bcc2538 100644 --- a/docs/getstarted/install.md +++ b/docs/getstarted/install.md @@ -8,7 +8,7 @@ Add the following to your `pom.xml` file; me.nathanfallet.makth makth-jvm - 1.2.1 + 1.2.2 ``` @@ -22,6 +22,22 @@ repositories { } dependencies { - implementation 'me.nathanfallet.makth:makth:1.2.1' + implementation 'me.nathanfallet.makth:makth:1.2.2' } ``` + +## npm + +Install the package using npm: + +```bash +npm install makth +``` + +## yarn + +Install the package using yarn: + +```bash +yarn add makth +``` diff --git a/docs/numbers/instantiate.md b/docs/numbers/instantiate.md index 40781ef..7e9a063 100644 --- a/docs/numbers/instantiate.md +++ b/docs/numbers/instantiate.md @@ -1,13 +1,13 @@ # Instantiate numbers ```kotlin -val integer = Integer.instantiate(2) // 2 -val rational = Rational.instantiate(3, 2) // 3/2 -val real = Real.instantiate(1.23456789) // 1.23456789 +val integer = IntegerFactory.instantiate(2) // 2 +val rational = RationalFactory.instantiate(3, 2) // 3/2 +val real = RealFactory.instantiate(1.23456789) // 1.23456789 ``` ## Predefined constants ```kotlin -val pi = Real.pi +val pi = RealFactory.pi ``` \ No newline at end of file diff --git a/docs/numbers/lexer.md b/docs/numbers/lexer.md index 8f03eb3..04efd35 100644 --- a/docs/numbers/lexer.md +++ b/docs/numbers/lexer.md @@ -16,7 +16,7 @@ Thanks to the context, you can pass variables: ```kotlin val context = Context(mapOf( - "x" to Integer.instantiate(2) + "x" to IntegerFactory.instantiate(2) )) val result = MathLexer("x + 3").execute(context) // 5 ``` \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/actions/ForAction.kt b/src/commonMain/kotlin/me/nathanfallet/makth/actions/ForAction.kt index 62eede9..a5f51cc 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/actions/ForAction.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/actions/ForAction.kt @@ -1,5 +1,8 @@ package me.nathanfallet.makth.actions +import me.nathanfallet.makth.exceptions.ExecutionException +import me.nathanfallet.makth.exceptions.NotIterableException +import me.nathanfallet.makth.exceptions.UnknownVariablesException import me.nathanfallet.makth.extensions.StringValue import me.nathanfallet.makth.extensions.indentedLines import me.nathanfallet.makth.interfaces.Action @@ -8,7 +11,9 @@ import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentTypeException import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport +import kotlin.jvm.JvmStatic /** * Action that executes a list of actions for a given set or interval. @@ -16,6 +21,7 @@ import me.nathanfallet.makth.resolvables.Variable * @param iterable Iterable to iterate during the loop * @param actions Actions to execute while condition is true */ +@JsExport data class ForAction(val identifier: String, val iterable: Value, val actions: List) : Action { companion object { @@ -25,6 +31,7 @@ data class ForAction(val identifier: String, val iterable: Value, val actions: L * @param args List of arguments * @return Action created from arguments */ + @JvmStatic fun handler(args: List): Action { if (args.count() != 2) { throw IncorrectArgumentCountException("for", args.count(), 2) @@ -38,19 +45,19 @@ data class ForAction(val identifier: String, val iterable: Value, val actions: L } } - @Throws(Action.ExecutionException::class) + @Throws(ExecutionException::class) override fun execute(context: Context): Context { // Eval iterator val evaluatedIterable = iterable.compute(context) // Check if there are missing variables evaluatedIterable.variables.takeIf { it.isNotEmpty() }?.let { - throw Action.UnknownVariablesException(this, context, it) + throw UnknownVariablesException(this, context, it) } // Check if condition is a boolean if (evaluatedIterable !is Iterable) { - throw Action.NotIterableException(this, context, evaluatedIterable) + throw NotIterableException(this, context, evaluatedIterable) } // Iterate diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/actions/IfAction.kt b/src/commonMain/kotlin/me/nathanfallet/makth/actions/IfAction.kt index dd560ce..0e39994 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/actions/IfAction.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/actions/IfAction.kt @@ -1,11 +1,17 @@ package me.nathanfallet.makth.actions +import me.nathanfallet.makth.exceptions.ExecutionException +import me.nathanfallet.makth.exceptions.NotABooleanException +import me.nathanfallet.makth.exceptions.UnknownVariablesException import me.nathanfallet.makth.extensions.BooleanValue import me.nathanfallet.makth.extensions.indentedLines import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException import me.nathanfallet.makth.resolvables.Context +import kotlin.js.JsExport +import kotlin.jvm.JvmOverloads +import kotlin.jvm.JvmStatic /** * Action that executes a list of actions if a condition is true, @@ -14,7 +20,8 @@ import me.nathanfallet.makth.resolvables.Context * @param actions Actions to execute if condition is true * @param elseActions Actions to execute if condition is false */ -data class IfAction( +@JsExport +data class IfAction @JvmOverloads constructor( val condition: Value, val actions: List, val elseActions: List = listOf() @@ -27,6 +34,7 @@ data class IfAction( * @param args List of arguments * @return Action created from arguments */ + @JvmStatic fun handler(args: List): Action { if (args.count() != 1) { throw IncorrectArgumentCountException("if", args.count(), 1) @@ -35,19 +43,19 @@ data class IfAction( } } - @Throws(Action.ExecutionException::class) + @Throws(ExecutionException::class) override fun execute(context: Context): Context { // Eval condition val evaluatedCondition = condition.compute(context) // Check if there are missing variables evaluatedCondition.variables.takeIf { it.isNotEmpty() }?.let { - throw Action.UnknownVariablesException(this, context, it) + throw UnknownVariablesException(this, context, it) } // Check if condition is a boolean if (evaluatedCondition !is BooleanValue) { - throw Action.NotABooleanException(this, context, evaluatedCondition) + throw NotABooleanException(this, context, evaluatedCondition) } // Execute if it is true diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/actions/PrintAction.kt b/src/commonMain/kotlin/me/nathanfallet/makth/actions/PrintAction.kt index b811830..7b0fb96 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/actions/PrintAction.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/actions/PrintAction.kt @@ -1,14 +1,19 @@ package me.nathanfallet.makth.actions +import me.nathanfallet.makth.exceptions.ExecutionException +import me.nathanfallet.makth.exceptions.UnknownVariablesException import me.nathanfallet.makth.extensions.StringValue import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context +import kotlin.js.JsExport +import kotlin.jvm.JvmStatic /** * Action that prints values * @param values Values to print */ +@JsExport data class PrintAction(val values: List) : Action { companion object { @@ -18,12 +23,13 @@ data class PrintAction(val values: List) : Action { * @param args Arguments of the action * @return Action created from the arguments */ + @JvmStatic fun handler(args: List): Action { return PrintAction(args.toList()) } } - @Throws(Action.ExecutionException::class) + @Throws(ExecutionException::class) override fun execute(context: Context): Context { // Generate output val output = @@ -33,7 +39,7 @@ data class PrintAction(val values: List) : Action { // Check for missing variables computed.variables.takeIf { it.isNotEmpty() }?.let { - throw Action.UnknownVariablesException(this, context, it) + throw UnknownVariablesException(this, context, it) } // Return diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/actions/SetAction.kt b/src/commonMain/kotlin/me/nathanfallet/makth/actions/SetAction.kt index d4201e9..edd27dd 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/actions/SetAction.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/actions/SetAction.kt @@ -1,18 +1,23 @@ package me.nathanfallet.makth.actions +import me.nathanfallet.makth.exceptions.ExecutionException +import me.nathanfallet.makth.exceptions.UnknownVariablesException import me.nathanfallet.makth.extensions.StringValue import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentTypeException import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport +import kotlin.jvm.JvmStatic /** * Action that sets a variable * @param identifier Identifier of the variable to set * @param value Value to set */ +@JsExport data class SetAction(val identifier: String, val value: Value) : Action { companion object { @@ -22,6 +27,7 @@ data class SetAction(val identifier: String, val value: Value) : Action { * @param args Arguments of the action * @return Action created from the arguments */ + @JvmStatic fun handler(args: List): Action { if (args.count() != 2) { throw IncorrectArgumentCountException("set", args.count(), 2) @@ -35,14 +41,14 @@ data class SetAction(val identifier: String, val value: Value) : Action { } } - @Throws(Action.ExecutionException::class) + @Throws(ExecutionException::class) override fun execute(context: Context): Context { // First, compute the value with the given context val valueToSet = value.compute(context) // Check if there are missing variables valueToSet.variables.takeIf { it.isNotEmpty() }?.let { - throw Action.UnknownVariablesException(this, context, it) + throw UnknownVariablesException(this, context, it) } // Return the new context diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/actions/WhileAction.kt b/src/commonMain/kotlin/me/nathanfallet/makth/actions/WhileAction.kt index 316fe18..271fc0b 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/actions/WhileAction.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/actions/WhileAction.kt @@ -1,17 +1,23 @@ package me.nathanfallet.makth.actions +import me.nathanfallet.makth.exceptions.ExecutionException +import me.nathanfallet.makth.exceptions.NotABooleanException +import me.nathanfallet.makth.exceptions.UnknownVariablesException import me.nathanfallet.makth.extensions.BooleanValue import me.nathanfallet.makth.extensions.indentedLines import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException import me.nathanfallet.makth.resolvables.Context +import kotlin.js.JsExport +import kotlin.jvm.JvmStatic /** * Action that executes a list of actions while a condition is true. * @param condition Condition to check * @param actions Actions to execute while condition is true */ +@JsExport data class WhileAction(val condition: Value, val actions: List) : Action { companion object { @@ -21,6 +27,7 @@ data class WhileAction(val condition: Value, val actions: List) : Action * @param args List of arguments * @return Action created from arguments */ + @JvmStatic fun handler(args: List): Action { if (args.count() != 1) { throw IncorrectArgumentCountException("while", args.count(), 1) @@ -29,19 +36,19 @@ data class WhileAction(val condition: Value, val actions: List) : Action } } - @Throws(Action.ExecutionException::class) + @Throws(ExecutionException::class) override fun execute(context: Context): Context { // Eval condition val evaluatedCondition = condition.compute(context) // Check if there are missing variables evaluatedCondition.variables.takeIf { it.isNotEmpty() }?.let { - throw Action.UnknownVariablesException(this, context, it) + throw UnknownVariablesException(this, context, it) } // Check if condition is a boolean if (evaluatedCondition !is BooleanValue) { - throw Action.NotABooleanException(this, context, evaluatedCondition) + throw NotABooleanException(this, context, evaluatedCondition) } // Execute if it is true diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/ExecutionException.kt b/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/ExecutionException.kt new file mode 100644 index 0000000..7f7ccce --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/ExecutionException.kt @@ -0,0 +1,18 @@ +package me.nathanfallet.makth.exceptions + +import me.nathanfallet.makth.interfaces.Action +import me.nathanfallet.makth.resolvables.Context +import kotlin.js.JsExport + +/** + * Base class for all execution errors + * @param action Action that failed + * @param context Context of the action + * @param message Error message + */ +@JsExport +open class ExecutionException( + val action: Action, + val context: Context, + message: String +) : Exception(message) diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/NotABooleanException.kt b/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/NotABooleanException.kt new file mode 100644 index 0000000..81f478c --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/NotABooleanException.kt @@ -0,0 +1,22 @@ +package me.nathanfallet.makth.exceptions + +import me.nathanfallet.makth.interfaces.Action +import me.nathanfallet.makth.interfaces.Value +import me.nathanfallet.makth.resolvables.Context +import kotlin.js.JsExport + +/** + * Exception thrown when a variable is not a boolean + * @param action Action that failed + * @param context Context of the action + * @param value Value that is not a boolean + */ +@JsExport +open class NotABooleanException( + action: Action, + context: Context, + val value: Value +) : ExecutionException( + action, context, + "Value is not a boolean: ${value.rawString}" +) \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/NotIterableException.kt b/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/NotIterableException.kt new file mode 100644 index 0000000..419dc09 --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/NotIterableException.kt @@ -0,0 +1,22 @@ +package me.nathanfallet.makth.exceptions + +import me.nathanfallet.makth.interfaces.Action +import me.nathanfallet.makth.interfaces.Value +import me.nathanfallet.makth.resolvables.Context +import kotlin.js.JsExport + +/** + * Exception thrown when a variable is not iterable + * @param action Action that failed + * @param context Context of the action + * @param value Value that is not iterable + */ +@JsExport +open class NotIterableException( + action: Action, + context: Context, + val value: Value +) : ExecutionException( + action, context, + "Value is not iterable: ${value.rawString}" +) \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/UnknownVariablesException.kt b/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/UnknownVariablesException.kt new file mode 100644 index 0000000..5bc107b --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/exceptions/UnknownVariablesException.kt @@ -0,0 +1,22 @@ +package me.nathanfallet.makth.exceptions + +import me.nathanfallet.makth.interfaces.Action +import me.nathanfallet.makth.resolvables.Context +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport + +/** + * Exception thrown when one (or more) variable is not found + * @param action Action that failed + * @param context Context of the action + * @param variable Variables that were not found + */ +@JsExport +open class UnknownVariablesException( + action: Action, + context: Context, + val variables: Set +) : ExecutionException( + action, context, + "Unknown variable(s): ${variables.joinToString(", ") { it.name }}" +) \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt b/src/commonMain/kotlin/me/nathanfallet/makth/extensions/BooleanValue.kt similarity index 89% rename from src/commonMain/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/extensions/BooleanValue.kt index 430c6ff..3d9b2c6 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/extensions/BooleanValue.kt @@ -2,12 +2,14 @@ package me.nathanfallet.makth.extensions import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport /** * Boolean value * @param value Boolean value */ +@JsExport data class BooleanValue(val value: Boolean) : Value { override fun compute(context: Context): Value { diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt b/src/commonMain/kotlin/me/nathanfallet/makth/extensions/StringValue.kt similarity index 92% rename from src/commonMain/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/extensions/StringValue.kt index 97102b3..4b0d357 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/extensions/StringValue.kt @@ -2,12 +2,14 @@ package me.nathanfallet.makth.extensions import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport /** * String value * @param value String value */ +@JsExport data class StringValue(val value: String, val latex: Boolean = false) : Value { override fun compute(context: Context): Value { diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Action.kt b/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Action.kt index b0b2b79..3f930e8 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Action.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Action.kt @@ -1,74 +1,15 @@ package me.nathanfallet.makth.interfaces +import me.nathanfallet.makth.exceptions.ExecutionException import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import kotlin.js.JsExport /** * Interface for all actions that can be executed */ +@JsExport interface Action { - // Errors - - /** - * Base class for all execution errors - * @param action Action that failed - * @param context Context of the action - * @param message Error message - */ - open class ExecutionException( - val action: Action, - val context: Context, - message: String - ) : Exception(message) - - /** - * Exception thrown when one (or more) variable is not found - * @param action Action that failed - * @param context Context of the action - * @param variable Variables that were not found - */ - open class UnknownVariablesException( - action: Action, - context: Context, - val variables: Set - ) : ExecutionException( - action, context, - "Unknown variable(s): ${variables.joinToString(", ") { it.name }}" - ) - - /** - * Exception thrown when a variable is not a boolean - * @param action Action that failed - * @param context Context of the action - * @param value Value that is not a boolean - */ - open class NotABooleanException( - action: Action, - context: Context, - val value: Value - ) : ExecutionException( - action, context, - "Value is not a boolean: ${value.rawString}" - ) - - /** - * Exception thrown when a variable is not iterable - * @param action Action that failed - * @param context Context of the action - * @param value Value that is not iterable - */ - open class NotIterableException( - action: Action, - context: Context, - val value: Value - ) : ExecutionException( - action, context, - "Value is not iterable: ${value.rawString}" - ) - - // Interface - /** * Execute the action in the given context * @param context Context of the action diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Iterable.kt b/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Iterable.kt index 063c87d..9703436 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Iterable.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Iterable.kt @@ -1,8 +1,11 @@ package me.nathanfallet.makth.interfaces +import kotlin.js.JsExport + /** * Interface for iterable values */ +@JsExport interface Iterable: Value { /** diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Output.kt b/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Output.kt index 030e37a..bb2b6a5 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Output.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Output.kt @@ -1,6 +1,9 @@ package me.nathanfallet.makth.interfaces +import kotlin.js.JsExport + /** * Interface for all outputs of actions */ +@JsExport interface Output \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Value.kt b/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Value.kt index 8349feb..560026a 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Value.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/interfaces/Value.kt @@ -1,11 +1,14 @@ package me.nathanfallet.makth.interfaces import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport +import kotlin.js.JsName /** * Interface for all values that can be computed */ +@JsExport interface Value: Output { /** @@ -49,6 +52,7 @@ interface Value: Output { * @param right Right value * @return True if equals, false otherwise */ + @JsName("equalsValue") fun equals(right: Value): Boolean { throw UnsupportedOperationException() } diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexer.kt b/src/commonMain/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexer.kt index 71be438..6bce2c1 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexer.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexer.kt @@ -7,12 +7,14 @@ import me.nathanfallet.makth.actions.WhileAction import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context +import kotlin.js.JsExport import kotlin.reflect.KClass /** * A lexer for algorithms * @param content Content of the algorithm */ +@JsExport class AlgorithmLexer(private var content: String) { // Errors diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt b/src/commonMain/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt index 146ffef..60c33ed 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt @@ -3,15 +3,17 @@ package me.nathanfallet.makth.lexers import me.nathanfallet.makth.extensions.StringValue import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.lexers.AlgorithmLexer.SyntaxException -import me.nathanfallet.makth.numbers.Integer -import me.nathanfallet.makth.operations.Operation +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.operations.OperationFactory import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory +import kotlin.js.JsExport /** * Lexer for math expressions * @param content Content to parse */ +@JsExport class MathLexer(private var content: String) { // Errors @@ -162,7 +164,7 @@ class MathLexer(private var content: String) { TODO("Power not implemented!") // insertValue(Fraction(Number(`val`), Power(Number(10), Number(powerOfTen)))) } else { - insertValue(Integer.instantiate(value)) + insertValue(IntegerFactory.instantiate(value)) } // Remove one, else current character is skipped @@ -182,7 +184,7 @@ class MathLexer(private var content: String) { } // Insert into values - insertValue(Variable.instantiate(name.toString())) + insertValue(VariableFactory.instantiate(name.toString())) } // Utils for parsing @@ -197,7 +199,8 @@ class MathLexer(private var content: String) { // While first operation has same of greater precedence to current, apply to two first // values while (operators.isNotEmpty() && - Operation.Utils.getPrecedence(operators[0]) >= Operation.Utils.getPrecedence(op)) { + OperationFactory.getPrecedence(operators[0]) >= OperationFactory.getPrecedence(op) + ) { // Create a token val value = createValue() if (value != null) { @@ -208,7 +211,7 @@ class MathLexer(private var content: String) { // If subtraction with no number before if (op == "-" && values.isEmpty()) { - insertValue(Integer.instantiate(0)) + insertValue(IntegerFactory.instantiate(0)) } // If next is "=" @@ -233,7 +236,7 @@ class MathLexer(private var content: String) { // Get operator and apply val op = getFirstOperationAndRemove() return if (op != null) { - Operation.Utils.initialize(op, left, right) + OperationFactory.initialize(op, left, right) } else null } diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/Natural.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/Natural.kt deleted file mode 100644 index 8357f4c..0000000 --- a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/Natural.kt +++ /dev/null @@ -1,29 +0,0 @@ -package me.nathanfallet.makth.numbers - -/** - * Natural representation - */ -interface Natural : Integer { - - // Instantiate - - companion object { - - /** - * Instantiate a natural from a long value - * @param value Long value - * @return Natural - */ - fun instantiate(value: Long): Natural { - return NaturalImpl(value) - } - - } - - // Real - - override val absoluteValue: Natural get() { - return this - } - -} \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/Integer.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/integers/Integer.kt similarity index 62% rename from src/commonMain/kotlin/me/nathanfallet/makth/numbers/Integer.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/numbers/integers/Integer.kt index a462269..1fd7680 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/Integer.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/integers/Integer.kt @@ -1,31 +1,26 @@ -package me.nathanfallet.makth.numbers +package me.nathanfallet.makth.numbers.integers import me.nathanfallet.makth.extensions.nthRoot import me.nathanfallet.makth.extensions.pow import me.nathanfallet.makth.interfaces.Value -import me.nathanfallet.makth.sets.Matrix +import me.nathanfallet.makth.numbers.naturals.Natural +import me.nathanfallet.makth.numbers.naturals.NaturalFactory +import me.nathanfallet.makth.numbers.rationals.Rational +import me.nathanfallet.makth.numbers.rationals.RationalFactory +import me.nathanfallet.makth.numbers.reals.Real +import me.nathanfallet.makth.numbers.reals.RealFactory +import me.nathanfallet.makth.numbers.reals.RealImpl +import me.nathanfallet.makth.sets.matrixes.Matrix +import me.nathanfallet.makth.sets.matrixes.MatrixFactory +import kotlin.js.JsExport import kotlin.math.abs /** * Integer representation */ +@JsExport interface Integer : Rational { - // Instantiate - - companion object { - - /** - * Instantiate an integer from a long value - * @param value Long value - * @return Integer - */ - fun instantiate(value: Long): Integer { - return if (value < 0) IntegerImpl(value) else Natural.instantiate(value) - } - - } - // Integer interface /** @@ -35,12 +30,14 @@ interface Integer : Rational { // Rational - override val numerator: Integer get() { + override val numerator: Integer + get() { return this } - override val denominator: Natural get() { - return Natural.instantiate(1) + override val denominator: Natural + get() { + return NaturalFactory.instantiate(1) } // Real @@ -49,8 +46,9 @@ interface Integer : Rational { return longValue.toDouble() } - override val absoluteValue: Natural get() { - return Natural.instantiate(abs(longValue)) + override val absoluteValue: Natural + get() { + return NaturalFactory.instantiate(abs(longValue)) } // Value @@ -67,13 +65,13 @@ interface Integer : Rational { override fun sum(right: Value): Value { if (right is Integer) { - return instantiate(longValue + right.longValue) + return IntegerFactory.instantiate(longValue + right.longValue) } if (right is Rational) { val newNumerator = this.multiply(right.denominator) .sum(right.numerator) if (newNumerator is Integer) { - return Rational.instantiate(newNumerator, right.denominator) + return RationalFactory.instantiate(newNumerator, right.denominator) } } return super.sum(right) @@ -81,12 +79,12 @@ interface Integer : Rational { override fun multiply(right: Value): Value { if (right is Integer) { - return instantiate(longValue * right.longValue) + return IntegerFactory.instantiate(longValue * right.longValue) } if (right is Rational) { val newNumerator = this.multiply(right.numerator) if (newNumerator is Integer) { - return Rational.instantiate( + return RationalFactory.instantiate( newNumerator, right.denominator ) @@ -96,7 +94,7 @@ interface Integer : Rational { return RealImpl(doubleValue).multiply(right) } if (right is Matrix) { - return Matrix.instantiate(right.rows.map { rows -> + return MatrixFactory.instantiate(right.rows.map { rows -> rows.map { multiply(it) } }) } @@ -105,7 +103,7 @@ interface Integer : Rational { override fun divide(right: Value): Value { if (right is Integer) { - return Rational.instantiate( + return RationalFactory.instantiate( this, right ) @@ -113,7 +111,7 @@ interface Integer : Rational { if (right is Rational) { val newNumerator = this.multiply(right.denominator) if (newNumerator is Integer) { - return Rational.instantiate( + return RationalFactory.instantiate( newNumerator, right.numerator ) @@ -124,12 +122,12 @@ interface Integer : Rational { override fun remainder(right: Value): Value { if (right is Integer) { - return instantiate(longValue % right.longValue) + return IntegerFactory.instantiate(longValue % right.longValue) } if (right is Rational) { val newNumerator = numerator.multiply(right.denominator).remainder(right.numerator) if (newNumerator is Integer) { - return Rational.instantiate( + return RationalFactory.instantiate( newNumerator, right.denominator ) @@ -141,30 +139,30 @@ interface Integer : Rational { override fun raise(right: Value): Value { if (right is Integer) { return if (right.longValue < 0) { - Rational.instantiate( - instantiate(1), - instantiate(longValue.pow(-right.longValue).toLong()) + RationalFactory.instantiate( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(longValue.pow(-right.longValue)) ) } else { - instantiate(longValue.pow(right.longValue).toLong()) + IntegerFactory.instantiate(longValue.pow(right.longValue)) } } if (right is Rational) { val firstRaised = raise(right.numerator) if (firstRaised is Integer) { return if (firstRaised.doubleValue < 0) { - // In case of negative number, we need to take the oppposite + // In case of negative number, we need to take the opposite // and check if the denominator is even (to avoid complex case). if (right.denominator.longValue and 1 == 0L) { - Real.instantiate(Double.NaN) + RealFactory.instantiate(Double.NaN) } else { - Real.instantiate( + RealFactory.instantiate( -(-firstRaised.longValue) - .nthRoot(right.denominator.longValue) + .nthRoot(right.denominator.longValue) ) } } else { - Real.instantiate(firstRaised.longValue.nthRoot(right.denominator.longValue)) + RealFactory.instantiate(firstRaised.longValue.nthRoot(right.denominator.longValue)) } } } diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/integers/IntegerFactory.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/integers/IntegerFactory.kt new file mode 100644 index 0000000..3ee0aea --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/integers/IntegerFactory.kt @@ -0,0 +1,18 @@ +package me.nathanfallet.makth.numbers.integers + +import me.nathanfallet.makth.numbers.naturals.NaturalFactory +import kotlin.js.JsExport + +@JsExport +object IntegerFactory { + + /** + * Instantiate an integer from a long value + * @param value Long value + * @return Integer + */ + fun instantiate(value: Long): Integer { + return if (value < 0) IntegerImpl(value) else NaturalFactory.instantiate(value) + } + +} \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/IntegerImpl.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/integers/IntegerImpl.kt similarity index 62% rename from src/commonMain/kotlin/me/nathanfallet/makth/numbers/IntegerImpl.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/numbers/integers/IntegerImpl.kt index ad66e36..a46a0c3 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/IntegerImpl.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/integers/IntegerImpl.kt @@ -1,4 +1,4 @@ -package me.nathanfallet.makth.numbers +package me.nathanfallet.makth.numbers.integers internal data class IntegerImpl( override val longValue: Long diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/Natural.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/Natural.kt new file mode 100644 index 0000000..99a0a51 --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/Natural.kt @@ -0,0 +1,19 @@ +package me.nathanfallet.makth.numbers.naturals + +import me.nathanfallet.makth.numbers.integers.Integer +import kotlin.js.JsExport + +/** + * Natural representation + */ +@JsExport +interface Natural : Integer { + + // Real + + override val absoluteValue: Natural + get() { + return this + } + +} \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalFactory.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalFactory.kt new file mode 100644 index 0000000..2d555f7 --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalFactory.kt @@ -0,0 +1,17 @@ +package me.nathanfallet.makth.numbers.naturals + +import kotlin.js.JsExport + +@JsExport +object NaturalFactory { + + /** + * Instantiate a natural from a long value + * @param value Long value + * @return Natural + */ + fun instantiate(value: Long): Natural { + return NaturalImpl(value) + } + +} \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/NaturalImpl.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalImpl.kt similarity index 82% rename from src/commonMain/kotlin/me/nathanfallet/makth/numbers/NaturalImpl.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalImpl.kt index 6a6265a..ad9f431 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/NaturalImpl.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalImpl.kt @@ -1,4 +1,4 @@ -package me.nathanfallet.makth.numbers +package me.nathanfallet.makth.numbers.naturals internal data class NaturalImpl( override val longValue: Long diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/Rational.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/rationals/Rational.kt similarity index 56% rename from src/commonMain/kotlin/me/nathanfallet/makth/numbers/Rational.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/numbers/rationals/Rational.kt index 40f1687..06e7a6b 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/Rational.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/rationals/Rational.kt @@ -1,81 +1,21 @@ -package me.nathanfallet.makth.numbers +package me.nathanfallet.makth.numbers.rationals -import me.nathanfallet.makth.extensions.gcd import me.nathanfallet.makth.interfaces.Value -import me.nathanfallet.makth.sets.Matrix +import me.nathanfallet.makth.numbers.integers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.numbers.naturals.Natural +import me.nathanfallet.makth.numbers.reals.Real +import me.nathanfallet.makth.numbers.reals.RealImpl +import me.nathanfallet.makth.sets.matrixes.Matrix +import me.nathanfallet.makth.sets.matrixes.MatrixFactory +import kotlin.js.JsExport /** * Rational representation */ +@JsExport interface Rational : Real { - // Instantiate - - companion object { - - /** - * Instantiate a rational from a numerator and a denominator - * @param numerator Numerator - * @param denominator Denominator - * @return Rational - */ - fun instantiate( - numerator: Integer, - denominator: Natural - ): Rational { - // Get GCD to simplify - val gcd = numerator.longValue.gcd(denominator.longValue) - return if (gcd != 1L) { - val newNumeratorValue = numerator.longValue / gcd - val newDenominatorValue = denominator.longValue / gcd - if (newDenominatorValue == 1L) { - Integer.instantiate(newNumeratorValue) - } else { - RationalImpl( - Integer.instantiate(newNumeratorValue), - Natural.instantiate(newDenominatorValue) - ) - } - } else { - RationalImpl(numerator, denominator) - } - } - - /** - * Instantiate a rational from a numerator and a denominator - * @param numerator Numerator - * @param denominator Denominator - * @return Rational - */ - fun instantiate( - numerator: Integer, - denominator: Integer - ): Rational { - return if (denominator.longValue < 0) { - instantiate( - Integer.instantiate(-1 * numerator.longValue), - denominator.absoluteValue - ) - } else { - instantiate(numerator, denominator.absoluteValue) - } - } - - /** - * Instantiate a rational from a numerator and a denominator - * @param numerator Numerator - * @param denominator Denominator - * @return Rational - */ - fun instantiate( - numerator: Long, - denominator: Long - ): Rational { - return instantiate(Integer.instantiate(numerator), Integer.instantiate(denominator)) - } - - } - // Rational interface /** @@ -94,8 +34,9 @@ interface Rational : Real { return numerator.doubleValue / denominator.doubleValue } - override val absoluteValue: Rational get() { - return instantiate(numerator.absoluteValue, denominator) + override val absoluteValue: Rational + get() { + return RationalFactory.instantiate(numerator.absoluteValue, denominator) } // Value @@ -114,7 +55,7 @@ interface Rational : Real { if (right is Integer) { val newNumerator = numerator.sum(denominator.multiply(right)) if (newNumerator is Integer) { - return instantiate(newNumerator, denominator) + return RationalFactory.instantiate(newNumerator, denominator) } } if (right is Rational) { @@ -122,7 +63,7 @@ interface Rational : Real { .sum(right.numerator.multiply(denominator)) val newDenominator = denominator.multiply(right.denominator) if (newNumerator is Integer && newDenominator is Integer) { - return instantiate(newNumerator, newDenominator) + return RationalFactory.instantiate(newNumerator, newDenominator) } } return super.sum(right) @@ -132,21 +73,21 @@ interface Rational : Real { if (right is Integer) { val newNumerator = numerator.multiply(right) if (newNumerator is Integer) { - return instantiate(newNumerator, denominator) + return RationalFactory.instantiate(newNumerator, denominator) } } if (right is Rational) { val newNumerator = numerator.multiply(right.numerator) val newDenominator = denominator.multiply(right.denominator) if (newNumerator is Integer && newDenominator is Integer) { - return instantiate(newNumerator, newDenominator) + return RationalFactory.instantiate(newNumerator, newDenominator) } } if (right is Real) { return RealImpl(doubleValue).multiply(right) } if (right is Matrix) { - return Matrix.instantiate(right.rows.map { rows -> + return MatrixFactory.instantiate(right.rows.map { rows -> rows.map { multiply(it) } }) } @@ -157,7 +98,7 @@ interface Rational : Real { if (right is Integer) { val newDenominator = denominator.multiply(right) if (newDenominator is Integer) { - return instantiate( + return RationalFactory.instantiate( numerator, newDenominator ) @@ -167,7 +108,7 @@ interface Rational : Real { val newNumerator = numerator.multiply(right.denominator) val newDenominator = denominator.multiply(right.numerator) if (newNumerator is Integer && newDenominator is Integer) { - return instantiate( + return RationalFactory.instantiate( newNumerator, newDenominator ) @@ -181,7 +122,7 @@ interface Rational : Real { val newRight = denominator.multiply(right) val newNumerator = numerator.remainder(newRight) if (newRight is Integer && newNumerator is Integer) { - return instantiate( + return RationalFactory.instantiate( newNumerator, denominator ) @@ -191,7 +132,7 @@ interface Rational : Real { val newNumerator = numerator.multiply(right.denominator).remainder(right.numerator.multiply(denominator)) val newDenominator = denominator.multiply(right.denominator) if (newNumerator is Integer && newDenominator is Integer) { - return instantiate( + return RationalFactory.instantiate( newNumerator, newDenominator ) @@ -202,7 +143,7 @@ interface Rational : Real { override fun raise(right: Value): Value { val denominator = denominator - if (denominator != Integer.instantiate(1)) { + if (denominator != IntegerFactory.instantiate(1)) { val newNumerator = numerator.raise(right) val newDenominator = denominator.raise(right) return newNumerator.divide(newDenominator) diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/rationals/RationalFactory.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/rationals/RationalFactory.kt new file mode 100644 index 0000000..8556ef8 --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/rationals/RationalFactory.kt @@ -0,0 +1,77 @@ +package me.nathanfallet.makth.numbers.rationals + +import me.nathanfallet.makth.extensions.gcd +import me.nathanfallet.makth.numbers.integers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.numbers.naturals.Natural +import me.nathanfallet.makth.numbers.naturals.NaturalFactory +import kotlin.js.JsExport +import kotlin.js.JsName + +@JsExport +object RationalFactory { + + /** + * Instantiate a rational from a numerator and a denominator + * @param numerator Numerator + * @param denominator Denominator + * @return Rational + */ + @JsName("instantiateWithIntegerAndNatural") + fun instantiate( + numerator: Integer, + denominator: Natural + ): Rational { + // Get GCD to simplify + val gcd = numerator.longValue.gcd(denominator.longValue) + return if (gcd != 1L) { + val newNumeratorValue = numerator.longValue / gcd + val newDenominatorValue = denominator.longValue / gcd + if (newDenominatorValue == 1L) { + IntegerFactory.instantiate(newNumeratorValue) + } else { + RationalImpl( + IntegerFactory.instantiate(newNumeratorValue), + NaturalFactory.instantiate(newDenominatorValue) + ) + } + } else { + RationalImpl(numerator, denominator) + } + } + + /** + * Instantiate a rational from a numerator and a denominator + * @param numerator Numerator + * @param denominator Denominator + * @return Rational + */ + @JsName("instantiateWithIntegers") + fun instantiate( + numerator: Integer, + denominator: Integer + ): Rational { + return if (denominator.longValue < 0) { + instantiate( + IntegerFactory.instantiate(-1 * numerator.longValue), + denominator.absoluteValue + ) + } else { + instantiate(numerator, denominator.absoluteValue) + } + } + + /** + * Instantiate a rational from a numerator and a denominator + * @param numerator Numerator + * @param denominator Denominator + * @return Rational + */ + fun instantiate( + numerator: Long, + denominator: Long + ): Rational { + return instantiate(IntegerFactory.instantiate(numerator), IntegerFactory.instantiate(denominator)) + } + +} \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/RationalImpl.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/rationals/RationalImpl.kt similarity index 63% rename from src/commonMain/kotlin/me/nathanfallet/makth/numbers/RationalImpl.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/numbers/rationals/RationalImpl.kt index 4184d1f..76c4a5e 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/RationalImpl.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/rationals/RationalImpl.kt @@ -1,4 +1,7 @@ -package me.nathanfallet.makth.numbers +package me.nathanfallet.makth.numbers.rationals + +import me.nathanfallet.makth.numbers.integers.Integer +import me.nathanfallet.makth.numbers.naturals.Natural internal data class RationalImpl( override val numerator: Integer, diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/Real.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/Real.kt similarity index 56% rename from src/commonMain/kotlin/me/nathanfallet/makth/numbers/Real.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/Real.kt index 84c6aab..39ff1b6 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/Real.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/Real.kt @@ -1,53 +1,21 @@ -package me.nathanfallet.makth.numbers +package me.nathanfallet.makth.numbers.reals import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable -import me.nathanfallet.makth.sets.Matrix -import me.nathanfallet.makth.sets.Vector +import me.nathanfallet.makth.resolvables.variables.Variable +import me.nathanfallet.makth.sets.matrixes.Matrix +import me.nathanfallet.makth.sets.matrixes.MatrixFactory +import me.nathanfallet.makth.sets.vectors.Vector +import kotlin.js.JsExport import kotlin.math.abs -import kotlin.math.floor import kotlin.math.pow /** * Real representation */ +@JsExport interface Real : Vector { - // Instantiate - - companion object { - - /** - * Pi constant - */ - val pi: Real = RealImplPi() - - /** - * Instantiate a real from a double value - * @param value Double value - * @return Real - */ - fun instantiate(value: Double): Real { - // Check if value is an integer - if (floor(value) == value) { - return Integer.instantiate(value.toLong()) - } - - // Check if value is a rational - // Is it possible, as a double has a finite number of digits? - - // Check for some constants - if (value == pi.doubleValue) { - return pi - } - - // Otherwise, it's a real - return RealImpl(value) - } - - } - // Real interface /** @@ -58,9 +26,8 @@ interface Real : Vector { /** * Get the absolute value of this real */ - val absoluteValue: Real get() { - return instantiate(abs(doubleValue)) - } + val absoluteValue: Real + get() = RealFactory.instantiate(abs(doubleValue)) // Vector @@ -104,17 +71,17 @@ interface Real : Vector { override fun sum(right: Value): Value { if (right is Real) { - return instantiate(doubleValue + right.doubleValue) + return RealFactory.instantiate(doubleValue + right.doubleValue) } return super.sum(right) } override fun multiply(right: Value): Value { if (right is Real) { - return instantiate(doubleValue * right.doubleValue) + return RealFactory.instantiate(doubleValue * right.doubleValue) } if (right is Matrix) { - return Matrix.instantiate(right.rows.map { rows -> + return MatrixFactory.instantiate(right.rows.map { rows -> rows.map { multiply(it) } }) } @@ -123,21 +90,21 @@ interface Real : Vector { override fun divide(right: Value): Value { if (right is Real) { - return instantiate(doubleValue / right.doubleValue) + return RealFactory.instantiate(doubleValue / right.doubleValue) } return super.divide(right) } override fun remainder(right: Value): Value { if (right is Real) { - return instantiate(doubleValue % right.doubleValue) + return RealFactory.instantiate(doubleValue % right.doubleValue) } return super.remainder(right) } override fun raise(right: Value): Value { if (right is Real) { - return instantiate(doubleValue.pow(right.doubleValue)) + return RealFactory.instantiate(doubleValue.pow(right.doubleValue)) } return super.raise(right) } diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/RealFactory.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/RealFactory.kt new file mode 100644 index 0000000..288d348 --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/RealFactory.kt @@ -0,0 +1,36 @@ +package me.nathanfallet.makth.numbers.reals + +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import kotlin.math.floor + +object RealFactory { + + /** + * Pi constant + */ + val pi: Real = RealImplPi() + + /** + * Instantiate a real from a double value + * @param value Double value + * @return Real + */ + fun instantiate(value: Double): Real { + // Check if value is an integer + if (floor(value) == value) { + return IntegerFactory.instantiate(value.toLong()) + } + + // Check if value is a rational + // Is it possible, as a double has a finite number of digits? + + // Check for some constants + if (value == pi.doubleValue) { + return pi + } + + // Otherwise, it's a real + return RealImpl(value) + } + +} \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/RealImpl.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/RealImpl.kt similarity index 63% rename from src/commonMain/kotlin/me/nathanfallet/makth/numbers/RealImpl.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/RealImpl.kt index 90c133f..4f21223 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/RealImpl.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/RealImpl.kt @@ -1,4 +1,4 @@ -package me.nathanfallet.makth.numbers +package me.nathanfallet.makth.numbers.reals internal data class RealImpl( override val doubleValue: Double diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/RealImplPi.kt b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/RealImplPi.kt similarity index 86% rename from src/commonMain/kotlin/me/nathanfallet/makth/numbers/RealImplPi.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/RealImplPi.kt index bdca9d3..315c534 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/numbers/RealImplPi.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/numbers/reals/RealImplPi.kt @@ -1,4 +1,4 @@ -package me.nathanfallet.makth.numbers +package me.nathanfallet.makth.numbers.reals import kotlin.math.PI diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Equality.kt b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Equality.kt index 4fceb2d..5a9b636 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Equality.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Equality.kt @@ -3,7 +3,8 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.extensions.BooleanValue import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport /** * Equality operation. @@ -11,6 +12,7 @@ import me.nathanfallet.makth.resolvables.Variable * @param right Right value * @param operator Operator, default to Equals */ +@JsExport data class Equality( val left: Value, val right: Value, @@ -85,7 +87,7 @@ data class Equality( } override val mainPrecedence: Int get() { - return Operation.Utils.getPrecedence("=") + return OperationFactory.getPrecedence("=") } override fun equals(right: Value): Boolean { diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Exponentiation.kt b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Exponentiation.kt index a55b212..67f4a89 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Exponentiation.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Exponentiation.kt @@ -2,13 +2,15 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport /** * Exponentiation operation. * @param left Left value * @param right Right value */ +@JsExport data class Exponentiation( val left: Value, val right: Value @@ -46,7 +48,7 @@ data class Exponentiation( } override val mainPrecedence: Int get() { - return Operation.Utils.getPrecedence("^") + return OperationFactory.getPrecedence("^") } override fun equals(right: Value): Boolean { diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Operation.kt b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Operation.kt index f264d4c..1093b33 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Operation.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Operation.kt @@ -1,88 +1,10 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.interfaces.Value -import me.nathanfallet.makth.lexers.AlgorithmLexer.SyntaxException -import me.nathanfallet.makth.lexers.MathLexer -import me.nathanfallet.makth.numbers.Integer -import me.nathanfallet.makth.sets.Matrix +import kotlin.js.JsExport /** * Interface for all operations between values */ -interface Operation : Value { - - object Utils { - - /** - * Initialize an operation from an operator and two values - * @param operator Operator - * @param left Left value - * @param right Right value - * @return Operation - */ - @Throws(SyntaxException::class) - fun initialize(operator: String, left: Value, right: Value): Value { - return when (operator) { - "+" -> Sum(left, right) - "-" -> Sum(left, Product(Integer.instantiate(-1), right)) - "*" -> Product(left, right) - "/" -> Quotient(left, right) - "%" -> Remainder(left, right) - "^" -> Exponentiation(left, right) - "=" -> Equality(left, right) - "!=" -> Equality(left, right, Equality.Operator.NotEquals) - "<" -> Equality(left, right, Equality.Operator.LessThan) - ">" -> Equality(left, right, Equality.Operator.GreaterThan) - "<=" -> Equality(left, right, Equality.Operator.LessThanOrEquals) - ">=" -> Equality(left, right, Equality.Operator.GreaterThanOrEquals) - "," -> createHorizontalMatrix(left, right) - ";" -> createVerticalMatrix(left, right) - else -> throw MathLexer.UnknownOperatorException(operator) - } - } - - /** - * Get the precedence of an operation - * @param operation Operation - * @return Precedence - */ - fun getPrecedence(operation: String): Int { - return when (operation) { - "^" -> 5 - "*", "/", "%" -> 4 - "+", "-" -> 3 - "," -> 2 - ";" -> 1 - else -> 0 - } - } - - internal fun createHorizontalMatrix(left: Value, right: Value): Matrix { - return if (left is Matrix && left.rows.count() == 1) { - // We add one element to the first and only row - Matrix.instantiate(listOf( - left.rows.first() + right - )) - } else { - // Create a new matrix with two elements on the only row - Matrix.instantiate(listOf( - listOf(left), listOf(right) - )) - } - } - - internal fun createVerticalMatrix(left: Value, right: Value): Matrix { - return if (left is Matrix && right is Matrix && right.rows.count() == 1) { - // We add one row to the first matrix - Matrix.instantiate(left.rows + right.rows) - } else { - // Create a new matrix with two rows - Matrix.instantiate(listOf( - listOf(left), listOf(right) - )) - } - } - - } - -} \ No newline at end of file +@JsExport +interface Operation : Value \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/operations/OperationFactory.kt b/src/commonMain/kotlin/me/nathanfallet/makth/operations/OperationFactory.kt new file mode 100644 index 0000000..7b3c4af --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/operations/OperationFactory.kt @@ -0,0 +1,90 @@ +package me.nathanfallet.makth.operations + +import me.nathanfallet.makth.interfaces.Value +import me.nathanfallet.makth.lexers.AlgorithmLexer +import me.nathanfallet.makth.lexers.MathLexer +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.sets.matrixes.Matrix +import me.nathanfallet.makth.sets.matrixes.MatrixFactory +import kotlin.js.JsExport + +@JsExport +object OperationFactory { + + /** + * Initialize an operation from an operator and two values + * @param operator Operator + * @param left Left value + * @param right Right value + * @return Operation + */ + @Throws(AlgorithmLexer.SyntaxException::class) + fun initialize(operator: String, left: Value, right: Value): Value { + return when (operator) { + "+" -> Sum(left, right) + "-" -> Sum(left, Product(IntegerFactory.instantiate(-1), right)) + "*" -> Product(left, right) + "/" -> Quotient(left, right) + "%" -> Remainder(left, right) + "^" -> Exponentiation(left, right) + "=" -> Equality(left, right) + "!=" -> Equality(left, right, Equality.Operator.NotEquals) + "<" -> Equality(left, right, Equality.Operator.LessThan) + ">" -> Equality(left, right, Equality.Operator.GreaterThan) + "<=" -> Equality(left, right, Equality.Operator.LessThanOrEquals) + ">=" -> Equality(left, right, Equality.Operator.GreaterThanOrEquals) + "," -> createHorizontalMatrix(left, right) + ";" -> createVerticalMatrix(left, right) + else -> throw MathLexer.UnknownOperatorException(operator) + } + } + + /** + * Get the precedence of an operation + * @param operation Operation + * @return Precedence + */ + internal fun getPrecedence(operation: String): Int { + return when (operation) { + "^" -> 5 + "*", "/", "%" -> 4 + "+", "-" -> 3 + "," -> 2 + ";" -> 1 + else -> 0 + } + } + + private fun createHorizontalMatrix(left: Value, right: Value): Matrix { + return if (left is Matrix && left.rows.count() == 1) { + // We add one element to the first and only row + MatrixFactory.instantiate( + listOf( + left.rows.first() + right + ) + ) + } else { + // Create a new matrix with two elements on the only row + MatrixFactory.instantiate( + listOf( + listOf(left), listOf(right) + ) + ) + } + } + + private fun createVerticalMatrix(left: Value, right: Value): Matrix { + return if (left is Matrix && right is Matrix && right.rows.count() == 1) { + // We add one row to the first matrix + MatrixFactory.instantiate(left.rows + right.rows) + } else { + // Create a new matrix with two rows + MatrixFactory.instantiate( + listOf( + listOf(left), listOf(right) + ) + ) + } + } + +} \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Product.kt b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Product.kt index 76db80c..cb33114 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Product.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Product.kt @@ -3,13 +3,15 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.extensions.orOrThrowUnsupportedOperationException import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport /** * Product operation. * @param left Left value * @param right Right value */ +@JsExport data class Product( val left: Value, val right: Value @@ -56,7 +58,7 @@ data class Product( } override val mainPrecedence: Int get() { - return Operation.Utils.getPrecedence("*") + return OperationFactory.getPrecedence("*") } override fun equals(right: Value): Boolean { diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Quotient.kt b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Quotient.kt index 800160d..5653b78 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Quotient.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Quotient.kt @@ -3,13 +3,15 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.extensions.orOrThrowUnsupportedOperationException import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport /** * Quotient operation. * @param left Left value * @param right Right value */ +@JsExport data class Quotient( val left: Value, val right: Value @@ -45,7 +47,7 @@ data class Quotient( } override val mainPrecedence: Int get() { - return Operation.Utils.getPrecedence("/") + return OperationFactory.getPrecedence("/") } override fun equals(right: Value): Boolean { diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Remainder.kt b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Remainder.kt index d55c5c4..f6d8ac0 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Remainder.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Remainder.kt @@ -2,13 +2,15 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport /** * Remainder operation. * @param left Left value * @param right Right value */ +@JsExport data class Remainder( val left: Value, val right: Value @@ -49,7 +51,7 @@ data class Remainder( } override val mainPrecedence: Int get() { - return Operation.Utils.getPrecedence("%") + return OperationFactory.getPrecedence("%") } override fun equals(right: Value): Boolean { diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Sum.kt b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Sum.kt index 8afa1c0..e1b905d 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/operations/Sum.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/operations/Sum.kt @@ -3,13 +3,15 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.extensions.orOrThrowUnsupportedOperationException import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport /** * Sum operation. * @param left Left value * @param right Right value */ +@JsExport data class Sum( val left: Value, val right: Value @@ -56,7 +58,7 @@ data class Sum( } override val mainPrecedence: Int get() { - return Operation.Utils.getPrecedence("+") + return OperationFactory.getPrecedence("+") } override fun equals(right: Value): Boolean { diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/Context.kt b/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/Context.kt index cd45995..0d8de9a 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/Context.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/Context.kt @@ -1,14 +1,18 @@ package me.nathanfallet.makth.resolvables +import me.nathanfallet.makth.exceptions.ExecutionException import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.interfaces.Output import me.nathanfallet.makth.interfaces.Value +import kotlin.js.JsExport +import kotlin.js.JsName /** * Context class * @param data Variables in current memory * @param outputs Outputs list */ +@JsExport data class Context( val data: Map = mapOf(), val outputs: List = listOf() @@ -19,7 +23,8 @@ data class Context( * @param action Action to execute * @return New context after execution */ - @Throws(Action.ExecutionException::class) + @JsName("executeAction") + @Throws(ExecutionException::class) fun execute(action: Action): Context { return action.execute(this) } @@ -29,7 +34,8 @@ data class Context( * @param actions Actions to execute * @return New context after execution */ - @Throws(Action.ExecutionException::class) + @JsName("executeActions") + @Throws(ExecutionException::class) fun execute(actions: List): Context { var context = this for (action in actions) { diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/Variable.kt b/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/Variable.kt deleted file mode 100644 index 42ac962..0000000 --- a/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/Variable.kt +++ /dev/null @@ -1,59 +0,0 @@ -package me.nathanfallet.makth.resolvables - -import me.nathanfallet.makth.extensions.BooleanValue -import me.nathanfallet.makth.interfaces.Value -import me.nathanfallet.makth.numbers.Real - -/** - * Variable representation - */ -data class Variable private constructor(val name: String) : Value { - - companion object { - - /** - * Instantiate a variable from a name - * @param name Variable name - * @return Variable instance - */ - fun instantiate(name: String): Value { - // Check for booleans - if (name == "true" || name == "false") { - return BooleanValue(name == "true") - } - - // Check for some constants - if (name == "pi" || name == "\u03C0") { - return Real.pi - } - - return Variable(name) - } - - } - - override fun compute(context: Context): Value { - return context.data[name] ?: this - } - - override val rawString: String get() { - return name - } - - override val laTeXString: String get() { - return name - } - - override val variables: Set get() { - return setOf(this) - } - - override fun equals(right: Value): Boolean { - // If name is different, we don't know what's inside each so we can't compare - if (right is Variable && name == right.name) { - return true - } - return super.equals(right) - } - -} diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/Variable.kt b/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/Variable.kt new file mode 100644 index 0000000..46a8813 --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/Variable.kt @@ -0,0 +1,42 @@ +package me.nathanfallet.makth.resolvables.variables + +import me.nathanfallet.makth.interfaces.Value +import me.nathanfallet.makth.resolvables.Context +import kotlin.js.JsExport + +/** + * Variable representation + */ +@JsExport +interface Variable : Value { + + /** + * Variable name + */ + val name: String + + override fun compute(context: Context): Value { + return context.data[name] ?: this + } + + override val rawString: String get() { + return name + } + + override val laTeXString: String get() { + return name + } + + override val variables: Set get() { + return setOf(this) + } + + override fun equals(right: Value): Boolean { + // If name is different, we don't know what's inside each so we can't compare + if (right is Variable && name == right.name) { + return true + } + return super.equals(right) + } + +} diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/VariableFactory.kt b/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/VariableFactory.kt new file mode 100644 index 0000000..3a67ab2 --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/VariableFactory.kt @@ -0,0 +1,30 @@ +package me.nathanfallet.makth.resolvables.variables + +import me.nathanfallet.makth.extensions.BooleanValue +import me.nathanfallet.makth.interfaces.Value +import me.nathanfallet.makth.numbers.reals.RealFactory +import kotlin.js.JsExport + +@JsExport +object VariableFactory { + + /** + * Instantiate a variable from a name + * @param name Variable name + * @return Variable instance + */ + fun instantiate(name: String): Value { + // Check for booleans + if (name == "true" || name == "false") { + return BooleanValue(name == "true") + } + + // Check for some constants + if (name == "pi" || name == "\u03C0") { + return RealFactory.pi + } + + return VariableImpl(name) + } + +} \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/VariableImpl.kt b/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/VariableImpl.kt new file mode 100644 index 0000000..cca3ddd --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/resolvables/variables/VariableImpl.kt @@ -0,0 +1,5 @@ +package me.nathanfallet.makth.resolvables.variables + +internal data class VariableImpl( + override val name: String +) : Variable \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/sets/Matrix.kt b/src/commonMain/kotlin/me/nathanfallet/makth/sets/matrixes/Matrix.kt similarity index 69% rename from src/commonMain/kotlin/me/nathanfallet/makth/sets/Matrix.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/sets/matrixes/Matrix.kt index 6f37d5d..f9e674a 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/sets/Matrix.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/sets/matrixes/Matrix.kt @@ -1,41 +1,17 @@ -package me.nathanfallet.makth.sets +package me.nathanfallet.makth.sets.matrixes import me.nathanfallet.makth.interfaces.Iterable import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import kotlin.js.JsExport /* * Represents a matrix */ +@JsExport interface Matrix : Iterable { - // Instantiate - - companion object { - - /** - * Instantiate a matrix from a list of rows - * @param rows List of rows - * @return Matrix - */ - fun instantiate(rows: List>): Matrix { - // Check that matrix is not empty and all rows have the same size - if (rows.isEmpty() || rows.any { it.count() != rows.first().count() }) { - throw IllegalArgumentException("Invalid matrix") - } - - // If matrix is a vector - if (rows.first().count() == 1) { - return Vector.instantiate(rows.flatMap { it }) - } - - // Normal matrix - return MatrixImpl(rows) - } - - } - // Matrix interface /** @@ -52,14 +28,14 @@ interface Matrix : Iterable { override val iterator: Iterator get() { return rows.map { - Matrix.instantiate(listOf(it)) + MatrixFactory.instantiate(listOf(it)) }.iterator() } // Value override fun compute(context: Context): Value { - return Matrix.instantiate(rows.map { row -> + return MatrixFactory.instantiate(rows.map { row -> row.map { it.compute(context) } }) } @@ -85,7 +61,7 @@ interface Matrix : Iterable { // Operations fun transpose(): Matrix { - return Matrix.instantiate(columns) + return MatrixFactory.instantiate(columns) } override fun equals(right: Value): Boolean { @@ -102,7 +78,7 @@ interface Matrix : Iterable { if (rows.count() != right.rows.count() || columns.count() != right.columns.count()) { throw UnsupportedOperationException("Cannot sum matrices with different sizes") } - return Matrix.instantiate(rows.zip(right.rows).map { rows -> + return MatrixFactory.instantiate(rows.zip(right.rows).map { rows -> rows.first.zip(rows.second).map { it.first.sum(it.second) } }) } @@ -114,7 +90,7 @@ interface Matrix : Iterable { if (columns.count() != right.rows.count()) { throw UnsupportedOperationException("Cannot multiply matrices with incompatible sizes") } - return Matrix.instantiate(rows.map { row -> + return MatrixFactory.instantiate(rows.map { row -> right.columns.map { column -> row.zip(column).map { it.first.multiply(it.second) diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixFactory.kt b/src/commonMain/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixFactory.kt new file mode 100644 index 0000000..c53812f --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixFactory.kt @@ -0,0 +1,30 @@ +package me.nathanfallet.makth.sets.matrixes + +import me.nathanfallet.makth.interfaces.Value +import me.nathanfallet.makth.sets.vectors.VectorFactory +import kotlin.js.JsExport + +@JsExport +object MatrixFactory { + + /** + * Instantiate a matrix from a list of rows + * @param rows List of rows + * @return Matrix + */ + fun instantiate(rows: List>): Matrix { + // Check that matrix is not empty and all rows have the same size + if (rows.isEmpty() || rows.any { it.count() != rows.first().count() }) { + throw IllegalArgumentException("Invalid matrix") + } + + // If matrix is a vector + if (rows.first().count() == 1) { + return VectorFactory.instantiate(rows.flatten()) + } + + // Normal matrix + return MatrixImpl(rows) + } + +} \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/sets/MatrixImpl.kt b/src/commonMain/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixImpl.kt similarity index 86% rename from src/commonMain/kotlin/me/nathanfallet/makth/sets/MatrixImpl.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixImpl.kt index 4e4137a..363cfce 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/sets/MatrixImpl.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixImpl.kt @@ -1,4 +1,4 @@ -package me.nathanfallet.makth.sets +package me.nathanfallet.makth.sets.matrixes import me.nathanfallet.makth.interfaces.Value diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/sets/Vector.kt b/src/commonMain/kotlin/me/nathanfallet/makth/sets/vectors/Vector.kt similarity index 67% rename from src/commonMain/kotlin/me/nathanfallet/makth/sets/Vector.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/sets/vectors/Vector.kt index 9e02f8c..4636999 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/sets/Vector.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/sets/vectors/Vector.kt @@ -1,36 +1,21 @@ -package me.nathanfallet.makth.sets +package me.nathanfallet.makth.sets.vectors import me.nathanfallet.makth.interfaces.Value -import me.nathanfallet.makth.numbers.Real +import me.nathanfallet.makth.numbers.reals.Real import me.nathanfallet.makth.operations.Sum import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.Variable +import me.nathanfallet.makth.sets.matrixes.Matrix +import kotlin.js.JsExport /** * Vector representation * It's a set of elements of the same type * @param elements List of elements */ +@JsExport interface Vector : Matrix { - // Instantiate - - companion object { - - /** - * Instantiate a vector from a list of elements - * @param elements List of elements - * @return Vector - */ - fun instantiate(elements: List): Vector { - if (elements.count() == 1 && elements.first() is Vector) { - return elements.first() as Vector - } - return VectorImpl(elements) - } - - } - // Vector interface /** @@ -47,7 +32,7 @@ interface Vector : Matrix { // Value override fun compute(context: Context): Value { - return Vector.instantiate(elements.map { it.compute(context) }) + return VectorFactory.instantiate(elements.map { it.compute(context) }) } override val rawString: String get() { @@ -79,7 +64,7 @@ interface Vector : Matrix { if (elements.count() != right.elements.count()) { throw UnsupportedOperationException("Cannot sum vectors of different sizes") } - return Vector.instantiate(elements.zip(right.elements).map { pair -> + return VectorFactory.instantiate(elements.zip(right.elements).map { pair -> Sum(pair.first, pair.second).compute(Context()) }) } @@ -88,7 +73,7 @@ interface Vector : Matrix { override fun multiply(right: Value): Value { if (right is Real) { - return Vector.instantiate(elements.map { it.multiply(right) }) + return VectorFactory.instantiate(elements.map { it.multiply(right) }) } return super.multiply(right) } diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/sets/vectors/VectorFactory.kt b/src/commonMain/kotlin/me/nathanfallet/makth/sets/vectors/VectorFactory.kt new file mode 100644 index 0000000..fc4593e --- /dev/null +++ b/src/commonMain/kotlin/me/nathanfallet/makth/sets/vectors/VectorFactory.kt @@ -0,0 +1,21 @@ +package me.nathanfallet.makth.sets.vectors + +import me.nathanfallet.makth.interfaces.Value +import kotlin.js.JsExport + +@JsExport +object VectorFactory { + + /** + * Instantiate a vector from a list of elements + * @param elements List of elements + * @return Vector + */ + fun instantiate(elements: List): Vector { + if (elements.count() == 1 && elements.first() is Vector) { + return elements.first() as Vector + } + return VectorImpl(elements) + } + +} \ No newline at end of file diff --git a/src/commonMain/kotlin/me/nathanfallet/makth/sets/VectorImpl.kt b/src/commonMain/kotlin/me/nathanfallet/makth/sets/vectors/VectorImpl.kt similarity index 75% rename from src/commonMain/kotlin/me/nathanfallet/makth/sets/VectorImpl.kt rename to src/commonMain/kotlin/me/nathanfallet/makth/sets/vectors/VectorImpl.kt index 70de2e1..c2dfe0c 100644 --- a/src/commonMain/kotlin/me/nathanfallet/makth/sets/VectorImpl.kt +++ b/src/commonMain/kotlin/me/nathanfallet/makth/sets/vectors/VectorImpl.kt @@ -1,4 +1,4 @@ -package me.nathanfallet.makth.sets +package me.nathanfallet.makth.sets.vectors import me.nathanfallet.makth.interfaces.Value diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/actions/ForActionTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/actions/ForActionTest.kt index c3f87bb..b8ace82 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/actions/ForActionTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/actions/ForActionTest.kt @@ -1,13 +1,14 @@ package me.nathanfallet.makth.actions +import me.nathanfallet.makth.exceptions.NotIterableException +import me.nathanfallet.makth.exceptions.UnknownVariablesException import me.nathanfallet.makth.extensions.BooleanValue import me.nathanfallet.makth.extensions.StringValue -import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable -import me.nathanfallet.makth.sets.Vector +import me.nathanfallet.makth.resolvables.variables.VariableFactory +import me.nathanfallet.makth.sets.vectors.VectorFactory import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -18,12 +19,12 @@ class ForActionTest { private val contextWithOutput = Context( mapOf( - "x" to Integer.instantiate(3) + "x" to IntegerFactory.instantiate(3) ), listOf( - Integer.instantiate(1), StringValue("\n"), - Integer.instantiate(2), StringValue("\n"), - Integer.instantiate(3), StringValue("\n") + IntegerFactory.instantiate(1), StringValue("\n"), + IntegerFactory.instantiate(2), StringValue("\n"), + IntegerFactory.instantiate(3), StringValue("\n") ) ) @@ -33,7 +34,13 @@ class ForActionTest { "for (x, (1; 2; 3)) {\n}", ForAction( "x", - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))), + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ), listOf() ).algorithmString ) @@ -45,8 +52,14 @@ class ForActionTest { "for (x, (1; 2; 3)) {\n print(x)\n}", ForAction( "x", - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))), - listOf(PrintAction(listOf(Variable.instantiate("x")))) + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ), + listOf(PrintAction(listOf(VariableFactory.instantiate("x")))) ).algorithmString ) } @@ -56,12 +69,24 @@ class ForActionTest { assertEquals( ForAction( "x", - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))), + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ), listOf() ), ForAction.handler(listOf( - Variable.instantiate("x"), - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))) + VariableFactory.instantiate("x"), + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ) )) ) } @@ -80,8 +105,14 @@ class ForActionTest { context.execute( ForAction( "x", - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))), - listOf(PrintAction(listOf(Variable.instantiate("x")))) + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ), + listOf(PrintAction(listOf(VariableFactory.instantiate("x")))) ) ) ) @@ -89,16 +120,16 @@ class ForActionTest { @Test fun forWithPrintWithoutContext() { - assertFailsWith(Action.UnknownVariablesException::class) { + assertFailsWith(UnknownVariablesException::class) { context.execute( - ForAction("x", Variable.instantiate("y"), listOf()) + ForAction("x", VariableFactory.instantiate("y"), listOf()) ) } } @Test fun forWhenNotIterable() { - assertFailsWith(Action.NotIterableException::class) { + assertFailsWith(NotIterableException::class) { context.execute( ForAction("x", BooleanValue(true), listOf()) ) diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/actions/IfActionTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/actions/IfActionTest.kt index 5c599cb..17696f2 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/actions/IfActionTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/actions/IfActionTest.kt @@ -1,13 +1,14 @@ package me.nathanfallet.makth.actions +import me.nathanfallet.makth.exceptions.NotABooleanException +import me.nathanfallet.makth.exceptions.UnknownVariablesException import me.nathanfallet.makth.extensions.BooleanValue import me.nathanfallet.makth.extensions.StringValue -import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.operations.Equality import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -18,14 +19,14 @@ class IfActionTest { private val contextWithX = Context( mapOf( - "x" to Integer.instantiate(2) + "x" to IntegerFactory.instantiate(2) ) ) private val contextWithXAndY = Context( mapOf( - "x" to Integer.instantiate(2), - "y" to Integer.instantiate(4) + "x" to IntegerFactory.instantiate(2), + "y" to IntegerFactory.instantiate(4) ) ) @@ -33,7 +34,10 @@ class IfActionTest { fun algorithmString() { assertEquals( "if (x = 2) {\n}", - IfAction(Equality(Variable.instantiate("x"), Integer.instantiate(2)), listOf()).algorithmString + IfAction( + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), + listOf() + ).algorithmString ) } @@ -42,7 +46,7 @@ class IfActionTest { assertEquals( "if (x = 2) {\n print(\"Test\")\n}", IfAction( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), listOf(PrintAction(listOf(StringValue("Test")))) ).algorithmString ) @@ -53,7 +57,7 @@ class IfActionTest { assertEquals( "if (x = 2) {\n print(\"Test\")\n} else {\n print(\"Test2\")\n}", IfAction( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), listOf(PrintAction(listOf(StringValue("Test")))), listOf(PrintAction(listOf(StringValue("Test2")))) ).algorithmString @@ -64,11 +68,11 @@ class IfActionTest { fun handler() { assertEquals( IfAction( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), listOf(), listOf() ), - IfAction.handler(listOf(Equality(Variable.instantiate("x"), Integer.instantiate(2)))) + IfAction.handler(listOf(Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)))) ) } @@ -86,7 +90,7 @@ class IfActionTest { context.execute( IfAction( BooleanValue(true), - listOf(SetAction("x", Integer.instantiate(2))), + listOf(SetAction("x", IntegerFactory.instantiate(2))), listOf() ) ) @@ -101,7 +105,7 @@ class IfActionTest { IfAction( BooleanValue(false), listOf(), - listOf(SetAction("x", Integer.instantiate(2))) + listOf(SetAction("x", IntegerFactory.instantiate(2))) ) ) ) @@ -113,8 +117,8 @@ class IfActionTest { contextWithXAndY, contextWithX.execute( IfAction( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), - listOf(SetAction("y", Integer.instantiate(4))), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), + listOf(SetAction("y", IntegerFactory.instantiate(4))), listOf() ) ) @@ -123,10 +127,10 @@ class IfActionTest { @Test fun ifWithVariableWithoutContext() { - assertFailsWith(Action.UnknownVariablesException::class) { + assertFailsWith(UnknownVariablesException::class) { context.execute( IfAction( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), listOf(), listOf() ) @@ -136,10 +140,10 @@ class IfActionTest { @Test fun ifWhenNotABoolean() { - assertFailsWith(Action.NotABooleanException::class) { + assertFailsWith(NotABooleanException::class) { contextWithX.execute( IfAction( - Variable.instantiate("x"), + VariableFactory.instantiate("x"), listOf(), listOf() ) diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/actions/PrintActionTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/actions/PrintActionTest.kt index 79d7f65..8884a50 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/actions/PrintActionTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/actions/PrintActionTest.kt @@ -1,10 +1,10 @@ package me.nathanfallet.makth.actions +import me.nathanfallet.makth.exceptions.UnknownVariablesException import me.nathanfallet.makth.extensions.StringValue -import me.nathanfallet.makth.interfaces.Action -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -22,16 +22,16 @@ class PrintActionTest { private val contextWithX = Context( mapOf( - "x" to Integer.instantiate(2) + "x" to IntegerFactory.instantiate(2) ) ) private val contextWithXAndString = Context( mapOf( - "x" to Integer.instantiate(2) + "x" to IntegerFactory.instantiate(2) ), listOf( - StringValue("x = "), Integer.instantiate(2), StringValue("\n") + StringValue("x = "), IntegerFactory.instantiate(2), StringValue("\n") ) ) @@ -39,7 +39,7 @@ class PrintActionTest { fun algorithmString() { assertEquals( "print(\"x = \", x)", - PrintAction(listOf(StringValue("x = "), Variable.instantiate("x"))).algorithmString + PrintAction(listOf(StringValue("x = "), VariableFactory.instantiate("x"))).algorithmString ) } @@ -55,14 +55,14 @@ class PrintActionTest { fun printStringWithVariable() { assertEquals( contextWithXAndString, - contextWithX.execute(PrintAction(listOf(StringValue("x = "), Variable.instantiate("x")))) + contextWithX.execute(PrintAction(listOf(StringValue("x = "), VariableFactory.instantiate("x")))) ) } @Test fun printStringWithVariableWithoutContext() { - assertFailsWith(Action.UnknownVariablesException::class) { - context.execute(PrintAction(listOf(StringValue("x = "), Variable.instantiate("x")))) + assertFailsWith(UnknownVariablesException::class) { + context.execute(PrintAction(listOf(StringValue("x = "), VariableFactory.instantiate("x")))) } } diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/actions/SetActionTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/actions/SetActionTest.kt index d4d964d..e33ace1 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/actions/SetActionTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/actions/SetActionTest.kt @@ -1,13 +1,13 @@ package me.nathanfallet.makth.actions +import me.nathanfallet.makth.exceptions.UnknownVariablesException import me.nathanfallet.makth.extensions.StringValue -import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentTypeException -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.operations.Sum import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -18,14 +18,14 @@ class SetActionTest { private val contextWithX = Context( mapOf( - "x" to Integer.instantiate(2) + "x" to IntegerFactory.instantiate(2) ) ) private val contextWithXAndY = Context( mapOf( - "x" to Integer.instantiate(2), - "y" to Integer.instantiate(4) + "x" to IntegerFactory.instantiate(2), + "y" to IntegerFactory.instantiate(4) ) ) @@ -33,23 +33,23 @@ class SetActionTest { fun algorithmString() { assertEquals( "set(x, 2)", - SetAction("x", Integer.instantiate(2)).algorithmString + SetAction("x", IntegerFactory.instantiate(2)).algorithmString ) } @Test fun handlerWithVariable() { assertEquals( - SetAction("x", Integer.instantiate(2)), - SetAction.handler(listOf(Variable.instantiate("x"), Integer.instantiate(2))) + SetAction("x", IntegerFactory.instantiate(2)), + SetAction.handler(listOf(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2))) ) } @Test fun handlerWithString() { assertEquals( - SetAction("x", Integer.instantiate(2)), - SetAction.handler(listOf(StringValue("x"), Integer.instantiate(2))) + SetAction("x", IntegerFactory.instantiate(2)), + SetAction.handler(listOf(StringValue("x"), IntegerFactory.instantiate(2))) ) } @@ -63,7 +63,7 @@ class SetActionTest { @Test fun handlerWithWrongArgType() { assertFailsWith(IncorrectArgumentTypeException::class) { - SetAction.handler(listOf(Integer.instantiate(1), Integer.instantiate(2))) + SetAction.handler(listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2))) } } @@ -71,7 +71,7 @@ class SetActionTest { fun setInteger() { assertEquals( contextWithX, - context.execute(SetAction("x", Integer.instantiate(2))) + context.execute(SetAction("x", IntegerFactory.instantiate(2))) ) } @@ -79,14 +79,14 @@ class SetActionTest { fun setIntegerWithVariable() { assertEquals( contextWithXAndY, - contextWithX.execute(SetAction("y", Sum(Variable.instantiate("x"), Integer.instantiate(2)))) + contextWithX.execute(SetAction("y", Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)))) ) } @Test fun setIntegerWithVariableWithoutContext() { - assertFailsWith(Action.UnknownVariablesException::class) { - context.execute(SetAction("y", Sum(Variable.instantiate("x"), Integer.instantiate(2)))) + assertFailsWith(UnknownVariablesException::class) { + context.execute(SetAction("y", Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)))) } } diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/actions/WhileActionTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/actions/WhileActionTest.kt index 658fec7..57dedb8 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/actions/WhileActionTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/actions/WhileActionTest.kt @@ -1,12 +1,13 @@ package me.nathanfallet.makth.actions -import me.nathanfallet.makth.interfaces.Action +import me.nathanfallet.makth.exceptions.NotABooleanException +import me.nathanfallet.makth.exceptions.UnknownVariablesException import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.operations.Equality import me.nathanfallet.makth.operations.Sum import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -17,13 +18,13 @@ class WhileActionTest { private val contextWithX = Context( mapOf( - "x" to Integer.instantiate(2) + "x" to IntegerFactory.instantiate(2) ) ) private val contextWithXIncremented = Context( mapOf( - "x" to Integer.instantiate(10) + "x" to IntegerFactory.instantiate(10) ) ) @@ -33,8 +34,8 @@ class WhileActionTest { "while (x < 10) {\n}", WhileAction( Equality( - Variable.instantiate("x"), - Integer.instantiate(10), + VariableFactory.instantiate("x"), + IntegerFactory.instantiate(10), Equality.Operator.LessThan ), listOf() ).algorithmString @@ -46,8 +47,8 @@ class WhileActionTest { assertEquals( "while (x < 10) {\n set(x, x + 1)\n}", WhileAction( - Equality(Variable.instantiate("x"), Integer.instantiate(10), Equality.Operator.LessThan), - listOf(SetAction("x", Sum(Variable.instantiate("x"), Integer.instantiate(1)))) + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(10), Equality.Operator.LessThan), + listOf(SetAction("x", Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(1)))) ).algorithmString ) } @@ -56,10 +57,10 @@ class WhileActionTest { fun handler() { assertEquals( WhileAction( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), listOf() ), - WhileAction.handler(listOf(Equality(Variable.instantiate("x"), Integer.instantiate(2)))) + WhileAction.handler(listOf(Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)))) ) } @@ -76,8 +77,12 @@ class WhileActionTest { contextWithXIncremented, contextWithX.execute( WhileAction( - Equality(Variable.instantiate("x"), Integer.instantiate(10), Equality.Operator.LessThan), - listOf(SetAction("x", Sum(Variable.instantiate("x"), Integer.instantiate(1)))) + Equality( + VariableFactory.instantiate("x"), + IntegerFactory.instantiate(10), + Equality.Operator.LessThan + ), + listOf(SetAction("x", Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(1)))) ) ) ) @@ -85,10 +90,10 @@ class WhileActionTest { @Test fun whileWithVariableWithoutContext() { - assertFailsWith(Action.UnknownVariablesException::class) { + assertFailsWith(UnknownVariablesException::class) { context.execute( WhileAction( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), listOf() ) ) @@ -97,10 +102,10 @@ class WhileActionTest { @Test fun whileWhenNotABoolean() { - assertFailsWith(Action.NotABooleanException::class) { + assertFailsWith(NotABooleanException::class) { contextWithX.execute( WhileAction( - Variable.instantiate("x"), + VariableFactory.instantiate("x"), listOf() ) ) diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/extensions/BooleanExtensionTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/extensions/BooleanValueTest.kt similarity index 97% rename from src/commonTest/kotlin/me/nathanfallet/makth/extensions/BooleanExtensionTest.kt rename to src/commonTest/kotlin/me/nathanfallet/makth/extensions/BooleanValueTest.kt index 5fe9900..e199057 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/extensions/BooleanExtensionTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/extensions/BooleanValueTest.kt @@ -4,7 +4,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith -class BooleanExtensionTest { +class BooleanValueTest { @Test fun toAlgorithmStringTrue() { diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/extensions/StringExtensionTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/extensions/StringValueTest.kt similarity index 98% rename from src/commonTest/kotlin/me/nathanfallet/makth/extensions/StringExtensionTest.kt rename to src/commonTest/kotlin/me/nathanfallet/makth/extensions/StringValueTest.kt index bfeb591..2fd2067 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/extensions/StringExtensionTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/extensions/StringValueTest.kt @@ -4,7 +4,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith -class StringExtensionTest { +class StringValueTest { @Test fun algorithmString() { diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexerTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexerTest.kt index e5df8c0..ed0c2a4 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexerTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/lexers/AlgorithmLexerTest.kt @@ -8,11 +8,11 @@ import me.nathanfallet.makth.extensions.StringValue import me.nathanfallet.makth.interfaces.Action import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.operations.Equality import me.nathanfallet.makth.operations.Sum import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -23,8 +23,8 @@ class AlgorithmLexerTest { fun parseSingleLineComment() { assertEquals( listOf( - PrintAction(listOf(Integer.instantiate(2))), - PrintAction(listOf(Integer.instantiate(3))) + PrintAction(listOf(IntegerFactory.instantiate(2))), + PrintAction(listOf(IntegerFactory.instantiate(3))) ), AlgorithmLexer("print(2) // Prints 2\nprint(3) // Prints 3").execute() ) @@ -34,8 +34,8 @@ class AlgorithmLexerTest { fun parseMultiLineComment() { assertEquals( listOf( - PrintAction(listOf(Integer.instantiate(2))), - PrintAction(listOf(Integer.instantiate(3))) + PrintAction(listOf(IntegerFactory.instantiate(2))), + PrintAction(listOf(IntegerFactory.instantiate(3))) ), AlgorithmLexer("print(2) /* Prints 2 */ print(3) /* Prints 3 */").execute() ) @@ -45,7 +45,7 @@ class AlgorithmLexerTest { fun parseIfAction() { assertEquals( listOf( - IfAction(Equality(Variable.instantiate("x"), Integer.instantiate(2)), listOf()) + IfAction(Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), listOf()) ), AlgorithmLexer("if (x = 2)").execute() ) @@ -56,7 +56,7 @@ class AlgorithmLexerTest { assertEquals( listOf( IfAction( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), listOf( PrintAction(listOf(StringValue("Test"))) ) @@ -71,10 +71,10 @@ class AlgorithmLexerTest { assertEquals( listOf( IfAction( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), listOf( IfAction( - Equality(Variable.instantiate("y"), Integer.instantiate(3)), + Equality(VariableFactory.instantiate("y"), IntegerFactory.instantiate(3)), listOf( PrintAction(listOf(StringValue("Test"))) ) @@ -91,7 +91,7 @@ class AlgorithmLexerTest { assertEquals( listOf( IfAction( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), listOf( PrintAction(listOf(StringValue("Test"))) ), @@ -108,7 +108,7 @@ class AlgorithmLexerTest { fun parsePrintAction() { assertEquals( listOf( - PrintAction(listOf(Integer.instantiate(2))) + PrintAction(listOf(IntegerFactory.instantiate(2))) ), AlgorithmLexer("print(2)").execute() ) @@ -118,7 +118,7 @@ class AlgorithmLexerTest { fun parsePrintActionWithTwoArguments() { assertEquals( listOf( - PrintAction(listOf(StringValue("x = "), Variable.instantiate("x"))) + PrintAction(listOf(StringValue("x = "), VariableFactory.instantiate("x"))) ), AlgorithmLexer("print(\"x = \", x)").execute() ) @@ -128,7 +128,7 @@ class AlgorithmLexerTest { fun parseSetAction() { assertEquals( listOf( - SetAction("x", Integer.instantiate(2)) + SetAction("x", IntegerFactory.instantiate(2)) ), AlgorithmLexer("set(x, 2)").execute() ) @@ -140,8 +140,8 @@ class AlgorithmLexerTest { listOf( WhileAction( Equality( - Variable.instantiate("x"), - Integer.instantiate(10), + VariableFactory.instantiate("x"), + IntegerFactory.instantiate(10), Equality.Operator.LessThan ), listOf() ) @@ -155,8 +155,12 @@ class AlgorithmLexerTest { assertEquals( listOf( WhileAction( - Equality(Variable.instantiate("x"), Integer.instantiate(10), Equality.Operator.LessThan), - listOf(SetAction("x", Sum(Variable.instantiate("x"), Integer.instantiate(1)))) + Equality( + VariableFactory.instantiate("x"), + IntegerFactory.instantiate(10), + Equality.Operator.LessThan + ), + listOf(SetAction("x", Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(1)))) ) ), AlgorithmLexer("while (x < 10) { set(x, x + 1) }").execute() @@ -170,10 +174,10 @@ class AlgorithmLexerTest { assertEquals( Context( mapOf( - "x" to Integer.instantiate(10) + "x" to IntegerFactory.instantiate(10) ), listOf( - StringValue("x = "), Integer.instantiate(10), StringValue("\n") + StringValue("x = "), IntegerFactory.instantiate(10), StringValue("\n") ) ), Context().execute(actions) @@ -185,7 +189,7 @@ class AlgorithmLexerTest { fun registerAndParseCustomAction() { assertEquals( listOf( - CustomAction(Integer.instantiate(2)) + CustomAction(IntegerFactory.instantiate(2)) ), AlgorithmLexer("custom(2)").registerKeyword("custom", CustomAction::handler).execute() ) diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt index d876d36..8c1d6b5 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt @@ -3,14 +3,14 @@ package me.nathanfallet.makth.lexers import me.nathanfallet.makth.extensions.BooleanValue import me.nathanfallet.makth.extensions.StringValue import me.nathanfallet.makth.lexers.AlgorithmLexer.SyntaxException -import me.nathanfallet.makth.numbers.Integer -import me.nathanfallet.makth.numbers.Rational -import me.nathanfallet.makth.numbers.Real +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.numbers.rationals.RationalFactory +import me.nathanfallet.makth.numbers.reals.RealFactory import me.nathanfallet.makth.operations.* import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable -import me.nathanfallet.makth.sets.Matrix -import me.nathanfallet.makth.sets.Vector +import me.nathanfallet.makth.resolvables.variables.VariableFactory +import me.nathanfallet.makth.sets.matrixes.MatrixFactory +import me.nathanfallet.makth.sets.vectors.VectorFactory import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -21,7 +21,7 @@ class MathLexerTest { private val contextWithX = Context( mapOf( - "x" to Integer.instantiate(2) + "x" to IntegerFactory.instantiate(2) ) ) @@ -57,44 +57,44 @@ class MathLexerTest { @Test fun parseVariable() { - assertEquals(Variable.instantiate("x"), MathLexer("x").execute(context)) + assertEquals(VariableFactory.instantiate("x"), MathLexer("x").execute(context)) } @Test fun parsePi() { - assertEquals(Real.pi, MathLexer("pi").execute(context)) - assertEquals(Real.pi, MathLexer("\u03C0").execute(context)) + assertEquals(RealFactory.pi, MathLexer("pi").execute(context)) + assertEquals(RealFactory.pi, MathLexer("\u03C0").execute(context)) } @Test fun parseInteger() { - assertEquals(Integer.instantiate(2), MathLexer("2").execute(context)) + assertEquals(IntegerFactory.instantiate(2), MathLexer("2").execute(context)) } @Test fun parseInteger2() { - assertEquals(Integer.instantiate(-2), MathLexer("-2").execute(context)) + assertEquals(IntegerFactory.instantiate(-2), MathLexer("-2").execute(context)) } @Test fun parseInteger3() { - assertEquals(Integer.instantiate(-2), MathLexer("(-2)").execute(context)) + assertEquals(IntegerFactory.instantiate(-2), MathLexer("(-2)").execute(context)) } @Test fun parseSum() { - assertEquals(Integer.instantiate(3), MathLexer("1 + 2").execute(context)) + assertEquals(IntegerFactory.instantiate(3), MathLexer("1 + 2").execute(context)) } @Test fun parseSum2() { - assertEquals(Integer.instantiate(-1), MathLexer("1 + (-2)").execute(context)) + assertEquals(IntegerFactory.instantiate(-1), MathLexer("1 + (-2)").execute(context)) } @Test fun parseSumWithVariable() { assertEquals( - Sum(Integer.instantiate(1), Variable.instantiate("x")), + Sum(IntegerFactory.instantiate(1), VariableFactory.instantiate("x")), MathLexer("1 + x").execute(context) ) } @@ -102,20 +102,23 @@ class MathLexerTest { @Test fun parseSumWithVariableAndContext() { assertEquals( - Integer.instantiate(3), + IntegerFactory.instantiate(3), MathLexer("1 + x").execute(contextWithX) ) } @Test fun parseDifference() { - assertEquals(Integer.instantiate(-1), MathLexer("1 - 2").execute(context)) + assertEquals(IntegerFactory.instantiate(-1), MathLexer("1 - 2").execute(context)) } @Test fun parseDifferenceWithVariable() { assertEquals( - Sum(Integer.instantiate(1), Product(Integer.instantiate(-1), Variable.instantiate("x"))), + Sum( + IntegerFactory.instantiate(1), + Product(IntegerFactory.instantiate(-1), VariableFactory.instantiate("x")) + ), MathLexer("1 - x").execute(context) ) } @@ -123,25 +126,25 @@ class MathLexerTest { @Test fun parseDifferenceWithVariableAndContext() { assertEquals( - Integer.instantiate(-1), + IntegerFactory.instantiate(-1), MathLexer("1 - x").execute(contextWithX) ) } @Test fun parseProduct() { - assertEquals(Integer.instantiate(6), MathLexer("2 * 3").execute(context)) + assertEquals(IntegerFactory.instantiate(6), MathLexer("2 * 3").execute(context)) } @Test fun parseProduct2() { - assertEquals(Integer.instantiate(-6), MathLexer("2 * (-3)").execute(context)) + assertEquals(IntegerFactory.instantiate(-6), MathLexer("2 * (-3)").execute(context)) } @Test fun parseProductWithVariable() { assertEquals( - Product(Integer.instantiate(2), Variable.instantiate("x")), + Product(IntegerFactory.instantiate(2), VariableFactory.instantiate("x")), MathLexer("2 * x").execute(context) ) } @@ -149,25 +152,25 @@ class MathLexerTest { @Test fun parseProductWithVariableAndContext() { assertEquals( - Integer.instantiate(4), + IntegerFactory.instantiate(4), MathLexer("2 * x").execute(contextWithX) ) } @Test fun parseQuotient() { - assertEquals(Rational.instantiate(2, 3), MathLexer("2 / 3").execute(context)) + assertEquals(RationalFactory.instantiate(2, 3), MathLexer("2 / 3").execute(context)) } @Test fun parseQuotient2() { - assertEquals(Rational.instantiate(-2, 3), MathLexer("2 / (-3)").execute(context)) + assertEquals(RationalFactory.instantiate(-2, 3), MathLexer("2 / (-3)").execute(context)) } @Test fun parseQuotientWithVariable() { assertEquals( - Quotient(Integer.instantiate(2), Variable.instantiate("x")), + Quotient(IntegerFactory.instantiate(2), VariableFactory.instantiate("x")), MathLexer("2 / x").execute(context) ) } @@ -175,25 +178,25 @@ class MathLexerTest { @Test fun parseQuotientWithVariableAndContext() { assertEquals( - Integer.instantiate(1), + IntegerFactory.instantiate(1), MathLexer("2 / x").execute(contextWithX) ) } @Test fun parseRemainder() { - assertEquals(Integer.instantiate(2), MathLexer("2 % 3").execute(context)) + assertEquals(IntegerFactory.instantiate(2), MathLexer("2 % 3").execute(context)) } @Test fun parseRemainder2() { - assertEquals(Integer.instantiate(2), MathLexer("2 % (-3)").execute(context)) + assertEquals(IntegerFactory.instantiate(2), MathLexer("2 % (-3)").execute(context)) } @Test fun parseRemainderWithVariable() { assertEquals( - Remainder(Integer.instantiate(2), Variable.instantiate("x")), + Remainder(IntegerFactory.instantiate(2), VariableFactory.instantiate("x")), MathLexer("2 % x").execute(context) ) } @@ -201,25 +204,25 @@ class MathLexerTest { @Test fun parseRemainderWithVariableAndContext() { assertEquals( - Integer.instantiate(0), + IntegerFactory.instantiate(0), MathLexer("2 % x").execute(contextWithX) ) } @Test fun parseExponentiate() { - assertEquals(Integer.instantiate(8), MathLexer("2 ^ 3").execute(context)) + assertEquals(IntegerFactory.instantiate(8), MathLexer("2 ^ 3").execute(context)) } @Test fun parseExponentiate2() { - assertEquals(Rational.instantiate(1, 8), MathLexer("2 ^ (-3)").execute(context)) + assertEquals(RationalFactory.instantiate(1, 8), MathLexer("2 ^ (-3)").execute(context)) } @Test fun parseExponentiateWithVariable() { assertEquals( - Exponentiation(Integer.instantiate(2), Variable.instantiate("x")), + Exponentiation(IntegerFactory.instantiate(2), VariableFactory.instantiate("x")), MathLexer("2 ^ x").execute(context) ) } @@ -227,7 +230,7 @@ class MathLexerTest { @Test fun parseExponentiateWithVariableAndContext() { assertEquals( - Integer.instantiate(4), + IntegerFactory.instantiate(4), MathLexer("2 ^ x").execute(contextWithX) ) } @@ -235,7 +238,7 @@ class MathLexerTest { @Test fun parseExpressionWithParentheses() { assertEquals( - Integer.instantiate(9), + IntegerFactory.instantiate(9), MathLexer("(1 + 2) * (9 / (1 + 2))").execute(context) ) } @@ -243,7 +246,7 @@ class MathLexerTest { @Test fun parseExpressionWithoutParentheses() { assertEquals( - Integer.instantiate(26), + IntegerFactory.instantiate(26), MathLexer("2 * 3 + 4 * 5").execute(context) ) } @@ -251,7 +254,7 @@ class MathLexerTest { @Test fun parseEquality() { assertEquals( - Equality(Variable.instantiate("x"), Integer.instantiate(2)), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), MathLexer("x = 2").execute(context) ) } @@ -267,7 +270,7 @@ class MathLexerTest { @Test fun parseEqualityNotEquals() { assertEquals( - Equality(Variable.instantiate("x"), Integer.instantiate(2), Equality.Operator.NotEquals), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2), Equality.Operator.NotEquals), MathLexer("x != 2").execute(context) ) } @@ -275,7 +278,7 @@ class MathLexerTest { @Test fun parseEqualityLessThan() { assertEquals( - Equality(Variable.instantiate("x"), Integer.instantiate(2), Equality.Operator.LessThan), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2), Equality.Operator.LessThan), MathLexer("x < 2").execute(context) ) } @@ -283,7 +286,7 @@ class MathLexerTest { @Test fun parseEqualityGreaterThan() { assertEquals( - Equality(Variable.instantiate("x"), Integer.instantiate(2), Equality.Operator.GreaterThan), + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2), Equality.Operator.GreaterThan), MathLexer("x > 2").execute(context) ) } @@ -291,7 +294,11 @@ class MathLexerTest { @Test fun parseEqualityLessThanOrEquals() { assertEquals( - Equality(Variable.instantiate("x"), Integer.instantiate(2), Equality.Operator.LessThanOrEquals), + Equality( + VariableFactory.instantiate("x"), + IntegerFactory.instantiate(2), + Equality.Operator.LessThanOrEquals + ), MathLexer("x <= 2").execute(context) ) } @@ -299,7 +306,11 @@ class MathLexerTest { @Test fun parseEqualityGreaterThanOrEquals() { assertEquals( - Equality(Variable.instantiate("x"), Integer.instantiate(2), Equality.Operator.GreaterThanOrEquals), + Equality( + VariableFactory.instantiate("x"), + IntegerFactory.instantiate(2), + Equality.Operator.GreaterThanOrEquals + ), MathLexer("x >= 2").execute(context) ) } @@ -307,7 +318,13 @@ class MathLexerTest { @Test fun parseVector() { assertEquals( - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))), + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ), MathLexer("(1; 2; 3)").execute(context) ) } @@ -315,10 +332,11 @@ class MathLexerTest { @Test fun parseMatrix() { assertEquals( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3)), - listOf(Integer.instantiate(4), Integer.instantiate(5), Integer.instantiate(6)), - listOf(Integer.instantiate(7), Integer.instantiate(8), Integer.instantiate(9)) + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2), IntegerFactory.instantiate(3)), + listOf(IntegerFactory.instantiate(4), IntegerFactory.instantiate(5), IntegerFactory.instantiate(6)), + listOf(IntegerFactory.instantiate(7), IntegerFactory.instantiate(8), IntegerFactory.instantiate(9)) )), MathLexer("(1, 2, 3; 4, 5, 6; 7, 8, 9)").execute(context) ) diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/IntegerTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/IntegerTest.kt deleted file mode 100644 index 011aa7e..0000000 --- a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/IntegerTest.kt +++ /dev/null @@ -1,218 +0,0 @@ -package me.nathanfallet.makth.numbers - -import me.nathanfallet.makth.sets.Matrix -import kotlin.math.PI -import kotlin.test.Test -import kotlin.test.assertEquals - -class IntegerTest { - - @Test - fun rawString() { - assertEquals("-1", Integer.instantiate(-1).rawString) - } - - @Test - fun laTeXString() { - assertEquals("-1", Integer.instantiate(-1).laTeXString) - } - - @Test - fun correctNumerator() { - assertEquals( - Integer.instantiate(-2), - Integer.instantiate(-2).numerator - ) - } - - @Test - fun correctDenominator() { - assertEquals( - Integer.instantiate(1), - Integer.instantiate(-2).denominator - ) - } - - @Test - fun sumCorrectNatural() { - // -2 + 1 = -1 - assertEquals(Integer.instantiate(-1), Integer.instantiate(-2).sum(Integer.instantiate(1))) - } - - @Test - fun sumCorrectInteger() { - // -2 + -1 = -3 - assertEquals(Integer.instantiate(-3), Integer.instantiate(-2).sum(Integer.instantiate(-1))) - } - - @Test - fun sumCorrectRational() { - // -1 + 1/2 = -1/2 - assertEquals( - Rational.instantiate(-1, 2), - Integer.instantiate(-1).sum(Rational.instantiate(1, 2)) - ) - } - - @Test - fun sumCorrectReal() { - // -2 + pi = -2 + pi - assertEquals( - Real.instantiate(-2 + PI), - Integer.instantiate(-2).sum(Real.pi) - ) - } - - @Test - fun multiplyCorrectNatural() { - // -2 * 3 = -6 - assertEquals( - Integer.instantiate(-6), - Integer.instantiate(-2).multiply(Integer.instantiate(3)) - ) - } - - @Test - fun multiplyCorrectInteger() { - // -2 * -3 = 6 - assertEquals( - Integer.instantiate(6), - Integer.instantiate(-2).multiply(Integer.instantiate(-3)) - ) - } - - @Test - fun multiplyCorrectRational() { - // -3 * 1/2 = -3/2 - assertEquals( - Rational.instantiate(-3, 2), - Integer.instantiate(-3).multiply(Rational.instantiate(1, 2)) - ) - } - - @Test - fun multiplyCorrectReal() { - // -2 * pi = -2 * pi - assertEquals( - Real.instantiate(-2 * PI), - Integer.instantiate(-2).multiply(Real.pi) - ) - } - - @Test - fun multiplyCorrectMatrix() { - // -2 * [[1, 2], [3, 4]] = [[-2, -4], [-6, -8]] - assertEquals( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(-2), Integer.instantiate(-4)), - listOf(Integer.instantiate(-6), Integer.instantiate(-8)) - )), - Integer.instantiate(-2).multiply( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )) - ) - ) - } - - @Test - fun divideCorrectNatural() { - // -2 / 3 = -2/3 - assertEquals( - Rational.instantiate(-2, 3), - Integer.instantiate(-2).divide(Integer.instantiate(3)) - ) - } - - @Test - fun divideCorrectInteger() { - // -2 / -3 = 2/3 - assertEquals( - Rational.instantiate(2, 3), - Integer.instantiate(-2).divide(Integer.instantiate(-3)) - ) - } - - @Test - fun divideCorrectRational() { - // -3 / (5/2) = -6/5 - assertEquals( - Rational.instantiate(-6, 5), - Integer.instantiate(-3).divide(Rational.instantiate(5, 2)) - ) - } - - @Test - fun divideCorrectReal() { - // -2 / pi = -2 / pi - assertEquals( - Real.instantiate(-2 / PI), - Integer.instantiate(-2).divide(Real.pi) - ) - } - - @Test - fun remainderCorrectNatural() { - // -5 % 3 = -2 - assertEquals( - Integer.instantiate(-2), - Integer.instantiate(-5).remainder(Integer.instantiate(3)) - ) - } - - @Test - fun remainderCorrectInteger() { - // -5 % -3 = -2 - assertEquals( - Integer.instantiate(-2), - Integer.instantiate(-5).remainder(Integer.instantiate(-3)) - ) - } - - @Test - fun remainderCorrectRational() { - // -2 % (3/2) = -1/2 - assertEquals( - Rational.instantiate(-1, 2), - Integer.instantiate(-2).remainder(Rational.instantiate(3, 2)) - ) - } - - @Test - fun remainderCorrectReal() { - // -4 % pi = -4%pi - assertEquals( - Real.instantiate(-4 % PI), - Integer.instantiate(-4).remainder(Real.pi) - ) - } - - @Test - fun raiseCorrectNatural() { - // -2 ^ 2 = 4 - assertEquals( - Integer.instantiate(4), - Integer.instantiate(-2).raise(Integer.instantiate(2)) - ) - } - - @Test - fun raiseCorrectInteger() { - // -2 ^ -2 = 1/4 - assertEquals( - Rational.instantiate(1, 4), - Integer.instantiate(-2).raise(Integer.instantiate(-2)) - ) - } - - @Test - fun raiseCorrectRational() { - // -8 ^ (1/3) = -2 - assertEquals( - Integer.instantiate(-2), - Integer.instantiate(-8).raise(Rational.instantiate(1, 3)) - ) - } - -} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/NaturalTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/NaturalTest.kt deleted file mode 100644 index 9d5ba00..0000000 --- a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/NaturalTest.kt +++ /dev/null @@ -1,220 +0,0 @@ -package me.nathanfallet.makth.numbers - -import me.nathanfallet.makth.sets.Matrix -import kotlin.math.PI -import kotlin.math.pow -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith - -class NaturalTest { - - @Test - fun rawString() { - assertEquals("5", Integer.instantiate(5).rawString) - } - - @Test - fun laTeXString() { - assertEquals("5", Integer.instantiate(5).laTeXString) - } - - @Test - fun negativeThrows() { - assertFailsWith(IllegalArgumentException::class) { - Natural.instantiate(-1) - } - } - - @Test - fun sumCorrectNatural() { - // 2 + 3 = 5 - assertEquals(Integer.instantiate(5), Integer.instantiate(2).sum(Integer.instantiate(3))) - } - - @Test - fun sumCorrectInteger() { - // 2 + -3 = -1 - assertEquals(Integer.instantiate(-1), Integer.instantiate(2).sum(Integer.instantiate(-3))) - } - - @Test - fun sumCorrectRational() { - // 1 + 1/2 = 3/2 - assertEquals( - Rational.instantiate(3, 2), - Integer.instantiate(1).sum(Rational.instantiate(1, 2)) - ) - } - - @Test - fun sumCorrectReal() { - // 2 + pi = 2 + pi - assertEquals( - Real.instantiate(2 + PI), - Integer.instantiate(2).sum(Real.pi) - ) - } - - @Test - fun multiplyCorrectNatural() { - // 2 * 3 = 6 - assertEquals( - Integer.instantiate(6), - Integer.instantiate(2).multiply(Integer.instantiate(3)) - ) - } - - @Test - fun multiplyCorrectInteger() { - // 2 * -3 = -6 - assertEquals( - Integer.instantiate(-6), - Integer.instantiate(2).multiply(Integer.instantiate(-3)) - ) - } - - @Test - fun multiplyCorrectRational() { - // 3 * 1/2 = 3/2 - assertEquals( - Rational.instantiate(3, 2), - Integer.instantiate(3).multiply(Rational.instantiate(1, 2)) - ) - } - - @Test - fun multiplyCorrectReal() { - // 2 * pi = 2 * pi - assertEquals( - Real.instantiate(2 * PI), - Integer.instantiate(2).multiply(Real.pi) - ) - } - - @Test - fun multiplyCorrectMatrix() { - // 2 * [[1, 2], [3, 4]] = [[2, 4], [6, 8]] - assertEquals( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(2), Integer.instantiate(4)), - listOf(Integer.instantiate(6), Integer.instantiate(8)) - )), - Integer.instantiate(2).multiply( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )) - ) - ) - } - - @Test - fun divideCorrectNatural() { - // 2 / 3 = 2/3 - assertEquals( - Rational.instantiate(2, 3), - Integer.instantiate(2).divide(Integer.instantiate(3)) - ) - } - - @Test - fun divideCorrectInteger() { - // 2 / -3 = -2/3 - assertEquals( - Rational.instantiate(-2, 3), - Integer.instantiate(2).divide(Integer.instantiate(-3)) - ) - } - - @Test - fun divideCorrectRational() { - // 3 / (5/2) = 6/5 - assertEquals( - Rational.instantiate(6, 5), - Integer.instantiate(3).divide(Rational.instantiate(5, 2)) - ) - } - - @Test - fun divideCorrectReal() { - // 2 / pi = 2 / pi - assertEquals( - Real.instantiate(2 / PI), - Integer.instantiate(2).divide(Real.pi) - ) - } - - @Test - fun remainderCorrectNatural() { - // 5 % 3 = 2 - assertEquals( - Integer.instantiate(2), - Integer.instantiate(5).remainder(Integer.instantiate(3)) - ) - } - - @Test - fun remainderCorrectInteger() { - // 5 % -3 = 2 - assertEquals( - Integer.instantiate(2), - Integer.instantiate(5).remainder(Integer.instantiate(-3)) - ) - } - - @Test - fun remainderCorrectRational() { - // 2 % (3/2) = 1/2 - assertEquals( - Rational.instantiate(1, 2), - Integer.instantiate(2).remainder(Rational.instantiate(3, 2)) - ) - } - - @Test - fun remainderCorrectReal() { - // 4 % pi = 4%pi - assertEquals( - Real.instantiate(4 % PI), - Integer.instantiate(4).remainder(Real.pi) - ) - } - - @Test - fun raiseCorrectNatural() { - // 2 ^ 3 = 8 - assertEquals( - Integer.instantiate(8), - Integer.instantiate(2).raise(Integer.instantiate(3)) - ) - } - - @Test - fun raiseCorrectInteger() { - // 2 ^ -3 = 1/8 - assertEquals( - Rational.instantiate(1, 8), - Integer.instantiate(2).raise(Integer.instantiate(-3)) - ) - } - - @Test - fun raiseCorrectRational() { - // 64 ^ (2/3) = 16 - assertEquals( - Integer.instantiate(16), - Integer.instantiate(64).raise(Rational.instantiate(2, 3)) - ) - } - - @Test - fun raiseCorrectReal() { - // 2 ^ pi = 2^pi - assertEquals( - Real.instantiate(2.0.pow(PI)), - Integer.instantiate(2).raise(Real.pi) - ) - } - -} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/RationalTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/RationalTest.kt deleted file mode 100644 index 0b203b3..0000000 --- a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/RationalTest.kt +++ /dev/null @@ -1,252 +0,0 @@ -package me.nathanfallet.makth.numbers - -import me.nathanfallet.makth.sets.Matrix -import kotlin.math.PI -import kotlin.math.pow -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith - -class RationalTest { - - @Test - fun rawString() { - assertEquals("-1/2", Rational.instantiate(-1, 2).rawString) - } - - @Test - fun laTeXString() { - assertEquals("\\frac{-1}{2}", Rational.instantiate(-1, 2).laTeXString) - } - - @Test - fun nullDenominatorThrows() { - assertFailsWith(IllegalArgumentException::class) { - Rational.instantiate(1, 0) - } - } - - @Test - fun simplifyToRational() { - // 2/4 = 1/2 - assertEquals( - Rational.instantiate(1, 2), - Rational.instantiate(2, 4) - ) - } - - @Test - fun simplifyToInteger() { - // 4/2 = 2 - assertEquals( - Integer.instantiate(2), - Rational.instantiate(4, 2) - ) - } - - @Test - fun absoluteValue() { - assertEquals( - Rational.instantiate(1, 2), - Rational.instantiate(-1, 2).absoluteValue - ) - } - - @Test - fun sumCorrectNatural() { - // 1/2 + 1 = 3/2 - assertEquals( - Rational.instantiate(3, 2), - Rational.instantiate(1, 2).sum(Integer.instantiate(1)) - ) - } - - @Test - fun sumCorrectInteger() { - // 1/2 + -1 = -1/2 - assertEquals( - Rational.instantiate(-1, 2), - Rational.instantiate(1, 2).sum(Integer.instantiate(-1)) - ) - } - - @Test - fun sumCorrectRational() { - // 1/2 + 1/3 = 5/6 - assertEquals( - Rational.instantiate(5, 6), - Rational.instantiate(1, 2).sum(Rational.instantiate(1, 3)) - ) - } - - @Test - fun sumCorrectReal() { - // 1/2 + pi = 1/2 + pi - assertEquals( - Real.instantiate(0.5 + PI), - Rational.instantiate(1, 2).sum(Real.pi) - ) - } - - @Test - fun multiplyCorrectNatural() { - // 1/2 * 3 = 3/2 - assertEquals( - Rational.instantiate(3, 2), - Rational.instantiate(1, 2).multiply(Integer.instantiate(3)) - ) - } - - @Test - fun multiplyCorrectInteger() { - // 1/2 * -3 = -3/2 - assertEquals( - Rational.instantiate(-3, 2), - Rational.instantiate(1, 2).multiply(Integer.instantiate(-3)) - ) - } - - @Test - fun multiplyCorrectRational() { - // 1/2 * 1/3 = 1/6 - assertEquals( - Rational.instantiate(1, 6), - Rational.instantiate(1, 2).multiply(Rational.instantiate(1, 3)) - ) - } - - @Test - fun multiplyCorrectReal() { - // 1/2 * pi = 1/2 * pi - assertEquals( - Real.instantiate(0.5 * PI), - Rational.instantiate(1, 2).multiply(Real.pi) - ) - } - - @Test - fun mutliplyCorrectMatrix() { - // 1/2 * [[1, 2], [3, 4]] = [[1/2, 1], [3/2, 2]] - assertEquals( - Matrix.instantiate(listOf( - listOf(Rational.instantiate(1, 2), Integer.instantiate(1)), - listOf(Rational.instantiate(3, 2), Integer.instantiate(2)) - )), - Rational.instantiate(1, 2).multiply( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )) - ) - ) - } - - @Test - fun divideCorrectNatural() { - // 1/2 / 3 = 1/6 - assertEquals( - Rational.instantiate(1, 6), - Rational.instantiate(1, 2).divide(Integer.instantiate(3)) - ) - } - - @Test - fun divideCorrectInteger() { - // 1/2 / -3 = -1/6 - assertEquals( - Rational.instantiate(-1, 6), - Rational.instantiate(1, 2).divide(Integer.instantiate(-3)) - ) - } - - @Test - fun divideCorrectRational() { - // 1/2 / 1/3 = 3/2 - assertEquals( - Rational.instantiate(3, 2), - Rational.instantiate(1, 2).divide(Rational.instantiate(1, 3)) - ) - } - - @Test - fun divideCorrectReal() { - // 1/2 / pi = 1/2 * pi - assertEquals( - Real.instantiate(0.5 / PI), - Rational.instantiate(1, 2).divide(Real.pi) - ) - } - - @Test - fun remainderCorrectNatural() { - // 5/2 % 2 = 1/2 - assertEquals( - Rational.instantiate(1, 2), - Rational.instantiate(5, 2).remainder(Integer.instantiate(2)) - ) - } - - @Test - fun remainderCorrectInteger() { - // 5/2 % -2 = 1/2 - assertEquals( - Rational.instantiate(1, 2), - Rational.instantiate(5, 2).remainder(Integer.instantiate(-2)) - ) - } - - @Test - fun remainderCorrectRational() { - // 5/2 % 1/3 = 1/6 - assertEquals( - Rational.instantiate(1, 6), - Rational.instantiate(5, 2).remainder(Rational.instantiate(1, 3)) - ) - } - - @Test - fun remainderCorrectReal() { - // 7/2 % pi = 7/2%pi - assertEquals( - Real.instantiate(3.5 % PI), - Rational.instantiate(7, 2).remainder(Real.pi) - ) - } - - @Test - fun raiseCorrectNatural() { - // 2/3 ^ 2 = 4/9 - assertEquals( - Rational.instantiate(4, 9), - Rational.instantiate(2, 3).raise(Integer.instantiate(2)) - ) - } - - @Test - fun raiseCorrectInteger() { - // 2/3 ^ -2 = 9/4 - assertEquals( - Rational.instantiate(9, 4), - Rational.instantiate(2, 3).raise(Integer.instantiate(-2)) - ) - } - - @Test - fun raiseCorrectRational() { - // 4/9 ^ 1/2 = 2/3 - assertEquals( - Rational.instantiate(2, 3), - Rational.instantiate(4, 9).raise(Rational.instantiate(1, 2)) - ) - } - - @Test - fun raiseCorrectReal() { - // 1/2 ^ pi = 1/(2^pi) - assertEquals( - Real.instantiate(1.0 / 2.0.pow(PI)), - Rational.instantiate(1, 2).raise(Real.pi) - ) - } - -} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/RealTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/RealTest.kt deleted file mode 100644 index 373cc6e..0000000 --- a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/RealTest.kt +++ /dev/null @@ -1,300 +0,0 @@ -package me.nathanfallet.makth.numbers - -import me.nathanfallet.makth.extensions.StringValue -import me.nathanfallet.makth.sets.Matrix -import kotlin.math.PI -import kotlin.math.pow -import kotlin.math.sqrt -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith - -class RealTest { - - @Test - fun rawString() { - assertEquals(1.23456789.toString(), Real.instantiate(1.23456789).rawString) - } - - @Test - fun laTeXString() { - assertEquals(1.23456789.toString(), Real.instantiate(1.23456789).laTeXString) - } - - @Test - fun absoluteValue() { - assertEquals( - Real.instantiate(1.23456789), - Real.instantiate(-1.23456789).absoluteValue - ) - } - - @Test - fun testEqualsTrue() { - assertEquals(true, Real.instantiate(1.23456789).equals(Real.instantiate(1.23456789))) - } - - @Test - fun testEqualsFalse() { - assertEquals(false, Real.instantiate(1.23456789).equals(Real.instantiate(9.87654321))) - } - - @Test - fun testLessThanTrue() { - assertEquals(true, Real.instantiate(1.23456789).lessThan(Real.instantiate(9.87654321))) - } - - @Test - fun testLessThanFalse() { - assertEquals(false, Real.instantiate(9.87654321).lessThan(Real.instantiate(1.23456789))) - } - - @Test - fun sumCorrectNatural() { - // pi + 1 = 1 + pi - assertEquals( - Real.instantiate(1 + PI), - Real.pi.sum(Integer.instantiate(1)) - ) - } - - @Test - fun sumCorrectInteger() { - // pi + -1 = pi - 1 - assertEquals( - Real.instantiate(PI - 1), - Real.pi.sum(Integer.instantiate(-1)) - ) - } - - @Test - fun sumCorrectRational() { - // pi + 1/3 = 1/3 + pi - assertEquals( - Real.instantiate(1.0 / 3.0 + PI), - Real.pi.sum(Rational.instantiate(1, 3)) - ) - } - - @Test - fun sumCorrectReal() { - // pi + pi = 2pi - assertEquals( - Real.instantiate(2 * PI), - Real.pi.sum(Real.pi) - ) - } - - @Test - fun sumUnsupported() { - assertFailsWith(UnsupportedOperationException::class) { - Real.pi.sum(StringValue("test")) - } - } - - @Test - fun multiplyCorrectNatural() { - // pi * 3 = 3pi - assertEquals( - Real.instantiate(3 * PI), - Real.pi.multiply(Integer.instantiate(3)) - ) - } - - @Test - fun multiplyCorrectInteger() { - // pi * -3 = -3pi - assertEquals( - Real.instantiate(-3 * PI), - Real.pi.multiply(Integer.instantiate(-3)) - ) - } - - @Test - fun multiplyCorrectRational() { - // pi * 1/3 = pi/3 - assertEquals( - Real.instantiate(PI / 3), - Real.pi.multiply(Rational.instantiate(1, 3)) - ) - } - - @Test - fun multiplyCorrectReal() { - // pi * pi = pi^2 - assertEquals( - Real.instantiate(PI * PI), - Real.pi.multiply(Real.pi) - ) - } - - @Test - fun multiplyCorrectVector() { - // pi * [[1, 2], [3, 4]] = [[pi, 2pi], [3pi, 4pi]] - assertEquals( - Matrix.instantiate(listOf( - listOf(Real.pi, Real.pi.multiply(Integer.instantiate(2))), - listOf(Real.pi.multiply(Integer.instantiate(3)), Real.pi.multiply(Integer.instantiate(4))) - )), - Real.pi.multiply(Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - ))) - ) - } - - @Test - fun multiplyUnsupported() { - assertFailsWith(UnsupportedOperationException::class) { - Real.pi.multiply(StringValue("test")) - } - } - - @Test - fun divideCorrectNatural() { - // pi / 3 = pi/3 - assertEquals( - Real.instantiate(PI / 3), - Real.pi.divide(Integer.instantiate(3)) - ) - } - - @Test - fun divideCorrectInteger() { - // pi / -3 = -pi/3 - assertEquals( - Real.instantiate(-PI / 3), - Real.pi.divide(Integer.instantiate(-3)) - ) - } - - @Test - fun divideCorrectRational() { - // pi / 1/3 = 3pi - assertEquals( - Real.instantiate(3 * PI), - Real.pi.divide(Rational.instantiate(1, 3)) - ) - } - - @Test - fun divideCorrectReal() { - // pi / pi = 1 - assertEquals( - Integer.instantiate(1), - Real.pi.divide(Real.pi) - ) - } - - @Test - fun divideUnsupported() { - assertFailsWith(UnsupportedOperationException::class) { - Real.pi.divide(StringValue("test")) - } - } - - @Test - fun remainderCorrectNatural() { - // pi % 3 = pi%3 - assertEquals( - Real.instantiate(PI % 3), - Real.pi.remainder(Integer.instantiate(3)) - ) - } - - @Test - fun remainderCorrectInteger() { - // pi % -3 = pi%(-3) - assertEquals( - Real.instantiate(PI % -3), - Real.pi.remainder(Integer.instantiate(-3)) - ) - } - - @Test - fun remainderCorrectRational() { - // pi % 1/2 = pi%1/2 - assertEquals( - Real.instantiate(PI % 0.5), - Real.pi.remainder(Rational.instantiate(1, 2)) - ) - } - - @Test - fun remainderCorrectReal() { - // pi % pi = 0 - assertEquals( - Integer.instantiate(0), - Real.pi.remainder(Real.pi) - ) - } - - @Test - fun remainderUnsupported() { - assertFailsWith(UnsupportedOperationException::class) { - Real.pi.remainder(StringValue("test")) - } - } - - @Test - fun raiseCorrectNatural() { - // pi ^ 2 = pi^2 - assertEquals( - Real.instantiate(PI * PI), - Real.pi.raise(Integer.instantiate(2)) - ) - } - - @Test - fun raiseCorrectInteger() { - // pi ^ -2 = 1/(pi^2) - assertEquals( - Real.instantiate(1/(PI * PI)), - Real.pi.raise(Integer.instantiate(-2)) - ) - } - - @Test - fun raiseCorrectRational() { - // pi ^ 1/2 = sqrt(pi) - assertEquals( - Real.instantiate(sqrt(PI)), - Real.pi.raise(Rational.instantiate(1, 2)) - ) - } - - @Test - fun raiseCorrectReal() { - // pi ^ pi = pi^pi - assertEquals( - Real.instantiate(PI.pow(PI)), - Real.pi.raise(Real.pi) - ) - } - - @Test - fun raiseUnsupported() { - assertFailsWith(UnsupportedOperationException::class) { - Real.pi.raise(StringValue("test")) - } - } - - @Test - fun instantiatePi() { - assertEquals( - Real.pi, - Real.instantiate(PI) - ) - } - - @Test - fun piRawString() { - assertEquals("\u03C0", Real.pi.rawString) - } - - @Test - fun piLaTeXString() { - assertEquals("\\pi", Real.pi.laTeXString) - } - -} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/integers/IntegerTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/integers/IntegerTest.kt new file mode 100644 index 0000000..c92c7aa --- /dev/null +++ b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/integers/IntegerTest.kt @@ -0,0 +1,224 @@ +package me.nathanfallet.makth.numbers.integers + +import me.nathanfallet.makth.numbers.rationals.RationalFactory +import me.nathanfallet.makth.numbers.reals.RealFactory +import me.nathanfallet.makth.sets.matrixes.MatrixFactory +import kotlin.math.PI +import kotlin.test.Test +import kotlin.test.assertEquals + +class IntegerTest { + + @Test + fun rawString() { + assertEquals("-1", IntegerFactory.instantiate(-1).rawString) + } + + @Test + fun laTeXString() { + assertEquals("-1", IntegerFactory.instantiate(-1).laTeXString) + } + + @Test + fun correctNumerator() { + assertEquals( + IntegerFactory.instantiate(-2), + IntegerFactory.instantiate(-2).numerator + ) + } + + @Test + fun correctDenominator() { + assertEquals( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(-2).denominator + ) + } + + @Test + fun sumCorrectNatural() { + // -2 + 1 = -1 + assertEquals(IntegerFactory.instantiate(-1), IntegerFactory.instantiate(-2).sum(IntegerFactory.instantiate(1))) + } + + @Test + fun sumCorrectInteger() { + // -2 + -1 = -3 + assertEquals(IntegerFactory.instantiate(-3), IntegerFactory.instantiate(-2).sum(IntegerFactory.instantiate(-1))) + } + + @Test + fun sumCorrectRational() { + // -1 + 1/2 = -1/2 + assertEquals( + RationalFactory.instantiate(-1, 2), + IntegerFactory.instantiate(-1).sum(RationalFactory.instantiate(1, 2)) + ) + } + + @Test + fun sumCorrectReal() { + // -2 + pi = -2 + pi + assertEquals( + RealFactory.instantiate(-2 + PI), + IntegerFactory.instantiate(-2).sum(RealFactory.pi) + ) + } + + @Test + fun multiplyCorrectNatural() { + // -2 * 3 = -6 + assertEquals( + IntegerFactory.instantiate(-6), + IntegerFactory.instantiate(-2).multiply(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun multiplyCorrectInteger() { + // -2 * -3 = 6 + assertEquals( + IntegerFactory.instantiate(6), + IntegerFactory.instantiate(-2).multiply(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun multiplyCorrectRational() { + // -3 * 1/2 = -3/2 + assertEquals( + RationalFactory.instantiate(-3, 2), + IntegerFactory.instantiate(-3).multiply(RationalFactory.instantiate(1, 2)) + ) + } + + @Test + fun multiplyCorrectReal() { + // -2 * pi = -2 * pi + assertEquals( + RealFactory.instantiate(-2 * PI), + IntegerFactory.instantiate(-2).multiply(RealFactory.pi) + ) + } + + @Test + fun multiplyCorrectMatrix() { + // -2 * [[1, 2], [3, 4]] = [[-2, -4], [-6, -8]] + assertEquals( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(-2), IntegerFactory.instantiate(-4)), + listOf(IntegerFactory.instantiate(-6), IntegerFactory.instantiate(-8)) + ) + ), + IntegerFactory.instantiate(-2).multiply( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ) + ) + ) + } + + @Test + fun divideCorrectNatural() { + // -2 / 3 = -2/3 + assertEquals( + RationalFactory.instantiate(-2, 3), + IntegerFactory.instantiate(-2).divide(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun divideCorrectInteger() { + // -2 / -3 = 2/3 + assertEquals( + RationalFactory.instantiate(2, 3), + IntegerFactory.instantiate(-2).divide(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun divideCorrectRational() { + // -3 / (5/2) = -6/5 + assertEquals( + RationalFactory.instantiate(-6, 5), + IntegerFactory.instantiate(-3).divide(RationalFactory.instantiate(5, 2)) + ) + } + + @Test + fun divideCorrectReal() { + // -2 / pi = -2 / pi + assertEquals( + RealFactory.instantiate(-2 / PI), + IntegerFactory.instantiate(-2).divide(RealFactory.pi) + ) + } + + @Test + fun remainderCorrectNatural() { + // -5 % 3 = -2 + assertEquals( + IntegerFactory.instantiate(-2), + IntegerFactory.instantiate(-5).remainder(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun remainderCorrectInteger() { + // -5 % -3 = -2 + assertEquals( + IntegerFactory.instantiate(-2), + IntegerFactory.instantiate(-5).remainder(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun remainderCorrectRational() { + // -2 % (3/2) = -1/2 + assertEquals( + RationalFactory.instantiate(-1, 2), + IntegerFactory.instantiate(-2).remainder(RationalFactory.instantiate(3, 2)) + ) + } + + @Test + fun remainderCorrectReal() { + // -4 % pi = -4%pi + assertEquals( + RealFactory.instantiate(-4 % PI), + IntegerFactory.instantiate(-4).remainder(RealFactory.pi) + ) + } + + @Test + fun raiseCorrectNatural() { + // -2 ^ 2 = 4 + assertEquals( + IntegerFactory.instantiate(4), + IntegerFactory.instantiate(-2).raise(IntegerFactory.instantiate(2)) + ) + } + + @Test + fun raiseCorrectInteger() { + // -2 ^ -2 = 1/4 + assertEquals( + RationalFactory.instantiate(1, 4), + IntegerFactory.instantiate(-2).raise(IntegerFactory.instantiate(-2)) + ) + } + + @Test + fun raiseCorrectRational() { + // -8 ^ (1/3) = -2 + assertEquals( + IntegerFactory.instantiate(-2), + IntegerFactory.instantiate(-8).raise(RationalFactory.instantiate(1, 3)) + ) + } + +} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalTest.kt new file mode 100644 index 0000000..b16294c --- /dev/null +++ b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/naturals/NaturalTest.kt @@ -0,0 +1,227 @@ +package me.nathanfallet.makth.numbers.naturals + +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.numbers.rationals.RationalFactory +import me.nathanfallet.makth.numbers.reals.RealFactory +import me.nathanfallet.makth.sets.matrixes.MatrixFactory +import kotlin.math.PI +import kotlin.math.pow +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +class NaturalTest { + + @Test + fun rawString() { + assertEquals("5", IntegerFactory.instantiate(5).rawString) + } + + @Test + fun laTeXString() { + assertEquals("5", IntegerFactory.instantiate(5).laTeXString) + } + + @Test + fun negativeThrows() { + assertFailsWith(IllegalArgumentException::class) { + NaturalFactory.instantiate(-1) + } + } + + @Test + fun sumCorrectNatural() { + // 2 + 3 = 5 + assertEquals(IntegerFactory.instantiate(5), IntegerFactory.instantiate(2).sum(IntegerFactory.instantiate(3))) + } + + @Test + fun sumCorrectInteger() { + // 2 + -3 = -1 + assertEquals(IntegerFactory.instantiate(-1), IntegerFactory.instantiate(2).sum(IntegerFactory.instantiate(-3))) + } + + @Test + fun sumCorrectRational() { + // 1 + 1/2 = 3/2 + assertEquals( + RationalFactory.instantiate(3, 2), + IntegerFactory.instantiate(1).sum(RationalFactory.instantiate(1, 2)) + ) + } + + @Test + fun sumCorrectReal() { + // 2 + pi = 2 + pi + assertEquals( + RealFactory.instantiate(2 + PI), + IntegerFactory.instantiate(2).sum(RealFactory.pi) + ) + } + + @Test + fun multiplyCorrectNatural() { + // 2 * 3 = 6 + assertEquals( + IntegerFactory.instantiate(6), + IntegerFactory.instantiate(2).multiply(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun multiplyCorrectInteger() { + // 2 * -3 = -6 + assertEquals( + IntegerFactory.instantiate(-6), + IntegerFactory.instantiate(2).multiply(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun multiplyCorrectRational() { + // 3 * 1/2 = 3/2 + assertEquals( + RationalFactory.instantiate(3, 2), + IntegerFactory.instantiate(3).multiply(RationalFactory.instantiate(1, 2)) + ) + } + + @Test + fun multiplyCorrectReal() { + // 2 * pi = 2 * pi + assertEquals( + RealFactory.instantiate(2 * PI), + IntegerFactory.instantiate(2).multiply(RealFactory.pi) + ) + } + + @Test + fun multiplyCorrectMatrix() { + // 2 * [[1, 2], [3, 4]] = [[2, 4], [6, 8]] + assertEquals( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(2), IntegerFactory.instantiate(4)), + listOf(IntegerFactory.instantiate(6), IntegerFactory.instantiate(8)) + ) + ), + IntegerFactory.instantiate(2).multiply( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ) + ) + ) + } + + @Test + fun divideCorrectNatural() { + // 2 / 3 = 2/3 + assertEquals( + RationalFactory.instantiate(2, 3), + IntegerFactory.instantiate(2).divide(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun divideCorrectInteger() { + // 2 / -3 = -2/3 + assertEquals( + RationalFactory.instantiate(-2, 3), + IntegerFactory.instantiate(2).divide(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun divideCorrectRational() { + // 3 / (5/2) = 6/5 + assertEquals( + RationalFactory.instantiate(6, 5), + IntegerFactory.instantiate(3).divide(RationalFactory.instantiate(5, 2)) + ) + } + + @Test + fun divideCorrectReal() { + // 2 / pi = 2 / pi + assertEquals( + RealFactory.instantiate(2 / PI), + IntegerFactory.instantiate(2).divide(RealFactory.pi) + ) + } + + @Test + fun remainderCorrectNatural() { + // 5 % 3 = 2 + assertEquals( + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(5).remainder(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun remainderCorrectInteger() { + // 5 % -3 = 2 + assertEquals( + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(5).remainder(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun remainderCorrectRational() { + // 2 % (3/2) = 1/2 + assertEquals( + RationalFactory.instantiate(1, 2), + IntegerFactory.instantiate(2).remainder(RationalFactory.instantiate(3, 2)) + ) + } + + @Test + fun remainderCorrectReal() { + // 4 % pi = 4%pi + assertEquals( + RealFactory.instantiate(4 % PI), + IntegerFactory.instantiate(4).remainder(RealFactory.pi) + ) + } + + @Test + fun raiseCorrectNatural() { + // 2 ^ 3 = 8 + assertEquals( + IntegerFactory.instantiate(8), + IntegerFactory.instantiate(2).raise(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun raiseCorrectInteger() { + // 2 ^ -3 = 1/8 + assertEquals( + RationalFactory.instantiate(1, 8), + IntegerFactory.instantiate(2).raise(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun raiseCorrectRational() { + // 64 ^ (2/3) = 16 + assertEquals( + IntegerFactory.instantiate(16), + IntegerFactory.instantiate(64).raise(RationalFactory.instantiate(2, 3)) + ) + } + + @Test + fun raiseCorrectReal() { + // 2 ^ pi = 2^pi + assertEquals( + RealFactory.instantiate(2.0.pow(PI)), + IntegerFactory.instantiate(2).raise(RealFactory.pi) + ) + } + +} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/rationals/RationalTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/rationals/RationalTest.kt new file mode 100644 index 0000000..50f3fcf --- /dev/null +++ b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/rationals/RationalTest.kt @@ -0,0 +1,258 @@ +package me.nathanfallet.makth.numbers.rationals + +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.numbers.reals.RealFactory +import me.nathanfallet.makth.sets.matrixes.MatrixFactory +import kotlin.math.PI +import kotlin.math.pow +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +class RationalTest { + + @Test + fun rawString() { + assertEquals("-1/2", RationalFactory.instantiate(-1, 2).rawString) + } + + @Test + fun laTeXString() { + assertEquals("\\frac{-1}{2}", RationalFactory.instantiate(-1, 2).laTeXString) + } + + @Test + fun nullDenominatorThrows() { + assertFailsWith(IllegalArgumentException::class) { + RationalFactory.instantiate(1, 0) + } + } + + @Test + fun simplifyToRational() { + // 2/4 = 1/2 + assertEquals( + RationalFactory.instantiate(1, 2), + RationalFactory.instantiate(2, 4) + ) + } + + @Test + fun simplifyToInteger() { + // 4/2 = 2 + assertEquals( + IntegerFactory.instantiate(2), + RationalFactory.instantiate(4, 2) + ) + } + + @Test + fun absoluteValue() { + assertEquals( + RationalFactory.instantiate(1, 2), + RationalFactory.instantiate(-1, 2).absoluteValue + ) + } + + @Test + fun sumCorrectNatural() { + // 1/2 + 1 = 3/2 + assertEquals( + RationalFactory.instantiate(3, 2), + RationalFactory.instantiate(1, 2).sum(IntegerFactory.instantiate(1)) + ) + } + + @Test + fun sumCorrectInteger() { + // 1/2 + -1 = -1/2 + assertEquals( + RationalFactory.instantiate(-1, 2), + RationalFactory.instantiate(1, 2).sum(IntegerFactory.instantiate(-1)) + ) + } + + @Test + fun sumCorrectRational() { + // 1/2 + 1/3 = 5/6 + assertEquals( + RationalFactory.instantiate(5, 6), + RationalFactory.instantiate(1, 2).sum(RationalFactory.instantiate(1, 3)) + ) + } + + @Test + fun sumCorrectReal() { + // 1/2 + pi = 1/2 + pi + assertEquals( + RealFactory.instantiate(0.5 + PI), + RationalFactory.instantiate(1, 2).sum(RealFactory.pi) + ) + } + + @Test + fun multiplyCorrectNatural() { + // 1/2 * 3 = 3/2 + assertEquals( + RationalFactory.instantiate(3, 2), + RationalFactory.instantiate(1, 2).multiply(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun multiplyCorrectInteger() { + // 1/2 * -3 = -3/2 + assertEquals( + RationalFactory.instantiate(-3, 2), + RationalFactory.instantiate(1, 2).multiply(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun multiplyCorrectRational() { + // 1/2 * 1/3 = 1/6 + assertEquals( + RationalFactory.instantiate(1, 6), + RationalFactory.instantiate(1, 2).multiply(RationalFactory.instantiate(1, 3)) + ) + } + + @Test + fun multiplyCorrectReal() { + // 1/2 * pi = 1/2 * pi + assertEquals( + RealFactory.instantiate(0.5 * PI), + RationalFactory.instantiate(1, 2).multiply(RealFactory.pi) + ) + } + + @Test + fun mutliplyCorrectMatrix() { + // 1/2 * [[1, 2], [3, 4]] = [[1/2, 1], [3/2, 2]] + assertEquals( + MatrixFactory.instantiate( + listOf( + listOf(RationalFactory.instantiate(1, 2), IntegerFactory.instantiate(1)), + listOf(RationalFactory.instantiate(3, 2), IntegerFactory.instantiate(2)) + ) + ), + RationalFactory.instantiate(1, 2).multiply( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ) + ) + ) + } + + @Test + fun divideCorrectNatural() { + // 1/2 / 3 = 1/6 + assertEquals( + RationalFactory.instantiate(1, 6), + RationalFactory.instantiate(1, 2).divide(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun divideCorrectInteger() { + // 1/2 / -3 = -1/6 + assertEquals( + RationalFactory.instantiate(-1, 6), + RationalFactory.instantiate(1, 2).divide(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun divideCorrectRational() { + // 1/2 / 1/3 = 3/2 + assertEquals( + RationalFactory.instantiate(3, 2), + RationalFactory.instantiate(1, 2).divide(RationalFactory.instantiate(1, 3)) + ) + } + + @Test + fun divideCorrectReal() { + // 1/2 / pi = 1/2 * pi + assertEquals( + RealFactory.instantiate(0.5 / PI), + RationalFactory.instantiate(1, 2).divide(RealFactory.pi) + ) + } + + @Test + fun remainderCorrectNatural() { + // 5/2 % 2 = 1/2 + assertEquals( + RationalFactory.instantiate(1, 2), + RationalFactory.instantiate(5, 2).remainder(IntegerFactory.instantiate(2)) + ) + } + + @Test + fun remainderCorrectInteger() { + // 5/2 % -2 = 1/2 + assertEquals( + RationalFactory.instantiate(1, 2), + RationalFactory.instantiate(5, 2).remainder(IntegerFactory.instantiate(-2)) + ) + } + + @Test + fun remainderCorrectRational() { + // 5/2 % 1/3 = 1/6 + assertEquals( + RationalFactory.instantiate(1, 6), + RationalFactory.instantiate(5, 2).remainder(RationalFactory.instantiate(1, 3)) + ) + } + + @Test + fun remainderCorrectReal() { + // 7/2 % pi = 7/2%pi + assertEquals( + RealFactory.instantiate(3.5 % PI), + RationalFactory.instantiate(7, 2).remainder(RealFactory.pi) + ) + } + + @Test + fun raiseCorrectNatural() { + // 2/3 ^ 2 = 4/9 + assertEquals( + RationalFactory.instantiate(4, 9), + RationalFactory.instantiate(2, 3).raise(IntegerFactory.instantiate(2)) + ) + } + + @Test + fun raiseCorrectInteger() { + // 2/3 ^ -2 = 9/4 + assertEquals( + RationalFactory.instantiate(9, 4), + RationalFactory.instantiate(2, 3).raise(IntegerFactory.instantiate(-2)) + ) + } + + @Test + fun raiseCorrectRational() { + // 4/9 ^ 1/2 = 2/3 + assertEquals( + RationalFactory.instantiate(2, 3), + RationalFactory.instantiate(4, 9).raise(RationalFactory.instantiate(1, 2)) + ) + } + + @Test + fun raiseCorrectReal() { + // 1/2 ^ pi = 1/(2^pi) + assertEquals( + RealFactory.instantiate(1.0 / 2.0.pow(PI)), + RationalFactory.instantiate(1, 2).raise(RealFactory.pi) + ) + } + +} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/numbers/reals/RealTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/reals/RealTest.kt new file mode 100644 index 0000000..aea2a23 --- /dev/null +++ b/src/commonTest/kotlin/me/nathanfallet/makth/numbers/reals/RealTest.kt @@ -0,0 +1,311 @@ +package me.nathanfallet.makth.numbers.reals + +import me.nathanfallet.makth.extensions.StringValue +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.numbers.rationals.RationalFactory +import me.nathanfallet.makth.sets.matrixes.MatrixFactory +import kotlin.math.PI +import kotlin.math.pow +import kotlin.math.sqrt +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +class RealTest { + + @Test + fun rawString() { + assertEquals(1.23456789.toString(), RealFactory.instantiate(1.23456789).rawString) + } + + @Test + fun laTeXString() { + assertEquals(1.23456789.toString(), RealFactory.instantiate(1.23456789).laTeXString) + } + + @Test + fun absoluteValue() { + assertEquals( + RealFactory.instantiate(1.23456789), + RealFactory.instantiate(-1.23456789).absoluteValue + ) + } + + @Test + fun testEqualsTrue() { + assertEquals(true, RealFactory.instantiate(1.23456789).equals(RealFactory.instantiate(1.23456789))) + } + + @Test + fun testEqualsFalse() { + assertEquals(false, RealFactory.instantiate(1.23456789).equals(RealFactory.instantiate(9.87654321))) + } + + @Test + fun testLessThanTrue() { + assertEquals(true, RealFactory.instantiate(1.23456789).lessThan(RealFactory.instantiate(9.87654321))) + } + + @Test + fun testLessThanFalse() { + assertEquals(false, RealFactory.instantiate(9.87654321).lessThan(RealFactory.instantiate(1.23456789))) + } + + @Test + fun sumCorrectNatural() { + // pi + 1 = 1 + pi + assertEquals( + RealFactory.instantiate(1 + PI), + RealFactory.pi.sum(IntegerFactory.instantiate(1)) + ) + } + + @Test + fun sumCorrectInteger() { + // pi + -1 = pi - 1 + assertEquals( + RealFactory.instantiate(PI - 1), + RealFactory.pi.sum(IntegerFactory.instantiate(-1)) + ) + } + + @Test + fun sumCorrectRational() { + // pi + 1/3 = 1/3 + pi + assertEquals( + RealFactory.instantiate(1.0 / 3.0 + PI), + RealFactory.pi.sum(RationalFactory.instantiate(1, 3)) + ) + } + + @Test + fun sumCorrectReal() { + // pi + pi = 2pi + assertEquals( + RealFactory.instantiate(2 * PI), + RealFactory.pi.sum(RealFactory.pi) + ) + } + + @Test + fun sumUnsupported() { + assertFailsWith(UnsupportedOperationException::class) { + RealFactory.pi.sum(StringValue("test")) + } + } + + @Test + fun multiplyCorrectNatural() { + // pi * 3 = 3pi + assertEquals( + RealFactory.instantiate(3 * PI), + RealFactory.pi.multiply(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun multiplyCorrectInteger() { + // pi * -3 = -3pi + assertEquals( + RealFactory.instantiate(-3 * PI), + RealFactory.pi.multiply(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun multiplyCorrectRational() { + // pi * 1/3 = pi/3 + assertEquals( + RealFactory.instantiate(PI / 3), + RealFactory.pi.multiply(RationalFactory.instantiate(1, 3)) + ) + } + + @Test + fun multiplyCorrectReal() { + // pi * pi = pi^2 + assertEquals( + RealFactory.instantiate(PI * PI), + RealFactory.pi.multiply(RealFactory.pi) + ) + } + + @Test + fun multiplyCorrectVector() { + // pi * [[1, 2], [3, 4]] = [[pi, 2pi], [3pi, 4pi]] + assertEquals( + MatrixFactory.instantiate( + listOf( + listOf(RealFactory.pi, RealFactory.pi.multiply(IntegerFactory.instantiate(2))), + listOf( + RealFactory.pi.multiply(IntegerFactory.instantiate(3)), + RealFactory.pi.multiply(IntegerFactory.instantiate(4)) + ) + ) + ), + RealFactory.pi.multiply( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ) + ) + ) + } + + @Test + fun multiplyUnsupported() { + assertFailsWith(UnsupportedOperationException::class) { + RealFactory.pi.multiply(StringValue("test")) + } + } + + @Test + fun divideCorrectNatural() { + // pi / 3 = pi/3 + assertEquals( + RealFactory.instantiate(PI / 3), + RealFactory.pi.divide(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun divideCorrectInteger() { + // pi / -3 = -pi/3 + assertEquals( + RealFactory.instantiate(-PI / 3), + RealFactory.pi.divide(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun divideCorrectRational() { + // pi / 1/3 = 3pi + assertEquals( + RealFactory.instantiate(3 * PI), + RealFactory.pi.divide(RationalFactory.instantiate(1, 3)) + ) + } + + @Test + fun divideCorrectReal() { + // pi / pi = 1 + assertEquals( + IntegerFactory.instantiate(1), + RealFactory.pi.divide(RealFactory.pi) + ) + } + + @Test + fun divideUnsupported() { + assertFailsWith(UnsupportedOperationException::class) { + RealFactory.pi.divide(StringValue("test")) + } + } + + @Test + fun remainderCorrectNatural() { + // pi % 3 = pi%3 + assertEquals( + RealFactory.instantiate(PI % 3), + RealFactory.pi.remainder(IntegerFactory.instantiate(3)) + ) + } + + @Test + fun remainderCorrectInteger() { + // pi % -3 = pi%(-3) + assertEquals( + RealFactory.instantiate(PI % -3), + RealFactory.pi.remainder(IntegerFactory.instantiate(-3)) + ) + } + + @Test + fun remainderCorrectRational() { + // pi % 1/2 = pi%1/2 + assertEquals( + RealFactory.instantiate(PI % 0.5), + RealFactory.pi.remainder(RationalFactory.instantiate(1, 2)) + ) + } + + @Test + fun remainderCorrectReal() { + // pi % pi = 0 + assertEquals( + IntegerFactory.instantiate(0), + RealFactory.pi.remainder(RealFactory.pi) + ) + } + + @Test + fun remainderUnsupported() { + assertFailsWith(UnsupportedOperationException::class) { + RealFactory.pi.remainder(StringValue("test")) + } + } + + @Test + fun raiseCorrectNatural() { + // pi ^ 2 = pi^2 + assertEquals( + RealFactory.instantiate(PI * PI), + RealFactory.pi.raise(IntegerFactory.instantiate(2)) + ) + } + + @Test + fun raiseCorrectInteger() { + // pi ^ -2 = 1/(pi^2) + assertEquals( + RealFactory.instantiate(1 / (PI * PI)), + RealFactory.pi.raise(IntegerFactory.instantiate(-2)) + ) + } + + @Test + fun raiseCorrectRational() { + // pi ^ 1/2 = sqrt(pi) + assertEquals( + RealFactory.instantiate(sqrt(PI)), + RealFactory.pi.raise(RationalFactory.instantiate(1, 2)) + ) + } + + @Test + fun raiseCorrectReal() { + // pi ^ pi = pi^pi + assertEquals( + RealFactory.instantiate(PI.pow(PI)), + RealFactory.pi.raise(RealFactory.pi) + ) + } + + @Test + fun raiseUnsupported() { + assertFailsWith(UnsupportedOperationException::class) { + RealFactory.pi.raise(StringValue("test")) + } + } + + @Test + fun instantiatePi() { + assertEquals( + RealFactory.pi, + RealFactory.instantiate(PI) + ) + } + + @Test + fun piRawString() { + assertEquals("\u03C0", RealFactory.pi.rawString) + } + + @Test + fun piLaTeXString() { + assertEquals("\\pi", RealFactory.pi.laTeXString) + } + +} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/operations/EqualityTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/operations/EqualityTest.kt index e456857..4ed6d47 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/operations/EqualityTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/operations/EqualityTest.kt @@ -1,10 +1,10 @@ package me.nathanfallet.makth.operations import me.nathanfallet.makth.extensions.BooleanValue -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable -import me.nathanfallet.makth.sets.Matrix +import me.nathanfallet.makth.resolvables.variables.VariableFactory +import me.nathanfallet.makth.sets.matrixes.MatrixFactory import kotlin.test.Test import kotlin.test.assertEquals @@ -14,7 +14,7 @@ class EqualityTest { private val contextWithX = Context( mapOf( - "x" to Integer.instantiate(2) + "x" to IntegerFactory.instantiate(2) ) ) @@ -23,8 +23,8 @@ class EqualityTest { assertEquals( BooleanValue(true), Equality( - Integer.instantiate(1), - Integer.instantiate(1), + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(1), Equality.Operator.Equals ).compute(context) ) @@ -35,8 +35,8 @@ class EqualityTest { assertEquals( BooleanValue(false), Equality( - Integer.instantiate(1), - Integer.instantiate(2), + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), Equality.Operator.Equals ).compute(context) ) @@ -46,7 +46,7 @@ class EqualityTest { fun testEqualsTrueWithVariable() { assertEquals( BooleanValue(true), - Equality(Variable.instantiate("x"), Integer.instantiate(2), Equality.Operator.Equals).compute( + Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2), Equality.Operator.Equals).compute( contextWithX ) ) @@ -57,13 +57,15 @@ class EqualityTest { assertEquals( BooleanValue(true), Equality( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) )), - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) )), Equality.Operator.Equals ).compute(context) @@ -75,13 +77,15 @@ class EqualityTest { assertEquals( BooleanValue(false), Equality( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) )), - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(4), Integer.instantiate(3)) + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(4), IntegerFactory.instantiate(3)) )), Equality.Operator.Equals ).compute(context) @@ -92,7 +96,7 @@ class EqualityTest { fun equalsrawString() { assertEquals( "1 = 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.Equals).rawString + Equality(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2), Equality.Operator.Equals).rawString ) } @@ -100,7 +104,7 @@ class EqualityTest { fun equalslaTeXString() { assertEquals( "1 \\eq 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.Equals).laTeXString + Equality(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2), Equality.Operator.Equals).laTeXString ) } @@ -109,8 +113,8 @@ class EqualityTest { assertEquals( BooleanValue(true), Equality( - Integer.instantiate(1), - Integer.instantiate(2), + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), Equality.Operator.NotEquals ).compute(context) ) @@ -121,8 +125,8 @@ class EqualityTest { assertEquals( BooleanValue(false), Equality( - Integer.instantiate(1), - Integer.instantiate(1), + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(1), Equality.Operator.NotEquals ).compute(context) ) @@ -133,13 +137,15 @@ class EqualityTest { assertEquals( BooleanValue(true), Equality( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) )), - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(4), Integer.instantiate(3)) + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(4), IntegerFactory.instantiate(3)) )), Equality.Operator.NotEquals ).compute(context) @@ -151,13 +157,15 @@ class EqualityTest { assertEquals( BooleanValue(false), Equality( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) )), - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) )), Equality.Operator.NotEquals ).compute(context) @@ -168,7 +176,11 @@ class EqualityTest { fun notEqualsRawString() { assertEquals( "1 != 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.NotEquals).rawString + Equality( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + Equality.Operator.NotEquals + ).rawString ) } @@ -176,7 +188,11 @@ class EqualityTest { fun notEqualsLaTeXString() { assertEquals( "1 \\ne 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.NotEquals).laTeXString + Equality( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + Equality.Operator.NotEquals + ).laTeXString ) } @@ -185,8 +201,8 @@ class EqualityTest { assertEquals( BooleanValue(true), Equality( - Integer.instantiate(1), - Integer.instantiate(2), + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), Equality.Operator.LessThan ).compute(context) ) @@ -197,8 +213,8 @@ class EqualityTest { assertEquals( BooleanValue(false), Equality( - Integer.instantiate(2), - Integer.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(1), Equality.Operator.LessThan ).compute(context) ) @@ -209,8 +225,8 @@ class EqualityTest { assertEquals( BooleanValue(false), Equality( - Integer.instantiate(2), - Integer.instantiate(2), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(2), Equality.Operator.LessThan ).compute(context) ) @@ -220,7 +236,7 @@ class EqualityTest { fun lessThanRawString() { assertEquals( "1 < 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.LessThan).rawString + Equality(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2), Equality.Operator.LessThan).rawString ) } @@ -228,7 +244,11 @@ class EqualityTest { fun lessThanLaTeXString() { assertEquals( "1 \\lt 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.LessThan).laTeXString + Equality( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + Equality.Operator.LessThan + ).laTeXString ) } @@ -237,8 +257,8 @@ class EqualityTest { assertEquals( BooleanValue(true), Equality( - Integer.instantiate(2), - Integer.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(1), Equality.Operator.GreaterThan ).compute(context) ) @@ -249,8 +269,8 @@ class EqualityTest { assertEquals( BooleanValue(false), Equality( - Integer.instantiate(1), - Integer.instantiate(2), + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), Equality.Operator.GreaterThan ).compute(context) ) @@ -261,8 +281,8 @@ class EqualityTest { assertEquals( BooleanValue(false), Equality( - Integer.instantiate(2), - Integer.instantiate(2), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(2), Equality.Operator.GreaterThan ).compute(context) ) @@ -272,7 +292,11 @@ class EqualityTest { fun greaterThanRawString() { assertEquals( "1 > 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.GreaterThan).rawString + Equality( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + Equality.Operator.GreaterThan + ).rawString ) } @@ -280,7 +304,11 @@ class EqualityTest { fun greaterThanLaTeXString() { assertEquals( "1 \\gt 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.GreaterThan).laTeXString + Equality( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + Equality.Operator.GreaterThan + ).laTeXString ) } @@ -289,8 +317,8 @@ class EqualityTest { assertEquals( BooleanValue(true), Equality( - Integer.instantiate(1), - Integer.instantiate(2), + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), Equality.Operator.LessThanOrEquals ).compute(context) ) @@ -301,8 +329,8 @@ class EqualityTest { assertEquals( BooleanValue(true), Equality( - Integer.instantiate(1), - Integer.instantiate(1), + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(1), Equality.Operator.LessThanOrEquals ).compute(context) ) @@ -313,8 +341,8 @@ class EqualityTest { assertEquals( BooleanValue(false), Equality( - Integer.instantiate(2), - Integer.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(1), Equality.Operator.LessThanOrEquals ).compute(context) ) @@ -324,7 +352,11 @@ class EqualityTest { fun lessThanOrEqualsRawString() { assertEquals( "1 <= 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.LessThanOrEquals).rawString + Equality( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + Equality.Operator.LessThanOrEquals + ).rawString ) } @@ -332,7 +364,11 @@ class EqualityTest { fun lessThanOrEqualsRaTeXString() { assertEquals( "1 \\le 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.LessThanOrEquals).laTeXString + Equality( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + Equality.Operator.LessThanOrEquals + ).laTeXString ) } @@ -341,8 +377,8 @@ class EqualityTest { assertEquals( BooleanValue(true), Equality( - Integer.instantiate(2), - Integer.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(1), Equality.Operator.GreaterThanOrEquals ).compute(context) ) @@ -353,8 +389,8 @@ class EqualityTest { assertEquals( BooleanValue(true), Equality( - Integer.instantiate(1), - Integer.instantiate(1), + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(1), Equality.Operator.GreaterThanOrEquals ).compute(context) ) @@ -365,8 +401,8 @@ class EqualityTest { assertEquals( BooleanValue(false), Equality( - Integer.instantiate(1), - Integer.instantiate(2), + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), Equality.Operator.GreaterThanOrEquals ).compute(context) ) @@ -376,7 +412,11 @@ class EqualityTest { fun greaterThanOrEqualsRawString() { assertEquals( "1 >= 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.GreaterThanOrEquals).rawString + Equality( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + Equality.Operator.GreaterThanOrEquals + ).rawString ) } @@ -384,7 +424,11 @@ class EqualityTest { fun greaterThanOrEqualsLaTeXString() { assertEquals( "1 \\ge 2", - Equality(Integer.instantiate(1), Integer.instantiate(2), Equality.Operator.GreaterThanOrEquals).laTeXString + Equality( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + Equality.Operator.GreaterThanOrEquals + ).laTeXString ) } diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/operations/ExponentiationTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/operations/ExponentiationTest.kt index be8f835..ff2f45e 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/operations/ExponentiationTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/operations/ExponentiationTest.kt @@ -1,8 +1,8 @@ package me.nathanfallet.makth.operations -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory import kotlin.test.Test import kotlin.test.assertEquals @@ -14,7 +14,7 @@ class ExponentiationTest { fun rawString() { assertEquals( "1 ^ 2", - Exponentiation(Integer.instantiate(1), Integer.instantiate(2)).rawString + Exponentiation(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).rawString ) } @@ -22,7 +22,10 @@ class ExponentiationTest { fun toRawStringWithBraces() { assertEquals( "(1 + 2) ^ (3 + 4)", - Exponentiation(Sum(Integer.instantiate(1), Integer.instantiate(2)), Sum(Integer.instantiate(3), Integer.instantiate(4))).rawString + Exponentiation( + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + Sum(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ).rawString ) } @@ -30,7 +33,7 @@ class ExponentiationTest { fun laTeXString() { assertEquals( "1^{2}", - Exponentiation(Integer.instantiate(1), Integer.instantiate(2)).laTeXString + Exponentiation(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).laTeXString ) } @@ -38,15 +41,18 @@ class ExponentiationTest { fun toLaTeXStringWithBraces() { assertEquals( "(1 + 2)^{3 + 4}", - Exponentiation(Sum(Integer.instantiate(1), Integer.instantiate(2)), Sum(Integer.instantiate(3), Integer.instantiate(4))).laTeXString + Exponentiation( + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + Sum(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ).laTeXString ) } @Test fun variables() { assertEquals( - setOf(Variable.instantiate("x"), Variable.instantiate("y")), - Exponentiation(Variable.instantiate("x"), Variable.instantiate("y")).variables + setOf(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")), + Exponentiation(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")).variables ) } @@ -54,8 +60,8 @@ class ExponentiationTest { fun exponentiateNaturals() { // Check that an exponentiation is computed correctly assertEquals( - Integer.instantiate(8), - Exponentiation(Integer.instantiate(2), Integer.instantiate(3)).compute(context) + IntegerFactory.instantiate(8), + Exponentiation(IntegerFactory.instantiate(2), IntegerFactory.instantiate(3)).compute(context) ) } diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/operations/OperationsTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/operations/OperationsTest.kt index 3a604be..0ae7659 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/operations/OperationsTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/operations/OperationsTest.kt @@ -1,7 +1,7 @@ package me.nathanfallet.makth.operations; import me.nathanfallet.makth.lexers.AlgorithmLexer.SyntaxException -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -12,69 +12,69 @@ class OperationsTest { @Test fun syntaxException() { assertFailsWith(SyntaxException::class) { - Operation.Utils.initialize("a", Integer.instantiate(1), Integer.instantiate(2)) + OperationFactory.initialize("a", IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)) } } @Test fun precedenceExponentiateOverMultiply() { assertTrue( - Operation.Utils.getPrecedence("^") > Operation.Utils.getPrecedence("*") + OperationFactory.getPrecedence("^") > OperationFactory.getPrecedence("*") ) } @Test fun precedenceMultiplyOverSum() { assertTrue( - Operation.Utils.getPrecedence("*") > Operation.Utils.getPrecedence("+") + OperationFactory.getPrecedence("*") > OperationFactory.getPrecedence("+") ) } @Test fun precedenceSumOverEquals() { assertTrue( - Operation.Utils.getPrecedence("+") > Operation.Utils.getPrecedence("=") + OperationFactory.getPrecedence("+") > OperationFactory.getPrecedence("=") ) } @Test fun precedenceSumSameAsDifference() { - assertEquals(Operation.Utils.getPrecedence("+"), Operation.Utils.getPrecedence("-")) + assertEquals(OperationFactory.getPrecedence("+"), OperationFactory.getPrecedence("-")) } @Test fun precedenceMultiplySameAsDivide() { - assertEquals(Operation.Utils.getPrecedence("*"), Operation.Utils.getPrecedence("/")) + assertEquals(OperationFactory.getPrecedence("*"), OperationFactory.getPrecedence("/")) } @Test fun precedenceMultiplySameAsRemainder() { - assertEquals(Operation.Utils.getPrecedence("*"), Operation.Utils.getPrecedence("%")) + assertEquals(OperationFactory.getPrecedence("*"), OperationFactory.getPrecedence("%")) } @Test fun precedenceNotEqualsSameAsEquals() { - assertEquals(Operation.Utils.getPrecedence("!="), Operation.Utils.getPrecedence("=")) + assertEquals(OperationFactory.getPrecedence("!="), OperationFactory.getPrecedence("=")) } @Test fun precedenceLessThanSameAsEquals() { - assertEquals(Operation.Utils.getPrecedence("<"), Operation.Utils.getPrecedence("=")) + assertEquals(OperationFactory.getPrecedence("<"), OperationFactory.getPrecedence("=")) } @Test fun precedenceGreaterThanSameAsEquals() { - assertEquals(Operation.Utils.getPrecedence(">"), Operation.Utils.getPrecedence("=")) + assertEquals(OperationFactory.getPrecedence(">"), OperationFactory.getPrecedence("=")) } @Test fun precedenceLessThanOrEqualsSameAsEquals() { - assertEquals(Operation.Utils.getPrecedence("<="), Operation.Utils.getPrecedence("=")) + assertEquals(OperationFactory.getPrecedence("<="), OperationFactory.getPrecedence("=")) } @Test fun precedenceGreaterThanOrEqualsSameAsEquals() { - assertEquals(Operation.Utils.getPrecedence(">="), Operation.Utils.getPrecedence("=")) + assertEquals(OperationFactory.getPrecedence(">="), OperationFactory.getPrecedence("=")) } } \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/operations/ProductTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/operations/ProductTest.kt index eb9a982..5694f20 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/operations/ProductTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/operations/ProductTest.kt @@ -1,8 +1,8 @@ package me.nathanfallet.makth.operations -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory import kotlin.test.Test import kotlin.test.assertEquals @@ -14,7 +14,7 @@ class ProductTest { fun rawString() { assertEquals( "1 * 2", - Product(Integer.instantiate(1), Integer.instantiate(2)).rawString + Product(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).rawString ) } @@ -22,7 +22,10 @@ class ProductTest { fun toRawStringWithBraces() { assertEquals( "(1 + 2) * (3 + 4)", - Product(Sum(Integer.instantiate(1), Integer.instantiate(2)), Sum(Integer.instantiate(3), Integer.instantiate(4))).rawString + Product( + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + Sum(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ).rawString ) } @@ -30,7 +33,7 @@ class ProductTest { fun toRawStringWithMinus() { assertEquals( "-x", - Product(Integer.instantiate(-1), Variable.instantiate("x")).rawString + Product(IntegerFactory.instantiate(-1), VariableFactory.instantiate("x")).rawString ) } @@ -38,7 +41,7 @@ class ProductTest { fun laTeXString() { assertEquals( "1 \\times 2", - Product(Integer.instantiate(1), Integer.instantiate(2)).laTeXString + Product(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).laTeXString ) } @@ -46,7 +49,10 @@ class ProductTest { fun toLaTeXStringWithBraces() { assertEquals( "(1 + 2) \\times (3 + 4)", - Product(Sum(Integer.instantiate(1), Integer.instantiate(2)), Sum(Integer.instantiate(3), Integer.instantiate(4))).laTeXString + Product( + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + Sum(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ).laTeXString ) } @@ -54,15 +60,15 @@ class ProductTest { fun toLaTeXStringWithMinus() { assertEquals( "-x", - Product(Integer.instantiate(-1), Variable.instantiate("x")).laTeXString + Product(IntegerFactory.instantiate(-1), VariableFactory.instantiate("x")).laTeXString ) } @Test fun variables() { assertEquals( - setOf(Variable.instantiate("x"), Variable.instantiate("y")), - Product(Variable.instantiate("x"), Variable.instantiate("y")).variables + setOf(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")), + Product(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")).variables ) } @@ -70,8 +76,8 @@ class ProductTest { fun multiplyNaturals() { // Check that a product is computed correctly assertEquals( - Integer.instantiate(6), - Product(Integer.instantiate(2), Integer.instantiate(3)).compute(context) + IntegerFactory.instantiate(6), + Product(IntegerFactory.instantiate(2), IntegerFactory.instantiate(3)).compute(context) ) } diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/operations/QuotientTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/operations/QuotientTest.kt index d4e0ad7..28e800b 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/operations/QuotientTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/operations/QuotientTest.kt @@ -1,8 +1,8 @@ package me.nathanfallet.makth.operations -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory import kotlin.test.Test import kotlin.test.assertEquals @@ -14,7 +14,7 @@ class QuotientTest { fun rawString() { assertEquals( "1 / 2", - Quotient(Integer.instantiate(1), Integer.instantiate(2)).rawString + Quotient(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).rawString ) } @@ -22,7 +22,10 @@ class QuotientTest { fun toRawStringWithBraces() { assertEquals( "(1 + 2) / (3 + 4)", - Quotient(Sum(Integer.instantiate(1), Integer.instantiate(2)), Sum(Integer.instantiate(3), Integer.instantiate(4))).rawString + Quotient( + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + Sum(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ).rawString ) } @@ -30,7 +33,7 @@ class QuotientTest { fun laTeXString() { assertEquals( "\\frac{1}{2}", - Quotient(Integer.instantiate(1), Integer.instantiate(2)).laTeXString + Quotient(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).laTeXString ) } @@ -38,15 +41,18 @@ class QuotientTest { fun toLaTeXStringWithBraces() { assertEquals( "\\frac{1 + 2}{3 + 4}", - Quotient(Sum(Integer.instantiate(1), Integer.instantiate(2)), Sum(Integer.instantiate(3), Integer.instantiate(4))).laTeXString + Quotient( + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + Sum(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ).laTeXString ) } @Test fun variables() { assertEquals( - setOf(Variable.instantiate("x"), Variable.instantiate("y")), - Quotient(Variable.instantiate("x"), Variable.instantiate("y")).variables + setOf(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")), + Quotient(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")).variables ) } @@ -54,8 +60,8 @@ class QuotientTest { fun divideNaturals() { // Check that a quotient is computed correctly assertEquals( - Integer.instantiate(2), - Quotient(Integer.instantiate(6), Integer.instantiate(3)).compute(context) + IntegerFactory.instantiate(2), + Quotient(IntegerFactory.instantiate(6), IntegerFactory.instantiate(3)).compute(context) ) } diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/operations/RemainderTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/operations/RemainderTest.kt index 6e8d001..0d10071 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/operations/RemainderTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/operations/RemainderTest.kt @@ -1,8 +1,8 @@ package me.nathanfallet.makth.operations -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory import kotlin.test.Test import kotlin.test.assertEquals @@ -14,7 +14,7 @@ class RemainderTest { fun rawString() { assertEquals( "1 % 2", - Remainder(Integer.instantiate(1), Integer.instantiate(2)).rawString + Remainder(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).rawString ) } @@ -22,7 +22,10 @@ class RemainderTest { fun toRawStringWithBraces() { assertEquals( "(1 + 2) % (3 + 4)", - Remainder(Sum(Integer.instantiate(1), Integer.instantiate(2)), Sum(Integer.instantiate(3), Integer.instantiate(4))).rawString + Remainder( + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + Sum(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ).rawString ) } @@ -30,7 +33,7 @@ class RemainderTest { fun laTeXString() { assertEquals( "1 % 2", - Remainder(Integer.instantiate(1), Integer.instantiate(2)).laTeXString + Remainder(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).laTeXString ) } @@ -38,15 +41,18 @@ class RemainderTest { fun toLaTeXStringWithBraces() { assertEquals( "(1 + 2) % (3 + 4)", - Remainder(Sum(Integer.instantiate(1), Integer.instantiate(2)), Sum(Integer.instantiate(3), Integer.instantiate(4))).laTeXString + Remainder( + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + Sum(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ).laTeXString ) } @Test fun variables() { assertEquals( - setOf(Variable.instantiate("x"), Variable.instantiate("y")), - Remainder(Variable.instantiate("x"), Variable.instantiate("y")).variables + setOf(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")), + Remainder(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")).variables ) } @@ -54,8 +60,8 @@ class RemainderTest { fun divideNaturals() { // Check that a remainder is computed correctly assertEquals( - Integer.instantiate(1), - Remainder(Integer.instantiate(3), Integer.instantiate(2)).compute(context) + IntegerFactory.instantiate(1), + Remainder(IntegerFactory.instantiate(3), IntegerFactory.instantiate(2)).compute(context) ) } diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/operations/SumTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/operations/SumTest.kt index 2842136..bdced0d 100644 --- a/src/commonTest/kotlin/me/nathanfallet/makth/operations/SumTest.kt +++ b/src/commonTest/kotlin/me/nathanfallet/makth/operations/SumTest.kt @@ -1,8 +1,8 @@ package me.nathanfallet.makth.operations -import me.nathanfallet.makth.numbers.Integer +import me.nathanfallet.makth.numbers.integers.IntegerFactory import me.nathanfallet.makth.resolvables.Context -import me.nathanfallet.makth.resolvables.Variable +import me.nathanfallet.makth.resolvables.variables.VariableFactory import kotlin.test.Test import kotlin.test.assertEquals @@ -14,7 +14,7 @@ class SumTest { fun rawString() { assertEquals( "1 + 2", - Sum(Integer.instantiate(1), Integer.instantiate(2)).rawString + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).rawString ) } @@ -22,7 +22,7 @@ class SumTest { fun toRawStringWithMinus() { assertEquals( "1 - 2", - Sum(Integer.instantiate(1), Integer.instantiate(-2)).rawString + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(-2)).rawString ) } @@ -31,7 +31,10 @@ class SumTest { // This will technically never happen, but it's the only way to test it assertEquals( "(1 = 1) + (2 = 2)", - Sum(Equality(Integer.instantiate(1), Integer.instantiate(1)), Equality(Integer.instantiate(2), Integer.instantiate(2))).rawString + Sum( + Equality(IntegerFactory.instantiate(1), IntegerFactory.instantiate(1)), + Equality(IntegerFactory.instantiate(2), IntegerFactory.instantiate(2)) + ).rawString ) } @@ -39,7 +42,7 @@ class SumTest { fun laTeXString() { assertEquals( "1 + 2", - Sum(Integer.instantiate(1), Integer.instantiate(2)).laTeXString + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).laTeXString ) } @@ -47,7 +50,7 @@ class SumTest { fun toLaTeXStringWithMinus() { assertEquals( "1 - 2", - Sum(Integer.instantiate(1), Integer.instantiate(-2)).laTeXString + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(-2)).laTeXString ) } @@ -56,15 +59,18 @@ class SumTest { // This will technically never happen, but it's the only way to test it assertEquals( "(1 \\eq 1) + (2 \\eq 2)", - Sum(Equality(Integer.instantiate(1), Integer.instantiate(1)), Equality(Integer.instantiate(2), Integer.instantiate(2))).laTeXString + Sum( + Equality(IntegerFactory.instantiate(1), IntegerFactory.instantiate(1)), + Equality(IntegerFactory.instantiate(2), IntegerFactory.instantiate(2)) + ).laTeXString ) } @Test fun variables() { assertEquals( - setOf(Variable.instantiate("x"), Variable.instantiate("y")), - Sum(Variable.instantiate("x"), Variable.instantiate("y")).variables + setOf(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")), + Sum(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")).variables ) } @@ -72,8 +78,8 @@ class SumTest { fun sumNaturals() { // Check that a sum is computed correctly assertEquals( - Integer.instantiate(3), - Sum(Integer.instantiate(1), Integer.instantiate(2)).compute(context) + IntegerFactory.instantiate(3), + Sum(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)).compute(context) ) } @@ -81,8 +87,8 @@ class SumTest { fun testEqualsTrue() { assertEquals( true, - Sum(Variable.instantiate("x"), Integer.instantiate(2)).equals( - Sum(Variable.instantiate("x"), Integer.instantiate(2)) + Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)).equals( + Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)) ) ) } @@ -91,8 +97,8 @@ class SumTest { fun testEqualsTrue2() { assertEquals( true, - Sum(Integer.instantiate(2), Variable.instantiate("x")).equals( - Sum(Variable.instantiate("x"), Integer.instantiate(2)) + Sum(IntegerFactory.instantiate(2), VariableFactory.instantiate("x")).equals( + Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)) ) ) } @@ -101,8 +107,8 @@ class SumTest { fun testEqualsFalse() { assertEquals( false, - Sum(Variable.instantiate("x"), Integer.instantiate(1)).equals( - Sum(Variable.instantiate("x"), Integer.instantiate(2)) + Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(1)).equals( + Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)) ) ) } @@ -111,8 +117,8 @@ class SumTest { fun testLessThanTrue() { assertEquals( true, - Sum(Variable.instantiate("x"), Integer.instantiate(1)).lessThan( - Sum(Variable.instantiate("x"), Integer.instantiate(2)) + Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(1)).lessThan( + Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)) ) ) } @@ -121,8 +127,8 @@ class SumTest { fun testLessThanTrue2() { assertEquals( true, - Sum(Variable.instantiate("x"), Integer.instantiate(1)).lessThan( - Sum(Integer.instantiate(2), Variable.instantiate("x")) + Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(1)).lessThan( + Sum(IntegerFactory.instantiate(2), VariableFactory.instantiate("x")) ) ) } diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/resolvables/VariableTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/resolvables/VariableTest.kt deleted file mode 100644 index a375d93..0000000 --- a/src/commonTest/kotlin/me/nathanfallet/makth/resolvables/VariableTest.kt +++ /dev/null @@ -1,42 +0,0 @@ -package me.nathanfallet.makth.resolvables; - -import me.nathanfallet.makth.numbers.Integer -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith - -class VariableTest { - - @Test - fun variableResolvedCorrectly() { - assertEquals( - Integer.instantiate(2), - Variable.instantiate("x").compute(Context( - mapOf("x" to Integer.instantiate(2)) - )) - ) - } - - @Test - fun rawString() { - assertEquals("x", Variable.instantiate("x").rawString) - } - - @Test - fun laTeXString() { - assertEquals("x", Variable.instantiate("x").laTeXString) - } - - @Test - fun testEqualsTrue() { - assertEquals(true, Variable.instantiate("x").equals(Variable.instantiate("x"))) - } - - @Test - fun testEqualsUnsupported() { - assertFailsWith(UnsupportedOperationException::class) { - Variable.instantiate("x").equals(Variable.instantiate("y")) - } - } - -} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/resolvables/variables/VariableTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/resolvables/variables/VariableTest.kt new file mode 100644 index 0000000..83304b9 --- /dev/null +++ b/src/commonTest/kotlin/me/nathanfallet/makth/resolvables/variables/VariableTest.kt @@ -0,0 +1,45 @@ +package me.nathanfallet.makth.resolvables.variables; + +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.resolvables.Context +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +class VariableTest { + + @Test + fun variableResolvedCorrectly() { + assertEquals( + IntegerFactory.instantiate(2), + VariableFactory.instantiate("x").compute( + Context( + mapOf("x" to IntegerFactory.instantiate(2)) + ) + ) + ) + } + + @Test + fun rawString() { + assertEquals("x", VariableFactory.instantiate("x").rawString) + } + + @Test + fun laTeXString() { + assertEquals("x", VariableFactory.instantiate("x").laTeXString) + } + + @Test + fun testEqualsTrue() { + assertEquals(true, VariableFactory.instantiate("x").equals(VariableFactory.instantiate("x"))) + } + + @Test + fun testEqualsUnsupported() { + assertFailsWith(UnsupportedOperationException::class) { + VariableFactory.instantiate("x").equals(VariableFactory.instantiate("y")) + } + } + +} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/sets/MatrixTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/sets/MatrixTest.kt deleted file mode 100644 index 109dce2..0000000 --- a/src/commonTest/kotlin/me/nathanfallet/makth/sets/MatrixTest.kt +++ /dev/null @@ -1,187 +0,0 @@ -package me.nathanfallet.makth.sets - -import me.nathanfallet.makth.extensions.StringValue -import me.nathanfallet.makth.numbers.Integer -import me.nathanfallet.makth.resolvables.Variable -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith - -class MatrixTest { - - @Test - fun rawString() { - assertEquals( - "(1, 2; 3, 4)", - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )).rawString - ) - } - - @Test - fun laTeXString() { - assertEquals( - "\\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}", - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )).laTeXString - ) - } - - @Test - fun rows() { - assertEquals( - listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - ), - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )).rows - ) - } - - @Test - fun columns() { - assertEquals( - listOf( - listOf(Integer.instantiate(1), Integer.instantiate(3)), - listOf(Integer.instantiate(2), Integer.instantiate(4)) - ), - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )).columns - ) - } - - @Test - fun variables() { - assertEquals( - setOf(Variable.instantiate("x"), Variable.instantiate("y")), - Matrix.instantiate(listOf( - listOf(Variable.instantiate("x"), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Variable.instantiate("y")) - )).variables - ) - } - - @Test - fun instantiateInvalidMatrix() { - assertFailsWith(IllegalArgumentException::class) { - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3)) - )) - } - } - - @Test - fun instantiateReal() { - assertEquals( - Integer.instantiate(1), - Matrix.instantiate(listOf(listOf(Integer.instantiate(1)))) - ) - } - - @Test - fun transposition() { - assertEquals( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(3)), - listOf(Integer.instantiate(2), Integer.instantiate(4)) - )), - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )).transpose() - ) - } - - @Test - fun sumCorrectMatrix() { - assertEquals( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(6), Integer.instantiate(8)), - listOf(Integer.instantiate(10), Integer.instantiate(12)) - )), - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )).sum(Matrix.instantiate(listOf( - listOf(Integer.instantiate(5), Integer.instantiate(6)), - listOf(Integer.instantiate(7), Integer.instantiate(8)) - ))) - ) - } - - @Test - fun sumIncorrectMatrix() { - assertFailsWith(UnsupportedOperationException::class) { - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )).sum(Matrix.instantiate(listOf( - listOf(Integer.instantiate(5), Integer.instantiate(6)), - listOf(Integer.instantiate(7), Integer.instantiate(8)), - listOf(Integer.instantiate(9), Integer.instantiate(10)) - ))) - } - } - - @Test - fun sumUnsupported() { - assertFailsWith(UnsupportedOperationException::class) { - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )).sum(StringValue("test")) - } - } - - @Test - fun multiplyCorrectMatrix() { - assertEquals( - Matrix.instantiate(listOf( - listOf(Integer.instantiate(3), Integer.instantiate(2340)), - listOf(Integer.instantiate(0), Integer.instantiate(1000)) - )), - Matrix.instantiate(listOf( - listOf(Integer.instantiate(2), Integer.instantiate(3), Integer.instantiate(4)), - listOf(Integer.instantiate(1), Integer.instantiate(0), Integer.instantiate(0)) - )).multiply(Matrix.instantiate(listOf( - listOf(Integer.instantiate(0), Integer.instantiate(1000)), - listOf(Integer.instantiate(1), Integer.instantiate(100)), - listOf(Integer.instantiate(0), Integer.instantiate(10)) - ))) - ) - } - - @Test - fun multiplyIncorrectMatrix() { - assertFailsWith(UnsupportedOperationException::class) { - Matrix.instantiate(listOf( - listOf(Integer.instantiate(2), Integer.instantiate(3), Integer.instantiate(4)), - listOf(Integer.instantiate(1), Integer.instantiate(0), Integer.instantiate(0)) - )).multiply(Matrix.instantiate(listOf( - listOf(Integer.instantiate(2), Integer.instantiate(3), Integer.instantiate(4)), - listOf(Integer.instantiate(1), Integer.instantiate(0), Integer.instantiate(0)) - ))) - } - } - - @Test - fun multiplyUnsupported() { - assertFailsWith(UnsupportedOperationException::class) { - Matrix.instantiate(listOf( - listOf(Integer.instantiate(1), Integer.instantiate(2)), - listOf(Integer.instantiate(3), Integer.instantiate(4)) - )).multiply(StringValue("test")) - } - } - -} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/sets/VectorTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/sets/VectorTest.kt deleted file mode 100644 index 8389639..0000000 --- a/src/commonTest/kotlin/me/nathanfallet/makth/sets/VectorTest.kt +++ /dev/null @@ -1,107 +0,0 @@ -package me.nathanfallet.makth.sets - -import me.nathanfallet.makth.extensions.StringValue -import me.nathanfallet.makth.numbers.Integer -import me.nathanfallet.makth.resolvables.Variable -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith - -class VectorTest { - - @Test - fun rawString() { - assertEquals( - "(1; 2; 3)", - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))).rawString - ) - } - - @Test - fun laTeXString() { - assertEquals( - "\\begin{pmatrix} 1 \\\\ 2 \\\\ 3 \\end{pmatrix}", - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))).laTeXString - ) - } - - @Test - fun variables() { - assertEquals( - setOf(Variable.instantiate("x"), Variable.instantiate("y")), - Vector.instantiate(listOf(Variable.instantiate("x"), Variable.instantiate("y"))).variables - ) - } - - @Test - fun elements() { - assertEquals( - listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3)), - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))).elements - ) - } - - @Test - fun rows() { - assertEquals( - listOf(listOf(Integer.instantiate(1)), listOf(Integer.instantiate(2)), listOf(Integer.instantiate(3))), - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))).rows - ) - } - - @Test - fun columns() { - assertEquals( - listOf(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))), - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))).columns - ) - } - - @Test - fun sumCorrectVector() { - assertEquals( - Vector.instantiate(listOf(Integer.instantiate(5), Integer.instantiate(7), Integer.instantiate(9))), - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))).sum( - Vector.instantiate(listOf(Integer.instantiate(4), Integer.instantiate(5), Integer.instantiate(6))) - ) - ) - } - - @Test - fun sumIncorrectVector() { - assertFailsWith(UnsupportedOperationException::class) { - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))).sum( - Vector.instantiate(listOf(Integer.instantiate(4), Integer.instantiate(5))) - ) - } - } - - @Test - fun sumUnsupported() { - assertFailsWith(UnsupportedOperationException::class) { - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))).sum( - StringValue("test") - ) - } - } - - @Test - fun multiplyCorrectReal() { - assertEquals( - Vector.instantiate(listOf(Integer.instantiate(2), Integer.instantiate(4), Integer.instantiate(6))), - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))).multiply( - Integer.instantiate(2) - ) - ) - } - - @Test - fun multiplyUnsupported() { - assertFailsWith(UnsupportedOperationException::class) { - Vector.instantiate(listOf(Integer.instantiate(1), Integer.instantiate(2), Integer.instantiate(3))).multiply( - StringValue("test") - ) - } - } - -} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixTest.kt new file mode 100644 index 0000000..1ffdc0b --- /dev/null +++ b/src/commonTest/kotlin/me/nathanfallet/makth/sets/matrixes/MatrixTest.kt @@ -0,0 +1,243 @@ +package me.nathanfallet.makth.sets.matrixes + +import me.nathanfallet.makth.extensions.StringValue +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.resolvables.variables.VariableFactory +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +class MatrixTest { + + @Test + fun rawString() { + assertEquals( + "(1, 2; 3, 4)", + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ).rawString + ) + } + + @Test + fun laTeXString() { + assertEquals( + "\\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}", + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ).laTeXString + ) + } + + @Test + fun rows() { + assertEquals( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ), + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ).rows + ) + } + + @Test + fun columns() { + assertEquals( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(3)), + listOf(IntegerFactory.instantiate(2), IntegerFactory.instantiate(4)) + ), + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ).columns + ) + } + + @Test + fun variables() { + assertEquals( + setOf(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")), + MatrixFactory.instantiate( + listOf( + listOf(VariableFactory.instantiate("x"), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), VariableFactory.instantiate("y")) + ) + ).variables + ) + } + + @Test + fun instantiateInvalidMatrix() { + assertFailsWith(IllegalArgumentException::class) { + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3)) + ) + ) + } + } + + @Test + fun instantiateReal() { + assertEquals( + IntegerFactory.instantiate(1), + MatrixFactory.instantiate(listOf(listOf(IntegerFactory.instantiate(1)))) + ) + } + + @Test + fun transposition() { + assertEquals( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(3)), + listOf(IntegerFactory.instantiate(2), IntegerFactory.instantiate(4)) + ) + ), + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ).transpose() + ) + } + + @Test + fun sumCorrectMatrix() { + assertEquals( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(6), IntegerFactory.instantiate(8)), + listOf(IntegerFactory.instantiate(10), IntegerFactory.instantiate(12)) + ) + ), + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ).sum( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(5), IntegerFactory.instantiate(6)), + listOf(IntegerFactory.instantiate(7), IntegerFactory.instantiate(8)) + ) + ) + ) + ) + } + + @Test + fun sumIncorrectMatrix() { + assertFailsWith(UnsupportedOperationException::class) { + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ).sum( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(5), IntegerFactory.instantiate(6)), + listOf(IntegerFactory.instantiate(7), IntegerFactory.instantiate(8)), + listOf(IntegerFactory.instantiate(9), IntegerFactory.instantiate(10)) + ) + ) + ) + } + } + + @Test + fun sumUnsupported() { + assertFailsWith(UnsupportedOperationException::class) { + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ).sum(StringValue("test")) + } + } + + @Test + fun multiplyCorrectMatrix() { + assertEquals( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(2340)), + listOf(IntegerFactory.instantiate(0), IntegerFactory.instantiate(1000)) + ) + ), + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(2), IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)), + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(0), IntegerFactory.instantiate(0)) + ) + ).multiply( + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(0), IntegerFactory.instantiate(1000)), + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(100)), + listOf(IntegerFactory.instantiate(0), IntegerFactory.instantiate(10)) + ) + ) + ) + ) + } + + @Test + fun multiplyIncorrectMatrix() { + assertFailsWith(UnsupportedOperationException::class) { + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(2), IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)), + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(0), IntegerFactory.instantiate(0)) + ) + ).multiply( + MatrixFactory.instantiate( + listOf( + listOf( + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3), + IntegerFactory.instantiate(4) + ), + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(0), + IntegerFactory.instantiate(0) + ) + ) + ) + ) + } + } + + @Test + fun multiplyUnsupported() { + assertFailsWith(UnsupportedOperationException::class) { + MatrixFactory.instantiate( + listOf( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3), IntegerFactory.instantiate(4)) + ) + ).multiply(StringValue("test")) + } + } + +} \ No newline at end of file diff --git a/src/commonTest/kotlin/me/nathanfallet/makth/sets/vectors/VectorTest.kt b/src/commonTest/kotlin/me/nathanfallet/makth/sets/vectors/VectorTest.kt new file mode 100644 index 0000000..8c8b919 --- /dev/null +++ b/src/commonTest/kotlin/me/nathanfallet/makth/sets/vectors/VectorTest.kt @@ -0,0 +1,194 @@ +package me.nathanfallet.makth.sets.vectors + +import me.nathanfallet.makth.extensions.StringValue +import me.nathanfallet.makth.numbers.integers.IntegerFactory +import me.nathanfallet.makth.resolvables.variables.VariableFactory +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +class VectorTest { + + @Test + fun rawString() { + assertEquals( + "(1; 2; 3)", + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ).rawString + ) + } + + @Test + fun laTeXString() { + assertEquals( + "\\begin{pmatrix} 1 \\\\ 2 \\\\ 3 \\end{pmatrix}", + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ).laTeXString + ) + } + + @Test + fun variables() { + assertEquals( + setOf(VariableFactory.instantiate("x"), VariableFactory.instantiate("y")), + VectorFactory.instantiate( + listOf( + VariableFactory.instantiate("x"), + VariableFactory.instantiate("y") + ) + ).variables + ) + } + + @Test + fun elements() { + assertEquals( + listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2), IntegerFactory.instantiate(3)), + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ).elements + ) + } + + @Test + fun rows() { + assertEquals( + listOf( + listOf(IntegerFactory.instantiate(1)), + listOf(IntegerFactory.instantiate(2)), + listOf(IntegerFactory.instantiate(3)) + ), + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ).rows + ) + } + + @Test + fun columns() { + assertEquals( + listOf(listOf(IntegerFactory.instantiate(1), IntegerFactory.instantiate(2), IntegerFactory.instantiate(3))), + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ).columns + ) + } + + @Test + fun sumCorrectVector() { + assertEquals( + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(5), + IntegerFactory.instantiate(7), + IntegerFactory.instantiate(9) + ) + ), + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ).sum( + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(4), + IntegerFactory.instantiate(5), + IntegerFactory.instantiate(6) + ) + ) + ) + ) + } + + @Test + fun sumIncorrectVector() { + assertFailsWith(UnsupportedOperationException::class) { + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ).sum( + VectorFactory.instantiate(listOf(IntegerFactory.instantiate(4), IntegerFactory.instantiate(5))) + ) + } + } + + @Test + fun sumUnsupported() { + assertFailsWith(UnsupportedOperationException::class) { + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ).sum( + StringValue("test") + ) + } + } + + @Test + fun multiplyCorrectReal() { + assertEquals( + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(4), + IntegerFactory.instantiate(6) + ) + ), + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ).multiply( + IntegerFactory.instantiate(2) + ) + ) + } + + @Test + fun multiplyUnsupported() { + assertFailsWith(UnsupportedOperationException::class) { + VectorFactory.instantiate( + listOf( + IntegerFactory.instantiate(1), + IntegerFactory.instantiate(2), + IntegerFactory.instantiate(3) + ) + ).multiply( + StringValue("test") + ) + } + } + +} \ No newline at end of file From 08416a788b1cd9ae868358666e2959b65c62ac6f Mon Sep 17 00:00:00 2001 From: NathanFallet Date: Sun, 5 Nov 2023 00:36:55 +0100 Subject: [PATCH 2/2] Gradle config update --- build.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle.kts b/build.gradle.kts index 66db781..0a90ab8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -25,7 +25,9 @@ kotlin { } js { binaries.library() + nodejs() browser() + //generateTypeScriptDefinitions() // Not supported for now because of collections etc... } val hostOs = System.getProperty("os.name") val isArm64 = System.getProperty("os.arch") == "aarch64"