From 8078f83a76752cd7c223eafcb330c1fc52e78ec6 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Tue, 9 Apr 2013 22:48:27 +0300 Subject: [PATCH] specific development related number-to-string conversion tests for Number.prototype --- testcases/test-numconv-tostring-exp.js | 34 ++++++++++++++ testcases/test-numconv-tostring-fixed.js | 31 +++++++++++++ testcases/test-numconv-tostring-prec.js | 59 ++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 testcases/test-numconv-tostring-exp.js create mode 100644 testcases/test-numconv-tostring-fixed.js create mode 100644 testcases/test-numconv-tostring-prec.js diff --git a/testcases/test-numconv-tostring-exp.js b/testcases/test-numconv-tostring-exp.js new file mode 100644 index 00000000..384b0be9 --- /dev/null +++ b/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); +} + diff --git a/testcases/test-numconv-tostring-fixed.js b/testcases/test-numconv-tostring-fixed.js new file mode 100644 index 00000000..36c84da7 --- /dev/null +++ b/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); +} + diff --git a/testcases/test-numconv-tostring-prec.js b/testcases/test-numconv-tostring-prec.js new file mode 100644 index 00000000..cea99a1b --- /dev/null +++ b/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); +} +