Skip to content

Commit

Permalink
Add program translator and operations tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JedS6391 committed Jun 19, 2019
1 parent e7ee35a commit 67aad88
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 32 deletions.
30 changes: 30 additions & 0 deletions src/test/kotlin/nz/co/jedsimson/lgp/test/mocks/MockOperations.kt
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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,12 @@ package nz.co.jedsimson.lgp.test.mocks
import nz.co.jedsimson.lgp.core.environment.DefaultValueProviders
import nz.co.jedsimson.lgp.core.environment.EnvironmentDefinition
import nz.co.jedsimson.lgp.core.modules.ModuleInformation
import nz.co.jedsimson.lgp.core.program.Output
import nz.co.jedsimson.lgp.core.program.Outputs
import nz.co.jedsimson.lgp.core.program.Program
import nz.co.jedsimson.lgp.core.program.ProgramGenerator
import nz.co.jedsimson.lgp.core.program.*
import nz.co.jedsimson.lgp.core.program.instructions.*
import nz.co.jedsimson.lgp.core.program.registers.Arguments
import nz.co.jedsimson.lgp.core.program.registers.RegisterSet
import java.util.*

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")
}

class MockInstruction(
override var destination: RegisterIndex,
override var operands: MutableList<RegisterIndex>,
Expand Down Expand Up @@ -188,10 +161,10 @@ class MockMultipleOutputProgramGenerator(
override fun generateProgram(): Program<Double, Outputs.Multiple<Double>> {
val instructions = this.instructionGenerator.next().take(2).toList()
val registers = RegisterSet(
inputRegisters = 2,
calculationRegisters = 0,
constants = listOf(),
defaultValueProvider = DefaultValueProviders.constantValueProvider(1.0)
inputRegisters = 2,
calculationRegisters = 0,
constants = listOf(),
defaultValueProvider = DefaultValueProviders.constantValueProvider(1.0)
)

return MockMultipleOutputProgram(instructions, registers, listOf(0, 1))
Expand All @@ -200,4 +173,15 @@ class MockMultipleOutputProgramGenerator(
override val information: ModuleInformation
get() = TODO("not implemented")

}

class MockSingleOutputProgramTranslator : ProgramTranslator<Double, Outputs.Single<Double>>() {

override fun translate(program: Program<Double, Outputs.Single<Double>>): String {
return "MockSingleOutputProgram"
}

override val information: ModuleInformation
get() = TODO("not implemented")

}
168 changes: 168 additions & 0 deletions src/test/kotlin/nz/co/jedsimson/lgp/test/program/OperationFeature.kt
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" }
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nz.co.jedsimson.lgp.test.program
import nz.co.jedsimson.lgp.core.program.Outputs
import nz.co.jedsimson.lgp.core.program.Program
import nz.co.jedsimson.lgp.core.program.ProgramGenerator
import nz.co.jedsimson.lgp.core.program.ProgramTranslator
import nz.co.jedsimson.lgp.test.mocks.*
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.gherkin.Feature
Expand Down Expand Up @@ -78,5 +79,33 @@ object ProgramAndInstructionGeneratorFeature : Spek({
}
}
}

Scenario("Generated programs can be translated") {
var programGenerator: ProgramGenerator<Double, Outputs.Single<Double>>? = null
var programTranslator: ProgramTranslator<Double, Outputs.Single<Double>>? = null
var program: Program<Double, Outputs.Single<Double>>? = null
var translatedProgram: String? = null

Given("A program generator") {
programGenerator = MockSingleOutputProgramGenerator(MockEnvironment())
}

And("A program translator") {
programTranslator = MockSingleOutputProgramTranslator()
}

And("A program is generated") {
program = programGenerator!!.next().first()
}

When("The program is translated") {
translatedProgram = programTranslator!!.translate(program!!)
}

Then("The program is translated correctly") {
assert(translatedProgram != null) { "Translated program was null" }
assert(translatedProgram == "MockSingleOutputProgram") { "Translated program was not correct" }
}
}
}
})

0 comments on commit 67aad88

Please sign in to comment.