Browse Source

specific development related number-to-string conversion tests for Number.prototype

pull/1/head
Sami Vaarala 12 years ago
parent
commit
8078f83a76
  1. 34
      testcases/test-numconv-tostring-exp.js
  2. 31
      testcases/test-numconv-tostring-fixed.js
  3. 59
      testcases/test-numconv-tostring-prec.js

34
testcases/test-numconv-tostring-exp.js

@ -0,0 +1,34 @@
/*===
1e+2
8e+2
1e+3
1.23e+4
9.88e+4
1.00e+5
1e+4
1e-1
===*/
function expTest() {
function test(x,n) {
print(new Number(x).toExponential(n));
}
test(123, 0);
test(750, 0); // rounds up
test(999, 0); // rounds up, over first digit
test(12345, 2);
test(98750, 2); // rounds up
test(99999, 2); // ronuds up, over first digit
test(12345, undefined);
test(0.1, undefined);
}
try {
expTest();
} catch (e) {
print(e);
}

31
testcases/test-numconv-tostring-fixed.js

@ -0,0 +1,31 @@
/*===
1.000
900000000000000000000.00000000000000000000
1e+21
0
1
0
1
===*/
function fixedTest() {
function test(x, n) {
print(new Number(x).toFixed(n));
}
test(1, 3);
test(9e20, 20);
test(1e21, 20); // falls back to ToString()
test(0.1, 0);
test(0.9, 0); // rounds up
test(0.1, undefined);
test(0.9, undefined); // rounds up
}
try {
fixedTest();
} catch (e) {
print(e);
}

59
testcases/test-numconv-tostring-prec.js

@ -0,0 +1,59 @@
/*===
0.00
1.00
1.00
0.9999
0.99999
1.23457e+7
1.234568e+7
12345678
12345678.0
8.76543e+7
8.765432e+7
87654321
87654321.0
5.556e+4
55555
4.444e+4
44444
1.000e+5
99999
===*/
function precisionTest() {
function test(x, n) {
print(new Number(x).toPrecision(n));
}
test(0, 3);
test(1, 3);
test(0.9999, 3);
test(0.9999, 4);
test(0.9999, 5);
test(12345678, 6); // rounds up
test(12345678, 7); // rounds up
test(12345678, 8);
test(12345678, 9);
test(87654321, 6); // rounds down
test(87654321, 7); // rounds down
test(87654321, 8);
test(87654321, 9);
test(55555, 4); // rounds up
test(55555, 5);
test(44444, 4); // rounds down
test(44444, 5);
test(99999, 4); // rounds up, carries
test(99999, 5);
}
try {
precisionTest();
} catch (e) {
print(e);
}
Loading…
Cancel
Save