Skip to content

Commit 3efc0cd

Browse files
committed
Add quickcheck-laws tests, changed degree definition
1 parent 8fd56df commit 3efc0cd

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"package.json"
2121
],
2222
"dependencies": {
23-
"purescript-maybe": "^3.0.0"
23+
"purescript-maybe": "^3.0.0",
24+
"purescript-quickcheck-laws": "^3.0.0"
2425
},
2526
"devDependencies": {
2627
"purescript-integers": "^3.0.0",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"build": "pulp test && rimraf docs && pulp docs"
55
},
66
"dependencies": {
7-
"big-integer": "^1.6.17"
7+
"big-integer": "^1.6.24"
88
},
99
"devDependencies": {
1010
"pulp": "^9.0.0",

src/Data/BigInt.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Data.BigInt
2121

2222
import Prelude
2323

24+
import Data.Int (floor)
2425
import Data.Maybe (Maybe(..))
2526

2627
-- | An arbitrary length integer.
@@ -138,4 +139,4 @@ instance commutativeRingBigInt :: CommutativeRing BigInt
138139
instance euclideanRingBigInt :: EuclideanRing BigInt where
139140
div = biDiv
140141
mod = biMod
141-
degree = degree <<< toNumber
142+
degree = floor <<< toNumber <<< abs

test/Main.purs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
module Test.Main where
22

3-
import Prelude hiding (not)
43
import Control.Monad.Eff (Eff)
54
import Control.Monad.Eff.Console (CONSOLE, log)
5+
import Control.Monad.Eff.Exception (EXCEPTION)
6+
import Control.Monad.Eff.Random (RANDOM)
67
import Data.Array (filter, range)
7-
import Data.BigInt (BigInt, abs, fromInt, prime, pow, odd, even, fromString,
8-
toNumber, fromBase, toString, not, or, xor, and, shl, shr)
8+
import Data.BigInt (BigInt, abs, fromInt, prime, pow, odd, even, fromString, toNumber, fromBase, toString, not, or, xor, and, shl, shr)
99
import Data.Foldable (fold)
10+
import Data.Int as Int
1011
import Data.Maybe (Maybe(..), fromMaybe)
1112
import Data.NonEmpty ((:|))
13+
import Prelude hiding (not)
1214
import Test.Assert (ASSERT, assert)
13-
import Control.Monad.Eff.Random (RANDOM())
14-
import Control.Monad.Eff.Exception (EXCEPTION())
15-
import Test.QuickCheck (QC(), quickCheck)
15+
import Test.QuickCheck (QC, quickCheck)
1616
import Test.QuickCheck.Arbitrary (class Arbitrary)
17-
import Test.QuickCheck.Gen (Gen(), chooseInt, arrayOf, elements)
18-
import Data.Int as Int
17+
import Test.QuickCheck.Gen (Gen, arrayOf, chooseInt, elements, resize)
18+
import Test.QuickCheck.Laws.Data as Data
19+
import Type.Proxy (Proxy(..))
20+
1921

2022
-- | Newtype with an Arbitrary instance that generates only small integers
2123
newtype SmallInt = SmallInt Int
@@ -28,6 +30,12 @@ runSmallInt (SmallInt n) = n
2830

2931
-- | Arbitrary instance for BigInt
3032
newtype TestBigInt = TestBigInt BigInt
33+
derive newtype instance eqTestBigInt :: Eq TestBigInt
34+
derive newtype instance ordTestBigInt :: Ord TestBigInt
35+
derive newtype instance semiringTestBigInt :: Semiring TestBigInt
36+
derive newtype instance ringTestBigInt :: Ring TestBigInt
37+
derive newtype instance commutativeRingTestBigInt :: CommutativeRing TestBigInt
38+
derive newtype instance euclideanRingTestBigInt :: EuclideanRing TestBigInt
3139

3240
instance arbitraryBigInt :: Arbitrary TestBigInt where
3341
arbitrary = do
@@ -37,7 +45,7 @@ instance arbitraryBigInt :: Arbitrary TestBigInt where
3745
where digits :: Gen Int
3846
digits = chooseInt 0 9
3947
digitString :: Gen String
40-
digitString = (fold <<< map show) <$> arrayOf digits
48+
digitString = (fold <<< map show) <$> (resize 50 $ arrayOf digits)
4149

4250
-- | Convert SmallInt to BigInt
4351
fromSmallInt :: SmallInt -> BigInt
@@ -116,3 +124,12 @@ main = do
116124
log "Shifting"
117125
assert $ shl two one == four
118126
assert $ shr two one == one
127+
128+
let prxBigInt = Proxy Proxy TestBigInt
129+
Data.checkEq prxBigInt
130+
Data.checkOrd prxBigInt
131+
Data.checkSemiring prxBigInt
132+
Data.checkRing prxBigInt
133+
-- Data.checkEuclideanRing prxBigInt
134+
Data.checkCommutativeRing prxBigInt
135+

0 commit comments

Comments
 (0)