-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.array.js
More file actions
326 lines (245 loc) · 8.88 KB
/
Copy pathtest.array.js
File metadata and controls
326 lines (245 loc) · 8.88 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
var util = require('util');
should = require('should'),
JSUS = require('./../jsus').JSUS;
var obj_simple = {
a: 1,
b: 2,
c: 3,
};
// If update, update also getAllKeys test
var obj_complex = {
a: 1,
b: obj_simple,
c: 3,
};
var obj_with_null = {
a: 1,
b: null,
c: 3,
};
var obj_falsy = {
a: 0,
b: false,
c: 3,
};
describe('ARRAY: ', function(){
// removeElement
describe('#removeElement()', function(){
var test_arr = ['element1', 'element2', 'element3', 'element4'];
it('should return element3 when removing it from the array', function(){
JSUS.removeElement('element3', test_arr).should.eql(['element3']);
});
it('should return false when removing element5 from array', function(){
JSUS.removeElement('element5', test_arr).should.equal(false);
});
});
// inArray
describe('#inArray()', function(){
var test_arr = ['element1', 'element2', 'element3', 'element4'];
it('should return TRUE when looking for element1', function() {
JSUS.inArray('element2', test_arr).should.be.true;
});
it('should return FALSE when looking for element5', function() {
JSUS.inArray('element5', test_arr).should.be.false;
});
});
describe('#latinSquare()', function() {
var S = JSUS.randomInt(3,10); // start
var L = JSUS.randomInt(3,S); // limit
var ls = null
before(function(){
ls = JSUS.latinSquare(S,L);
});
it('The size of latin square should be correct', function() {
ls.length.should.equal(L);
});
it('The row of latin square should have correct length', function() {
for (var z = 0; z < L; z++) {
ls[z].length.should.equal((S));
}
});
it('Symbol "i" should appear once and only once each row', function() {
for (var i = 0; i < L; i++) {
var elements = [];
for (var j = 0; j < S; j++) {
for (var z = 0; z < S; z++) {
if (z == j) continue;
ls[i][j].should.not.be.equal(ls[i][z]);
}
}
}
});
it('Symbol "i" should appear at most once in each column', function() {
for (var j = 0; j < S; j++) {
for (var i=0; i < L; i++) {
for (var z = 0; z < L; z++) {
if (z == j) continue;
ls[i][j].should.not.be.equal(ls[i][z]);
}
}
}
});
});
describe('#latinSquareNoSelf()', function() {
var S = JSUS.randomInt(3,10); // start
var L = JSUS.randomInt(3,(S-1)); // limit
var ls = null
before(function(){
ls = JSUS.latinSquareNoSelf(S,L);
});
it('Column "j" should not contain "j" as index', function(){
for (var j = 0; j < S; j++) {
for (var i=0; i < L; i++) {
ls[i][j].should.not.be.equal(j);
}
}
});
});
describe('#isArray()', function() {
var tests = [
{},
null,
false,
undefined,
0,
1,
'a',
'ciao',
];
it('should distinguish between array and not array', function() {
JSUS.isArray([]).should.be.true;
JSUS.isArray(tests).should.be.true;
JSUS.each(tests, function(e) {
JSUS.isArray(e).should.not.be.true;
});
});
});
describe('#seq()', function() {
it('should reproduce the sequence 0:5', function() {
JSUS.seq(0,5).should.be.eql([0,1,2,3,4,5]);
});
it('should reproduce the sequence 5:0', function() {
JSUS.seq(5,0).should.be.eql([5,4,3,2,1,0]);
});
it('should reproduce the sequence -5:0', function() {
JSUS.seq(-5,0).should.be.eql([-5,-4,-3,-2,-1,0]);
});
it('should reproduce the sequence 0:-5', function() {
JSUS.seq(0,-5).should.be.eql([0,-1,-2,-3,-4,-5]);
});
it('should reproduce the sequence -2:-2', function() {
JSUS.seq(-2,2).should.be.eql([-2,-1,0,1,2]);
});
it('should reproduce return a single number array when start = end',
function() {
JSUS.seq(2,2).should.be.eql([2]);
});
it('should reproduce the sequence 0:5 with increment 2', function() {
JSUS.seq(0,5,2).should.be.eql([0,2,4]);
});
it('should reproduce the sequence 5:0 with increment 2', function() {
JSUS.seq(5,0,2).should.be.eql([5,3,1]);
});
it('should return false if parameters are missing or wrong',
function() {
JSUS.seq(-2).should.be.false;
JSUS.seq('a',0).should.be.false;
JSUS.seq().should.be.false;
JSUS.seq(null, 0).should.be.false;
JSUS.seq(undefined, 2).should.be.false;
JSUS.seq(Infinity, 5).should.be.false;
JSUS.seq(5,0,0).should.be.false;
});
it('should reproduce the sequence 5:0 and execute the callback',
function() {
var func = function(e){
if (e < 10) {
e = '0' + e;
}
return 'R_' + e;
};
JSUS.seq(1,4,1,func)
.should.be.eql(['R_01','R_02','R_03','R_04']);
});
});
describe('#map()', function() {
var func = function(e, i, t, m) {
if (i) e = e + i;
if (t) e = e + t;
if (m) e = e + m;
if (e > 5) return e;
};
var array = JSUS.seq(1,10);
it('should return [6,7,8,9,10]', function() {
JSUS.map(array, func).should.eql([6,7,8,9,10]);
});
it('should return [6,7,8,9,10,11]', function() {
JSUS.map(array, func, 1).should.eql([6,7,8,9,10,11]);
});
it('should return [6,7,8,9,10,11,12]', function() {
JSUS.map(array, func, 1, 1).should.eql([6,7,8,9,10,11,12]);
});
it('should return [6,7,8,9,10,11,12,13]', function() {
JSUS.map(array, func, 1, 1, 1).should.eql([6,7,8,9,10,11,12,13]);
});
});
describe('#rep()', function() {
var array = JSUS.seq(1,5);
it('should replicate an array twice', function() {
JSUS.rep(array, 2).should.eql([1,2,3,4,5,1,2,3,4,5]);
});
it('should return the same array', function(){
JSUS.rep(array, 1).should.be.eql(array);
JSUS.rep(array).should.be.eql(array);
});
it('should replicate an array three times', function(){
JSUS.rep(array, 3).should.eql([1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]);
});
});
describe('#distinct()', function() {
it('should eliminate duplicated strings', function(){
var array = ["a", "a", "b", "b", "c"];
JSUS.distinct(array).should.eql(["a","b","c"]);
});
it('should eliminate duplicated numbers', function(){
var array = [1,2,3,1,2,3,4,5,1,8];
JSUS.distinct(array).should.eql([1,2,3,4,5,8]);
});
it('should eliminate duplicated special values', function(){
var array = [null,null,{},{},0,0, undefined,Infinity,
Infinity, undefined];
JSUS.distinct(array).should.be.eql([null,{},0,undefined,Infinity]);
});
it('should eliminate duplicated special values (NaN)', function(){
var array = [NaN, NaN];
var distinct = JSUS.distinct(array);
distinct.length.should.be.eql(1);
distinct[0].should.be.NaN;
});
});
describe('#stretch()', function() {
var array = [1,2,3];
it('should repeat each element of the array twice', function(){
JSUS.stretch(array, 2).should.eql([1,1,2,2,3,3]);
});
it('should return the same array', function(){
JSUS.stretch(array, 1).should.be.eql(array);
JSUS.stretch(array).should.be.eql(array);
});
it('should repeat each element a custom number of times',
function(){
JSUS.stretch(array, [1,2,3]).should.eql([1,2,2,3,3,3]);
});
it('should repeat each element a custom number of times (Recycling)',
function(){
JSUS.stretch(array, [2,1]).should.eql([1,1,2,3,3]);
});
});
describe('#transpose()', function() {
var array = [ [1,2,3], [4,5,6] ];
var t = [ [1,4], [2,5], [3,6] ];
it('should transpose a 2D matrix', function(){
JSUS.transpose(array).should.eql(t);
});
});
});