This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.ts
221 lines (200 loc) · 4.38 KB
/
tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// const assert = require('assert');
// const hx = require('./').hx;
// const lib = require('./lib');
import * as assert from 'assert';
import { hx } from './';
import * as lib from './lib';
assert.equal(hx([lib.id, 1]), 1)
assert.equal(hx([lib.id, 'hi']), 'hi');
// assert.deepEqual(e([lib.id, [1, 2, 3]]), [1, 2, 3]);
assert.deepEqual(hx([lib.id, { a: 1, b: 2 }]), { a: 1, b: 2 });
assert.deepEqual(hx([lib.id,
[lib.id, 1]
]), 1);
/**
* Math
*/
// add
assert.equal(
hx([lib.add, 1, 1]),
2);
assert.equal(
hx([lib.add, 1, [lib.id, 1]]),
2
);
assert.equal(
hx([lib.add, 1, 1, 1, 1, 1, 1]),
6, "add many");
assert.equal(
hx([lib.add, [lib.add, 1, 1], 1]),
3, "add nested 1");
assert.equal(
hx([lib.add, [lib.add, [lib.add, 1, 1], 1], 1]),
4, "add nested 2");
assert.equal(
hx([lib.add, [lib.add, 1, 1], [lib.add, 1, 1]]),
4, "add multi nested");
assert.equal(
hx([[lib.add, 1], 1, 1]),
3, "add curried");
// mul
assert.equal(
hx([lib.mul, 2, 2]),
4);
assert.equal(
hx([lib.mul, 2, 2, 2, 2, 2, 2]),
64, "mul many");
assert.equal(
hx([lib.mul, [lib.mul, 2, 2], 2]),
8, "mul nested 1");
assert.equal(
hx([lib.mul, [lib.mul, [lib.mul, 2, 2], 2], 2]),
16, "mul nested 2");
assert.equal(
hx([lib.mul, [lib.mul, 2, 2], [lib.mul, 2, 2]]),
16, "mul multi nested");
assert.equal(
hx([[lib.mul, 2], 2, 2]),
8, "mul curried");
// div
assert.equal(
hx([lib.div, 2, 2]),
1);
// assert.equal(
// e([lib.div, 2, 2, 2]),
// .5);
// assert.equal(
// e([lib.div, [lib.div, 2, 2], 2]),
// 8, "nested 1");
// assert.equal(
// e([lib.div, [lib.div, [lib.div, 2, 2], 2], 2]),
// 16, "nested 2");
// assert.equal(
// e([lib.div, [lib.div, 2, 2], [lib.div, 2, 2]]),
// 16, "multi nested");
// assert.equal(
// e([[lib.div, 2], 2, 2]),
// 8, "curried");
// inc
assert.equal(
hx([lib.inc, 1]),
2);
assert.equal(
hx([lib.inc, 1, 1, 1, 1, 1, 1]),
2, "inc many");
assert.equal(
hx([lib.inc, [lib.inc, 1]]),
3, "inc nested 1");
assert.equal(
hx([lib.inc, [lib.inc, [lib.inc, 1]]]),
4, "inc nested 2");
// dec
assert.equal(
hx([lib.dec, 3]),
2);
assert.equal(
hx([lib.dec, 3, 3, 3, 3, 3, 3]),
2, "dec many");
assert.equal(
hx([lib.dec, [lib.dec, 3]]),
1, "dec nested 3");
assert.equal(
hx([lib.dec, [lib.dec, [lib.dec, 3]]]),
0, "dec nested 2");
// sqr
/**
* Strings
*/
// str
assert.equal(
hx([lib.str, 'hello']),
'hello', "str");
assert.equal(
hx([lib.str, 'hello', 'world']),
'helloworld', "str multi");
assert.equal(
hx([lib.str,
[lib.str, 'hello'],
'world'
]),
'helloworld', "str nested 1");
assert.equal(
hx([lib.str, [lib.str, 'hello', 'world']]),
'helloworld', "str nested multi");
assert.equal(
hx([lib.str, [lib.str, 'hello', 'world'], 'omg']),
'helloworldomg', "str nested with multi");
assert.equal(
hx([lib.str, 'omg', [lib.str, 'hello', 'world']]),
'omghelloworld', "str nested with multi again");
assert.equal(
hx([lib.str,
[lib.str, 'hel',
[lib.str, 'lo']
],
'world',
]),
'helloworld', "str nested 2");
// join
assert.equal(
hx([lib.join, '', ['h', 'e','l','l','o']]), 'hello'
);
assert.equal(
hx([lib.join, '', [
[lib.join, ', ', [lib.list, 'hello', 'world']],
'!'
]]), 'hello, world!'
);
// upper
// lower
// print?
/**
* Higher order functions
*/
// map
assert.deepEqual(
hx([lib.map, lib.id, [1, 2 , 3]]), [1, 2, 3]
);
assert.deepEqual(
hx([lib.map, lib.id,
[lib.id, [1, 2, 3]]
]), [1, 2, 3]
);
assert.deepEqual(
hx([lib.map, lib.id,
[lib.map, lib.id, [1, 2, 3]]
]), [1, 2, 3]
);
assert.deepEqual(
hx([lib.map, [lib.add, 1],
[lib.map, [lib.add, 1], [1, 2, 3]]
]), [3, 4, 5]
);
assert.deepEqual(
hx([lib.map, lib.inc, [1, 2, 3]]), [2, 3, 4]
);
// filter
assert.deepEqual(
hx([lib.filter, lib.id, [1, 2 , 3]]), [1, 2, 3]
);
assert.deepEqual(
hx([lib.filter, x => x > 2, [1, 2 , 3]]), [3]
);
assert.deepEqual(
hx([lib.filter, lib.id,
[lib.id, [1, 2, 3]]
]), [1, 2, 3]
);
assert.deepEqual(
hx([lib.filter, lib.id,
[lib.filter, lib.id, [1, 2, 3]]
]), [1, 2, 3]
);
assert.deepEqual(
hx([lib.filter, x => x > 1,
[lib.filter, x => x < 3,
[lib.list, 1, 2, 3],
],
]), [2]
);
console.log('Success!');