Skip to content

Commit

Permalink
Temporal: Add tests for new rounding modes
Browse files Browse the repository at this point in the history
See tc39/proposal-temporal#2262 which added new
rounding modes from NumberFormat V3.

These tests use the same format as the previous ones. The tests for the
"half" rounding modes aren't very good yet, as they don't show any of the
differences between the tiebreaking schemes; there aren't any ties in the
data to be broken. (Except in .toString().) A subsequent commit will
correct this.
  • Loading branch information
ptomato authored and Ms2ger committed Sep 21, 2022
1 parent 460a48e commit 5d969ee
Show file tree
Hide file tree
Showing 105 changed files with 3,865 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.round
description: Tests calculations with roundingMode "expand".
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 500, 987);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);

const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 9], [-5, -6, -9]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 31], [-5, -6, -7, -9, -16, -31]],
["seconds", [5, 6, 7, 9, 16, 30, 21], [-5, -6, -7, -9, -16, -30, -21]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 501], [-5, -6, -7, -9, -16, -30, -20, -123, -501]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 500, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -500, -987]],
];

const roundingMode = "expand";

expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.round
description: Tests calculations with roundingMode "halfCeil".
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 500, 987);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);

const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 8], [-5, -6, -8]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 501], [-5, -6, -7, -9, -16, -30, -20, -123, -501]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 500, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -500, -987]],
];

const roundingMode = "halfCeil";

expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.round
description: Tests calculations with roundingMode "halfEven".
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 500, 987);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);

const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 8], [-5, -6, -8]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 501], [-5, -6, -7, -9, -16, -30, -20, -123, -501]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 500, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -500, -987]],
];

const roundingMode = "halfEven";

expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.round
description: Tests calculations with roundingMode "halfFloor".
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 500, 987);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);

const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 8], [-5, -6, -8]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 501], [-5, -6, -7, -9, -16, -30, -20, -123, -501]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 500, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -500, -987]],
];

const roundingMode = "halfFloor";

expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.round
description: Tests calculations with roundingMode "halfTrunc".
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 500, 987);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);

const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 8], [-5, -6, -8]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 501], [-5, -6, -7, -9, -16, -30, -20, -123, -501]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 500, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -500, -987]],
];

const roundingMode = "halfTrunc";

expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.prototype.round
description: Tests calculations with roundingMode "expand".
features: [Temporal]
---*/

const instance = new Temporal.Instant(217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */);

const expected = [
["hour", 217177200_000_000_000n /* 1976-11-18T15:00:00Z */],
["minute", 217175040_000_000_000n /* 1976-11-18T14:24:00Z */],
["second", 217175011_000_000_000n /* 1976-11-18T14:23:31Z */],
["millisecond", 217175010_124_000_000n /* 1976-11-18T14:23:30.124Z */],
["microsecond", 217175010_123_457_000n /* 1976-11-18T14:23:30.123457Z */],
["nanosecond", 217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */],
];

const roundingMode = "expand";

expected.forEach(([smallestUnit, expected]) => {
assert.sameValue(
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
expected,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.prototype.round
description: Tests calculations with roundingMode "halfCeil".
features: [Temporal]
---*/

const instance = new Temporal.Instant(217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */);

const expected = [
["hour", 217173600_000_000_000n /* 1976-11-18T14:00:00Z */],
["minute", 217175040_000_000_000n /* 1976-11-18T14:24:00Z */],
["second", 217175010_000_000_000n /* 1976-11-18T14:23:30Z */],
["millisecond", 217175010_123_000_000n /* 1976-11-18T14:23:30.123Z */],
["microsecond", 217175010_123_457_000n /* 1976-11-18T14:23:30.123457Z */],
["nanosecond", 217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */],
];

const roundingMode = "halfCeil";

expected.forEach(([smallestUnit, expected]) => {
assert.sameValue(
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
expected,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.prototype.round
description: Tests calculations with roundingMode "halfEven".
features: [Temporal]
---*/

const instance = new Temporal.Instant(217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */);

const expected = [
["hour", 217173600_000_000_000n /* 1976-11-18T14:00:00Z */],
["minute", 217175040_000_000_000n /* 1976-11-18T14:24:00Z */],
["second", 217175010_000_000_000n /* 1976-11-18T14:23:30Z */],
["millisecond", 217175010_123_000_000n /* 1976-11-18T14:23:30.123Z */],
["microsecond", 217175010_123_457_000n /* 1976-11-18T14:23:30.123457Z */],
["nanosecond", 217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */],
];

const roundingMode = "halfEven";

expected.forEach(([smallestUnit, expected]) => {
assert.sameValue(
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
expected,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.prototype.round
description: Tests calculations with roundingMode "halfFloor".
features: [Temporal]
---*/

const instance = new Temporal.Instant(217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */);

const expected = [
["hour", 217173600_000_000_000n /* 1976-11-18T14:00:00Z */],
["minute", 217175040_000_000_000n /* 1976-11-18T14:24:00Z */],
["second", 217175010_000_000_000n /* 1976-11-18T14:23:30Z */],
["millisecond", 217175010_123_000_000n /* 1976-11-18T14:23:30.123Z */],
["microsecond", 217175010_123_457_000n /* 1976-11-18T14:23:30.123457Z */],
["nanosecond", 217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */],
];

const roundingMode = "halfFloor";

expected.forEach(([smallestUnit, expected]) => {
assert.sameValue(
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
expected,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.prototype.round
description: Tests calculations with roundingMode "halfTrunc".
features: [Temporal]
---*/

const instance = new Temporal.Instant(217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */);

const expected = [
["hour", 217173600_000_000_000n /* 1976-11-18T14:00:00Z */],
["minute", 217175040_000_000_000n /* 1976-11-18T14:24:00Z */],
["second", 217175010_000_000_000n /* 1976-11-18T14:23:30Z */],
["millisecond", 217175010_123_000_000n /* 1976-11-18T14:23:30.123Z */],
["microsecond", 217175010_123_457_000n /* 1976-11-18T14:23:30.123457Z */],
["nanosecond", 217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */],
];

const roundingMode = "halfTrunc";

expected.forEach(([smallestUnit, expected]) => {
assert.sameValue(
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
expected,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
);
});
Loading

0 comments on commit 5d969ee

Please sign in to comment.