1
1
module Test.Properties.Typed.Laws where
2
2
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 )
4
7
import Data.ArrayBuffer.Typed.Gen (genFloat32 , genFloat64 , genInt16 , genInt32 , genInt8 , genTypedArray , genUint16 , genUint32 , genUint8 )
5
- import Data.ArrayBuffer.Typed.Unsafe (AV (..))
6
8
import Data.ArrayBuffer.Types (ArrayView , Float32 , Float64 , Int16 , Int32 , Int8 , Uint16 , Uint32 , Uint8 , Uint8Clamped , ArrayViewType )
7
9
import Data.Float32 as F
8
10
import Data.UInt (UInt )
9
11
import Effect (Effect )
10
12
import Effect.Ref (Ref )
11
13
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 (class Arbitrary , arbitrary )
14
15
import Test.QuickCheck.Gen (Gen )
15
16
import Test.QuickCheck.Laws.Data (checkEq , checkMonoid , checkOrd , checkSemigroup )
16
17
import 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 )
17
22
18
23
newtype A a = A a
19
24
@@ -102,3 +107,35 @@ typedArrayLaws count = do
102
107
f (Proxy :: Proxy (A (AV Uint32 UInt )))
103
108
f (Proxy :: Proxy (A (AV Uint8 UInt )))
104
109
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