|
| 1 | +-- | Test basic text functions |
| 2 | + |
| 3 | +{-# OPTIONS_GHC -fno-enable-rewrite-rules -fno-warn-missing-signatures #-} |
| 4 | +module Tests.Properties.Basics |
| 5 | + ( testBasics |
| 6 | + ) where |
| 7 | + |
| 8 | +import Control.Arrow (first, second) |
| 9 | +import Test.Tasty (TestTree, testGroup) |
| 10 | +import Test.Tasty.QuickCheck (testProperty) |
| 11 | +import Tests.QuickCheckUtils |
| 12 | +import Text.Show.Functions () |
| 13 | +import qualified Data.List as L |
| 14 | +import qualified Data.Text as T |
| 15 | +import qualified Data.Text.Internal.Fusion as S |
| 16 | +import qualified Data.Text.Internal.Fusion.Common as S |
| 17 | +import qualified Data.Text.Internal.Lazy.Fusion as SL |
| 18 | +import qualified Data.Text.Lazy as TL |
| 19 | + |
| 20 | +s_cons x = (x:) `eqP` (unpackS . S.cons x) |
| 21 | +s_cons_s x = (x:) `eqP` (unpackS . S.unstream . S.cons x) |
| 22 | +sf_cons p x = ((x:) . L.filter p) `eqP` (unpackS . S.cons x . S.filter p) |
| 23 | +t_cons x = (x:) `eqP` (unpackS . T.cons x) |
| 24 | +tl_cons x = (x:) `eqP` (unpackS . TL.cons x) |
| 25 | +s_snoc x = (++ [x]) `eqP` (unpackS . (flip S.snoc) x) |
| 26 | +t_snoc x = (++ [x]) `eqP` (unpackS . (flip T.snoc) x) |
| 27 | +tl_snoc x = (++ [x]) `eqP` (unpackS . (flip TL.snoc) x) |
| 28 | +s_append s = (s++) `eqP` (unpackS . S.append (S.streamList s)) |
| 29 | +s_append_s s = (s++) `eqP` |
| 30 | + (unpackS . S.unstream . S.append (S.streamList s)) |
| 31 | +sf_append p s = (L.filter p s++) `eqP` |
| 32 | + (unpackS . S.append (S.filter p $ S.streamList s)) |
| 33 | +t_append s = (s++) `eqP` (unpackS . T.append (packS s)) |
| 34 | + |
| 35 | +uncons (x:xs) = Just (x,xs) |
| 36 | +uncons _ = Nothing |
| 37 | + |
| 38 | +s_uncons = uncons `eqP` (fmap (second unpackS) . S.uncons) |
| 39 | +sf_uncons p = (uncons . L.filter p) `eqP` |
| 40 | + (fmap (second unpackS) . S.uncons . S.filter p) |
| 41 | +t_uncons = uncons `eqP` (fmap (second unpackS) . T.uncons) |
| 42 | +tl_uncons = uncons `eqP` (fmap (second unpackS) . TL.uncons) |
| 43 | + |
| 44 | +unsnoc xs@(_:_) = Just (init xs, last xs) |
| 45 | +unsnoc [] = Nothing |
| 46 | + |
| 47 | +t_unsnoc = unsnoc `eqP` (fmap (first unpackS) . T.unsnoc) |
| 48 | +tl_unsnoc = unsnoc `eqP` (fmap (first unpackS) . TL.unsnoc) |
| 49 | + |
| 50 | +s_head = head `eqP` S.head |
| 51 | +sf_head p = (head . L.filter p) `eqP` (S.head . S.filter p) |
| 52 | +t_head = head `eqP` T.head |
| 53 | +tl_head = head `eqP` TL.head |
| 54 | +s_last = last `eqP` S.last |
| 55 | +sf_last p = (last . L.filter p) `eqP` (S.last . S.filter p) |
| 56 | +t_last = last `eqP` T.last |
| 57 | +tl_last = last `eqP` TL.last |
| 58 | +s_tail = tail `eqP` (unpackS . S.tail) |
| 59 | +s_tail_s = tail `eqP` (unpackS . S.unstream . S.tail) |
| 60 | +sf_tail p = (tail . L.filter p) `eqP` (unpackS . S.tail . S.filter p) |
| 61 | +t_tail = tail `eqP` (unpackS . T.tail) |
| 62 | +tl_tail = tail `eqP` (unpackS . TL.tail) |
| 63 | +s_init = init `eqP` (unpackS . S.init) |
| 64 | +s_init_s = init `eqP` (unpackS . S.unstream . S.init) |
| 65 | +sf_init p = (init . L.filter p) `eqP` (unpackS . S.init . S.filter p) |
| 66 | +t_init = init `eqP` (unpackS . T.init) |
| 67 | +tl_init = init `eqP` (unpackS . TL.init) |
| 68 | +s_null = null `eqP` S.null |
| 69 | +sf_null p = (null . L.filter p) `eqP` (S.null . S.filter p) |
| 70 | +t_null = null `eqP` T.null |
| 71 | +tl_null = null `eqP` TL.null |
| 72 | +s_length = length `eqP` S.length |
| 73 | +sf_length p = (length . L.filter p) `eqP` (S.length . S.filter p) |
| 74 | +sl_length = (fromIntegral . length) `eqP` SL.length |
| 75 | +t_length = length `eqP` T.length |
| 76 | +tl_length = L.genericLength `eqP` TL.length |
| 77 | +t_compareLength t = (compare (T.length t)) `eq` T.compareLength t |
| 78 | +tl_compareLength t= (compare (TL.length t)) `eq` TL.compareLength t |
| 79 | + |
| 80 | +-- Regression tests. |
| 81 | +s_filter_eq s = S.filter p t == S.streamList (filter p s) |
| 82 | + where p = (/= S.last t) |
| 83 | + t = S.streamList s |
| 84 | + |
| 85 | +testBasics :: TestTree |
| 86 | +testBasics = |
| 87 | + testGroup "basics" [ |
| 88 | + testProperty "s_cons" s_cons, |
| 89 | + testProperty "s_cons_s" s_cons_s, |
| 90 | + testProperty "sf_cons" sf_cons, |
| 91 | + testProperty "t_cons" t_cons, |
| 92 | + testProperty "tl_cons" tl_cons, |
| 93 | + testProperty "s_snoc" s_snoc, |
| 94 | + testProperty "t_snoc" t_snoc, |
| 95 | + testProperty "tl_snoc" tl_snoc, |
| 96 | + testProperty "s_append" s_append, |
| 97 | + testProperty "s_append_s" s_append_s, |
| 98 | + testProperty "sf_append" sf_append, |
| 99 | + testProperty "t_append" t_append, |
| 100 | + testProperty "s_uncons" s_uncons, |
| 101 | + testProperty "sf_uncons" sf_uncons, |
| 102 | + testProperty "t_uncons" t_uncons, |
| 103 | + testProperty "tl_uncons" tl_uncons, |
| 104 | + testProperty "t_unsnoc" t_unsnoc, |
| 105 | + testProperty "tl_unsnoc" tl_unsnoc, |
| 106 | + testProperty "s_head" s_head, |
| 107 | + testProperty "sf_head" sf_head, |
| 108 | + testProperty "t_head" t_head, |
| 109 | + testProperty "tl_head" tl_head, |
| 110 | + testProperty "s_last" s_last, |
| 111 | + testProperty "sf_last" sf_last, |
| 112 | + testProperty "t_last" t_last, |
| 113 | + testProperty "tl_last" tl_last, |
| 114 | + testProperty "s_tail" s_tail, |
| 115 | + testProperty "s_tail_s" s_tail_s, |
| 116 | + testProperty "sf_tail" sf_tail, |
| 117 | + testProperty "t_tail" t_tail, |
| 118 | + testProperty "tl_tail" tl_tail, |
| 119 | + testProperty "s_init" s_init, |
| 120 | + testProperty "s_init_s" s_init_s, |
| 121 | + testProperty "sf_init" sf_init, |
| 122 | + testProperty "t_init" t_init, |
| 123 | + testProperty "tl_init" tl_init, |
| 124 | + testProperty "s_null" s_null, |
| 125 | + testProperty "sf_null" sf_null, |
| 126 | + testProperty "t_null" t_null, |
| 127 | + testProperty "tl_null" tl_null, |
| 128 | + testProperty "s_length" s_length, |
| 129 | + testProperty "sf_length" sf_length, |
| 130 | + testProperty "sl_length" sl_length, |
| 131 | + testProperty "t_length" t_length, |
| 132 | + testProperty "tl_length" tl_length, |
| 133 | + testProperty "t_compareLength" t_compareLength, |
| 134 | + testProperty "tl_compareLength" tl_compareLength, |
| 135 | + |
| 136 | + testGroup "regressions" [ |
| 137 | + testProperty "s_filter_eq" s_filter_eq |
| 138 | + ] |
| 139 | + ] |
0 commit comments