Skip to content

Commit fe0d6f3

Browse files
committed
Refactor array tests using smash.
1 parent 5c4282f commit fe0d6f3

24 files changed

Lines changed: 831 additions & 877 deletions

src/arrays/zip.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import "min";
2+
13
d3.zip = function() {
24
if (!(n = arguments.length)) return [];
35
for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m;) {

src/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ console.log(JSON.stringify({
3333
"jsdom": "~0.5.2"
3434
},
3535
"devDependencies": {
36-
"smash": "~0.0.5",
36+
"smash": "~0.0.7",
3737
"uglify-js": "2.2.x",
3838
"vows": "0.7.x"
3939
},

test/arrays/ascending-test.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
1-
require("../env");
2-
31
var vows = require("vows"),
2+
load = require("../load"),
43
assert = require("../env-assert");
54

65
var suite = vows.describe("d3.ascending");
76

87
suite.addBatch({
9-
"numbers": {
10-
"returns a negative number if a < b": function() {
11-
assert.isTrue(d3.ascending(0, 1) < 0);
12-
},
13-
"returns a positive number if a > b": function() {
14-
assert.isTrue(d3.ascending(1, 0) > 0);
15-
},
16-
"returns zero if a == b": function() {
17-
assert.equal(d3.ascending(0, 0), 0);
18-
},
19-
"returns NaN if a or b is undefined": function() {
20-
assert.isNaN(d3.ascending(0, undefined));
21-
assert.isNaN(d3.ascending(undefined, 0));
22-
assert.isNaN(d3.ascending(undefined, undefined));
23-
},
24-
"returns NaN if a or b is NaN": function() {
25-
assert.isNaN(d3.ascending(0, NaN));
26-
assert.isNaN(d3.ascending(NaN, 0));
27-
assert.isNaN(d3.ascending(NaN, NaN));
28-
}
29-
}
30-
});
31-
32-
suite.addBatch({
33-
"strings": {
34-
"returns a negative number if a < b": function() {
35-
assert.isTrue(d3.ascending("a", "b") < 0);
36-
},
37-
"returns a positive number if a > b": function() {
38-
assert.isTrue(d3.ascending("b", "a") > 0);
8+
"d3.ascending": {
9+
topic: load("arrays/ascending"),
10+
"numbers": {
11+
"returns a negative number if a < b": function(d3) {
12+
assert.isTrue(d3.ascending(0, 1) < 0);
13+
},
14+
"returns a positive number if a > b": function(d3) {
15+
assert.isTrue(d3.ascending(1, 0) > 0);
16+
},
17+
"returns zero if a == b": function(d3) {
18+
assert.equal(d3.ascending(0, 0), 0);
19+
},
20+
"returns NaN if a or b is undefined": function(d3) {
21+
assert.isNaN(d3.ascending(0, undefined));
22+
assert.isNaN(d3.ascending(undefined, 0));
23+
assert.isNaN(d3.ascending(undefined, undefined));
24+
},
25+
"returns NaN if a or b is NaN": function(d3) {
26+
assert.isNaN(d3.ascending(0, NaN));
27+
assert.isNaN(d3.ascending(NaN, 0));
28+
assert.isNaN(d3.ascending(NaN, NaN));
29+
}
3930
},
40-
"returns zero if a == b": function() {
41-
assert.equal(d3.ascending("a", "a"), 0);
31+
"strings": {
32+
"returns a negative number if a < b": function(d3) {
33+
assert.isTrue(d3.ascending("a", "b") < 0);
34+
},
35+
"returns a positive number if a > b": function(d3) {
36+
assert.isTrue(d3.ascending("b", "a") > 0);
37+
},
38+
"returns zero if a == b": function(d3) {
39+
assert.equal(d3.ascending("a", "a"), 0);
40+
}
4241
}
4342
}
4443
});

test/arrays/bisect-test.js

Lines changed: 84 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
require("../env");
2-
31
var vows = require("vows"),
2+
load = require("../load"),
43
assert = require("../env-assert");
54

65
var suite = vows.describe("d3.bisect");
@@ -9,138 +8,126 @@ var i30 = 1 << 30;
98

109
suite.addBatch({
1110
"bisectLeft": {
12-
topic: function() {
13-
return d3.bisectLeft;
14-
},
15-
"finds the index of an exact match": function(bisect) {
11+
topic: load("arrays/bisect"),
12+
"finds the index of an exact match": function(d3) {
1613
var array = [1, 2, 3];
17-
assert.equal(bisect(array, 1), 0);
18-
assert.equal(bisect(array, 2), 1);
19-
assert.equal(bisect(array, 3), 2);
14+
assert.equal(d3.bisectLeft(array, 1), 0);
15+
assert.equal(d3.bisectLeft(array, 2), 1);
16+
assert.equal(d3.bisectLeft(array, 3), 2);
2017
},
21-
"finds the index of the first match": function(bisect) {
18+
"finds the index of the first match": function(d3) {
2219
var array = [1, 2, 2, 3];
23-
assert.equal(bisect(array, 1), 0);
24-
assert.equal(bisect(array, 2), 1);
25-
assert.equal(bisect(array, 3), 3);
20+
assert.equal(d3.bisectLeft(array, 1), 0);
21+
assert.equal(d3.bisectLeft(array, 2), 1);
22+
assert.equal(d3.bisectLeft(array, 3), 3);
2623
},
27-
"finds the insertion point of a non-exact match": function(bisect) {
24+
"finds the insertion point of a non-exact match": function(d3) {
2825
var array = [1, 2, 3];
29-
assert.equal(bisect(array, 0.5), 0);
30-
assert.equal(bisect(array, 1.5), 1);
31-
assert.equal(bisect(array, 2.5), 2);
32-
assert.equal(bisect(array, 3.5), 3);
26+
assert.equal(d3.bisectLeft(array, 0.5), 0);
27+
assert.equal(d3.bisectLeft(array, 1.5), 1);
28+
assert.equal(d3.bisectLeft(array, 2.5), 2);
29+
assert.equal(d3.bisectLeft(array, 3.5), 3);
3330
},
34-
"observes the optional lower bound": function(bisect) {
31+
"observes the optional lower bound": function(d3) {
3532
var array = [1, 2, 3, 4, 5];
36-
assert.equal(bisect(array, 0, 2), 2);
37-
assert.equal(bisect(array, 1, 2), 2);
38-
assert.equal(bisect(array, 2, 2), 2);
39-
assert.equal(bisect(array, 3, 2), 2);
40-
assert.equal(bisect(array, 4, 2), 3);
41-
assert.equal(bisect(array, 5, 2), 4);
42-
assert.equal(bisect(array, 6, 2), 5);
33+
assert.equal(d3.bisectLeft(array, 0, 2), 2);
34+
assert.equal(d3.bisectLeft(array, 1, 2), 2);
35+
assert.equal(d3.bisectLeft(array, 2, 2), 2);
36+
assert.equal(d3.bisectLeft(array, 3, 2), 2);
37+
assert.equal(d3.bisectLeft(array, 4, 2), 3);
38+
assert.equal(d3.bisectLeft(array, 5, 2), 4);
39+
assert.equal(d3.bisectLeft(array, 6, 2), 5);
4340
},
44-
"observes the optional bounds": function(bisect) {
41+
"observes the optional bounds": function(d3) {
4542
var array = [1, 2, 3, 4, 5];
46-
assert.equal(bisect(array, 0, 2, 3), 2);
47-
assert.equal(bisect(array, 1, 2, 3), 2);
48-
assert.equal(bisect(array, 2, 2, 3), 2);
49-
assert.equal(bisect(array, 3, 2, 3), 2);
50-
assert.equal(bisect(array, 4, 2, 3), 3);
51-
assert.equal(bisect(array, 5, 2, 3), 3);
52-
assert.equal(bisect(array, 6, 2, 3), 3);
43+
assert.equal(d3.bisectLeft(array, 0, 2, 3), 2);
44+
assert.equal(d3.bisectLeft(array, 1, 2, 3), 2);
45+
assert.equal(d3.bisectLeft(array, 2, 2, 3), 2);
46+
assert.equal(d3.bisectLeft(array, 3, 2, 3), 2);
47+
assert.equal(d3.bisectLeft(array, 4, 2, 3), 3);
48+
assert.equal(d3.bisectLeft(array, 5, 2, 3), 3);
49+
assert.equal(d3.bisectLeft(array, 6, 2, 3), 3);
5350
},
54-
"large arrays": function(bisect) {
51+
"large arrays": function(d3) {
5552
var array = [],
5653
i = i30;
5754
array[i++] = 1;
5855
array[i++] = 2;
5956
array[i++] = 3;
6057
array[i++] = 4;
6158
array[i++] = 5;
62-
assert.equal(bisect(array, 0, i - 5, i), i - 5);
63-
assert.equal(bisect(array, 1, i - 5, i), i - 5);
64-
assert.equal(bisect(array, 2, i - 5, i), i - 4);
65-
assert.equal(bisect(array, 3, i - 5, i), i - 3);
66-
assert.equal(bisect(array, 4, i - 5, i), i - 2);
67-
assert.equal(bisect(array, 5, i - 5, i), i - 1);
68-
assert.equal(bisect(array, 6, i - 5, i), i - 0);
59+
assert.equal(d3.bisectLeft(array, 0, i - 5, i), i - 5);
60+
assert.equal(d3.bisectLeft(array, 1, i - 5, i), i - 5);
61+
assert.equal(d3.bisectLeft(array, 2, i - 5, i), i - 4);
62+
assert.equal(d3.bisectLeft(array, 3, i - 5, i), i - 3);
63+
assert.equal(d3.bisectLeft(array, 4, i - 5, i), i - 2);
64+
assert.equal(d3.bisectLeft(array, 5, i - 5, i), i - 1);
65+
assert.equal(d3.bisectLeft(array, 6, i - 5, i), i - 0);
6966
}
70-
}
71-
});
72-
73-
suite.addBatch({
67+
},
7468
"bisectRight": {
75-
topic: function() {
76-
return d3.bisectRight;
77-
},
78-
"finds the index after an exact match": function(bisect) {
69+
topic: load("arrays/bisect"),
70+
"finds the index after an exact match": function(d3) {
7971
var array = [1, 2, 3];
80-
assert.equal(bisect(array, 1), 1);
81-
assert.equal(bisect(array, 2), 2);
82-
assert.equal(bisect(array, 3), 3);
72+
assert.equal(d3.bisectRight(array, 1), 1);
73+
assert.equal(d3.bisectRight(array, 2), 2);
74+
assert.equal(d3.bisectRight(array, 3), 3);
8375
},
84-
"finds the index after the last match": function(bisect) {
76+
"finds the index after the last match": function(d3) {
8577
var array = [1, 2, 2, 3];
86-
assert.equal(bisect(array, 1), 1);
87-
assert.equal(bisect(array, 2), 3);
88-
assert.equal(bisect(array, 3), 4);
78+
assert.equal(d3.bisectRight(array, 1), 1);
79+
assert.equal(d3.bisectRight(array, 2), 3);
80+
assert.equal(d3.bisectRight(array, 3), 4);
8981
},
90-
"finds the insertion point of a non-exact match": function(bisect) {
82+
"finds the insertion point of a non-exact match": function(d3) {
9183
var array = [1, 2, 3];
92-
assert.equal(bisect(array, 0.5), 0);
93-
assert.equal(bisect(array, 1.5), 1);
94-
assert.equal(bisect(array, 2.5), 2);
95-
assert.equal(bisect(array, 3.5), 3);
84+
assert.equal(d3.bisectRight(array, 0.5), 0);
85+
assert.equal(d3.bisectRight(array, 1.5), 1);
86+
assert.equal(d3.bisectRight(array, 2.5), 2);
87+
assert.equal(d3.bisectRight(array, 3.5), 3);
9688
},
97-
"observes the optional lower bound": function(bisect) {
89+
"observes the optional lower bound": function(d3) {
9890
var array = [1, 2, 3, 4, 5];
99-
assert.equal(bisect(array, 0, 2), 2);
100-
assert.equal(bisect(array, 1, 2), 2);
101-
assert.equal(bisect(array, 2, 2), 2);
102-
assert.equal(bisect(array, 3, 2), 3);
103-
assert.equal(bisect(array, 4, 2), 4);
104-
assert.equal(bisect(array, 5, 2), 5);
105-
assert.equal(bisect(array, 6, 2), 5);
91+
assert.equal(d3.bisectRight(array, 0, 2), 2);
92+
assert.equal(d3.bisectRight(array, 1, 2), 2);
93+
assert.equal(d3.bisectRight(array, 2, 2), 2);
94+
assert.equal(d3.bisectRight(array, 3, 2), 3);
95+
assert.equal(d3.bisectRight(array, 4, 2), 4);
96+
assert.equal(d3.bisectRight(array, 5, 2), 5);
97+
assert.equal(d3.bisectRight(array, 6, 2), 5);
10698
},
107-
"observes the optional bounds": function(bisect) {
99+
"observes the optional bounds": function(d3) {
108100
var array = [1, 2, 3, 4, 5];
109-
assert.equal(bisect(array, 0, 2, 3), 2);
110-
assert.equal(bisect(array, 1, 2, 3), 2);
111-
assert.equal(bisect(array, 2, 2, 3), 2);
112-
assert.equal(bisect(array, 3, 2, 3), 3);
113-
assert.equal(bisect(array, 4, 2, 3), 3);
114-
assert.equal(bisect(array, 5, 2, 3), 3);
115-
assert.equal(bisect(array, 6, 2, 3), 3);
101+
assert.equal(d3.bisectRight(array, 0, 2, 3), 2);
102+
assert.equal(d3.bisectRight(array, 1, 2, 3), 2);
103+
assert.equal(d3.bisectRight(array, 2, 2, 3), 2);
104+
assert.equal(d3.bisectRight(array, 3, 2, 3), 3);
105+
assert.equal(d3.bisectRight(array, 4, 2, 3), 3);
106+
assert.equal(d3.bisectRight(array, 5, 2, 3), 3);
107+
assert.equal(d3.bisectRight(array, 6, 2, 3), 3);
116108
},
117-
"large arrays": function(bisect) {
109+
"large arrays": function(d3) {
118110
var array = [],
119111
i = i30;
120112
array[i++] = 1;
121113
array[i++] = 2;
122114
array[i++] = 3;
123115
array[i++] = 4;
124116
array[i++] = 5;
125-
assert.equal(bisect(array, 0, i - 5, i), i - 5);
126-
assert.equal(bisect(array, 1, i - 5, i), i - 4);
127-
assert.equal(bisect(array, 2, i - 5, i), i - 3);
128-
assert.equal(bisect(array, 3, i - 5, i), i - 2);
129-
assert.equal(bisect(array, 4, i - 5, i), i - 1);
130-
assert.equal(bisect(array, 5, i - 5, i), i - 0);
131-
assert.equal(bisect(array, 6, i - 5, i), i - 0);
117+
assert.equal(d3.bisectRight(array, 0, i - 5, i), i - 5);
118+
assert.equal(d3.bisectRight(array, 1, i - 5, i), i - 4);
119+
assert.equal(d3.bisectRight(array, 2, i - 5, i), i - 3);
120+
assert.equal(d3.bisectRight(array, 3, i - 5, i), i - 2);
121+
assert.equal(d3.bisectRight(array, 4, i - 5, i), i - 1);
122+
assert.equal(d3.bisectRight(array, 5, i - 5, i), i - 0);
123+
assert.equal(d3.bisectRight(array, 6, i - 5, i), i - 0);
132124
}
133-
}
134-
});
135-
136-
suite.addBatch({
125+
},
137126
"bisector(key)": {
138-
topic: function() {
139-
return d3.bisector(function(d) { return d.key; });
140-
},
127+
topic: load("arrays/bisect"),
141128
"left": {
142-
topic: function(bisector) {
143-
return bisector.left;
129+
topic: function(d3) {
130+
return d3.bisector(function(d) { return d.key; }).left;
144131
},
145132
"finds the index of an exact match": function(bisect) {
146133
var array = [{key: 1}, {key: 2}, {key: 3}];
@@ -199,8 +186,8 @@ suite.addBatch({
199186
}
200187
},
201188
"right": {
202-
topic: function(bisector) {
203-
return bisector.right;
189+
topic: function(d3) {
190+
return d3.bisector(function(d) { return d.key; }).right;
204191
},
205192
"finds the index after an exact match": function(bisect) {
206193
var array = [{key: 1}, {key: 2}, {key: 3}];

0 commit comments

Comments
 (0)