time for unit tests, instructionExecIf
This commit is contained in:
parent
d8de5ff761
commit
9e40df0012
22
src/Push.hs
22
src/Push.hs
@ -13,11 +13,7 @@ data Gene
|
||||
| StringGene String
|
||||
| StateFunc (State -> State)
|
||||
| Close
|
||||
|
||||
-- | Block [Gene]
|
||||
-- If we do plushy (as opposed to just detecting the Close itself,
|
||||
-- then we may need to make a structually recursive data structure for the "program" data structure
|
||||
-- exampleGenome = [Program] rather than [Gene], or just include the Block above?
|
||||
| Block [Gene]
|
||||
|
||||
data State = State
|
||||
{ exec :: [Gene],
|
||||
@ -46,7 +42,19 @@ emptyState =
|
||||
-- which should take the number and type of parameter they have.
|
||||
instructionIntAdd :: State -> State
|
||||
instructionIntAdd (State es [] fs bs ss ps) = State es [] fs bs ss ps
|
||||
instructionIntAdd (State es (i : is) fs bs ss ps) = State es ((i + head is) : drop 1 is) fs bs ss ps
|
||||
instructionIntAdd (State es (i : is) fs bs ss ps) = State es ((head is + i) : drop 1 is) fs bs ss ps
|
||||
|
||||
instructionIntSubtract :: State -> State
|
||||
instructionIntSubtract (State es [] fs bs ss ps) = State es [] fs bs ss ps
|
||||
instructionIntSubtract (State es (i : is) fs bs ss ps) = State es ((head is - i) : drop 1 is) fs bs ss ps
|
||||
|
||||
instructionIntMultiply :: State -> State
|
||||
instructionIntMultiply (State es [] fs bs ss ps) = State es [] fs bs ss ps
|
||||
instructionIntMultiply (State es (i : is) fs bs ss ps) = State es ((head is * i) : drop 1 is) fs bs ss ps
|
||||
|
||||
instructionIntDivide :: State -> State
|
||||
instructionIntDivide (State es [] fs bs ss ps) = State es [] fs bs ss ps
|
||||
instructionIntDivide (State es (i : is) fs bs ss ps) = State es (div (head is) i : drop 1 is) fs bs ss ps
|
||||
|
||||
-- This is one of the push genome functions itself, not infrastructure.
|
||||
-- Optionally, split this off into independent functions
|
||||
@ -90,7 +98,7 @@ interpretExec (State (e : es) is fs bs ss ps) =
|
||||
(BoolGene val) -> interpretExec (State es is fs (val : bs) ss ps)
|
||||
(StringGene val) -> interpretExec (State es is fs bs (val : ss) ps)
|
||||
(StateFunc func) -> interpretExec (func (State es is fs bs ss ps))
|
||||
-- (Block block) -> interpretExec (State ((reverse block) ++ es) is fs bs ss ps)
|
||||
(Block block) -> interpretExec (State (reverse block ++ es) is fs bs ss ps)
|
||||
|
||||
-- The safety of interpretExec on empty stacks depends on the functions it calls.
|
||||
-- Need to make interpretExec strict, right?
|
||||
|
21
src/Tests.hs
21
src/Tests.hs
@ -5,22 +5,25 @@ import Push
|
||||
exampleState =
|
||||
State
|
||||
{ exec = [IntGene 5, StateFunc instructionParameterLoad, StateFunc instructionIntAdd],
|
||||
int = [1, 2, 3],
|
||||
int = [2, 6, 3],
|
||||
float = [1.2, 1.7],
|
||||
bool = [True, False],
|
||||
string = ["Hello", "Push"],
|
||||
parameter = [IntGene 1, StringGene "Hi", BoolGene True, FloatGene 1.3]
|
||||
}
|
||||
|
||||
-- intAdd
|
||||
testResult1 = [3, 3] == int (instructionIntAdd exampleState)
|
||||
testResult1 = [8, 3] == int (instructionIntAdd exampleState)
|
||||
|
||||
-- interpretExec
|
||||
testResult2 = [6,1,2,3] == int (interpretExec exampleState)
|
||||
testResult2 = [4, 3] == int (instructionIntSubtract exampleState)
|
||||
|
||||
-- This nukes the exec stack, just as an example of how to load at the start.
|
||||
loadedState = loadProgarm [IntGene 6, IntGene 6, StateFunc instructionParameterLoad, StateFunc instructionIntAdd] emptyState
|
||||
testResult3 = [12, 3] == int (instructionIntMultiply exampleState)
|
||||
|
||||
-- interpretExec
|
||||
testResult3 = [12] == int (interpretExec loadedState)
|
||||
testResult4 = [3, 3] == int (instructionIntDivide exampleState)
|
||||
|
||||
testResult5 = [6, 2, 6, 3] == int (interpretExec exampleState)
|
||||
|
||||
loadedState = loadProgarm [IntGene 6, IntGene 6, StateFunc instructionIntAdd] emptyState
|
||||
|
||||
testResult6 = [12] == int (interpretExec loadedState)
|
||||
|
||||
allTests = and [testResult1, testResult2, testResult3, testResult4, testResult5, testResult6]
|
||||
|
Loading…
x
Reference in New Issue
Block a user