Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 993e2c3

Browse files
committed
add tests
1 parent fea0cdb commit 993e2c3

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ install:
1414
- bower install
1515
script:
1616
- npm run -s build
17+
- npm run -s test
1718
after_success:
1819
- >-
1920
test $TRAVIS_TAG &&

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"private": true,
33
"scripts": {
44
"clean": "rimraf output && rimraf .pulp-cache",
5+
"test": "pulp test",
56
"build": "eslint src && pulp build -- --censor-lib --strict"
67
},
78
"devDependencies": {

test/Test/Main.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"use strict";
2+
3+
exports.mkArr = function(){
4+
return [];
5+
};
6+
7+
exports.unArr = function(xs){
8+
return xs.slice(0);
9+
};
10+
11+
exports.pushToArr = function(xs) {
12+
return function(x) {
13+
return function() {
14+
xs.push(x);
15+
return x;
16+
};
17+
};
18+
};
19+
20+
exports.assert = function(isOk) {
21+
return function(msg) {
22+
return function() {
23+
if (isOk == false) {
24+
throw new Error("assertion failed: " + msg);
25+
};
26+
};
27+
};
28+
};
29+
30+
exports.naturals = function(n) {
31+
var res = [];
32+
for (var index = 0; index < n; index++) {
33+
res[index] = index;
34+
}
35+
return res;
36+
};
37+
38+
exports.log = function(x) {
39+
return function(){
40+
console.log(x)
41+
}
42+
};
43+
44+
45+
exports.time = function(x) {
46+
return function(){
47+
console.time(x)
48+
}
49+
};
50+
51+
52+
exports.timeEnd = function(x) {
53+
return function(){
54+
console.timeEnd(x)
55+
}
56+
};

test/Test/Main.purs

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
module Test.Main where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff (Eff)
6+
import Control.Apply (lift2)
7+
import Control.Monad.Eff.Class (class MonadEff)
8+
import Control.Monad.Eff (Eff)
9+
-- import Control.Monad.Eff.Console (CONSOLE)
10+
11+
12+
testLift2 :: Eff () Unit
13+
testLift2 = do
14+
arr <- mkArr
15+
res <- (pushToArr arr 1) `lift2 (+)` (pushToArr arr 2)
16+
res' <- (pure 1) `lift2 (+)` (pure 2)
17+
assert ([1, 2] == unArr arr) "lift2 1/3"
18+
assert (3 == res') "lift2 2/3"
19+
assert (3 == res) "lift2 3/3"
20+
21+
22+
testApply :: Int -> Eff () Unit
23+
testApply n' = do
24+
arr <- mkArr
25+
applyLoop (void <<< pushToArr arr) n'
26+
assert (naturals n' == unArr arr) $ "apply " <> show n'
27+
where
28+
applyLoop eff max = go (pure unit) 0
29+
where
30+
go acc n | n == max = acc
31+
go acc n = go (acc <* eff n) (n + 1)
32+
33+
34+
35+
testBindRight :: Int -> Eff () Unit
36+
testBindRight n' = do
37+
arr <- mkArr
38+
bindRightLoop (void <<< pushToArr arr) n'
39+
assert (naturals n' == unArr arr) $ "bind right " <> show n'
40+
where
41+
bindRightLoop eff max = go (pure unit) 0
42+
where
43+
go acc n | n == max = acc
44+
go acc n = go (eff (max - n - 1) >>= const acc) (n + 1)
45+
46+
47+
testBindLeft :: Int -> Eff () Unit
48+
testBindLeft n' = do
49+
arr <- mkArr
50+
bindLeftLoop (void <<< pushToArr arr) n'
51+
assert (naturals n' == unArr arr) $ "bind left " <> show n'
52+
where
53+
bindLeftLoop eff max = go (pure unit) 0
54+
where
55+
go acc n | n == max = acc
56+
go acc n = go (acc >>= const (eff n)) (n + 1)
57+
58+
59+
testMap :: Int -> Eff () Unit
60+
testMap n = do
61+
arr <- mkArr
62+
res <- mapLoop n (pushToArr arr 0)
63+
assert (res == n) $ "map " <> show n
64+
assert ([0] == unArr arr) $ "map"
65+
where
66+
mapLoop max i =
67+
if max == 0
68+
then i
69+
else mapLoop (max - 1) (map (_ + 1) i)
70+
71+
72+
main :: Eff () Unit
73+
main = do
74+
test "testLift2" $ testLift2
75+
test "testBindRight" $ testBindRight 1000000
76+
test "testBindLeft" $ testMap 1000000
77+
test "testMap" $ testMap 5000000
78+
test "testApply" $ testApply 1000000
79+
where
80+
test msg eff = do
81+
time msg
82+
eff
83+
timeEnd msg
84+
85+
86+
foreign import data Arr :: Type -> Type
87+
88+
89+
foreign import mkArr :: forall e a. Eff e (Arr a)
90+
foreign import pushToArr :: forall e a. Arr a -> a -> Eff e a
91+
foreign import assert :: forall e. Boolean -> String -> Eff e Unit
92+
foreign import log :: forall e a. a -> Eff e Unit
93+
foreign import unArr :: forall a. Arr a -> Array a
94+
foreign import naturals :: Int -> Array Int
95+
96+
foreign import time :: forall e. String -> Eff e Unit
97+
foreign import timeEnd :: forall e. String -> Eff e Unit

0 commit comments

Comments
 (0)