-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchai-tests.js
More file actions
242 lines (199 loc) · 6.72 KB
/
Copy pathchai-tests.js
File metadata and controls
242 lines (199 loc) · 6.72 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
/* chai tests */
// Hush up about to.be.ok and so forth
/*jshint expr: true */
// Get this to work in the browser and at the command line
// TODO: fix this, problem is that I'm not actually using the CommonJS
// stuff with module.exports and whatnot
// What I probably need is amdefine.js, plus a rudimentary grasp of AMD
if (typeof require !== 'undefined') {
chai = require('chai');
WeightedList = require('../js-weighted-list');
console.log(WeightedList);
//.WeightedList;
}
// else chai has been defined for us via a <script src="chai.js"/>
//chai.Assertion.includeStack = true;
var should = chai.should();
var expect = chai.expect;
// Seed data - is would be nice if this were included in some other file for
// inclusion in all tests
var planets = [
{'key': 'mercury', 'weight': 1},
{'key': 'venus', 'weight': 1},
{'key': 'earth', 'weight': 5},
{'key': 'mars', 'weight': 4},
{'key': 'jupiter', 'weight': 2},
{'key': 'saturn', 'weight': 1},
{'key': 'uranus', 'weight': 1},
{'key': 'neptune', 'weight': 1}
];
var planetsWithData = [
{'key': 'mercury', 'weight': 1, 'data': {'orbit': 1}},
{'key': 'venus', 'weight': 1, 'data': {'orbit': 2}},
{'key': 'earth', 'weight': 5, 'data': {'orbit': 3}},
{'key': 'mars', 'weight': 4, 'data': {'orbit': 4}},
{'key': 'jupiter', 'weight': 2, 'data': {'orbit': 5}},
{'key': 'saturn', 'weight': 1, 'data': {'orbit': 6}},
{'key': 'uranus', 'weight': 1, 'data': {'orbit': 7}},
{'key': 'neptune', 'weight': 1, 'data': {'orbit': 8}}
];
var planetsArray = [
['mercury', 1],
['venus', 1],
['earth', 5],
['mars', 4],
['jupiter', 2],
['saturn', 1],
['uranus', 1],
['neptune', 1]
];
var planetsArrayWithData = [
['mercury', 1, {'orbit': 1}],
['venus', 1, {'orbit': 2}],
['earth', 5, {'orbit': 3}],
['mars', 4, {'orbit': 4}],
['jupiter', 2, {'orbit': 5}],
['saturn', 1, {'orbit': 6}],
['uranus', 1, {'orbit': 7}],
['neptune', 1, {'orbit': 8}]
];
describe('WeightedList', function() {
describe('Constructor', function() {
it('should succeed with no arguments', function() {
expect(new WeightedList()).to.be.ok;
});
it('should succeed with an empty array', function() {
expect(new WeightedList([])).to.be.ok;
});
it('should succeed with an single element', function() {
var wl = new WeightedList([ ['k', 3] ]);
expect(wl).to.be.ok;
wl = new WeightedList([ {'key': 'k', 'weight': 3} ]);
expect(wl).to.be.ok;
});
it('should succeed with various input arrays', function() {
expect(new WeightedList(planets)).to.be.ok;
expect(new WeightedList(planetsWithData)).to.be.ok;
expect(new WeightedList(planetsArray)).to.be.ok;
expect(new WeightedList(planetsArrayWithData)).to.be.ok;
});
it('should throw errors on bad inputs', function() {
var badConstructor = function() {
return new WeightedList( {'wrong': 'field names'} );
};
expect(badConstructor).to.Throw(Error);
badConstructor = function() {
return new WeightedList( [ [2] ] );
};
expect(badConstructor).to.Throw(Error);
});
});
describe('Empty List', function() {
var wl = new WeightedList();
it('should have length zero', function() {
wl.should.have.length(0);
});
it('should give an empty list on shuffle', function() {
wl.shuffle().should.deep.equal([]);
});
it('should throw a stack underflow error', function() {
var underflow = function() {
wl.pop();
};
expect(underflow).to.Throw(Error);
});
it('should throw an underflow error on too much peeking', function() {
var underflow = function() {
wl.peek(2);
};
expect(underflow).to.Throw(Error);
});
it('should be pushable', function() {
wl.push(['k', 3]);
wl.should.be.ok;
});
});
describe('WeightedList.push()', function() {
//var empty = new WeightedList(), planets = new WeightedList(planets);
it('should accept an array', function() {
var make = function() {
new WeightedList().push(['a', 1]);
};
expect(make).to.not.Throw(Error);
});
it('should accept an array with data', function() {
var make = function() {
new WeightedList().push(['a', 1, {}]);
};
expect(make).to.not.Throw(Error);
});
it('should accept an object', function() {
var make = function() {
new WeightedList().push({'key': 'a', 'weight': 1});
};
expect(make).to.not.Throw(Error);
});
it('should accept an object with data', function() {
var make = function() {
new WeightedList().push({'key': 'a', 'weight': 1, 'data': 42});
};
expect(make).to.not.Throw(Error);
});
it('should increase the length', function() {
var empty = new WeightedList();
empty.push(['a', 2]);
empty.should.have.length(1);
});
it('should validate its weights', function() {
var badWeight = function() {
var wl = new WeightedList();
wl.push(['a', -2]);
};
expect(badWeight).to.Throw(Error);
});
});
describe('WeightedList', function() {
it('should retain its input', function() {
var wl = new WeightedList();
wl.push(['k', 1]);
var result = wl.shuffle();
result.should.have.length(1);
result.should.deep.equal(['k']);
});
it('should retain its input with data', function() {
var wl = new WeightedList();
wl.push(['k', 1, {'d': 42}]);
var result = wl.shuffle();
result.should.have.length(1);
var element = result[0];
element.should.deep.equal({'key': 'k', 'data': {'d': 42}});
});
it('should pop out unique elements', function() {
var wl = new WeightedList(planets);
wl.should.have.length(8);
var results = {};
// Pop out the elements one at a time, make sure that each one only occurs once
for (var i = 0; i < planets.length; i++) {
var key = wl.pop();
results[key] = (results[key] || 0) + 1;
}
results.should.deep.equal({'mercury': 1, 'venus': 1, 'earth': 1, 'mars': 1,
'jupiter': 1, 'saturn':1, 'uranus': 1, 'neptune': 1});
wl.should.have.length(0);
wl.shuffle().should.deep.equal([]);
});
it('should return the right amounts of elements in peek()', function() {
var wl = new WeightedList(planets);
wl.should.have.length(8);
var one = wl.peek();
one.should.have.length(1);
wl.should.have.length(8);
var seven = wl.peek(7);
seven.should.have.length(7);
wl.should.have.length(8);
var eight = wl.peek(8);
eight.should.have.length(8);
wl.should.have.length(8);
});
});
});