|
| 1 | +module Test.Properties.Typed.Laws where |
| 2 | + |
| 3 | +import Data.ArrayBuffer.Typed (class TypedArray) |
| 4 | +import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8) |
| 5 | +import Data.ArrayBuffer.Typed.Unsafe (AV(..)) |
| 6 | +import Data.ArrayBuffer.Types (ArrayView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped, kind ArrayViewType) |
| 7 | +import Data.Float32 as F |
| 8 | +import Data.UInt (UInt) |
| 9 | +import Effect (Effect) |
| 10 | +import Effect.Ref (Ref) |
| 11 | +import Effect.Ref as Ref |
| 12 | +import Prelude (class Eq, class Monoid, class Ord, class Semigroup, Unit, discard, void, ($), (+), (<$>), (<<<)) |
| 13 | +import Test.QuickCheck (class Arbitrary) |
| 14 | +import Test.QuickCheck.Gen (Gen) |
| 15 | +import Test.QuickCheck.Laws.Data (checkEq, checkMonoid, checkOrd, checkSemigroup) |
| 16 | +import Type.Prelude (Proxy(..)) |
| 17 | + |
| 18 | +newtype A a = A a |
| 19 | + |
| 20 | +foreign import data ArrayElt :: ArrayViewType -> Type -> Type |
| 21 | + |
| 22 | +class ArrayEl (a :: ArrayViewType) (t :: Type) where |
| 23 | + arb :: Proxy (ArrayView a) -> Gen t |
| 24 | + |
| 25 | +instance arrayElUint8Clamped :: ArrayEl Uint8Clamped UInt where |
| 26 | + arb _ = genUint8 |
| 27 | +instance arrayElUint32 :: ArrayEl Uint32 UInt where |
| 28 | + arb _ = genUint32 |
| 29 | +instance arrayElUint16 :: ArrayEl Uint16 UInt where |
| 30 | + arb _ = genUint16 |
| 31 | +instance arrayElUint8 :: ArrayEl Uint8 UInt where |
| 32 | + arb _ = genUint8 |
| 33 | +instance arrayElInt32 :: ArrayEl Int32 Int where |
| 34 | + arb _ = genInt32 |
| 35 | +instance arrayElInt16 :: ArrayEl Int16 Int where |
| 36 | + arb _ = genInt16 |
| 37 | +instance arrayElInt8 :: ArrayEl Int8 Int where |
| 38 | + arb _ = genInt8 |
| 39 | +instance arrayElFloat32 :: ArrayEl Float32 F.Float32 where |
| 40 | + arb _ = genFloat32 |
| 41 | +instance arrayElFloat64 :: ArrayEl Float64 Number where |
| 42 | + arb _ = genFloat64 |
| 43 | + |
| 44 | +instance arbitraryAAV :: (TypedArray a t, ArrayEl a t) => Arbitrary (A (AV a t)) where |
| 45 | + arbitrary = (A <<< AV) <$> genTypedArray (arb (Proxy :: Proxy (ArrayView a))) |
| 46 | + |
| 47 | +derive newtype instance eqA :: Eq t => Eq (A t) |
| 48 | +derive newtype instance ordA :: Ord t => Ord (A t) |
| 49 | +derive newtype instance semigroupA :: Semigroup t => Semigroup (A t) |
| 50 | +derive newtype instance monoidA :: Monoid t => Monoid (A t) |
| 51 | + |
| 52 | +typedArrayLaws :: Ref Int -> Effect Unit |
| 53 | +typedArrayLaws count = do |
| 54 | + do |
| 55 | + let f = checkEq |
| 56 | + void $ Ref.modify (_ + 1) count |
| 57 | + f (Proxy :: Proxy (A (AV Float32 F.Float32))) |
| 58 | + f (Proxy :: Proxy (A (AV Float64 Number))) |
| 59 | + f (Proxy :: Proxy (A (AV Int16 Int))) |
| 60 | + f (Proxy :: Proxy (A (AV Int32 Int))) |
| 61 | + f (Proxy :: Proxy (A (AV Int8 Int))) |
| 62 | + f (Proxy :: Proxy (A (AV Uint16 UInt))) |
| 63 | + f (Proxy :: Proxy (A (AV Uint32 UInt))) |
| 64 | + f (Proxy :: Proxy (A (AV Uint8 UInt))) |
| 65 | + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) |
| 66 | + |
| 67 | + do |
| 68 | + let f = checkOrd |
| 69 | + void $ Ref.modify (_ + 1) count |
| 70 | + f (Proxy :: Proxy (A (AV Float32 F.Float32))) |
| 71 | + f (Proxy :: Proxy (A (AV Float64 Number))) |
| 72 | + f (Proxy :: Proxy (A (AV Int16 Int))) |
| 73 | + f (Proxy :: Proxy (A (AV Int32 Int))) |
| 74 | + f (Proxy :: Proxy (A (AV Int8 Int))) |
| 75 | + f (Proxy :: Proxy (A (AV Uint16 UInt))) |
| 76 | + f (Proxy :: Proxy (A (AV Uint32 UInt))) |
| 77 | + f (Proxy :: Proxy (A (AV Uint8 UInt))) |
| 78 | + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) |
| 79 | + |
| 80 | + do |
| 81 | + let f = checkSemigroup |
| 82 | + void $ Ref.modify (_ + 1) count |
| 83 | + f (Proxy :: Proxy (A (AV Float32 F.Float32))) |
| 84 | + f (Proxy :: Proxy (A (AV Float64 Number))) |
| 85 | + f (Proxy :: Proxy (A (AV Int16 Int))) |
| 86 | + f (Proxy :: Proxy (A (AV Int32 Int))) |
| 87 | + f (Proxy :: Proxy (A (AV Int8 Int))) |
| 88 | + f (Proxy :: Proxy (A (AV Uint16 UInt))) |
| 89 | + f (Proxy :: Proxy (A (AV Uint32 UInt))) |
| 90 | + f (Proxy :: Proxy (A (AV Uint8 UInt))) |
| 91 | + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) |
| 92 | + |
| 93 | + do |
| 94 | + let f = checkMonoid |
| 95 | + void $ Ref.modify (_ + 1) count |
| 96 | + f (Proxy :: Proxy (A (AV Float32 F.Float32))) |
| 97 | + f (Proxy :: Proxy (A (AV Float64 Number))) |
| 98 | + f (Proxy :: Proxy (A (AV Int16 Int))) |
| 99 | + f (Proxy :: Proxy (A (AV Int32 Int))) |
| 100 | + f (Proxy :: Proxy (A (AV Int8 Int))) |
| 101 | + f (Proxy :: Proxy (A (AV Uint16 UInt))) |
| 102 | + f (Proxy :: Proxy (A (AV Uint32 UInt))) |
| 103 | + f (Proxy :: Proxy (A (AV Uint8 UInt))) |
| 104 | + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) |
0 commit comments