Skip to content

Commit 41537f1

Browse files
committed
Add GFill and GVoid to Trace
1 parent 2c93809 commit 41537f1

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/Trace.hs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,18 @@ instance Coded NearDiff where
9191

9292
instance Coded FarDiff where
9393
encode (FarDiff dx dy dz) = do
94-
putBits 7 0 ((dx + 30) :: Word8)
95-
putBits 7 0 ((dy + 30) :: Word8)
96-
putBits 7 0 ((dz + 30) :: Word8)
94+
putWord8 (fromIntegral $ dx + 30)
95+
putWord8 (fromIntegral $ dy + 30)
96+
putWord8 (fromIntegral $ dz + 30)
9797

9898
decode = do
9999
dx <- getWord8
100100
dy <- getWord8
101101
dz <- getWord8
102102
FarDiff
103-
<$> ((fromIntegral dx)-30)
104-
<*> ((fromIntegral dy)-30)
105-
<*> ((fromIntegral dz)-30)
103+
<$> pure ((fromIntegral dx)-30)
104+
<*> pure ((fromIntegral dy)-30)
105+
<*> pure ((fromIntegral dz)-30)
106106

107107
instance Coded Command where
108108
encode Halt = putBits 7 0 (0b11111111 :: Word8)
@@ -140,9 +140,15 @@ instance Coded Command where
140140
encode nd
141141
putBits 2 0 (0b011 :: Int)
142142

143-
encode (GFill nd fd) = undefined
143+
encode (GFill nd fd) = do
144+
encode nd
145+
putBits 2 0 (0b001 :: Int)
146+
encode fd
144147

145-
encode (GVoid nd fd) = undefined
148+
encode (GVoid nd fd) = do
149+
encode nd
150+
putBits 2 0 (0b000 :: Int)
151+
encode fd
146152

147153
decode = parseOpcode =<< getWord8
148154
where
@@ -188,6 +194,28 @@ instance Coded Command where
188194
| byte .&. 0b111 == 0b011
189195
= Fill <$> pure (testDecode [byte])
190196

197+
| byte .&.0b111 == 0b001 = do
198+
{- GFill -}
199+
let nd = testDecode [byte]
200+
201+
dx <- getWord8
202+
dy <- getWord8
203+
dz <- getWord8
204+
let fd = testDecode [dx, dy, dz]
205+
206+
return $ GFill nd fd
207+
208+
| byte .&.0b111 == 0b000 = do
209+
{- GVoid -}
210+
let nd = testDecode [byte]
211+
212+
dx <- getWord8
213+
dy <- getWord8
214+
dz <- getWord8
215+
let fd = testDecode [dx, dy, dz]
216+
217+
return $ GVoid nd fd
218+
191219
| otherwise = error "Command.decode: invalid input"
192220

193221
-- | Converts short linear difference into a long linear difference.

test/TestSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ specExamples = testGroup "Specification examples"
5454
, testCase "encode.Fill" $
5555
testEncode (Fill (NearDiff 0 (-1) 0)) @?= "01010011"
5656

57-
, testCsae "encode.GFill" $
57+
, testCase "encode.GFill" $
5858
testEncode (GFill (NearDiff 0 (-1) 0) (FarDiff 10 (-15) 20)) @?= "01010001 00101000 00001111 00110010"
5959

6060
, testCase "encode.GVoid" $
@@ -90,7 +90,7 @@ codec = testGroup "Encoding-then-decoding returns the same value"
9090

9191
, testCase "Fill" $ testCodecFor (Fill (NearDiff 0 (-1) 0))
9292

93-
, testCsae "GFill" $
93+
, testCase "GFill" $
9494
testCodecFor (GFill (NearDiff 0 (-1) 0) (FarDiff 10 (-15) 20))
9595

9696
, testCase "GVoid" $

0 commit comments

Comments
 (0)