packages feed

rap-0.2.0.0: test/Main.hs

{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Control.Monad
import Data.Binary.Get
import Data.Binary.Put
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as BS
import Data.Rap
import Data.Word
import Paths_rap
import System.FilePath
import Test.Tasty
import Test.Tasty.HUnit

main :: IO ()
main = getDataDir >>= \dataDir -> defaultMain $ testGroup "Data.Rap"
  [ getVerseTest, getStanzaTest
  , putVerseTest, putStanzaTest
  , tillICollapseTest, wavTest dataDir
  , putVerseTooLongTest, infiniteVerseTest
  , badVerseID, infVerseID, badStanzaIDType, infStanzaID, infStanzaTy
  ]

getVerseTest :: TestTree
getVerseTest = testCase "getVerse" $ do
  runGet getRap (BS.pack emptyVerseBS) @?= emptyVerse
  runGet getRap (BS.pack foooBaarBS) @?= foooBaarVerse
  runGet getRap (BS.pack baazZooBS) @?= baazZooVerse
  runGet getRap (BS.pack krs1VerseBS) @?= krs1Verse

getStanzaTest :: TestTree
getStanzaTest = testCase "getStanza" $ do
  runGet getRap (BS.pack emptyStanzaBS) @?= emptyStanza
  runGet getRap (BS.pack foooBaarStanzaBS) @?= foooBaarStanza
  runGet getRap (BS.pack krs1StanzaBS) @?= krs1Stanza
  runGetOrFail getRap (BS.pack overflowStanzaBS) @?= Left ("", 28, "LIST: ovfl: overflow")

putVerseTest :: TestTree
putVerseTest = testCase "putVerse" $ do
  fmap (BS.unpack . runPut) (putRap emptyVerse) @?= Right emptyVerseBS
  fmap (BS.unpack . runPut) (putRap foooBaarVerse) @?= Right foooBaarBS
  fmap (BS.unpack . runPut) (putRap baazZooVerse) @?= Right baazZooBS
  fmap (BS.unpack . runPut) (putRap krs1Verse) @?= Right krs1VerseBS

putStanzaTest :: TestTree
putStanzaTest = testCase "putStanza" $ do
  fmap (BS.unpack . runPut) (putRap emptyStanza) @?= Right emptyStanzaBS
  fmap (BS.unpack . runPut) (putRap foooBaarStanza) @?= Right foooBaarStanzaBS
  fmap (BS.unpack . runPut) (putRap krs1Stanza) @?= Right krs1StanzaBS

tillICollapseTest :: TestTree
tillICollapseTest = testCase "TillICollapse" $
  fmap (runGet getRap . runPut) (putRap tillICollapse) @?= Right tillICollapse

wavTest :: FilePath -> TestTree
wavTest dataDir = testCase "wav" $ do
  bs <- BS.readFile $ dataDir </> "hello.wav"
  fmap runPut (putRap $ runGet getRap bs) @?= Right bs

putVerseTooLongTest :: TestTree
putVerseTooLongTest = testCase "verseTooLong" $
  (void . putRap) (Verse "fooo" $ BS.replicate (fromIntegral (maxBound :: Word32) + 1) 0xFF)
    @?= Left ("fooo: length > " ++ show (maxBound :: Word32))

infiniteVerseTest :: TestTree
infiniteVerseTest = testCase "infiniteVerse" $
  (void . putRap) (Verse "fooo" $ BS.repeat 0xFF)
    @?= Left ("fooo: length > " ++ show (maxBound :: Word32))

badVerseID :: TestTree
badVerseID = testCase "badVerseID" $ do
  (void . putRap) (Verse "foo" mempty) @?= Left "expecting 4 byte ID: \"foo\""
  (void . putRap) (Verse "foobar" mempty) @?= Left "expecting 4 byte ID: \"foob...\""

infVerseID :: TestTree
infVerseID = testCase "infVerseID" $
  (void . putRap) (Verse (repeat 'a') mempty) @?= Left "expecting 4 byte ID: \"aaaa...\""

badStanzaIDType :: TestTree
badStanzaIDType = testCase "badStanzaIDType" $ do
  (void . putRap) (Stanza "foo" 4 "baar" []) @?= Left "expecting 4 byte ID: \"foo\""
  (void . putRap) (Stanza "foobar" 4 "baar" []) @?= Left "expecting 4 byte ID: \"foob...\""
  (void . putRap) (Stanza "fooo" 4 "bar" []) @?= Left "expecting 4 byte type: \"bar\""
  (void . putRap) (Stanza "fooo" 4 "barbaz" []) @?= Left "expecting 4 byte type: \"barb...\""

infStanzaID :: TestTree
infStanzaID = testCase "infStanzaID" $ do
  (void . putRap) (Stanza (repeat 'a') 4 "baar" []) @?= Left "expecting 4 byte ID: \"aaaa...\""
  (void . putRap) (Stanza "aaaa" 16 "baar" [Stanza (repeat 'b') 4 "foob" []])
    @?= Left "expecting 4 byte ID: \"bbbb...\""

infStanzaTy :: TestTree
infStanzaTy = testCase "infStanzaTy" $ do
  (void . putRap) (Stanza "fooo" 4 (repeat 'a') []) @?= Left "expecting 4 byte type: \"aaaa...\""
  (void . putRap) (Stanza "fooo" 16 "baar" [Stanza "zooo" 4 (repeat 'b') []])
    @?= Left "expecting 4 byte type: \"bbbb...\""

emptyVerse :: Rap
emptyVerse = Verse "empt" ""

emptyVerseBS :: [Word8]
emptyVerseBS = BS.unpack $ "empt" <> runPut (putWord32le 0)

foooBaarVerse :: Rap
foooBaarVerse = Verse "fooo" "baar"

foooBaarBS :: [Word8]
foooBaarBS = BS.unpack $ "fooo" <> runPut (putWord32le 4) <> "baar"

baazZooVerse :: Rap
baazZooVerse = Verse "baaz" "zoo"

baazZooBS :: [Word8]
baazZooBS = BS.unpack ("baaz" <> runPut (putWord32le 3) <> "zoo") ++ [0x00]

emptyStanza :: Rap
emptyStanza = Stanza "RIFF" 4 "empt" []

emptyStanzaBS :: [Word8]
emptyStanzaBS = BS.unpack $ "RIFF" <> runPut (putWord32le 4) <> "empt"

foooBaarStanza :: Rap
foooBaarStanza = Stanza "LIST" (4 + fromIntegral (length foooBaarBS)) "list" [foooBaarVerse]

foooBaarStanzaBS :: [Word8]
foooBaarStanzaBS = BS.unpack $ "LIST" <>
  runPut (putWord32le $ 4 + fromIntegral (length foooBaarBS)) <> "list"
  <> BS.pack foooBaarBS

overflowStanzaBS :: [Word8]
overflowStanzaBS = BS.unpack $ "LIST" <> runPut (putWord32le 16) <> "ovfl" <>
  "fooo" <> runPut (putWord32le 8) <> "12345678"

stepIntoAWorld :: ByteString
stepIntoAWorld = "step into a world where theres no one left but the very best"

stepIntoAWorldLen :: Word32 -- 60
stepIntoAWorldLen = fromIntegral $ BS.length stepIntoAWorld

krs1Verse :: Rap
krs1Verse = Verse "krs1" stepIntoAWorld

krs1VerseBS :: [Word8]
krs1VerseBS = BS.unpack $ "krs1" <> runPut (putWord32le stepIntoAWorldLen) <> stepIntoAWorld

krs1Stanza :: Rap
krs1Stanza = Stanza "RIFF" (4 + fromIntegral (length krs1VerseBS)) "krs1" [krs1Verse]

krs1StanzaBS :: [Word8]
krs1StanzaBS = BS.unpack $ "RIFF" <>
  runPut (putWord32le $ 4 + fromIntegral (length krs1VerseBS)) <> "krs1"
  <> BS.pack krs1VerseBS

tillICollapse :: Rap
tillICollapse = Stanza "RIFF" 918 "mnem"
  [ Verse "lin1" "Til I collapse, I'm spillin' these raps long as you feel em"
  , Verse "lin2" "Til the day that I drop, you'll never say that I'm not killin em"
  , Verse "lin3" "Cause when I am not, then Ima stop pennin em"
  , Verse "lin4" "And I am not hip-hop and Im just not Eminem"
  , Verse "lin5" "Subliminal throughts, when Ima stop sendin em?"
  , Verse "lin6" "Women are caught in webs, spin em and hock venom"
  , Verse "lin7" "Adrenaline shots of penicillin could not get the illin to stop"
  , Verse "lin8" "Amoxicillins just not real enough"
  , Verse "lin9" "The criminal, cop-killin, hip-hop villain"
  , Verse "li10" "A minimal swap to cop millions of Pac listeners"
  , Verse "li11" "Youre comin with me, feel it or not"
  , Verse "li12" "You're gonna fear it like I showed ya the spirit of God lives in us"
  , Verse "li13" "You hear it a lot, lyrics to shock"
  , Verse "li14" "Is it a miracle or am I just product of pop fizzin' up?"
  , Verse "li15" "Fa' shizzle, my wizzle, this is the plot, listen up"
  , Verse "li16" "You bizzles forgot, Slizzle does not give a fuck"
  ]