/
Util.spec.ts
89 lines (70 loc) · 2.34 KB
/
Util.spec.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
import chai = require('chai');
import mocha = require('mocha');
import {Pt} from '../Pt';
import {Const, Util} from '../Util';
import {Num, Geom} from '../Num';
import {Line} from '../Op';
var {assert} = chai;
var {describe, it} = mocha;
describe('Util: ', function() {
describe('Const: ', function() {
it('can convert radian', function() {
assert.equal( Const.deg_to_rad*30, 30*Math.PI/180 );
});
it('can convert angle', function() {
assert.equal( Const.rad_to_deg*Math.PI/4, 45 );
});
});
describe('Util: ', function() {
it('can getArgs with list of numbers', function() {
let c = Util.getArgs( [1,2,3] )
assert.equal( c[1], 2 );
});
it('can getArgs with array', function() {
let c = Util.getArgs( [[1,2,3,4,5]] );
assert.equal( c[4], 5 );
});
it('can getArgs with Pt like object', function() {
let c = Util.getArgs( [{x:1, y:2, z:3, w:4}] );
assert.equal( c[3], 4 );
});
let group = [1,2,3,4,5,6,7];
it('can split an array with size only', function() {
let g = Util.split( group, 2);
assert.isTrue( g.length === 3 && g[2][0] === 5 );
});
it('can split an array with size and stride 1', function() {
let g = Util.split( group, 3, 1);
assert.isTrue( g.length === 5 && g[4][0] === 5 );
});
it('can split an array with size and stride 3', function() {
let g = Util.split( group, 2, 3);
assert.isTrue( g.length === 2 && g[1][1] === 5 );
});
it('can split an array with size and stride 3 and loop back', function() {
let g = Util.split( group, 2, 3, true);
assert.isTrue( g.length === 3 && g[2][1] === 1 );
});
it("can split an array with size and stride 3 and don't match size", function() {
let g = Util.split( group, 2, 3, false, false);
assert.isTrue( g.length === 3 && g[2].length === 1 && g[2][0] === 7 );
});
it('can create and run a stepper', function() {
let g = Util.stepper( 50, 0, 3 );
let c = 0;
for (let i=0; i<61; i++) {
c = g();
}
assert.equal( c, 33 );
});
it('can loop with a range', function() {
let k = 0;
let f = (i) => {
k += i;
return {id: `id${i}`, value:i*2}
};
let g = Util.forRange( f, 100 );
assert.isTrue( g[12].id == "id12" && k === 4950 );
})
});
});