forked from JedS6391/LGP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add program translator and operations tests.
- Loading branch information
Showing
4 changed files
with
243 additions
and
32 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/test/kotlin/nz/co/jedsimson/lgp/test/mocks/MockOperations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package nz.co.jedsimson.lgp.test.mocks | ||
|
||
import nz.co.jedsimson.lgp.core.modules.ModuleInformation | ||
import nz.co.jedsimson.lgp.core.program.instructions.BinaryOperation | ||
import nz.co.jedsimson.lgp.core.program.instructions.RegisterIndex | ||
import nz.co.jedsimson.lgp.core.program.instructions.UnaryOperation | ||
|
||
class Identity : UnaryOperation<Double>({ arguments -> arguments.get(0) }) { | ||
override val representation: String | ||
get() = TODO("not implemented") | ||
|
||
override fun toString(operands: List<RegisterIndex>, destination: RegisterIndex): String { | ||
TODO("not implemented") | ||
} | ||
|
||
override val information: ModuleInformation | ||
get() = TODO("not implemented") | ||
} | ||
|
||
class Zero : BinaryOperation<Double>({ arguments -> 0.0 }) { | ||
override val representation: String | ||
get() = TODO("not implemented") | ||
|
||
override fun toString(operands: List<RegisterIndex>, destination: RegisterIndex): String { | ||
TODO("not implemented") | ||
} | ||
|
||
override val information: ModuleInformation | ||
get() = TODO("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
src/test/kotlin/nz/co/jedsimson/lgp/test/program/OperationFeature.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package nz.co.jedsimson.lgp.test.program | ||
|
||
import nz.co.jedsimson.lgp.core.program.instructions.ArityException | ||
import nz.co.jedsimson.lgp.core.program.instructions.BinaryOperation | ||
import nz.co.jedsimson.lgp.core.program.instructions.UnaryOperation | ||
import nz.co.jedsimson.lgp.core.program.registers.Argument | ||
import nz.co.jedsimson.lgp.core.program.registers.Arguments | ||
import nz.co.jedsimson.lgp.test.mocks.Identity | ||
import nz.co.jedsimson.lgp.test.mocks.Zero | ||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.gherkin.Feature | ||
|
||
object OperationFeature : Spek({ | ||
|
||
Feature("Operations") { | ||
Scenario("Unary operations can be executed with a valid argument set") { | ||
val rawArguments = listOf(1.0) | ||
var operation: UnaryOperation<Double>? = null | ||
var result: Double? = null | ||
|
||
Given("A unary operation") { | ||
operation = Identity() | ||
} | ||
|
||
When("The operation is executed with a single argument") { | ||
val arguments = Arguments(rawArguments.map { a -> Argument(a) }) | ||
result = operation!!.execute(arguments) | ||
} | ||
|
||
Then("The operation execution is successful") { | ||
assert(result != null) { "Result was null" } | ||
assert(result == 1.0) { "Result was not expected" } | ||
} | ||
} | ||
|
||
Scenario("Unary operation throws when executed with too few arguments") { | ||
val rawArguments = listOf<Double>() | ||
var operation: UnaryOperation<Double>? = null | ||
var result: Double? = null | ||
var exception: Exception? = null | ||
|
||
Given("A unary operation") { | ||
operation = Identity() | ||
} | ||
|
||
When("The operation is executed with zero arguments") { | ||
val arguments = Arguments(rawArguments.map { a -> Argument(a) }) | ||
|
||
try { | ||
result = operation!!.execute(arguments) | ||
} | ||
catch (ex: Exception) { | ||
exception = ex | ||
} | ||
} | ||
|
||
Then("The operation execution is not successful") { | ||
assert(result == null) { "Result was not null" } | ||
assert(exception != null) { "Exception was null" } | ||
assert(exception is ArityException) { "Exception was not the correct type" } | ||
} | ||
} | ||
|
||
Scenario("Unary operation throws when executed with too many arguments") { | ||
val rawArguments = listOf(1.0, 2.0) | ||
var operation: UnaryOperation<Double>? = null | ||
var result: Double? = null | ||
var exception: Exception? = null | ||
|
||
Given("A unary operation") { | ||
operation = Identity() | ||
} | ||
|
||
When("The operation is executed with two arguments") { | ||
val arguments = Arguments(rawArguments.map { a -> Argument(a) }) | ||
|
||
try { | ||
result = operation!!.execute(arguments) | ||
} | ||
catch (ex: Exception) { | ||
exception = ex | ||
} | ||
} | ||
|
||
Then("The operation execution is not successful") { | ||
assert(result == null) { "Result was not null" } | ||
assert(exception != null) { "Exception was null" } | ||
assert(exception is ArityException) { "Exception was not the correct type" } | ||
} | ||
} | ||
|
||
Scenario("Binary operations can be executed with a valid argument set") { | ||
val rawArguments = listOf(1.0, 2.0) | ||
var operation: BinaryOperation<Double>? = null | ||
var result: Double? = null | ||
|
||
Given("A binary operation") { | ||
operation = Zero() | ||
} | ||
|
||
When("The operation is executed with two arguments") { | ||
val arguments = Arguments(rawArguments.map { a -> Argument(a) }) | ||
result = operation!!.execute(arguments) | ||
} | ||
|
||
Then("The operation execution is successful") { | ||
assert(result != null) { "Result was null" } | ||
assert(result == 0.0) { "Result was not expected" } | ||
} | ||
} | ||
|
||
Scenario("Binary operation throws when executed with too few arguments") { | ||
val rawArguments = listOf(1.0) | ||
var operation: BinaryOperation<Double>? = null | ||
var result: Double? = null | ||
var exception: Exception? = null | ||
|
||
Given("A binary operation") { | ||
operation = Zero() | ||
} | ||
|
||
When("The operation is executed with one argument") { | ||
val arguments = Arguments(rawArguments.map { a -> Argument(a) }) | ||
|
||
try { | ||
result = operation!!.execute(arguments) | ||
} | ||
catch (ex: Exception) { | ||
exception = ex | ||
} | ||
} | ||
|
||
Then("The operation execution is not successful") { | ||
assert(result == null) { "Result was not null" } | ||
assert(exception != null) { "Exception was null" } | ||
assert(exception is ArityException) { "Exception was not the correct type" } | ||
} | ||
} | ||
|
||
Scenario("Binary operation throws when executed with too many arguments") { | ||
val rawArguments = listOf(1.0, 2.0, 3.0) | ||
var operation: BinaryOperation<Double>? = null | ||
var result: Double? = null | ||
var exception: Exception? = null | ||
|
||
Given("A binary operation") { | ||
operation = Zero() | ||
} | ||
|
||
When("The operation is executed with three argument") { | ||
val arguments = Arguments(rawArguments.map { a -> Argument(a) }) | ||
|
||
try { | ||
result = operation!!.execute(arguments) | ||
} | ||
catch (ex: Exception) { | ||
exception = ex | ||
} | ||
} | ||
|
||
Then("The operation execution is not successful") { | ||
assert(result == null) { "Result was not null" } | ||
assert(exception != null) { "Exception was null" } | ||
assert(exception is ArityException) { "Exception was not the correct type" } | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters