Skip to content

Commit e6a0fd5

Browse files
committed
test: additional tests (ghcjs in particular)
1 parent 9feab33 commit e6a0fd5

File tree

2 files changed

+69
-65
lines changed

2 files changed

+69
-65
lines changed

test/Spec.hs

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
1-
{-# LANGUAGE BinaryLiterals #-}
2-
{-# LANGUAGE CPP #-}
3-
{-# LANGUAGE FlexibleContexts #-}
4-
{-# LANGUAGE FlexibleInstances #-}
5-
{-# LANGUAGE MultiParamTypeClasses #-}
6-
{-# LANGUAGE NegativeLiterals #-}
1+
{-# LANGUAGE BinaryLiterals #-}
2+
{-# LANGUAGE CPP #-}
3+
{-# LANGUAGE FlexibleContexts #-}
4+
{-# LANGUAGE FlexibleInstances #-}
5+
{-# LANGUAGE MultiParamTypeClasses #-}
6+
{-# LANGUAGE NegativeLiterals #-}
77
{-# LANGUAGE NoMonomorphismRestriction #-}
8-
{-# LANGUAGE ScopedTypeVariables #-}
8+
{-# LANGUAGE ScopedTypeVariables #-}
99

1010
-- | Tests for the flat module
1111
module Main where
1212

1313
import Control.Monad
1414
import Data.Bits
15-
import qualified Data.ByteString as B
16-
import qualified Data.ByteString.Lazy as L
15+
import qualified Data.ByteString as B
16+
import qualified Data.ByteString.Lazy as L
1717
import qualified Data.ByteString.Short as SBS
1818
import Data.Char
1919
import Data.Either
20-
import Flat
21-
import Flat.Bits
22-
import Flat.Decoder
23-
import qualified Flat.Encoder as E
24-
import qualified Flat.Encoder.Prim as E
25-
import qualified Flat.Encoder.Strict as E
20+
import Data.FloatCast
2621
import Data.Int
2722
import Data.Proxy
28-
import qualified Data.Sequence as Seq
29-
import Data.String (fromString)
30-
import qualified Data.Text as T
23+
import qualified Data.Sequence as Seq
24+
import Data.String (fromString)
25+
import qualified Data.Text as T
26+
import Data.Text.Arbitrary
3127
import Data.Word
28+
import Flat
29+
import Flat.Bits
30+
import Flat.Decoder
31+
import qualified Flat.Encoder as E
32+
import qualified Flat.Encoder.Prim as E
33+
import qualified Flat.Encoder.Strict as E
34+
import Flat.Endian
3235
import Numeric.Natural
3336
import System.Exit
3437
import Test.Data
35-
import Test.Data.Arbitrary ()
38+
import Test.Data.Arbitrary ()
3639
import Test.Data.Flat
37-
import Test.Data.Values hiding (lbs, ns)
40+
import Test.Data.Values hiding (lbs, ns)
3841
import Test.E
39-
import Test.E.Arbitrary ()
42+
import Test.E.Arbitrary ()
4043
import Test.E.Flat
4144
import Test.Tasty
4245
import Test.Tasty.HUnit
4346
import Test.Tasty.QuickCheck as QC hiding (getSize)
44-
import Flat.Endian
45-
import Data.FloatCast
46-
import Data.Text.Arbitrary
4747
-- import Test.QuickCheck.Arbitrary
48-
import qualified Data.Complex as B
49-
import qualified Data.Ratio as B
50-
import qualified Data.Map as C
51-
import qualified Data.Map.Strict as CS
52-
import qualified Data.Map.Lazy as CL
53-
import qualified Data.IntMap.Strict as CS
54-
import qualified Data.IntMap.Lazy as CL
48+
import qualified Data.Complex as B
49+
import qualified Data.IntMap.Lazy as CL
50+
import qualified Data.IntMap.Strict as CS
51+
import qualified Data.Map as C
52+
import qualified Data.Map.Lazy as CL
53+
import qualified Data.Map.Strict as CS
54+
import qualified Data.Ratio as B
5555
-- import Data.List
5656
-- import Data.Ord
5757
#if MIN_VERSION_base(4,9,0)
58-
import qualified Data.List.NonEmpty as BI
58+
import qualified Data.List.NonEmpty as BI
5959
#endif
6060

6161
instance Arbitrary UTF8Text where
6262
arbitrary = UTF8Text <$> arbitrary
6363

6464
shrink t = UTF8Text <$> shrink (unUTF8 t)
6565

66-
#if! defined(ghcjs_HOST_OS) && ! defined (ETA_VERSION)
66+
#if! defined (ETA_VERSION)
6767
instance Arbitrary UTF16Text where
6868
arbitrary = UTF16Text <$> arbitrary
6969

@@ -74,9 +74,6 @@ instance Arbitrary UTF16Text where
7474
-- instance Flat [Word8]
7575
-- instance Flat [Bool]
7676
main = do
77-
-- #ifdef ghcjs_HOST_OS
78-
-- print "GHCJS"
79-
-- #endif
8077
-- printInfo
8178
-- print $ flat asciiStrT
8279
mainTest
@@ -96,16 +93,15 @@ tests :: TestTree
9693
tests = testGroup "Tests" [testPrimitives, testEncDec, testFlat]
9794

9895
testPrimitives =
99-
testGroup "conversion/memory primitives" [testEndian, testFloatingConvert]
96+
testGroup "conversion/memory primitives" [testEndian, testFloatingConvert,testShifts ]
10097

101-
--,testShifts -- ghcjs fails this
10298
testEncDec = testGroup
10399
"encode/decode primitives"
104100
[ testEncodingPrim
105101
, testDecodingPrim
106-
#ifdef TEST_DECBITS
102+
#ifdef TEST_DECBITS
107103
, testDecBits
108-
#endif
104+
#endif
109105
]
110106

111107
testFlat = testGroup
@@ -141,7 +137,6 @@ conv f v e = testCase
141137
(unwords ["conv", sshow v, showB . flat $ v, "to", sshow e])
142138
$ f v @?= e
143139

144-
-- ghcjs bug on shiftR 0, see: https://github.com/ghcjs/ghcjs/issues/706
145140
testShifts = testGroup "Shifts" $ map tst [0 .. 33]
146141
where
147142
tst n = testCase ("shiftR " ++ show n)
@@ -262,7 +257,7 @@ testDecBits = testGroup "Decode Bits"
262257
return r
263258
-- we expect the first numBitsToTake bits of the value
264259
expectedD@(Right expected) :: Decoded a = Right
265-
$ val `shR` (sz - numBitsToTake) -- ghcjs: shiftR fails, see: https://github.com/ghcjs/ghcjs/issues/706
260+
$ val `shR` (sz - numBitsToTake)
266261
actualD@(Right actual) :: Decoded a = unflatRawWith dec vs
267262
in testCase
268263
(unwords
@@ -312,25 +307,31 @@ testSize = testGroup "Size"
312307
, sz bs (4 + 3 * 8)
313308
, sz stBS bsSize
314309
, sz lzBS bsSize
315-
#ifndef ghcjs_HOST_OS
316310
, sz shBS bsSize
317-
#endif
318311
, sz tx utf8Size
319312
, sz (UTF8Text tx) utf8Size
320-
#if! defined(ghcjs_HOST_OS) && ! defined (ETA_VERSION)
313+
#if ! defined (ETA_VERSION)
321314
, sz (UTF16Text tx) utf16Size
322315
#endif
323316
]
324317
where
325318
tx = T.pack "txt"
326319

327-
utf8Size = 8 + 8 + 3 * 32 + 8
328-
320+
#if MIN_VERSION_text(2,0,0)
321+
utf8Size = 8 + 8 + (3 * 8) + 8
322+
#else
323+
utf8Size = 8 + 8 + (3 * 3 * 8) + 8
324+
#endif
329325
utf16Size = 8 + 8 + 3 * 16 + 8
330326

331327
bsSize = 8 + 8 + 3 * 8 + 8
332328

333-
sz v e = [testCase (unwords ["size of", sshow v]) $ getSize v @?= e]
329+
sz v e = let calculated = getSize v
330+
actual = B.length (flat v) * 8 - 1 -- FIX
331+
in
332+
[testCase (unwords ["size of", sshow v]) $ calculated @?= e
333+
-- ,testCase (unwords ["calculated size <= actual", sshow v]) $ actual <= calculated @? unwords ["calculated size",show calculated,"actual",show actual]
334+
]
334335

335336
-- E258_256 = 11111110 _257 = 111111110 _258 = 111111111
336337
testLargeEnum = testGroup "test enum with more than 256 constructors"
@@ -371,7 +372,7 @@ flatUnflatRT = testGroup
371372
, rt "String" (prop_Flat_roundtrip :: RT String)
372373
#if MIN_VERSION_base(4,9,0)
373374
, rt "NonEmpty" (prop_Flat_roundtrip :: RT (BI.NonEmpty Bool))
374-
#endif
375+
#endif
375376
, rt "Maybe N" (prop_Flat_roundtrip :: RT (Maybe N))
376377
, rt "Ratio" (prop_Flat_roundtrip :: RT (B.Ratio Int32))
377378
, rt "Word8" (prop_Flat_Large_roundtrip :: RTL Word8)
@@ -385,14 +386,12 @@ flatUnflatRT = testGroup
385386
, rt "Double" (prop_Flat_roundtrip :: RT Double)
386387
, rt "Text" (prop_Flat_roundtrip :: RT T.Text)
387388
, rt "UTF8 Text" (prop_Flat_roundtrip :: RT UTF8Text)
388-
#if! defined(ghcjs_HOST_OS) && ! defined (ETA_VERSION)
389+
#if! defined (ETA_VERSION)
389390
, rt "UTF16 Text" (prop_Flat_roundtrip :: RT UTF16Text)
390391
#endif
391392
, rt "ByteString" (prop_Flat_roundtrip :: RT B.ByteString)
392393
, rt "Lazy ByteString" (prop_Flat_roundtrip :: RT L.ByteString)
393-
#ifndef ghcjs_HOST_OS
394394
, rt "Short ByteString" (prop_Flat_roundtrip :: RT SBS.ShortByteString)
395-
#endif
396395
, rt "Map.Strict" (prop_Flat_roundtrip :: RT (CS.Map Int Bool))
397396
, rt "Map.Lazy" (prop_Flat_roundtrip :: RT (CL.Map Int Bool))
398397
, rt "IntMap.Strict" (prop_Flat_roundtrip :: RT (CS.IntMap Bool))
@@ -635,13 +634,13 @@ flatTests = testGroup "flat/unflat Unit tests"
635634
, [trip (T.pack "abc")]
636635
, [trip unicodeText]
637636
, [trip unicodeTextUTF8T]
638-
, [trip longBS, trip longLBS]
639-
#ifndef ghcjs_HOST_OS
640-
, [trip longSBS]
641-
#endif
642-
#if! defined(ghcjs_HOST_OS) && ! defined (ETA_VERSION)
637+
, [trip chineseTextUTF8T]
638+
#if ! defined (ETA_VERSION)
639+
, [trip chineseTextUTF16T]
643640
, [trip unicodeTextUTF16T]
644641
#endif
642+
, [trip longBS, trip longLBS]
643+
, [trip longSBS]
645644
]
646645
--al = (1:) -- prealign
647646
where
@@ -732,11 +731,9 @@ nsII = nsI_
732731

733732
nsI_ = [((-) (2 ^ (((-) i 1) * 7)) 1, fromIntegral (8 * i)) | i <- [1 .. 10]]
734733

735-
#ifndef ghcjs_HOST_OS
736734
shBS = SBS.toShort stBS
737735

738736
longSBS = SBS.toShort longBS
739-
#endif
740737

741738
sshow = take 80 . show
742739

@@ -797,7 +794,7 @@ prop_common_unsigned n _ = let n2 :: h = fromIntegral n
797794
-- b1 :: BLOB UTF8
798795
-- b1 = BLOB UTF8 (preAligned (List255 [97,98,99]))
799796
-- -- b1 = BLOB (preAligned (UTF8 (List255 [97,98,99])))
800-
801-
802-
803-
797+
798+
799+
800+

test/Test/Data/Values.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import qualified Data.ByteString as B
1111
import qualified Data.ByteString.Lazy as L
1212
import qualified Data.ByteString.Short.Internal as SBS
1313
import Data.Char
14-
import Flat
1514
import Data.Foldable
1615
import Data.Int
1716
import qualified Data.IntMap as IM
17+
import Flat
1818
-- import qualified Data.IntSet as IS
1919
-- import Data.List
2020
import qualified Data.Map as M
@@ -191,10 +191,17 @@ asciiTextT = ("asciiText", T.pack $ longS english )
191191

192192
unicodeTextUTF8T = ("unicodeTextUTF8",UTF8Text unicodeText)
193193

194-
#if! defined(ghcjs_HOST_OS) && ! defined (ETA_VERSION)
194+
chineseTextUTF8T = ("chineseTextUTF8",UTF8Text chineseText)
195+
196+
#if ! defined (ETA_VERSION)
195197
unicodeTextUTF16T = ("unicodeTextUTF16",UTF16Text unicodeText)
198+
chineseTextUTF16T = ("chineseTextUTF16",UTF16Text chineseText)
196199
#endif
197200

201+
-- chineseTextT = ("chineseText",chinesText)
202+
chineseText = T.pack $ longS chinese
203+
204+
198205
unicodeTextT = ("unicodeText",unicodeText)
199206
unicodeText = T.pack unicodeStr
200207

0 commit comments

Comments
 (0)