11module Test.Properties.Typed.Laws where
22
3- import Data.ArrayBuffer.Typed (class TypedArray )
3+ import Prelude
4+ -- import Prelude (class Eq, class Monoid, class Ord, class Semigroup, class Show, bind, discard, pure, void, ($), (+), (<>), (<$>))
5+ -- import Prelude (class Eq, class Monoid, class Ord, class Semigroup, Unit, discard, void, ($), (+), (<$>), (<<<))
6+ import Data.ArrayBuffer.Typed (class TypedArray , toString )
47import Data.ArrayBuffer.Typed.Gen (genFloat32 , genFloat64 , genInt16 , genInt32 , genInt8 , genTypedArray , genUint16 , genUint32 , genUint8 )
5- import Data.ArrayBuffer.Typed.Unsafe (AV (..))
68import Data.ArrayBuffer.Types (ArrayView , Float32 , Float64 , Int16 , Int32 , Int8 , Uint16 , Uint32 , Uint8 , Uint8Clamped , ArrayViewType )
79import Data.Float32 as F
810import Data.UInt (UInt )
911import Effect (Effect )
1012import Effect.Ref (Ref )
1113import 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 (class Arbitrary , arbitrary )
1415import Test.QuickCheck.Gen (Gen )
1516import Test.QuickCheck.Laws.Data (checkEq , checkMonoid , checkOrd , checkSemigroup )
1617import Type.Prelude (Proxy (..))
18+ import Data.ArrayBuffer.Typed as TA
19+ import Data.Generic.Rep (class Generic )
20+ import Data.Maybe (Maybe (..))
21+ import Effect.Unsafe (unsafePerformEffect )
1722
1823newtype A a = A a
1924
@@ -102,3 +107,35 @@ typedArrayLaws count = do
102107 f (Proxy :: Proxy (A (AV Uint32 UInt )))
103108 f (Proxy :: Proxy (A (AV Uint8 UInt )))
104109 f (Proxy :: Proxy (A (AV Uint8Clamped UInt )))
110+
111+ newtype AV :: forall k . ArrayViewType -> k -> Type
112+ newtype AV a t = AV (ArrayView a )
113+
114+ derive instance genericAV :: Generic (AV a t ) _
115+
116+ instance ordArrayView :: (TypedArray a t , Ord t ) => Ord (AV a t ) where
117+ compare (AV a) (AV b) = unsafePerformEffect $ TA .compare a b
118+
119+ instance eqArrayView :: (TypedArray a t , Eq t ) => Eq (AV a t ) where
120+ eq (AV a) (AV b) = unsafePerformEffect $ TA .eq a b
121+
122+ instance showArrayView :: (TypedArray a t , Show t ) => Show (AV a t ) where
123+ show (AV a) = " T[" <> s <> " ]"
124+ where s = unsafePerformEffect $ toString a
125+
126+ instance semigroupArrayView :: TypedArray a t => Semigroup (AV a t ) where
127+ append (AV a) (AV b) = unsafePerformEffect do
128+ let la = TA .length a
129+ lb = TA .length b
130+ r <- TA .empty $ la + lb
131+ void $ TA .setTyped r (Just 0 ) a
132+ void $ TA .setTyped r (Just la) b
133+ pure $ AV r
134+
135+ instance monoidArrayView :: TypedArray a t => Monoid (AV a t ) where
136+ mempty = AV $ unsafePerformEffect $ TA .empty 0
137+
138+ instance arbitraryArrayView :: (TypedArray a t , Arbitrary t ) => Arbitrary (AV a t ) where
139+ arbitrary = do
140+ xs <- arbitrary
141+ pure $ unsafePerformEffect $ AV <$> TA .fromArray xs
0 commit comments