From eb8fd9575631f175be603dffaa13ad3f94a7c9e8 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 15 Jan 2025 23:55:02 -0600 Subject: [PATCH] fix execDoRange, clean up my tests --- src/Push.hs | 15 +++++++++++++-- src/Tests.hs | 18 ++++-------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Push.hs b/src/Push.hs index f50c9e3..d338a16 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -18,7 +18,6 @@ data Gene | Close | Block [Gene] --- Not going to have instances for Block or StateFunc instance Eq Gene where IntGene x == IntGene y = x == y FloatGene x == FloatGene y = x == y @@ -28,6 +27,17 @@ instance Eq Gene where Close == Close = True StateFunc x == StateFunc y = True -- This line is probably not the best thing to do Block [x] == Block [y] = [x] == [y] + _ == _ = False + +instance Show Gene where + show (IntGene x) = "Int: " <> show x + show (FloatGene x) = "Float: " <> show x + show (BoolGene x) = "Bool: " <> show x + show (StringGene x) = "String: " <> x + show (StateFunc func) = "Func: unnamed" + show (PlaceInput x) = "In: " <> x + show Close = "Close" + show (Block xs) = "Block: " <> show xs data State = State { exec :: [Gene], @@ -38,6 +48,7 @@ data State = State parameter :: [Gene], input :: Map.Map String Gene } + deriving Show emptyState :: State emptyState = @@ -87,7 +98,7 @@ instructionExecDup state = state instructionExecDoRange :: State -> State instructionExecDoRange (State (e1 : es) (i0 : i1 : is) fs bs ss ps im) = if increment i0 i1 /= 0 - then State (IntGene i1 : Block [IntGene (i1 + increment i0 i1), IntGene i0, StateFunc instructionExecDoRange] : es) (i1 : is) fs bs ss ps im + then State (e1 : Block [IntGene (i1 + increment i0 i1), IntGene i0, StateFunc instructionExecDoRange, e1] : es) (i1 : is) fs bs ss ps im else State (e1 : es) (i1 : is) fs bs ss ps im where increment :: Int -> Int -> Int diff --git a/src/Tests.hs b/src/Tests.hs index 60c1590..315b1f3 100644 --- a/src/Tests.hs +++ b/src/Tests.hs @@ -14,17 +14,6 @@ exampleState = input = Map.fromList [("in0" , IntGene 1)] } -doRangeState = - State - { exec = [Block [IntGene 1, IntGene 6, StateFunc instructionIntAdd], Block [IntGene 4, IntGene 1, StateFunc instructionExecDoRange]] - , int = [] - , float = [] - , string = ["abc"] - , bool = [] - , parameter = [] - , input = Map.empty - } - testResult1 = [8, 3] == int (instructionIntAdd exampleState) testResult2 = [4, 3] == int (instructionIntSub exampleState) @@ -49,10 +38,11 @@ loadedState4 = loadProgram [BoolGene False, PlaceInput "in0", StateFunc instruct testResult9 = [3, 6, 3] == int (interpretExec loadedState4) -- Tests execDup -loadedState5 = instructionExecDup exampleState -testResult10 = exec loadedState5 !! 0 == IntGene 5 && exec loadedState5 !! 1 == IntGene 5 +loadedState5 = interpretExec $ loadProgram [StateFunc instructionExecDup, IntGene 2] emptyState +testResult10 = int loadedState5 !! 0 == 2 && int loadedState5 !! 1 == 2 -- Tests execDoRange -testResult11 = [1, 4, 7] == int (interpretExec doRangeState) +loadedState6 = loadProgram [IntGene 2, Block [IntGene 4, IntGene 1, StateFunc instructionExecDoRange], StateFunc instructionIntAdd] emptyState +testResult11 = [12] == int (interpretExec loadedState6) allTests = and [testResult1, testResult2, testResult3, testResult4, testResult5, testResult6, testResult7, testResult8, testResult9]