|
3 | 3 | {-# LANGUAGE TypeApplications #-} |
4 | 4 | module Main where |
5 | 5 |
|
| 6 | +import Control.Monad (forM_, unless) |
| 7 | +import Data.IORef (IORef, newIORef, readIORef, writeIORef) |
6 | 8 | import Data.Proxy |
7 | 9 | import Spec (spec) |
| 10 | +import System.Exit (exitFailure) |
8 | 11 | import Test.Hspec (hspec) |
9 | 12 | import Test.QuickCheck |
10 | 13 | import Test.QuickCheck.Classes |
@@ -55,33 +58,43 @@ instance (A.AFType a, Arbitrary a) => Arbitrary (Scalar a) where |
55 | 58 | x : _ -> shrink x |
56 | 59 | [] -> [] |
57 | 60 |
|
| 61 | +-- Run a Laws check, print results in the same format as lawsCheck, and mark |
| 62 | +-- the IORef False on any failure so we can call exitFailure at the end. |
| 63 | +checkLaws :: IORef Bool -> Laws -> IO () |
| 64 | +checkLaws ref laws = do |
| 65 | + let cls = lawsTypeclass laws |
| 66 | + forM_ (lawsProperties laws) $ \(name, prop) -> do |
| 67 | + putStr $ cls ++ ": " ++ name ++ " " |
| 68 | + r <- quickCheckWithResult stdArgs { chatty = False } prop |
| 69 | + putStr (output r) |
| 70 | + unless (isSuccess r) (writeIORef ref False) |
| 71 | + |
58 | 72 | main :: IO () |
59 | 73 | main = do |
60 | | - hspec spec |
| 74 | + ref <- newIORef True |
| 75 | + let check = checkLaws ref |
61 | 76 | -- IEEE 754 is not an exact ring; only Eq laws for floating-point arrays. |
62 | | - lawsCheck (eqLaws (Proxy :: Proxy (Array Double))) |
63 | | - lawsCheck (eqLaws (Proxy :: Proxy (Array Float))) |
64 | | - lawsCheck (showLaws (Proxy :: Proxy (Array Float))) |
65 | | - lawsCheck (showLaws (Proxy :: Proxy (Array Double))) |
| 77 | + check (eqLaws (Proxy :: Proxy (Array Double))) |
| 78 | + check (eqLaws (Proxy :: Proxy (Array Float))) |
66 | 79 | -- Complex: Eq only (IEEE 754 + gt/lt undefined for complex numbers). |
67 | | - lawsCheck (eqLaws (Proxy :: Proxy (Array (A.Complex Double)))) |
68 | | - lawsCheck (eqLaws (Proxy :: Proxy (Array (A.Complex Float)))) |
69 | | - lawsCheck (showLaws (Proxy :: Proxy (Array (A.Complex Double)))) |
70 | | - lawsCheck (showLaws (Proxy :: Proxy (Array (A.Complex Float)))) |
| 80 | + check (eqLaws (Proxy :: Proxy (Array (A.Complex Double)))) |
| 81 | + check (eqLaws (Proxy :: Proxy (Array (A.Complex Float)))) |
71 | 82 | -- Integral types: exact ring laws via Scalar, Eq laws via multi-dim Array. |
72 | | - intChecks (Proxy :: Proxy Int) |
73 | | - intChecks (Proxy :: Proxy A.Int16) |
74 | | - intChecks (Proxy :: Proxy A.Int32) |
75 | | - intChecks (Proxy :: Proxy A.Int64) |
76 | | - intChecks (Proxy :: Proxy A.Word8) |
77 | | - intChecks (Proxy :: Proxy A.Word16) |
78 | | - intChecks (Proxy :: Proxy A.Word32) |
79 | | - intChecks (Proxy :: Proxy A.Word64) |
80 | | - intChecks (Proxy :: Proxy Word) |
81 | | - intChecks (Proxy :: Proxy A.CBool) |
| 83 | + intChecks ref (Proxy :: Proxy Int) |
| 84 | + intChecks ref (Proxy :: Proxy A.Int16) |
| 85 | + intChecks ref (Proxy :: Proxy A.Int32) |
| 86 | + intChecks ref (Proxy :: Proxy A.Int64) |
| 87 | + intChecks ref (Proxy :: Proxy A.Word8) |
| 88 | + intChecks ref (Proxy :: Proxy A.Word16) |
| 89 | + intChecks ref (Proxy :: Proxy A.Word32) |
| 90 | + intChecks ref (Proxy :: Proxy A.Word64) |
| 91 | + intChecks ref (Proxy :: Proxy Word) |
| 92 | + intChecks ref (Proxy :: Proxy A.CBool) |
| 93 | + hspec spec |
| 94 | + ok <- readIORef ref |
| 95 | + unless ok exitFailure |
82 | 96 |
|
83 | | -intChecks :: forall a. (A.AFType a, Arbitrary a, Num a, Eq a) => Proxy a -> IO () |
84 | | -intChecks _ = do |
85 | | - lawsCheck (showLaws (Proxy :: Proxy (Array a))) |
86 | | - lawsCheck (numLaws (Proxy :: Proxy (Scalar a))) |
87 | | - lawsCheck (eqLaws (Proxy :: Proxy (Array a))) |
| 97 | +intChecks :: forall a. (A.AFType a, Arbitrary a, Num a, Eq a) => IORef Bool -> Proxy a -> IO () |
| 98 | +intChecks ref _ = do |
| 99 | + checkLaws ref (numLaws (Proxy :: Proxy (Scalar a))) |
| 100 | + checkLaws ref (eqLaws (Proxy :: Proxy (Array a))) |
0 commit comments