This commit is contained in:
Mike Bostock 2011-08-14 09:29:42 -07:00
Родитель cb7ebe11e8
Коммит 01fb201f4e
3 изменённых файлов: 70 добавлений и 141 удалений

70
test/core/round-test.js Normal file
Просмотреть файл

@ -0,0 +1,70 @@
require("../env");
require("../../d3");
var vows = require("vows"),
assert = require("assert");
var suite = vows.describe("d3.round");
suite.addBatch({
"round": {
topic: function() {
return d3.round;
},
"returns a number": function(round) {
assert.isNumber(round(42));
},
"returns zero for zero": function(round) {
assert.equal(round(0), 0);
},
"ignores degenerate input": function(round) {
assert.isNaN(round(NaN));
assert.equal(round(Infinity), Infinity);
assert.equal(round(-Infinity), -Infinity);
},
"returns integers by default": function(round) {
assert.equal(round(10.6), 11);
assert.equal(round(10.4), 10);
assert.equal(round(0.6), 1);
assert.equal(round(0.4), 0);
assert.equal(round(-0.6), -1);
assert.equal(round(-0.4), 0);
assert.equal(round(-10.6), -11);
assert.equal(round(-10.4), -10);
},
"rounds to the specified decimal place": function(round) {
assert.inDelta(round(10.56, 1), 10.6, 1e-6);
assert.inDelta(round(10.54, 1), 10.5, 1e-6);
assert.inDelta(round(0.56, 1), 0.6, 1e-6);
assert.inDelta(round(0.54, 1), 0.5, 1e-6);
assert.inDelta(round(-0.56, 1), -0.6, 1e-6);
assert.inDelta(round(-0.54, 1), -0.5, 1e-6);
assert.inDelta(round(-10.56, 1), -10.6, 1e-6);
assert.inDelta(round(-10.54, 1), -10.5, 1e-6);
assert.inDelta(round(10.556, 2), 10.56, 1e-6);
assert.inDelta(round(10.554, 2), 10.55, 1e-6);
assert.inDelta(round(0.556, 2), 0.56, 1e-6);
assert.inDelta(round(0.554, 2), 0.55, 1e-6);
assert.inDelta(round(-0.556, 2), -0.56, 1e-6);
assert.inDelta(round(-0.554, 2), -0.55, 1e-6);
assert.inDelta(round(-10.556, 2), -10.56, 1e-6);
assert.inDelta(round(-10.554, 2), -10.55, 1e-6);
},
"rounds to the specified significant digits": function(round) {
assert.equal(round(123.45, -1), 120);
assert.equal(round(345.67, -1), 350);
assert.equal(round(-123.45, -1), -120);
assert.equal(round(-345.67, -1), -350);
assert.equal(round(123.45, -2), 100);
assert.equal(round(456.78, -2), 500);
assert.equal(round(-123.45, -2), -100);
assert.equal(round(-456.78, -2), -500);
assert.equal(round(123.45, -3), 0);
assert.equal(round(567.89, -3), 1000);
assert.equal(round(-123.45, -3), 0);
assert.equal(round(-567.89, -3), -1000);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,75 +0,0 @@
require("./../../lib/env-js/envjs/node");
require("./../../d3");
console.log("degenerate:");
console.log(" 0 ->", d3.round(0));
console.log(" NaN ->", d3.round(NaN));
console.log(" Infinity ->", d3.round(Infinity));
console.log(" -Infinity ->", d3.round(-Infinity));
console.log("");
console.log("default:");
console.log(" 10.51 ->", d3.round(10.51));
console.log(" 10.50 ->", d3.round(10.50));
console.log(" 10.49 ->", d3.round(10.49));
console.log(" 0.51 ->", d3.round(0.51));
console.log(" 0.50 ->", d3.round(0.50));
console.log(" 0.49 ->", d3.round(0.49));
console.log(" -0.51 ->", d3.round(-0.51));
console.log(" -0.50 ->", d3.round(-0.50));
console.log(" -0.49 ->", d3.round(-0.49));
console.log(" -10.51 ->", d3.round(-10.51));
console.log(" -10.50 ->", d3.round(-10.50));
console.log(" -10.49 ->", d3.round(-10.49));
console.log("");
console.log("precision 1:");
console.log(" 10.51 ->", d3.round(10.51, 1));
console.log(" 10.50 ->", d3.round(10.50, 1));
console.log(" 10.49 ->", d3.round(10.49, 1));
console.log(" 0.51 ->", d3.round(0.51, 1));
console.log(" 0.50 ->", d3.round(0.50, 1));
console.log(" 0.49 ->", d3.round(0.49, 1));
console.log(" -0.51 ->", d3.round(-0.51, 1));
console.log(" -0.50 ->", d3.round(-0.50, 1));
console.log(" -0.49 ->", d3.round(-0.49, 1));
console.log(" -10.51 ->", d3.round(-10.51, 1));
console.log(" -10.50 ->", d3.round(-10.50, 1));
console.log(" -10.49 ->", d3.round(-10.49, 1));
console.log("");
console.log("precision 2:");
console.log(" 10.51 ->", d3.round(10.51, 2));
console.log(" 10.50 ->", d3.round(10.50, 2));
console.log(" 10.49 ->", d3.round(10.49, 2));
console.log(" 0.51 ->", d3.round(0.51, 2));
console.log(" 0.50 ->", d3.round(0.50, 2));
console.log(" 0.49 ->", d3.round(0.49, 2));
console.log(" -0.51 ->", d3.round(-0.51, 2));
console.log(" -0.50 ->", d3.round(-0.50, 2));
console.log(" -0.49 ->", d3.round(-0.49, 2));
console.log(" -10.51 ->", d3.round(-10.51, 2));
console.log(" -10.50 ->", d3.round(-10.50, 2));
console.log(" -10.49 ->", d3.round(-10.49, 2));
console.log("");
console.log("precision -1:");
console.log(" 123.45 ->", d3.round(123.45, -1));
console.log(" 345.67 ->", d3.round(345.67, -1));
console.log(" -123.45 ->", d3.round(-123.45, -1));
console.log(" -345.67 ->", d3.round(-345.67, -1));
console.log("");
console.log("precision -2:");
console.log(" 123.45 ->", d3.round(123.45, -2));
console.log(" 456.78 ->", d3.round(456.78, -2));
console.log(" -123.45 ->", d3.round(-123.45, -2));
console.log(" -456.78 ->", d3.round(-456.78, -2));
console.log("");
console.log("precision -3:");
console.log(" 123.45 ->", d3.round(123.45, -3));
console.log(" 567.89 ->", d3.round(567.89, -3));
console.log(" -123.45 ->", d3.round(-123.45, -3));
console.log(" -567.89 ->", d3.round(-567.89, -3));
console.log("");

Просмотреть файл

@ -1,66 +0,0 @@
degenerate:
0 -> 0
NaN -> NaN
Infinity -> Infinity
-Infinity -> -Infinity
default:
10.51 -> 11
10.50 -> 11
10.49 -> 10
0.51 -> 1
0.50 -> 1
0.49 -> 0
-0.51 -> -1
-0.50 -> 0
-0.49 -> 0
-10.51 -> -11
-10.50 -> -10
-10.49 -> -10
precision 1:
10.51 -> 10.5
10.50 -> 10.5
10.49 -> 10.5
0.51 -> 0.5
0.50 -> 0.5
0.49 -> 0.5
-0.51 -> -0.5
-0.50 -> -0.5
-0.49 -> -0.5
-10.51 -> -10.5
-10.50 -> -10.5
-10.49 -> -10.5
precision 2:
10.51 -> 10.51
10.50 -> 10.5
10.49 -> 10.49
0.51 -> 0.51
0.50 -> 0.5
0.49 -> 0.49
-0.51 -> -0.51
-0.50 -> -0.5
-0.49 -> -0.49
-10.51 -> -10.51
-10.50 -> -10.5
-10.49 -> -10.49
precision -1:
123.45 -> 120
345.67 -> 350
-123.45 -> -120
-345.67 -> -350
precision -2:
123.45 -> 100
456.78 -> 500
-123.45 -> -100
-456.78 -> -500
precision -3:
123.45 -> 0
567.89 -> 1000
-123.45 -> 0
-567.89 -> -1000