packages feed

willow-0.1.0.0: test/Test/Willow/Property/Encoding/Big5.hs

{-|
Description:    

Copyright:      (c) 2020 Samuel May
License:        MPL-2.0
Maintainer:     ag.eitilt@gmail.com

Stability:      experimental
Portability:    portable
-}
module Test.Willow.Property.Encoding.Big5
    ( tests
    ) where


import qualified Hedgehog as H
import qualified Hedgehog.Gen as H.G
import qualified Hedgehog.Range as H.R

import Web.Willow.Common.Encoding

import Test.Willow.Property.Common


tests :: H.Group
tests = packGroup "Web.Willow.Common.Encoding.Big5"
    -- Single byte tests handled in "unit" due to limited number
    [ decodeEndOfStream
    , decodeTwoByteInvalid
    , decodeOneByteLookup
    , decodeTwoByteLookup
    ]


decodeEndOfStream :: Test
decodeEndOfStream = packTest "incomplete character" $ do
    first <- H.forAll . H.G.word8 $ H.R.linear 0x81 0xFE
    checkEndOfStream Big5 [first]


decodeTwoByteInvalid :: Test
decodeTwoByteInvalid = packTest "invalid second byte" $ do
    first <- H.forAll . H.G.word8 $ H.R.linear 0x81 0xFE
    second <- H.forAll $ H.G.choice
        [ H.G.word8 $ H.R.linear 0x00 0x3F
        , H.G.word8 $ H.R.linear 0x7F 0xA0
        , H.G.constant 0xFF
        ]
    let check = if second <= 0x7F
            then checkInvalid
            else checkInvalidAll
    check Big5 [first, second]


decodeOneByteLookup :: Test
decodeOneByteLookup = packTest "one-byte sequence" $ do
    first <- H.forAll $ H.G.choice
        [ H.G.word8 $ H.R.linear 0x00 0x80
        , H.G.constant 0xFF
        ]
    checkTrailing Big5 [first]

decodeTwoByteLookup :: Test
decodeTwoByteLookup = packTest "two-byte sequence" $ do
    first <- H.forAll . H.G.word8 $ H.R.linear 0x81 0xFE
    second <- H.forAll . H.G.choice $ map H.G.word8
        [ H.R.linear 0x40 0x7E
        , H.R.linear 0xA1 0xFE
        ]
    if second <= 0x7F
        then checkTrailing Big5 [first, second]
        else checkTrailingAll Big5 [first, second]