mirror of https://github.com/tc39/test262.git
Temporal: Regularize and expand tests for round() rounding modes
Take all the existing tests for round() calculations using different rounding modes and standardize them. Add tests for Duration, Instant and ZonedDateTime, which were still in the old format in staging.
This commit is contained in:
parent
a788188c2f
commit
4155842c37
|
@ -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 "ceil".
|
||||
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], [-5]],
|
||||
["months", [5, 8], [-5, -7]],
|
||||
["weeks", [5, 6, 9], [-5, -6, -8]],
|
||||
["days", [5, 6, 7, 10], [-5, -6, -7, -9]],
|
||||
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -16]],
|
||||
["minutes", [5, 6, 7, 9, 16, 31], [-5, -6, -7, -9, -16, -30]],
|
||||
["seconds", [5, 6, 7, 9, 16, 30, 21], [-5, -6, -7, -9, -16, -30, -20]],
|
||||
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -123]],
|
||||
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 501], [-5, -6, -7, -9, -16, -30, -20, -123, -500]],
|
||||
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 500, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -500, -987]],
|
||||
];
|
||||
|
||||
const roundingMode = "ceil";
|
||||
|
||||
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)`
|
||||
);
|
||||
});
|
|
@ -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 "floor".
|
||||
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", [5], [-6]],
|
||||
["months", [5, 7], [-5, -8]],
|
||||
["weeks", [5, 6, 8], [-5, -6, -9]],
|
||||
["days", [5, 6, 7, 9], [-5, -6, -7, -10]],
|
||||
["hours", [5, 6, 7, 9, 16], [-5, -6, -7, -9, -17]],
|
||||
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -31]],
|
||||
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -21]],
|
||||
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 123], [-5, -6, -7, -9, -16, -30, -20, -124]],
|
||||
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 500], [-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 = "floor";
|
||||
|
||||
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)`
|
||||
);
|
||||
});
|
|
@ -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 "halfExpand".
|
||||
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 = "halfExpand";
|
||||
|
||||
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)`
|
||||
);
|
||||
});
|
|
@ -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 "trunc".
|
||||
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", [5], [-5]],
|
||||
["months", [5, 7], [-5, -7]],
|
||||
["weeks", [5, 6, 8], [-5, -6, -8]],
|
||||
["days", [5, 6, 7, 9], [-5, -6, -7, -9]],
|
||||
["hours", [5, 6, 7, 9, 16], [-5, -6, -7, -9, -16]],
|
||||
["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, 123], [-5, -6, -7, -9, -16, -30, -20, -123]],
|
||||
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 500], [-5, -6, -7, -9, -16, -30, -20, -123, -500]],
|
||||
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 500, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -500, -987]],
|
||||
];
|
||||
|
||||
const roundingMode = "trunc";
|
||||
|
||||
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)`
|
||||
);
|
||||
});
|
|
@ -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 "ceil".
|
||||
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 = "ceil";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
|
||||
expected,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
|
@ -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 "floor".
|
||||
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", 217174980_000_000_000n /* 1976-11-18T14:23: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_456_000n /* 1976-11-18T14:23:30.123456Z */],
|
||||
["nanosecond", 217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */],
|
||||
];
|
||||
|
||||
const roundingMode = "floor";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
|
||||
expected,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
|
@ -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 "halfExpand".
|
||||
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 = "halfExpand";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
|
||||
expected,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
|
@ -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 "trunc".
|
||||
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", 217174980_000_000_000n /* 1976-11-18T14:23: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_456_000n /* 1976-11-18T14:23:30.123456Z */],
|
||||
["nanosecond", 217175010_123_456_789n /* 1976-11-18T14:23:30.123456789Z */],
|
||||
];
|
||||
|
||||
const roundingMode = "trunc";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
|
||||
expected,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
|
@ -1,30 +0,0 @@
|
|||
// 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.plaindatetime.prototype.round
|
||||
description: Basic checks for ceiling rounding mode
|
||||
features: [Temporal]
|
||||
includes: [temporalHelpers.js]
|
||||
---*/
|
||||
|
||||
const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789);
|
||||
|
||||
const incrementOneCeil = {
|
||||
"day": [1976, 11, "M11", 19, 0, 0, 0, 0, 0, 0],
|
||||
"hour": [1976, 11, "M11", 18, 15, 0, 0, 0, 0, 0],
|
||||
"minute": [1976, 11, "M11", 18, 14, 24, 0, 0, 0, 0],
|
||||
"second": [1976, 11, "M11", 18, 14, 23, 31, 0, 0, 0],
|
||||
"millisecond": [1976, 11, "M11", 18, 14, 23, 30, 124, 0, 0],
|
||||
"microsecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 457, 0],
|
||||
"nanosecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 789]
|
||||
};
|
||||
|
||||
Object.entries(incrementOneCeil).forEach(([smallestUnit, expected]) => {
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
dt.round({ smallestUnit, roundingMode: "ceil" }),
|
||||
...expected,
|
||||
`rounds up to ${smallestUnit} (ceil)`,
|
||||
undefined
|
||||
);
|
||||
});
|
|
@ -0,0 +1,32 @@
|
|||
// 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.plaindatetime.prototype.round
|
||||
description: Tests calculations with roundingMode "ceil".
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789);
|
||||
|
||||
const expected = [
|
||||
["day", [1976, 11, 'M11', 19]],
|
||||
["hour", [1976, 11, 'M11', 18, 15]],
|
||||
["minute", [1976, 11, 'M11', 18, 14, 24]],
|
||||
["second", [1976, 11, 'M11', 18, 14, 23, 31]],
|
||||
["millisecond", [1976, 11, 'M11', 18, 14, 23, 30, 124]],
|
||||
["microsecond", [1976, 11, 'M11', 18, 14, 23, 30, 123, 457]],
|
||||
["nanosecond", [1976, 11, 'M11', 18, 14, 23, 30, 123, 456, 789]],
|
||||
];
|
||||
|
||||
const roundingMode = "ceil";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
const [y, mon, mc, d, h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
instance.round({ smallestUnit, roundingMode }),
|
||||
y, mon, mc, d, h, min, s, ms, µs, ns,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
|
@ -1,29 +0,0 @@
|
|||
// 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.plaindatetime.prototype.round
|
||||
description: Basic checks for the floor rounding mode
|
||||
features: [Temporal]
|
||||
includes: [temporalHelpers.js]
|
||||
---*/
|
||||
|
||||
const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789);
|
||||
|
||||
const incrementOneFloor = {
|
||||
"day": [1976, 11, "M11", 18, 0, 0, 0, 0, 0, 0],
|
||||
"hour": [1976, 11, "M11", 18, 14, 0, 0, 0, 0, 0],
|
||||
"minute": [1976, 11, "M11", 18, 14, 23, 0, 0, 0, 0],
|
||||
"second": [1976, 11, "M11", 18, 14, 23, 30, 0, 0, 0],
|
||||
"millisecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 0, 0],
|
||||
"microsecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 0],
|
||||
"nanosecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 789]
|
||||
};
|
||||
|
||||
Object.entries(incrementOneFloor).forEach(([smallestUnit, expected]) => {
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
dt.round({ smallestUnit, roundingMode: "floor" }),
|
||||
...expected,
|
||||
`rounds down to ${smallestUnit} (floor)`
|
||||
);
|
||||
});
|
|
@ -0,0 +1,32 @@
|
|||
// 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.plaindatetime.prototype.round
|
||||
description: Tests calculations with roundingMode "floor".
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789);
|
||||
|
||||
const expected = [
|
||||
["day", [1976, 11, 'M11', 18]],
|
||||
["hour", [1976, 11, 'M11', 18, 14]],
|
||||
["minute", [1976, 11, 'M11', 18, 14, 23]],
|
||||
["second", [1976, 11, 'M11', 18, 14, 23, 30]],
|
||||
["millisecond", [1976, 11, 'M11', 18, 14, 23, 30, 123]],
|
||||
["microsecond", [1976, 11, 'M11', 18, 14, 23, 30, 123, 456]],
|
||||
["nanosecond", [1976, 11, 'M11', 18, 14, 23, 30, 123, 456, 789]],
|
||||
];
|
||||
|
||||
const roundingMode = "floor";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
const [y, mon, mc, d, h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
instance.round({ smallestUnit, roundingMode }),
|
||||
y, mon, mc, d, h, min, s, ms, µs, ns,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
32
test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-halfExpand.js
vendored
Normal file
32
test/built-ins/Temporal/PlainDateTime/prototype/round/roundingmode-halfExpand.js
vendored
Normal file
|
@ -0,0 +1,32 @@
|
|||
// 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.plaindatetime.prototype.round
|
||||
description: Tests calculations with roundingMode "halfExpand".
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789);
|
||||
|
||||
const expected = [
|
||||
["day", [1976, 11, 'M11', 19]],
|
||||
["hour", [1976, 11, 'M11', 18, 14]],
|
||||
["minute", [1976, 11, 'M11', 18, 14, 24]],
|
||||
["second", [1976, 11, 'M11', 18, 14, 23, 30]],
|
||||
["millisecond", [1976, 11, 'M11', 18, 14, 23, 30, 123]],
|
||||
["microsecond", [1976, 11, 'M11', 18, 14, 23, 30, 123, 457]],
|
||||
["nanosecond", [1976, 11, 'M11', 18, 14, 23, 30, 123, 456, 789]],
|
||||
];
|
||||
|
||||
const roundingMode = "halfExpand";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
const [y, mon, mc, d, h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
instance.round({ smallestUnit, roundingMode }),
|
||||
y, mon, mc, d, h, min, s, ms, µs, ns,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
|
@ -1,30 +0,0 @@
|
|||
// 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.plaindatetime.prototype.round
|
||||
description: Basic checks for half-expand rounding mode
|
||||
features: [Temporal]
|
||||
includes: [temporalHelpers.js]
|
||||
---*/
|
||||
|
||||
const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789);
|
||||
|
||||
const incrementOneNearest = {
|
||||
"day": [1976, 11, "M11", 19, 0, 0, 0, 0, 0, 0],
|
||||
"hour": [1976, 11, "M11", 18, 14, 0, 0, 0, 0, 0],
|
||||
"minute": [1976, 11, "M11", 18, 14, 24, 0, 0, 0, 0],
|
||||
"second": [1976, 11, "M11", 18, 14, 23, 30, 0, 0, 0],
|
||||
"millisecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 0, 0],
|
||||
"microsecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 457, 0],
|
||||
"nanosecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 789]
|
||||
};
|
||||
|
||||
Object.entries(incrementOneNearest).forEach(([smallestUnit, expected]) => {
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
dt.round({ smallestUnit, roundingMode: "halfExpand" }),
|
||||
...expected,
|
||||
`rounds to nearest ${smallestUnit} (half-expand)`,
|
||||
undefined
|
||||
);
|
||||
});
|
|
@ -1,29 +0,0 @@
|
|||
// 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.plaindatetime.prototype.round
|
||||
description: Basic checks for truncation rounding mode
|
||||
features: [Temporal]
|
||||
includes: [temporalHelpers.js]
|
||||
---*/
|
||||
|
||||
const dt = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789);
|
||||
|
||||
const incrementOneFloor = {
|
||||
"day": [1976, 11, "M11", 18, 0, 0, 0, 0, 0, 0],
|
||||
"hour": [1976, 11, "M11", 18, 14, 0, 0, 0, 0, 0],
|
||||
"minute": [1976, 11, "M11", 18, 14, 23, 0, 0, 0, 0],
|
||||
"second": [1976, 11, "M11", 18, 14, 23, 30, 0, 0, 0],
|
||||
"millisecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 0, 0],
|
||||
"microsecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 0],
|
||||
"nanosecond": [1976, 11, "M11", 18, 14, 23, 30, 123, 456, 789]
|
||||
};
|
||||
|
||||
Object.entries(incrementOneFloor).forEach(([smallestUnit, expected]) => {
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
dt.round({ smallestUnit, roundingMode: "trunc" }),
|
||||
...expected,
|
||||
`truncates to ${smallestUnit}`
|
||||
);
|
||||
});
|
|
@ -0,0 +1,32 @@
|
|||
// 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.plaindatetime.prototype.round
|
||||
description: Tests calculations with roundingMode "trunc".
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.PlainDateTime(1976, 11, 18, 14, 23, 30, 123, 456, 789);
|
||||
|
||||
const expected = [
|
||||
["day", [1976, 11, 'M11', 18]],
|
||||
["hour", [1976, 11, 'M11', 18, 14]],
|
||||
["minute", [1976, 11, 'M11', 18, 14, 23]],
|
||||
["second", [1976, 11, 'M11', 18, 14, 23, 30]],
|
||||
["millisecond", [1976, 11, 'M11', 18, 14, 23, 30, 123]],
|
||||
["microsecond", [1976, 11, 'M11', 18, 14, 23, 30, 123, 456]],
|
||||
["nanosecond", [1976, 11, 'M11', 18, 14, 23, 30, 123, 456, 789]],
|
||||
];
|
||||
|
||||
const roundingMode = "trunc";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
const [y, mon, mc, d, h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
instance.round({ smallestUnit, roundingMode }),
|
||||
y, mon, mc, d, h, min, s, ms, µs, ns,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
|
@ -4,33 +4,28 @@
|
|||
/*---
|
||||
esid: sec-temporal.plaintime.prototype.round
|
||||
description: Tests calculations with roundingMode "ceil".
|
||||
features: [Temporal]
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const plainTime = Temporal.PlainTime.from("13:46:23.123456789");
|
||||
const instance = new Temporal.PlainTime(13, 46, 23, 123, 456, 789);
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "hour", roundingMode: "ceil" }),
|
||||
14, 0, 0, 0, 0, 0, "hour");
|
||||
const expected = [
|
||||
["hour", [14]],
|
||||
["minute", [13, 47]],
|
||||
["second", [13, 46, 24]],
|
||||
["millisecond", [13, 46, 23, 124]],
|
||||
["microsecond", [13, 46, 23, 123, 457]],
|
||||
["nanosecond", [13, 46, 23, 123, 456, 789]],
|
||||
];
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "minute", roundingMode: "ceil" }),
|
||||
13, 47, 0, 0, 0, 0, "minute");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "second", roundingMode: "ceil" }),
|
||||
13, 46, 24, 0, 0, 0, "second");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "millisecond", roundingMode: "ceil" }),
|
||||
13, 46, 23, 124, 0, 0, "millisecond");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "microsecond", roundingMode: "ceil" }),
|
||||
13, 46, 23, 123, 457, 0, "microsecond");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "nanosecond", roundingMode: "ceil" }),
|
||||
13, 46, 23, 123, 456, 789, "nanosecond");
|
||||
const roundingMode = "ceil";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
const [h, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
|
||||
TemporalHelpers.assertPlainTime(
|
||||
instance.round({ smallestUnit, roundingMode }),
|
||||
h, min, s, ms, µs, ns,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
||||
|
|
|
@ -4,32 +4,28 @@
|
|||
/*---
|
||||
esid: sec-temporal.plaintime.prototype.round
|
||||
description: Tests calculations with roundingMode "floor".
|
||||
features: [Temporal]
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const plainTime = Temporal.PlainTime.from("13:46:23.123456789");
|
||||
const instance = new Temporal.PlainTime(13, 46, 23, 123, 456, 789);
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "hour", roundingMode: "floor" }),
|
||||
13, 0, 0, 0, 0, 0, "hour");
|
||||
const expected = [
|
||||
["hour", [13]],
|
||||
["minute", [13, 46]],
|
||||
["second", [13, 46, 23]],
|
||||
["millisecond", [13, 46, 23, 123]],
|
||||
["microsecond", [13, 46, 23, 123, 456]],
|
||||
["nanosecond", [13, 46, 23, 123, 456, 789]],
|
||||
];
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "minute", roundingMode: "floor" }),
|
||||
13, 46, 0, 0, 0, 0, "minute");
|
||||
const roundingMode = "floor";
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "second", roundingMode: "floor" }),
|
||||
13, 46, 23, 0, 0, 0, "second");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "millisecond", roundingMode: "floor" }),
|
||||
13, 46, 23, 123, 0, 0, "millisecond");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "microsecond", roundingMode: "floor" }),
|
||||
13, 46, 23, 123, 456, 0, "microsecond");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "nanosecond", roundingMode: "floor" }),
|
||||
13, 46, 23, 123, 456, 789, "nanosecond");
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
const [h, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
|
||||
TemporalHelpers.assertPlainTime(
|
||||
instance.round({ smallestUnit, roundingMode }),
|
||||
h, min, s, ms, µs, ns,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
||||
|
|
|
@ -3,33 +3,29 @@
|
|||
|
||||
/*---
|
||||
esid: sec-temporal.plaintime.prototype.round
|
||||
description: Tests calculations with roundingMode "floor".
|
||||
features: [Temporal]
|
||||
description: Tests calculations with roundingMode "halfExpand".
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const plainTime = Temporal.PlainTime.from("13:46:23.123456789");
|
||||
const instance = new Temporal.PlainTime(13, 46, 23, 123, 456, 789);
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "hour", roundingMode: "halfExpand" }),
|
||||
14, 0, 0, 0, 0, 0, "hour");
|
||||
const expected = [
|
||||
["hour", [14]],
|
||||
["minute", [13, 46]],
|
||||
["second", [13, 46, 23]],
|
||||
["millisecond", [13, 46, 23, 123]],
|
||||
["microsecond", [13, 46, 23, 123, 457]],
|
||||
["nanosecond", [13, 46, 23, 123, 456, 789]],
|
||||
];
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "minute", roundingMode: "halfExpand" }),
|
||||
13, 46, 0, 0, 0, 0, "minute");
|
||||
const roundingMode = "halfExpand";
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "second", roundingMode: "halfExpand" }),
|
||||
13, 46, 23, 0, 0, 0, "second");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "millisecond", roundingMode: "halfExpand" }),
|
||||
13, 46, 23, 123, 0, 0, "millisecond");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "microsecond", roundingMode: "halfExpand" }),
|
||||
13, 46, 23, 123, 457, 0, "microsecond");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "nanosecond", roundingMode: "halfExpand" }),
|
||||
13, 46, 23, 123, 456, 789, "nanosecond");
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
const [h, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
|
||||
TemporalHelpers.assertPlainTime(
|
||||
instance.round({ smallestUnit, roundingMode }),
|
||||
h, min, s, ms, µs, ns,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
||||
|
|
|
@ -4,32 +4,28 @@
|
|||
/*---
|
||||
esid: sec-temporal.plaintime.prototype.round
|
||||
description: Tests calculations with roundingMode "trunc".
|
||||
features: [Temporal]
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const plainTime = Temporal.PlainTime.from("13:46:23.123456789");
|
||||
const instance = new Temporal.PlainTime(13, 46, 23, 123, 456, 789);
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "hour", roundingMode: "trunc" }),
|
||||
13, 0, 0, 0, 0, 0, "hour");
|
||||
const expected = [
|
||||
["hour", [13]],
|
||||
["minute", [13, 46]],
|
||||
["second", [13, 46, 23]],
|
||||
["millisecond", [13, 46, 23, 123]],
|
||||
["microsecond", [13, 46, 23, 123, 456]],
|
||||
["nanosecond", [13, 46, 23, 123, 456, 789]],
|
||||
];
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "minute", roundingMode: "trunc" }),
|
||||
13, 46, 0, 0, 0, 0, "minute");
|
||||
const roundingMode = "trunc";
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "second", roundingMode: "trunc" }),
|
||||
13, 46, 23, 0, 0, 0, "second");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "millisecond", roundingMode: "trunc" }),
|
||||
13, 46, 23, 123, 0, 0, "millisecond");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "microsecond", roundingMode: "trunc" }),
|
||||
13, 46, 23, 123, 456, 0, "microsecond");
|
||||
|
||||
TemporalHelpers.assertPlainTime(
|
||||
plainTime.round({ smallestUnit: "nanosecond", roundingMode: "trunc" }),
|
||||
13, 46, 23, 123, 456, 789, "nanosecond");
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
const [h, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
|
||||
TemporalHelpers.assertPlainTime(
|
||||
instance.round({ smallestUnit, roundingMode }),
|
||||
h, min, s, ms, µs, ns,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
||||
|
|
|
@ -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.zoneddatetime.prototype.round
|
||||
description: Tests calculations with roundingMode "ceil".
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.ZonedDateTime(217175010_123_456_789n /* 1976-11-18T15:23:30.123456789+01:00 */, "+01:00");
|
||||
|
||||
const expected = [
|
||||
["day", 217206000_000_000_000n /* 1976-11-19T00:00:00+01:00 */],
|
||||
["minute", 217175040_000_000_000n /* 1976-11-18T15:24:00+01:00 */],
|
||||
["second", 217175011_000_000_000n /* 1976-11-18T15:23:31+01:00 */],
|
||||
["millisecond", 217175010_124_000_000n /* 1976-11-18T15:23:30.124+01:00 */],
|
||||
["microsecond", 217175010_123_457_000n /* 1976-11-18T15:23:30.123457+01:00 */],
|
||||
["nanosecond", 217175010_123_456_789n /* 1976-11-18T15:23:30.123456789+01:00 */],
|
||||
];
|
||||
|
||||
const roundingMode = "ceil";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
|
||||
expected,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
|
@ -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.zoneddatetime.prototype.round
|
||||
description: Tests calculations with roundingMode "floor".
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.ZonedDateTime(217175010_123_456_789n /* 1976-11-18T15:23:30.123456789+01:00 */, "+01:00");
|
||||
|
||||
const expected = [
|
||||
["day", 217119600_000_000_000n /* 1976-11-18T00:00:00+01:00 */],
|
||||
["minute", 217174980_000_000_000n /* 1976-11-18T15:23:00+01:00 */],
|
||||
["second", 217175010_000_000_000n /* 1976-11-18T15:23:30+01:00 */],
|
||||
["millisecond", 217175010_123_000_000n /* 1976-11-18T15:23:30.123+01:00 */],
|
||||
["microsecond", 217175010_123_456_000n /* 1976-11-18T15:23:30.123456+01:00 */],
|
||||
["nanosecond", 217175010_123_456_789n /* 1976-11-18T15:23:30.123456789+01:00 */],
|
||||
];
|
||||
|
||||
const roundingMode = "floor";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
|
||||
expected,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
29
test/built-ins/Temporal/ZonedDateTime/prototype/round/roundingmode-halfExpand.js
vendored
Normal file
29
test/built-ins/Temporal/ZonedDateTime/prototype/round/roundingmode-halfExpand.js
vendored
Normal file
|
@ -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.zoneddatetime.prototype.round
|
||||
description: Tests calculations with roundingMode "halfExpand".
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.ZonedDateTime(217175010_123_456_789n /* 1976-11-18T15:23:30.123456789+01:00 */, "+01:00");
|
||||
|
||||
const expected = [
|
||||
["day", 217206000_000_000_000n /* 1976-11-19T00:00:00+01:00 */],
|
||||
["minute", 217175040_000_000_000n /* 1976-11-18T15:24:00+01:00 */],
|
||||
["second", 217175010_000_000_000n /* 1976-11-18T15:23:30+01:00 */],
|
||||
["millisecond", 217175010_123_000_000n /* 1976-11-18T15:23:30.123+01:00 */],
|
||||
["microsecond", 217175010_123_457_000n /* 1976-11-18T15:23:30.123457+01:00 */],
|
||||
["nanosecond", 217175010_123_456_789n /* 1976-11-18T15:23:30.123456789+01:00 */],
|
||||
];
|
||||
|
||||
const roundingMode = "halfExpand";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
|
||||
expected,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
|
@ -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.zoneddatetime.prototype.round
|
||||
description: Tests calculations with roundingMode "trunc".
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.ZonedDateTime(217175010_123_456_789n /* 1976-11-18T15:23:30.123456789+01:00 */, "+01:00");
|
||||
|
||||
const expected = [
|
||||
["day", 217119600_000_000_000n /* 1976-11-18T00:00:00+01:00 */],
|
||||
["minute", 217174980_000_000_000n /* 1976-11-18T15:23:00+01:00 */],
|
||||
["second", 217175010_000_000_000n /* 1976-11-18T15:23:30+01:00 */],
|
||||
["millisecond", 217175010_123_000_000n /* 1976-11-18T15:23:30.123+01:00 */],
|
||||
["microsecond", 217175010_123_456_000n /* 1976-11-18T15:23:30.123456+01:00 */],
|
||||
["nanosecond", 217175010_123_456_789n /* 1976-11-18T15:23:30.123456789+01:00 */],
|
||||
];
|
||||
|
||||
const roundingMode = "trunc";
|
||||
|
||||
expected.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit, roundingMode }).epochNanoseconds,
|
||||
expected,
|
||||
`rounds to ${smallestUnit} (roundingMode = ${roundingMode})`
|
||||
);
|
||||
});
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
/*---
|
||||
esid: sec-temporal-duration-objects
|
||||
description: Temporal.Duration.prototype.round() works as expected
|
||||
description: Temporal.Duration.prototype.round() works as expected
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
|
@ -39,7 +39,7 @@ var hours12 = new Temporal.Duration(0, 0, 0, 0, 12);
|
|||
|
||||
// relativeTo affects days if ZonedDateTime, and duration encompasses DST change"
|
||||
|
||||
// start inside repeated hour, end after",
|
||||
// start inside repeated hour, end after",
|
||||
assert.sameValue(`${ hours25.round({
|
||||
largestUnit: "days",
|
||||
relativeTo: inRepeatedHour
|
||||
|
@ -49,7 +49,7 @@ assert.sameValue(`${ oneDay.round({
|
|||
relativeTo: inRepeatedHour
|
||||
}) }`, "PT25H");
|
||||
|
||||
// start after repeated hour, end inside (negative)",
|
||||
// start after repeated hour, end inside (negative)",
|
||||
var relativeTo = Temporal.ZonedDateTime.from("2019-11-04T01:00[America/Vancouver]");
|
||||
assert.sameValue(`${ hours25.negated().round({
|
||||
largestUnit: "days",
|
||||
|
@ -60,7 +60,7 @@ assert.sameValue(`${ oneDay.negated().round({
|
|||
relativeTo
|
||||
}) }`, "-PT25H");
|
||||
|
||||
// start inside repeated hour, end in skipped hour",
|
||||
// start inside repeated hour, end in skipped hour",
|
||||
assert.sameValue(`${ Temporal.Duration.from({
|
||||
days: 126,
|
||||
hours: 1
|
||||
|
@ -76,7 +76,7 @@ assert.sameValue(`${ Temporal.Duration.from({
|
|||
relativeTo: inRepeatedHour
|
||||
}) }`, "PT3026H");
|
||||
|
||||
// start in normal hour, end in skipped hour",
|
||||
// start in normal hour, end in skipped hour",
|
||||
var relativeTo = Temporal.ZonedDateTime.from("2019-03-09T02:30[America/Vancouver]");
|
||||
assert.sameValue(`${ hours25.round({
|
||||
largestUnit: "days",
|
||||
|
@ -87,7 +87,7 @@ assert.sameValue(`${ oneDay.round({
|
|||
relativeTo
|
||||
}) }`, "PT24H");
|
||||
|
||||
// start before skipped hour, end >1 day after",
|
||||
// start before skipped hour, end >1 day after",
|
||||
assert.sameValue(`${ hours25.round({
|
||||
largestUnit: "days",
|
||||
relativeTo: skippedHourDay
|
||||
|
@ -97,7 +97,7 @@ assert.sameValue(`${ oneDay.round({
|
|||
relativeTo: skippedHourDay
|
||||
}) }`, "PT23H");
|
||||
|
||||
// start after skipped hour, end >1 day before (negative)",
|
||||
// start after skipped hour, end >1 day before (negative)",
|
||||
var relativeTo = Temporal.ZonedDateTime.from("2019-03-11T00:00[America/Vancouver]");
|
||||
assert.sameValue(`${ hours25.negated().round({
|
||||
largestUnit: "days",
|
||||
|
@ -108,20 +108,20 @@ assert.sameValue(`${ oneDay.negated().round({
|
|||
relativeTo
|
||||
}) }`, "-PT23H");
|
||||
|
||||
// start before skipped hour, end <1 day after",
|
||||
// start before skipped hour, end <1 day after",
|
||||
assert.sameValue(`${ hours12.round({
|
||||
largestUnit: "days",
|
||||
relativeTo: skippedHourDay
|
||||
}) }`, "PT12H");
|
||||
|
||||
// start after skipped hour, end <1 day before (negative)",
|
||||
// start after skipped hour, end <1 day before (negative)",
|
||||
var relativeTo = Temporal.ZonedDateTime.from("2019-03-10T12:00[America/Vancouver]");
|
||||
assert.sameValue(`${ hours12.negated().round({
|
||||
largestUnit: "days",
|
||||
relativeTo
|
||||
}) }`, "-PT12H");
|
||||
|
||||
// start before repeated hour, end >1 day after",
|
||||
// start before repeated hour, end >1 day after",
|
||||
assert.sameValue(`${ hours25.round({
|
||||
largestUnit: "days",
|
||||
relativeTo: repeatedHourDay
|
||||
|
@ -131,7 +131,7 @@ assert.sameValue(`${ oneDay.round({
|
|||
relativeTo: repeatedHourDay
|
||||
}) }`, "PT25H");
|
||||
|
||||
// start after repeated hour, end >1 day before (negative)",
|
||||
// start after repeated hour, end >1 day before (negative)",
|
||||
var relativeTo = Temporal.ZonedDateTime.from("2019-11-04T00:00[America/Vancouver]");
|
||||
assert.sameValue(`${ hours25.negated().round({
|
||||
largestUnit: "days",
|
||||
|
@ -142,20 +142,20 @@ assert.sameValue(`${ oneDay.negated().round({
|
|||
relativeTo
|
||||
}) }`, "-PT25H");
|
||||
|
||||
// start before repeated hour, end <1 day after",
|
||||
// start before repeated hour, end <1 day after",
|
||||
assert.sameValue(`${ hours12.round({
|
||||
largestUnit: "days",
|
||||
relativeTo: repeatedHourDay
|
||||
}) }`, "PT12H");
|
||||
|
||||
// start after repeated hour, end <1 day before (negative)",
|
||||
// start after repeated hour, end <1 day before (negative)",
|
||||
var relativeTo = Temporal.ZonedDateTime.from("2019-11-03T12:00[America/Vancouver]");
|
||||
assert.sameValue(`${ hours12.negated().round({
|
||||
largestUnit: "days",
|
||||
relativeTo
|
||||
}) }`, "-PT12H");
|
||||
|
||||
// Samoa skipped 24 hours",
|
||||
// Samoa skipped 24 hours",
|
||||
var relativeTo = Temporal.ZonedDateTime.from("2011-12-29T12:00-10:00[Pacific/Apia]");
|
||||
assert.sameValue(`${ hours25.round({
|
||||
largestUnit: "days",
|
||||
|
@ -424,36 +424,6 @@ for (var [largestUnit, entry] of Object.entries(balanceLosePrecisionResults)) {
|
|||
}) }`.startsWith("PT174373505.005"));
|
||||
}
|
||||
}
|
||||
var roundingModeResults = {
|
||||
halfExpand: [
|
||||
"P6Y",
|
||||
"-P6Y"
|
||||
],
|
||||
ceil: [
|
||||
"P6Y",
|
||||
"-P5Y"
|
||||
],
|
||||
floor: [
|
||||
"P5Y",
|
||||
"-P6Y"
|
||||
],
|
||||
trunc: [
|
||||
"P5Y",
|
||||
"-P5Y"
|
||||
]
|
||||
};
|
||||
for (var [roundingMode, [posResult, negResult]] of Object.entries(roundingModeResults)) {
|
||||
assert.sameValue(`${ d.round({
|
||||
smallestUnit: "years",
|
||||
relativeTo,
|
||||
roundingMode
|
||||
}) }`, posResult);
|
||||
assert.sameValue(`${ d.negated().round({
|
||||
smallestUnit: "years",
|
||||
relativeTo,
|
||||
roundingMode
|
||||
}) }`, negResult);
|
||||
}
|
||||
|
||||
// halfExpand is the default
|
||||
assert.sameValue(`${ d.round({
|
||||
|
|
|
@ -16,142 +16,6 @@ assert.throws(RangeError, () => inst.round({
|
|||
roundingMode: "ceil"
|
||||
}));
|
||||
|
||||
// halfExpand
|
||||
var incrementOneNearest = [
|
||||
[
|
||||
"hour",
|
||||
"1976-11-18T14:00:00Z"
|
||||
],
|
||||
[
|
||||
"minute",
|
||||
"1976-11-18T14:24:00Z"
|
||||
],
|
||||
[
|
||||
"second",
|
||||
"1976-11-18T14:23:30Z"
|
||||
],
|
||||
[
|
||||
"millisecond",
|
||||
"1976-11-18T14:23:30.123Z"
|
||||
],
|
||||
[
|
||||
"microsecond",
|
||||
"1976-11-18T14:23:30.123457Z"
|
||||
],
|
||||
[
|
||||
"nanosecond",
|
||||
"1976-11-18T14:23:30.123456789Z"
|
||||
]
|
||||
];
|
||||
incrementOneNearest.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(`${ inst.round({
|
||||
smallestUnit,
|
||||
roundingMode: "halfExpand"
|
||||
}) }`, expected);
|
||||
});
|
||||
|
||||
// ceil
|
||||
var incrementOneCeil = [
|
||||
[
|
||||
"hour",
|
||||
"1976-11-18T15:00:00Z"
|
||||
],
|
||||
[
|
||||
"minute",
|
||||
"1976-11-18T14:24:00Z"
|
||||
],
|
||||
[
|
||||
"second",
|
||||
"1976-11-18T14:23:31Z"
|
||||
],
|
||||
[
|
||||
"millisecond",
|
||||
"1976-11-18T14:23:30.124Z"
|
||||
],
|
||||
[
|
||||
"microsecond",
|
||||
"1976-11-18T14:23:30.123457Z"
|
||||
],
|
||||
[
|
||||
"nanosecond",
|
||||
"1976-11-18T14:23:30.123456789Z"
|
||||
]
|
||||
];
|
||||
incrementOneCeil.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(`${ inst.round({
|
||||
smallestUnit,
|
||||
roundingMode: "ceil"
|
||||
}) }`, expected);
|
||||
});
|
||||
|
||||
// floor
|
||||
var incrementOneFloor = [
|
||||
[
|
||||
"hour",
|
||||
"1976-11-18T14:00:00Z"
|
||||
],
|
||||
[
|
||||
"minute",
|
||||
"1976-11-18T14:23:00Z"
|
||||
],
|
||||
[
|
||||
"second",
|
||||
"1976-11-18T14:23:30Z"
|
||||
],
|
||||
[
|
||||
"millisecond",
|
||||
"1976-11-18T14:23:30.123Z"
|
||||
],
|
||||
[
|
||||
"microsecond",
|
||||
"1976-11-18T14:23:30.123456Z"
|
||||
],
|
||||
[
|
||||
"nanosecond",
|
||||
"1976-11-18T14:23:30.123456789Z"
|
||||
]
|
||||
];
|
||||
incrementOneFloor.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(`${ inst.round({
|
||||
smallestUnit,
|
||||
roundingMode: "floor"
|
||||
}) }`, expected);
|
||||
});
|
||||
|
||||
// trunc
|
||||
var incrementOneFloor = [
|
||||
[
|
||||
"hour",
|
||||
"1976-11-18T14:00:00Z"
|
||||
],
|
||||
[
|
||||
"minute",
|
||||
"1976-11-18T14:23:00Z"
|
||||
],
|
||||
[
|
||||
"second",
|
||||
"1976-11-18T14:23:30Z"
|
||||
],
|
||||
[
|
||||
"millisecond",
|
||||
"1976-11-18T14:23:30.123Z"
|
||||
],
|
||||
[
|
||||
"microsecond",
|
||||
"1976-11-18T14:23:30.123456Z"
|
||||
],
|
||||
[
|
||||
"nanosecond",
|
||||
"1976-11-18T14:23:30.123456789Z"
|
||||
]
|
||||
];
|
||||
incrementOneFloor.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(`${ inst.round({
|
||||
smallestUnit,
|
||||
roundingMode: "trunc"
|
||||
}) }`, expected);
|
||||
});
|
||||
|
||||
// rounds to an increment of hours
|
||||
assert.sameValue(`${ inst.round({
|
||||
smallestUnit: "hour",
|
||||
|
|
|
@ -32,118 +32,6 @@ assert.throws(RangeError, () => zdt.round({
|
|||
].forEach(smallestUnit => {
|
||||
assert.throws(RangeError, () => zdt.round(smallestUnit));
|
||||
});
|
||||
var incrementOneNearest = [
|
||||
[
|
||||
"day",
|
||||
"1976-11-19T00:00:00+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"hour",
|
||||
"1976-11-18T15:00:00+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"minute",
|
||||
"1976-11-18T15:24:00+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"second",
|
||||
"1976-11-18T15:23:30+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"millisecond",
|
||||
"1976-11-18T15:23:30.123+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"microsecond",
|
||||
"1976-11-18T15:23:30.123457+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"nanosecond",
|
||||
"1976-11-18T15:23:30.123456789+01:00[Europe/Vienna]"
|
||||
]
|
||||
];
|
||||
incrementOneNearest.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(`${ zdt.round({
|
||||
smallestUnit,
|
||||
roundingMode: "halfExpand"
|
||||
}) }`, expected);
|
||||
});
|
||||
var incrementOneCeil = [
|
||||
[
|
||||
"day",
|
||||
"1976-11-19T00:00:00+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"hour",
|
||||
"1976-11-18T16:00:00+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"minute",
|
||||
"1976-11-18T15:24:00+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"second",
|
||||
"1976-11-18T15:23:31+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"millisecond",
|
||||
"1976-11-18T15:23:30.124+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"microsecond",
|
||||
"1976-11-18T15:23:30.123457+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"nanosecond",
|
||||
"1976-11-18T15:23:30.123456789+01:00[Europe/Vienna]"
|
||||
]
|
||||
];
|
||||
incrementOneCeil.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(`${ zdt.round({
|
||||
smallestUnit,
|
||||
roundingMode: "ceil"
|
||||
}) }`, expected);
|
||||
});
|
||||
var incrementOneFloor = [
|
||||
[
|
||||
"day",
|
||||
"1976-11-18T00:00:00+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"hour",
|
||||
"1976-11-18T15:00:00+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"minute",
|
||||
"1976-11-18T15:23:00+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"second",
|
||||
"1976-11-18T15:23:30+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"millisecond",
|
||||
"1976-11-18T15:23:30.123+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"microsecond",
|
||||
"1976-11-18T15:23:30.123456+01:00[Europe/Vienna]"
|
||||
],
|
||||
[
|
||||
"nanosecond",
|
||||
"1976-11-18T15:23:30.123456789+01:00[Europe/Vienna]"
|
||||
]
|
||||
];
|
||||
incrementOneFloor.forEach(([smallestUnit, expected]) => {
|
||||
assert.sameValue(`${ zdt.round({
|
||||
smallestUnit,
|
||||
roundingMode: "floor"
|
||||
}) }`, expected);
|
||||
assert.sameValue(`${ zdt.round({
|
||||
smallestUnit,
|
||||
roundingMode: "trunc"
|
||||
}) }`, expected);
|
||||
});
|
||||
|
||||
// rounds to an increment of hours
|
||||
assert.sameValue(`${ zdt.round({
|
||||
|
|
Loading…
Reference in New Issue