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-mode.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-mode.js | 287 |
1 files changed, 287 insertions, 0 deletions
diff --git a/js/src/tests/non262/Intl/NumberFormat/rounding-mode.js b/js/src/tests/non262/Intl/NumberFormat/rounding-mode.js new file mode 100644 index 0000000000..e4b85b0172 --- /dev/null +++ b/js/src/tests/non262/Intl/NumberFormat/rounding-mode.js @@ -0,0 +1,287 @@ +// |reftest| skip-if(!this.hasOwnProperty("Intl")||release_or_beta) + +const tests = [ + // Special values: Zeros and non-finite values. + { + value: 0, + options: {}, + roundingModes: { + ceil: "0", + floor: "0", + expand: "0", + trunc: "0", + halfCeil: "0", + halfFloor: "0", + halfExpand: "0", + halfTrunc: "0", + halfEven: "0", + }, + }, + { + value: -0, + options: {}, + roundingModes: { + ceil: "-0", + floor: "-0", + expand: "-0", + trunc: "-0", + halfCeil: "-0", + halfFloor: "-0", + halfExpand: "-0", + halfTrunc: "-0", + halfEven: "-0", + }, + }, + { + value: -Infinity, + options: {}, + roundingModes: { + ceil: "-∞", + floor: "-∞", + expand: "-∞", + trunc: "-∞", + halfCeil: "-∞", + halfFloor: "-∞", + halfExpand: "-∞", + halfTrunc: "-∞", + halfEven: "-∞", + }, + }, + { + value: Infinity, + options: {}, + roundingModes: { + ceil: "∞", + floor: "∞", + expand: "∞", + trunc: "∞", + halfCeil: "∞", + halfFloor: "∞", + halfExpand: "∞", + halfTrunc: "∞", + halfEven: "∞", + }, + }, + { + value: NaN, + options: {}, + roundingModes: { + ceil: "NaN", + floor: "NaN", + expand: "NaN", + trunc: "NaN", + halfCeil: "NaN", + halfFloor: "NaN", + halfExpand: "NaN", + halfTrunc: "NaN", + halfEven: "NaN", + }, + }, + + // Integer rounding with positive values. + { + value: 0.4, + options: {maximumFractionDigits: 0}, + roundingModes: { + ceil: "1", + floor: "0", + expand: "1", + trunc: "0", + halfCeil: "0", + halfFloor: "0", + halfExpand: "0", + halfTrunc: "0", + halfEven: "0", + }, + }, + { + value: 0.5, + options: {maximumFractionDigits: 0}, + roundingModes: { + ceil: "1", + floor: "0", + expand: "1", + trunc: "0", + halfCeil: "1", + halfFloor: "0", + halfExpand: "1", + halfTrunc: "0", + halfEven: "0", + }, + }, + { + value: 0.6, + options: {maximumFractionDigits: 0}, + roundingModes: { + ceil: "1", + floor: "0", + expand: "1", + trunc: "0", + halfCeil: "1", + halfFloor: "1", + halfExpand: "1", + halfTrunc: "1", + halfEven: "1", + }, + }, + + // Integer rounding with negative values. + { + value: -0.4, + options: {maximumFractionDigits: 0}, + roundingModes: { + ceil: "-0", + floor: "-1", + expand: "-1", + trunc: "-0", + halfCeil: "-0", + halfFloor: "-0", + halfExpand: "-0", + halfTrunc: "-0", + halfEven: "-0", + }, + }, + { + value: -0.5, + options: {maximumFractionDigits: 0}, + roundingModes: { + ceil: "-0", + floor: "-1", + expand: "-1", + trunc: "-0", + halfCeil: "-0", + halfFloor: "-1", + halfExpand: "-1", + halfTrunc: "-0", + halfEven: "-0", + }, + }, + { + value: -0.6, + options: {maximumFractionDigits: 0}, + roundingModes: { + ceil: "-0", + floor: "-1", + expand: "-1", + trunc: "-0", + halfCeil: "-1", + halfFloor: "-1", + halfExpand: "-1", + halfTrunc: "-1", + halfEven: "-1", + }, + }, + + // Fractional digits rounding with positive values. + { + value: 0.04, + options: {maximumFractionDigits: 1}, + roundingModes: { + ceil: "0.1", + floor: "0", + expand: "0.1", + trunc: "0", + halfCeil: "0", + halfFloor: "0", + halfExpand: "0", + halfTrunc: "0", + halfEven: "0", + }, + }, + { + value: 0.05, + options: {maximumFractionDigits: 1}, + roundingModes: { + ceil: "0.1", + floor: "0", + expand: "0.1", + trunc: "0", + halfCeil: "0.1", + halfFloor: "0", + halfExpand: "0.1", + halfTrunc: "0", + halfEven: "0", + }, + }, + { + value: 0.06, + options: {maximumFractionDigits: 1}, + roundingModes: { + ceil: "0.1", + floor: "0", + expand: "0.1", + trunc: "0", + halfCeil: "0.1", + halfFloor: "0.1", + halfExpand: "0.1", + halfTrunc: "0.1", + halfEven: "0.1", + }, + }, + + // Fractional digits rounding with negative values. + { + value: -0.04, + options: {maximumFractionDigits: 1}, + roundingModes: { + ceil: "-0", + floor: "-0.1", + expand: "-0.1", + trunc: "-0", + halfCeil: "-0", + halfFloor: "-0", + halfExpand: "-0", + halfTrunc: "-0", + halfEven: "-0", + }, + }, + { + value: -0.05, + options: {maximumFractionDigits: 1}, + roundingModes: { + ceil: "-0", + floor: "-0.1", + expand: "-0.1", + trunc: "-0", + halfCeil: "-0", + halfFloor: "-0.1", + halfExpand: "-0.1", + halfTrunc: "-0", + halfEven: "-0", + }, + }, + { + value: -0.06, + options: {maximumFractionDigits: 1}, + roundingModes: { + ceil: "-0", + floor: "-0.1", + expand: "-0.1", + trunc: "-0", + halfCeil: "-0.1", + halfFloor: "-0.1", + halfExpand: "-0.1", + halfTrunc: "-0.1", + halfEven: "-0.1", + }, + }, +]; + +for (let {value, options, roundingModes} of tests) { + for (let [roundingMode, expected] of Object.entries(roundingModes)) { + let nf = new Intl.NumberFormat("en", {...options, roundingMode}); + assertEq(nf.format(value), expected, `value=${value}, roundingMode=${roundingMode}`); + assertEq(nf.resolvedOptions().roundingMode, roundingMode); + } +} + +// Default value is "halfExpand". +assertEq(new Intl.NumberFormat().resolvedOptions().roundingMode, "halfExpand"); + +// Invalid values. +for (let roundingMode of ["", null, "halfOdd", "halfUp", "Up", "up"]){ + assertThrowsInstanceOf(() => new Intl.NumberFormat("en", {roundingMode}), RangeError); +} + +if (typeof reportCompare === "function") + reportCompare(true, true); |