|
1 | 1 | -- | Tests for operations that don't fit in the other @Test.Properties.*@ modules. |
2 | 2 |
|
3 | | -{-# LANGUAGE BangPatterns #-} |
4 | | -{-# LANGUAGE CPP #-} |
5 | | -{-# LANGUAGE ViewPatterns #-} |
| 3 | +{-# LANGUAGE BangPatterns #-} |
| 4 | +{-# LANGUAGE CPP #-} |
| 5 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 6 | +{-# LANGUAGE ViewPatterns #-} |
6 | 7 |
|
7 | 8 | {-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
| 9 | +{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} |
| 10 | +{-# HLINT ignore "Avoid restricted function" #-} |
8 | 11 |
|
9 | 12 | module Tests.Properties.Text |
10 | 13 | ( testText |
11 | 14 | ) where |
12 | 15 |
|
| 16 | +import Control.Exception (SomeException, evaluate, try) |
13 | 17 | import Data.Char (isLower, isLetter, isUpper) |
14 | 18 | import Data.Maybe (mapMaybe) |
15 | 19 | import Data.Text.Internal.Fusion.Size |
@@ -263,13 +267,21 @@ tl_partition (applyFun -> p) = L.partition p `eqP` (unpack2 . TL.partition p) |
263 | 267 | sf_index (applyFun -> p) s i = ((L.filter p s L.!!) `eq` S.index (S.filter p $ packS s)) j |
264 | 268 | where l = L.length s |
265 | 269 | j = if l == 0 then 0 else i `mod` (3 * l) - l |
266 | | -t_index s i = ((s L.!!) `eq` T.index (packS s)) j |
267 | | - where l = L.length s |
268 | | - j = if l == 0 then 0 else i `mod` (3 * l) - l |
269 | 270 |
|
270 | | -tl_index s i = ((s L.!!) `eq` (TL.index (packS s) . fromIntegral)) j |
271 | | - where l = L.length s |
272 | | - j = if l == 0 then 0 else i `mod` (3 * l) - l |
| 271 | +t_index :: T.Text -> Int -> Property |
| 272 | +t_index xs i = ioProperty $ do |
| 273 | + ch <- try (evaluate (T.index xs i)) |
| 274 | + pure $ case ch of |
| 275 | + Left (_ :: SomeException) -> i < 0 .||. i >= T.length xs |
| 276 | + Right c -> i >= 0 .&&. i < T.length xs .&&. c === T.unpack xs L.!! i |
| 277 | + |
| 278 | +tl_index :: TL.Text -> Int -> Property |
| 279 | +tl_index xs i = ioProperty $ do |
| 280 | + let i' = fromIntegral i |
| 281 | + ch <- try (evaluate (TL.index xs i')) |
| 282 | + pure $ case ch of |
| 283 | + Left (_ :: SomeException) -> i' < 0 .||. i' >= TL.length xs |
| 284 | + Right c -> i >= 0 .&&. i' < TL.length xs .&&. c === TL.unpack xs L.!! i |
273 | 285 |
|
274 | 286 | t_findIndex (applyFun -> p) = L.findIndex p `eqP` T.findIndex p |
275 | 287 | t_count (NotEmpty t) = (subtract 1 . L.length . T.splitOn t) `eq` T.count t |
|
0 commit comments