2019-06-25 15:18:34 -04:00
|
|
|
{-# language BangPatterns #-}
|
2020-12-18 13:03:04 -05:00
|
|
|
{-# language NumericUnderscores #-}
|
2021-09-15 14:57:10 -04:00
|
|
|
{-# language OverloadedStrings #-}
|
|
|
|
{-# language QuasiQuotes #-}
|
2019-06-25 15:18:34 -04:00
|
|
|
{-# language ScopedTypeVariables #-}
|
2019-06-17 13:24:24 -04:00
|
|
|
{-# language TypeApplications #-}
|
|
|
|
|
2019-10-17 11:26:14 -04:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2020-07-29 14:42:40 -04:00
|
|
|
import Prelude hiding (replicate)
|
|
|
|
|
2019-10-17 11:26:14 -04:00
|
|
|
import Control.Applicative (liftA2)
|
2019-06-25 15:18:34 -04:00
|
|
|
import Control.Monad.ST (runST)
|
2020-02-27 13:58:00 -05:00
|
|
|
import Data.Bytes.Builder
|
2021-11-19 15:52:59 -05:00
|
|
|
import Data.Bytes.Builder.Template (bldr)
|
2020-01-15 16:50:24 -05:00
|
|
|
import Data.Bytes.Types (MutableBytes(MutableBytes))
|
2019-09-19 12:06:50 -04:00
|
|
|
import Data.Char (ord,chr)
|
2019-11-22 16:24:48 -05:00
|
|
|
import Data.IORef (IORef,newIORef,readIORef,writeIORef)
|
2021-09-15 14:57:10 -04:00
|
|
|
import Data.Maybe (fromMaybe)
|
2019-06-17 13:24:24 -04:00
|
|
|
import Data.Primitive (ByteArray)
|
2021-09-15 14:57:10 -04:00
|
|
|
import Data.Primitive (PrimArray)
|
|
|
|
import Data.Text.Short (ShortText)
|
2020-02-06 13:58:55 -05:00
|
|
|
import Data.WideWord (Word128(Word128),Word256(Word256))
|
2021-09-15 14:57:10 -04:00
|
|
|
import Data.Word
|
2020-04-13 11:29:38 -04:00
|
|
|
import Numeric.Natural (Natural)
|
2019-10-17 11:26:14 -04:00
|
|
|
import Test.QuickCheck ((===),Arbitrary)
|
2020-04-13 11:29:38 -04:00
|
|
|
import Test.QuickCheck.Instances.Natural ()
|
2021-09-15 14:57:10 -04:00
|
|
|
import Test.Tasty (defaultMain,testGroup,TestTree)
|
2019-08-05 12:07:02 -04:00
|
|
|
import Test.Tasty.HUnit ((@=?))
|
2021-09-15 14:57:10 -04:00
|
|
|
import Text.Printf (printf)
|
2019-09-03 14:43:58 -04:00
|
|
|
|
2019-09-03 14:58:03 -04:00
|
|
|
import qualified Arithmetic.Nat as Nat
|
2020-04-13 11:29:38 -04:00
|
|
|
import qualified Data.Bits as Bits
|
|
|
|
import qualified Data.Bytes as Bytes
|
2021-09-15 14:57:10 -04:00
|
|
|
import qualified Data.Bytes.Builder as Builder
|
|
|
|
import qualified Data.Bytes.Builder.Bounded as Bounded
|
|
|
|
import qualified Data.Bytes.Chunks as Chunks
|
2021-11-19 15:52:59 -05:00
|
|
|
import qualified Data.Bytes.Text.Ascii as Ascii
|
|
|
|
import qualified Data.Bytes.Text.Latin1 as Latin1
|
2019-09-03 14:43:58 -04:00
|
|
|
import qualified Data.ByteString as ByteString
|
2019-07-03 16:28:36 -04:00
|
|
|
import qualified Data.ByteString.Builder as BB
|
2019-09-03 14:43:58 -04:00
|
|
|
import qualified Data.ByteString.Lazy.Char8 as LB
|
2019-06-25 15:18:34 -04:00
|
|
|
import qualified Data.List as L
|
2019-09-03 14:43:58 -04:00
|
|
|
import qualified Data.Primitive as PM
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Encoding as TE
|
2019-06-17 13:24:24 -04:00
|
|
|
import qualified GHC.Exts as Exts
|
2020-07-29 14:42:40 -04:00
|
|
|
import qualified Prelude
|
2019-08-05 12:07:02 -04:00
|
|
|
import qualified Test.Tasty.HUnit as THU
|
2019-09-03 14:43:58 -04:00
|
|
|
import qualified Test.Tasty.QuickCheck as TQC
|
2019-06-17 13:24:24 -04:00
|
|
|
|
2019-07-05 12:35:05 -04:00
|
|
|
import qualified HexWord64
|
2019-09-22 09:20:03 -04:00
|
|
|
import qualified Word16Tree
|
2019-07-05 12:35:05 -04:00
|
|
|
|
2019-06-17 13:24:24 -04:00
|
|
|
main :: IO ()
|
|
|
|
main = defaultMain tests
|
|
|
|
|
|
|
|
tests :: TestTree
|
|
|
|
tests = testGroup "Tests"
|
2019-07-05 12:35:05 -04:00
|
|
|
[ testGroup "live"
|
|
|
|
[ TQC.testProperty "word64Dec" $ \w ->
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat 1 (word64Dec w) === pack (show w)
|
2019-07-05 12:35:05 -04:00
|
|
|
, TQC.testProperty "word64Dec-x3" $ \x y z ->
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat 1 (word64Dec x <> word64Dec y <> word64Dec z)
|
2019-07-05 12:35:05 -04:00
|
|
|
===
|
|
|
|
pack (show x ++ show y ++ show z)
|
|
|
|
, TQC.testProperty "int64Dec-x3" $ \x y z ->
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat 1 (int64Dec x <> int64Dec y <> int64Dec z)
|
2019-07-05 12:35:05 -04:00
|
|
|
===
|
|
|
|
pack (show x ++ show y ++ show z)
|
|
|
|
, TQC.testProperty "word64BE-x3" $ \x y z ->
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat 1 (word64BE x <> word64BE y <> word64BE z)
|
2019-07-05 12:35:05 -04:00
|
|
|
===
|
|
|
|
pack (LB.unpack (BB.toLazyByteString (BB.word64BE x <> BB.word64BE y <> BB.word64BE z)))
|
2020-02-06 13:58:55 -05:00
|
|
|
, TQC.testProperty "word256PaddedLowerHex" $ \w ->
|
|
|
|
Bounded.run Nat.constant (Bounded.word256PaddedLowerHex w)
|
|
|
|
===
|
|
|
|
pack (showWord256PaddedLowerHex w)
|
|
|
|
, TQC.testProperty "word128PaddedUpperHex" $ \w ->
|
|
|
|
Bounded.run Nat.constant (Bounded.word128PaddedUpperHex w)
|
|
|
|
===
|
|
|
|
pack (showWord128PaddedUpperHex w)
|
2019-07-05 12:35:05 -04:00
|
|
|
, TQC.testProperty "word64PaddedUpperHex" $ \w ->
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat 1 (word64PaddedUpperHex w)
|
2019-07-05 12:35:05 -04:00
|
|
|
===
|
|
|
|
pack (showWord64PaddedUpperHex w)
|
2020-01-03 06:42:32 -05:00
|
|
|
, TQC.testProperty "word16PaddedLowerHex" $ \w ->
|
|
|
|
runConcat 1 (word16PaddedLowerHex w)
|
|
|
|
===
|
|
|
|
pack (showWord16PaddedLowerHex w)
|
2019-12-30 20:43:36 -05:00
|
|
|
, TQC.testProperty "wordPaddedDec2" $ TQC.forAll (TQC.choose (0,99)) $ \w ->
|
|
|
|
Bounded.run Nat.two (Bounded.wordPaddedDec2 w)
|
2019-12-30 20:14:15 -05:00
|
|
|
===
|
|
|
|
pack (zeroPadL 2 (show w))
|
2020-02-12 14:56:32 -05:00
|
|
|
, TQC.testProperty "wordPaddedDec4" $ TQC.forAll (TQC.choose (0,9999)) $ \w ->
|
|
|
|
Bounded.run Nat.constant (Bounded.wordPaddedDec4 w)
|
|
|
|
===
|
|
|
|
pack (zeroPadL 4 (show w))
|
2019-12-30 20:43:36 -05:00
|
|
|
, TQC.testProperty "wordPaddedDec9" $ TQC.forAll (TQC.choose (0,999999999)) $ \w ->
|
|
|
|
Bounded.run Nat.constant (Bounded.wordPaddedDec9 w)
|
|
|
|
===
|
|
|
|
pack (zeroPadL 9 (show w))
|
2019-09-05 09:02:50 -04:00
|
|
|
, TQC.testProperty "word8Dec" $ \w ->
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat 1 (word8Dec w)
|
2019-09-05 09:02:50 -04:00
|
|
|
===
|
|
|
|
pack (show w)
|
2019-09-19 12:06:50 -04:00
|
|
|
, TQC.testProperty "consLength32BE" $ \w ->
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat 1 (consLength32BE (word8Dec w))
|
2019-09-19 12:06:50 -04:00
|
|
|
===
|
|
|
|
pack ('\x00' : '\x00' : '\x00' : chr (L.length (show w)) : show w)
|
2019-10-10 09:10:44 -04:00
|
|
|
, TQC.testProperty "consLength64BE-uni" $ \w ->
|
2019-10-09 16:30:02 -04:00
|
|
|
pack
|
|
|
|
( '\x00' : '\x00' : '\x00' : '\x00'
|
|
|
|
: '\x00' : '\x00' : '\x00' : chr (L.length (show w))
|
|
|
|
: show w
|
|
|
|
)
|
2019-07-05 12:35:05 -04:00
|
|
|
===
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat 1 (consLength64BE (word16Dec w))
|
2019-10-10 09:10:44 -04:00
|
|
|
, TQC.testProperty "consLength64BE-multi" $ \w ->
|
|
|
|
pack
|
|
|
|
( '\x00' : '\x00' : '\x00' : '\x00'
|
|
|
|
: '\x00' : '\x00' : '\x00' : chr (1 + L.length (show w))
|
|
|
|
: '\x42' : show w
|
|
|
|
)
|
|
|
|
===
|
|
|
|
runConcat 1 (consLength64BE (word8 0x42 <> flush 2 <> word16Dec w))
|
2019-09-03 14:43:58 -04:00
|
|
|
, THU.testCase "stringUtf8" $
|
|
|
|
packUtf8 "¿Cómo estás? I am doing well." @=?
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat 1 (stringUtf8 "¿Cómo estás? I am doing well.")
|
2019-08-05 12:07:02 -04:00
|
|
|
, THU.testCase "doubleDec-A" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack (show (2 :: Int)) @=? runConcat 1 (doubleDec 2.0)
|
2019-08-05 12:07:02 -04:00
|
|
|
, THU.testCase "doubleDec-B" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack (show (2.5 :: Double)) @=? runConcat 1 (doubleDec 2.5)
|
2019-08-05 12:07:02 -04:00
|
|
|
, THU.testCase "doubleDec-C" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("1e+15") @=? runConcat 1 (doubleDec 1e15)
|
2019-08-05 12:07:02 -04:00
|
|
|
, THU.testCase "doubleDec-D" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("-42") @=? runConcat 1 (doubleDec (-42))
|
2019-08-05 12:07:02 -04:00
|
|
|
, THU.testCase "doubleDec-E" $
|
2021-01-22 11:13:02 -05:00
|
|
|
AsciiByteArray (pack ("-8.8888888888889e+14")) @=? AsciiByteArray (runConcat 1 (doubleDec (-888888888888888.8888888)))
|
2019-08-05 12:07:02 -04:00
|
|
|
, THU.testCase "doubleDec-F" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("42") @=? runConcat 1 (doubleDec 42)
|
2019-08-05 12:07:02 -04:00
|
|
|
, THU.testCase "doubleDec-G" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("0") @=? runConcat 1 (doubleDec 0)
|
2019-08-05 12:07:02 -04:00
|
|
|
, THU.testCase "doubleDec-H" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("0.5") @=? runConcat 1 (doubleDec 0.5)
|
2019-08-05 12:07:02 -04:00
|
|
|
, THU.testCase "doubleDec-I" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("-0.5") @=? runConcat 1 (doubleDec (-0.5))
|
2019-08-05 12:12:37 -04:00
|
|
|
, THU.testCase "doubleDec-J" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("999999999") @=? runConcat 1 (doubleDec 999999999)
|
2019-08-05 12:12:37 -04:00
|
|
|
, THU.testCase "doubleDec-K" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("-99999999") @=? runConcat 1 (doubleDec (-99999999))
|
2020-12-18 13:03:04 -05:00
|
|
|
, THU.testCase "doubleDec-L" $
|
2021-01-22 11:13:02 -05:00
|
|
|
AsciiByteArray (pack ("6.6666666666667e-12")) @=? AsciiByteArray (runConcat 1 (doubleDec (2 / 300_000_000_000)))
|
|
|
|
, THU.testCase "doubleDec-M" $
|
|
|
|
AsciiByteArray (pack ("6.6666666666667e-10")) @=? AsciiByteArray (runConcat 1 (doubleDec 6.666666666666667e-10))
|
|
|
|
, THU.testCase "doubleDec-N" $
|
|
|
|
AsciiByteArray (pack ("5e-10")) @=? AsciiByteArray (runConcat 1 (doubleDec 5.0e-10))
|
|
|
|
, THU.testCase "doubleDec-O" $
|
|
|
|
AsciiByteArray (pack ("1.6666666666667e-10")) @=? AsciiByteArray (runConcat 1 (doubleDec 1.6666666666666669e-10))
|
|
|
|
, THU.testCase "doubleDec-P" $
|
|
|
|
AsciiByteArray (pack ("1e-09")) @=? AsciiByteArray (runConcat 1 (doubleDec 1.0e-9))
|
|
|
|
, THU.testCase "doubleDec-Q" $
|
|
|
|
AsciiByteArray (pack ("1e-08")) @=? AsciiByteArray (runConcat 1 (doubleDec 1.0e-8))
|
2019-08-07 15:23:02 -04:00
|
|
|
, THU.testCase "shortTextJsonString-A" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("\"hello\"") @=? runConcat 1 (shortTextJsonString "hello")
|
2019-08-07 15:23:02 -04:00
|
|
|
, THU.testCase "shortTextJsonString-B" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("\"\\\\_\\\"_/\"") @=? runConcat 1 (shortTextJsonString "\\_\"_/")
|
2019-08-07 15:23:02 -04:00
|
|
|
, THU.testCase "shortTextJsonString-C" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("\"Hi\\r\\nLo\"") @=? runConcat 1 (shortTextJsonString "Hi\r\nLo")
|
2019-08-07 15:23:02 -04:00
|
|
|
, THU.testCase "shortTextJsonString-D" $
|
2019-10-09 16:30:02 -04:00
|
|
|
pack ("\"Hi\\u001BLo\"") @=? runConcat 1 (shortTextJsonString "Hi\ESCLo")
|
2019-09-22 09:20:03 -04:00
|
|
|
, THU.testCase "word-16-tree" $
|
2019-10-09 16:30:02 -04:00
|
|
|
Word16Tree.expectedSmall @=? runConcat 1
|
2019-09-22 09:20:03 -04:00
|
|
|
(Word16Tree.encode Word16Tree.exampleSmall)
|
2019-10-09 16:30:02 -04:00
|
|
|
, THU.testCase "byteArray-small" $
|
|
|
|
let a = replicateByte 3 0x50
|
|
|
|
b = replicateByte 5 0x51
|
|
|
|
in mconcat [a,b] @=? runConcat 1
|
|
|
|
( byteArray a <> byteArray b )
|
|
|
|
, THU.testCase "byteArray-big" $
|
|
|
|
let a = replicateByte 2105 0x50
|
|
|
|
b = replicateByte 725 0x51
|
|
|
|
c = replicateByte 900 0x52
|
|
|
|
d = replicateByte 800 0x53
|
|
|
|
e = replicateByte 700 0x54
|
|
|
|
f = replicateByte 950 0x55
|
|
|
|
g = replicateByte 975 0x56
|
|
|
|
h = replicateByte 3000 0x57
|
|
|
|
i = replicateByte 125 0x58
|
|
|
|
in mconcat [a,b,c,d,e,f,g,h,i] @=? runConcat 1
|
|
|
|
( byteArray a <> byteArray b <> byteArray c <>
|
|
|
|
byteArray d <> byteArray e <> byteArray f <>
|
|
|
|
byteArray g <> byteArray h <> byteArray i
|
|
|
|
)
|
2019-10-10 09:40:39 -04:00
|
|
|
, TQC.testProperty "word16ArrayLE" $ \(xs :: [Word16]) ->
|
|
|
|
let ys = Exts.fromList xs :: PrimArray Word16
|
|
|
|
in runConcat 1 (foldMap word16LE xs)
|
|
|
|
===
|
|
|
|
runConcat 1 (word16ArrayLE ys 0 (Prelude.length xs))
|
|
|
|
, TQC.testProperty "word16ArrayBE" $ \(xs :: [Word16]) ->
|
|
|
|
let ys = Exts.fromList xs :: PrimArray Word16
|
|
|
|
in runConcat 1 (foldMap word16BE xs)
|
|
|
|
===
|
|
|
|
runConcat 1 (word16ArrayBE ys 0 (Prelude.length xs))
|
|
|
|
, TQC.testProperty "word32ArrayLE" $ \(xs :: [Word32]) ->
|
|
|
|
let ys = Exts.fromList xs :: PrimArray Word32
|
|
|
|
in runConcat 1 (foldMap word32LE xs)
|
|
|
|
===
|
|
|
|
runConcat 1 (word32ArrayLE ys 0 (Prelude.length xs))
|
|
|
|
, TQC.testProperty "word32ArrayBE" $ \(xs :: [Word32]) ->
|
|
|
|
let ys = Exts.fromList xs :: PrimArray Word32
|
|
|
|
in runConcat 1 (foldMap word32BE xs)
|
|
|
|
===
|
|
|
|
runConcat 1 (word32ArrayBE ys 0 (Prelude.length xs))
|
|
|
|
, TQC.testProperty "word64ArrayLE" $ \(xs :: [Word64]) ->
|
|
|
|
let ys = Exts.fromList xs :: PrimArray Word64
|
|
|
|
in runConcat 1 (foldMap word64LE xs)
|
|
|
|
===
|
|
|
|
runConcat 1 (word64ArrayLE ys 0 (Prelude.length xs))
|
|
|
|
, TQC.testProperty "word64ArrayBE" $ \(xs :: [Word64]) ->
|
|
|
|
let ys = Exts.fromList xs :: PrimArray Word64
|
|
|
|
in runConcat 1 (foldMap word64BE xs)
|
|
|
|
===
|
|
|
|
runConcat 1 (word64ArrayBE ys 0 (Prelude.length xs))
|
2019-10-17 11:26:14 -04:00
|
|
|
, TQC.testProperty "word128ArrayLE" $ \(xs :: [Word128]) ->
|
|
|
|
let ys = Exts.fromList xs :: PrimArray Word128
|
|
|
|
in runConcat 1 (foldMap word128LE xs)
|
|
|
|
===
|
|
|
|
runConcat 1 (word128ArrayLE ys 0 (Prelude.length xs))
|
|
|
|
, TQC.testProperty "word128ArrayBE" $ \(xs :: [Word128]) ->
|
|
|
|
let ys = Exts.fromList xs :: PrimArray Word128
|
|
|
|
in runConcat 1 (foldMap word128BE xs)
|
|
|
|
===
|
|
|
|
runConcat 1 (word128ArrayBE ys 0 (Prelude.length xs))
|
2020-02-07 09:39:59 -05:00
|
|
|
, TQC.testProperty "word256ArrayLE" $ \(xs :: [Word256]) ->
|
|
|
|
let ys = Exts.fromList xs :: PrimArray Word256
|
|
|
|
in runConcat 1 (foldMap word256LE xs)
|
|
|
|
===
|
|
|
|
runConcat 1 (word256ArrayLE ys 0 (Prelude.length xs))
|
|
|
|
, TQC.testProperty "word256ArrayBE" $ \(xs :: [Word256]) ->
|
|
|
|
let ys = Exts.fromList xs :: PrimArray Word256
|
|
|
|
in runConcat 1 (foldMap word256BE xs)
|
|
|
|
===
|
|
|
|
runConcat 1 (word256ArrayBE ys 0 (Prelude.length xs))
|
2020-04-13 11:29:38 -04:00
|
|
|
, TQC.testProperty "word64LEB128" $ \(x :: Word64) ->
|
|
|
|
runConcat 1 (word64LEB128 x)
|
|
|
|
===
|
|
|
|
naiveLeb128 (fromIntegral x)
|
|
|
|
, TQC.testProperty "naturalDec-A" $ \(x :: Natural) ->
|
|
|
|
runConcat 1 (naturalDec x)
|
|
|
|
===
|
|
|
|
pack (show x)
|
|
|
|
, TQC.testProperty "naturalDec-B" $ \(x :: Natural) ->
|
|
|
|
let y = 1234567892345678934678987654321 * x in
|
|
|
|
runConcat 1 (naturalDec y)
|
|
|
|
===
|
|
|
|
pack (show y)
|
2022-04-11 15:49:02 -04:00
|
|
|
, testGroup "leb128-encoding"
|
|
|
|
[ THU.testCase "16" $
|
|
|
|
Chunks.concat (run 16 (word64LEB128 16))
|
|
|
|
@=?
|
|
|
|
Latin1.fromString "\x10"
|
2022-06-14 10:41:15 -04:00
|
|
|
, THU.testCase "1000000" $
|
|
|
|
Chunks.concat (run 16 (word64LEB128 1000000))
|
|
|
|
@=?
|
|
|
|
Exts.fromList [0xc0,0x84,0x3d]
|
2022-04-11 15:49:02 -04:00
|
|
|
, THU.testCase "deadbeef-smile" $ do
|
|
|
|
let inp = Latin1.fromString "\xDE\xAD\xBE\xEF"
|
|
|
|
(Chunks.concat . run 16) (sevenEightSmile inp)
|
|
|
|
@=?Latin1.fromString "\x6F\x2B\x37\x6E\x0F"
|
|
|
|
]
|
2022-01-31 09:41:05 -05:00
|
|
|
, testGroup "seven/eight encoding"
|
|
|
|
[ THU.testCase "deadbeef" $ do
|
|
|
|
let inp = Latin1.fromString "\xDE\xAD\xBE\xEF"
|
|
|
|
(Chunks.concat . run 16) (sevenEightRight inp)
|
|
|
|
@=? Latin1.fromString "\x6F\x2B\x37\x6E\x78"
|
|
|
|
, THU.testCase "deadbeef-smile" $ do
|
|
|
|
let inp = Latin1.fromString "\xDE\xAD\xBE\xEF"
|
|
|
|
(Chunks.concat . run 16) (sevenEightSmile inp)
|
|
|
|
@=?Latin1.fromString "\x6F\x2B\x37\x6E\x0F"
|
|
|
|
]
|
2019-07-05 12:35:05 -04:00
|
|
|
]
|
|
|
|
, testGroup "alternate"
|
|
|
|
[ TQC.testProperty "HexWord64" $ \x y ->
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat 1
|
2019-09-03 14:58:03 -04:00
|
|
|
( fromBounded Nat.constant (HexWord64.word64PaddedUpperHex x)
|
|
|
|
<> fromBounded Nat.constant (HexWord64.word64PaddedUpperHex y)
|
2019-07-05 12:35:05 -04:00
|
|
|
)
|
|
|
|
===
|
|
|
|
pack (showWord64PaddedUpperHex x <> showWord64PaddedUpperHex y)
|
|
|
|
]
|
2019-11-22 16:24:48 -05:00
|
|
|
, testGroup "putMany"
|
|
|
|
[ THU.testCase "A" $ do
|
|
|
|
ref <- newIORef []
|
|
|
|
let txt = "hello_world_are_you_listening" :: [Char]
|
2019-11-25 10:52:00 -05:00
|
|
|
putMany 7 ascii txt (bytesOntoRef ref)
|
2019-11-22 16:24:48 -05:00
|
|
|
res <- readIORef ref
|
|
|
|
id $
|
2019-11-25 10:52:00 -05:00
|
|
|
[ map c2w "hello_"
|
|
|
|
, map c2w "world_"
|
|
|
|
, map c2w "are_yo"
|
|
|
|
, map c2w "u_list"
|
2019-11-22 16:24:48 -05:00
|
|
|
, map c2w "ening"
|
|
|
|
] @=? map Exts.toList (Exts.toList res)
|
|
|
|
]
|
2019-11-23 08:56:52 -05:00
|
|
|
, testGroup "putManyConsLength"
|
|
|
|
[ THU.testCase "A" $ do
|
|
|
|
ref <- newIORef []
|
|
|
|
let txt = "hello_world_are_you_listening" :: [Char]
|
|
|
|
putManyConsLength Nat.constant
|
|
|
|
(\n -> Bounded.word16BE (fromIntegral n))
|
2019-11-25 10:52:00 -05:00
|
|
|
16 ascii txt (bytesOntoRef ref)
|
2019-11-23 08:56:52 -05:00
|
|
|
res <- readIORef ref
|
|
|
|
id $
|
2019-11-25 10:52:00 -05:00
|
|
|
[ 0x00 : 0x0A : map c2w "hello_worl"
|
|
|
|
, 0x00 : 0x0A : map c2w "d_are_you_"
|
|
|
|
, 0x00 : 0x09 : map c2w "listening"
|
2019-11-23 08:56:52 -05:00
|
|
|
] @=? map Exts.toList (Exts.toList res)
|
|
|
|
]
|
2021-09-15 14:57:10 -04:00
|
|
|
, testGroup "bytes templates"
|
|
|
|
[ THU.testCase "A" $ do
|
|
|
|
let name = Just ("foo" :: ShortText)
|
2021-11-19 15:52:59 -05:00
|
|
|
msgBuilder = [bldr|Hello `fromMaybe "World" name`!\n|]
|
2021-09-15 14:57:10 -04:00
|
|
|
msg = Chunks.concat . Builder.run 200 $ msgBuilder
|
2021-11-19 15:52:59 -05:00
|
|
|
in Ascii.fromString "Hello foo!\n" @=? msg
|
2021-09-15 14:57:10 -04:00
|
|
|
, THU.testCase "B" $ do
|
|
|
|
let one = "foo" :: ShortText
|
|
|
|
two = "bar" :: String
|
2021-11-19 15:52:59 -05:00
|
|
|
msgBuilder = [bldr|`one``two`|]
|
2021-09-15 14:57:10 -04:00
|
|
|
msg = Chunks.concat . Builder.run 200 $ msgBuilder
|
2021-11-19 15:52:59 -05:00
|
|
|
in Ascii.fromString "foobar" @=? msg
|
2021-09-15 14:57:10 -04:00
|
|
|
, THU.testCase "C" $ do
|
2021-11-19 15:52:59 -05:00
|
|
|
let msgBuilder = [bldr|a backtick for you: \`|]
|
2021-09-15 14:57:10 -04:00
|
|
|
msg = Chunks.concat . Builder.run 200 $ msgBuilder
|
2021-11-19 15:52:59 -05:00
|
|
|
in Ascii.fromString "a backtick for you: `" @=? msg
|
2021-09-15 14:57:10 -04:00
|
|
|
, THU.testCase "D" $ do
|
|
|
|
let i = 137 :: Int
|
2021-11-19 15:52:59 -05:00
|
|
|
msgBuilder = [bldr|there are `i` lights!|]
|
2021-09-15 14:57:10 -04:00
|
|
|
msg = Chunks.concat . Builder.run 200 $ msgBuilder
|
2021-11-19 15:52:59 -05:00
|
|
|
in Ascii.fromString "there are 137 lights!" @=? msg
|
2021-09-15 14:57:10 -04:00
|
|
|
]
|
2019-06-17 13:24:24 -04:00
|
|
|
]
|
|
|
|
|
2019-11-25 10:52:00 -05:00
|
|
|
bytesOntoRef ::
|
2019-11-22 16:24:48 -05:00
|
|
|
IORef [PM.ByteArray]
|
2019-11-25 10:52:00 -05:00
|
|
|
-> MutableBytes Exts.RealWorld
|
2019-11-22 16:24:48 -05:00
|
|
|
-> IO ()
|
2019-11-25 10:52:00 -05:00
|
|
|
bytesOntoRef !ref (MutableBytes buf off len) = do
|
2019-11-22 16:24:48 -05:00
|
|
|
rs <- readIORef ref
|
2019-11-25 10:52:00 -05:00
|
|
|
dst <- PM.newByteArray len
|
|
|
|
PM.copyMutableByteArray dst 0 buf off len
|
|
|
|
dst' <- PM.unsafeFreezeByteArray dst
|
|
|
|
writeIORef ref (rs ++ [dst'])
|
2019-11-22 16:24:48 -05:00
|
|
|
|
2019-10-09 16:30:02 -04:00
|
|
|
replicateByte :: Int -> Word8 -> ByteArray
|
|
|
|
replicateByte n w = runST $ do
|
|
|
|
m <- PM.newByteArray n
|
|
|
|
PM.setByteArray m 0 n w
|
|
|
|
PM.unsafeFreezeByteArray m
|
|
|
|
|
2019-06-17 13:24:24 -04:00
|
|
|
pack :: String -> ByteArray
|
|
|
|
pack = Exts.fromList . map (fromIntegral @Int @Word8 . ord)
|
|
|
|
|
2019-09-03 14:43:58 -04:00
|
|
|
packUtf8 :: String -> ByteArray
|
|
|
|
packUtf8 = Exts.fromList . ByteString.unpack . TE.encodeUtf8 . T.pack
|
|
|
|
|
2020-02-06 13:58:55 -05:00
|
|
|
showWord256PaddedLowerHex :: Word256 -> String
|
|
|
|
showWord256PaddedLowerHex (Word256 hi mhi mlo lo) = printf "%016x%016x%016x%016x" hi mhi mlo lo
|
|
|
|
|
|
|
|
showWord128PaddedUpperHex :: Word128 -> String
|
|
|
|
showWord128PaddedUpperHex (Word128 hi lo) = printf "%016X%016X" hi lo
|
|
|
|
|
2019-06-25 15:18:34 -04:00
|
|
|
showWord64PaddedUpperHex :: Word64 -> String
|
2020-02-06 13:58:55 -05:00
|
|
|
showWord64PaddedUpperHex = printf "%016X"
|
2019-10-09 16:30:02 -04:00
|
|
|
|
2020-01-03 06:42:32 -05:00
|
|
|
showWord16PaddedLowerHex :: Word16 -> String
|
2020-02-06 13:58:55 -05:00
|
|
|
showWord16PaddedLowerHex = printf "%04x"
|
2020-01-03 06:42:32 -05:00
|
|
|
|
2019-10-09 16:30:02 -04:00
|
|
|
runConcat :: Int -> Builder -> ByteArray
|
2020-01-20 18:54:43 -05:00
|
|
|
runConcat n = Chunks.concatU . run n
|
2019-10-17 11:26:14 -04:00
|
|
|
|
2019-11-22 16:24:48 -05:00
|
|
|
c2w :: Char -> Word8
|
|
|
|
c2w = fromIntegral . ord
|
|
|
|
|
2020-12-18 13:03:04 -05:00
|
|
|
-- Just a wrapper with a show instance that displays as ascii when possible.
|
|
|
|
newtype AsciiByteArray = AsciiByteArray ByteArray
|
|
|
|
deriving (Eq)
|
|
|
|
|
|
|
|
instance Show AsciiByteArray where
|
|
|
|
show (AsciiByteArray b) = if Bytes.all (\w -> w >= 32 && w < 127) (Bytes.fromByteArray b)
|
2021-11-19 15:52:59 -05:00
|
|
|
then Latin1.toString (Bytes.fromByteArray b)
|
2020-12-18 13:03:04 -05:00
|
|
|
else show (show b)
|
|
|
|
|
2019-10-17 11:26:14 -04:00
|
|
|
instance Arbitrary Word128 where
|
|
|
|
arbitrary = liftA2 Word128 TQC.arbitrary TQC.arbitrary
|
2019-12-30 20:14:15 -05:00
|
|
|
|
2020-02-06 13:58:55 -05:00
|
|
|
instance Arbitrary Word256 where
|
|
|
|
arbitrary = Word256 <$> TQC.arbitrary <*> TQC.arbitrary <*> TQC.arbitrary <*> TQC.arbitrary
|
|
|
|
|
2019-12-30 20:14:15 -05:00
|
|
|
zeroPadL :: Int -> String -> String
|
|
|
|
zeroPadL n s
|
2020-07-29 14:42:40 -04:00
|
|
|
| length s < n = Prelude.replicate (n - length s) '0' ++ s
|
2019-12-30 20:14:15 -05:00
|
|
|
| otherwise = s
|
2020-04-13 11:29:38 -04:00
|
|
|
|
|
|
|
naiveLeb128 :: Natural -> ByteArray
|
|
|
|
naiveLeb128 x =
|
|
|
|
Bytes.toByteArray (Bytes.unsafeDrop 1 (Exts.fromList (0xFF : go [] x)))
|
|
|
|
where
|
|
|
|
go !xs !n =
|
|
|
|
let (q,r) = quotRem n 128
|
|
|
|
r' = fromIntegral @Natural @Word8 r
|
|
|
|
w = if q == 0
|
|
|
|
then r'
|
|
|
|
else Bits.setBit r' 7
|
|
|
|
xs' = w : xs
|
|
|
|
in if q == 0
|
|
|
|
then L.reverse xs'
|
|
|
|
else go xs' q
|