diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/Intl/NumberFormat/rounding-increment.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | js/src/tests/non262/Intl/NumberFormat/rounding-increment.js | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/js/src/tests/non262/Intl/NumberFormat/rounding-increment.js b/js/src/tests/non262/Intl/NumberFormat/rounding-increment.js new file mode 100644 index 0000000000..84d4480227 --- /dev/null +++ b/js/src/tests/non262/Intl/NumberFormat/rounding-increment.js @@ -0,0 +1,102 @@ +// |reftest| skip-if(!this.hasOwnProperty("Intl")||release_or_beta) + +// Nickel rounding. +{ + let nf = new Intl.NumberFormat("en", { + minimumFractionDigits: 2, + maximumFractionDigits: 2, + roundingIncrement: 5, + }); + + assertEq(nf.format(1.22), "1.20"); + assertEq(nf.format(1.224), "1.20"); + assertEq(nf.format(1.225), "1.25"); + assertEq(nf.format(1.23), "1.25"); +} + +// Dime rounding. +{ + let nf = new Intl.NumberFormat("en", { + minimumFractionDigits: 2, + maximumFractionDigits: 2, + roundingIncrement: 10, + }); + + assertEq(nf.format(1.24), "1.20"); + assertEq(nf.format(1.249), "1.20"); + assertEq(nf.format(1.250), "1.30"); + assertEq(nf.format(1.25), "1.30"); +} + +// Rounding increment option is rounded down. +{ + let nf1 = new Intl.NumberFormat("en", { + minimumFractionDigits: 0, + maximumFractionDigits: 0, + roundingIncrement: 10, + }); + + let nf2 = new Intl.NumberFormat("en", { + minimumFractionDigits: 0, + maximumFractionDigits: 0, + roundingIncrement: 10.1, + }); + + let nf3 = new Intl.NumberFormat("en", { + minimumFractionDigits: 0, + maximumFractionDigits: 0, + roundingIncrement: 10.9, + }); + + assertEq(nf1.resolvedOptions().roundingIncrement, 10); + assertEq(nf2.resolvedOptions().roundingIncrement, 10); + assertEq(nf3.resolvedOptions().roundingIncrement, 10); + + assertEq(nf1.format(123), "120"); + assertEq(nf2.format(123), "120"); + assertEq(nf3.format(123), "120"); +} + +// |minimumFractionDigits| must be equal to |maximumFractionDigits| when +// |roundingIncrement| is used. +// +// |minimumFractionDigits| defaults to zero. +{ + let nf = new Intl.NumberFormat("en", { + roundingIncrement: 10, + // minimumFractionDigits: 0, (default) + maximumFractionDigits: 0, + }); + + let resolved = nf.resolvedOptions(); + assertEq(resolved.minimumFractionDigits, 0); + assertEq(resolved.maximumFractionDigits, 0); + assertEq(resolved.roundingIncrement, 10); + + assertEq(nf.format(123), "120"); + assertEq(nf.format(123.456), "120"); +} + +// |maximumFractionDigits| defaults to three. And because |0 !== 3|, a +// RangeError is thrown. +{ + let options = { + roundingIncrement: 10, + // minimumFractionDigits: 0, (default) + // maximumFractionDigits: 3, (default) + }; + assertThrowsInstanceOf(() => new Intl.NumberFormat("en", options), RangeError); +} + +// Invalid values. +for (let roundingIncrement of [-1, 0, Infinity, NaN]){ + let options = { + roundingIncrement, + minimumFractionDigits: 0, + maximumFractionDigits: 0, + }; + assertThrowsInstanceOf(() => new Intl.NumberFormat("en", options), RangeError); +} + +if (typeof reportCompare === "function") + reportCompare(true, true); |