Skip to content

Commit

Permalink
Add GFill and GVoid to Trace
Browse files Browse the repository at this point in the history
  • Loading branch information
Minoru committed Jul 21, 2018
1 parent 2c93809 commit 41537f1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
44 changes: 36 additions & 8 deletions src/Trace.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ instance Coded NearDiff where

instance Coded FarDiff where
encode (FarDiff dx dy dz) = do
putBits 7 0 ((dx + 30) :: Word8)
putBits 7 0 ((dy + 30) :: Word8)
putBits 7 0 ((dz + 30) :: Word8)
putWord8 (fromIntegral $ dx + 30)
putWord8 (fromIntegral $ dy + 30)
putWord8 (fromIntegral $ dz + 30)

decode = do
dx <- getWord8
dy <- getWord8
dz <- getWord8
FarDiff
<$> ((fromIntegral dx)-30)
<*> ((fromIntegral dy)-30)
<*> ((fromIntegral dz)-30)
<$> pure ((fromIntegral dx)-30)
<*> pure ((fromIntegral dy)-30)
<*> pure ((fromIntegral dz)-30)

instance Coded Command where
encode Halt = putBits 7 0 (0b11111111 :: Word8)
Expand Down Expand Up @@ -140,9 +140,15 @@ instance Coded Command where
encode nd
putBits 2 0 (0b011 :: Int)

encode (GFill nd fd) = undefined
encode (GFill nd fd) = do
encode nd
putBits 2 0 (0b001 :: Int)
encode fd

encode (GVoid nd fd) = undefined
encode (GVoid nd fd) = do
encode nd
putBits 2 0 (0b000 :: Int)
encode fd

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

| byte .&.0b111 == 0b001 = do
{- GFill -}
let nd = testDecode [byte]

dx <- getWord8
dy <- getWord8
dz <- getWord8
let fd = testDecode [dx, dy, dz]

return $ GFill nd fd

| byte .&.0b111 == 0b000 = do
{- GVoid -}
let nd = testDecode [byte]

dx <- getWord8
dy <- getWord8
dz <- getWord8
let fd = testDecode [dx, dy, dz]

return $ GVoid nd fd

| otherwise = error "Command.decode: invalid input"

-- | Converts short linear difference into a long linear difference.
Expand Down
4 changes: 2 additions & 2 deletions test/TestSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ specExamples = testGroup "Specification examples"
, testCase "encode.Fill" $
testEncode (Fill (NearDiff 0 (-1) 0)) @?= "01010011"

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

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

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

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

, testCase "GVoid" $
Expand Down

0 comments on commit 41537f1

Please sign in to comment.