mirror of https://github.com/tc39/test262.git
Add tests for direction of rounding functionality
The round() and toString() methods of Temporal.Instant, PlainDateTime, and ZonedDateTime can round up or down. However, the instance must not be treated as "negative" even when the time is before 1 BCE (years are negative) or before the Unix epoch (epoch nanoseconds are negative). That is, rounding down is always towards the Big Bang, and rounding up is always away from it. Add tests that verify this.
This commit is contained in:
parent
3905c0c80a
commit
d9616ed91f
|
@ -0,0 +1,30 @@
|
|||
// 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: Rounding down is towards the Big Bang, not the epoch or 1 BCE
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.Instant(-65_261_246_399_500_000_000n); // -000099-12-15T12:00:00.5Z
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "floor" }).epochNanoseconds,
|
||||
-65_261_246_400_000_000_000n, // -000099-12-15T12:00:00Z
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode floor)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "trunc" }).epochNanoseconds,
|
||||
-65_261_246_400_000_000_000n, // -000099-12-15T12:00:00Z
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "ceil" }).epochNanoseconds,
|
||||
-65_261_246_399_000_000_000n, // -000099-12-15T12:00:01Z
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "halfExpand" }).epochNanoseconds,
|
||||
-65_261_246_399_000_000_000n, // -000099-12-15T12:00:01Z
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
|
||||
);
|
|
@ -0,0 +1,30 @@
|
|||
// 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.tostring
|
||||
description: Rounding down is towards the Big Bang, not the epoch or 1 BCE
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.Instant(-65_261_246_399_500_000_000n); // -000099-12-15T12:00:00.5Z
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "floor" }),
|
||||
"-000099-12-15T12:00:00Z",
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "trunc" }),
|
||||
"-000099-12-15T12:00:00Z",
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "ceil" }),
|
||||
"-000099-12-15T12:00:01Z",
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "halfExpand" }),
|
||||
"-000099-12-15T12:00:01Z",
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
|
||||
);
|
|
@ -0,0 +1,31 @@
|
|||
// 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: Rounding down is towards the Big Bang, not the epoch or 1 BCE
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.PlainDateTime(-99, 12, 15, 12, 0, 0, 500);
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "floor" }),
|
||||
-99, 12, "M12", 15, 12, 0, 0, 0, 0, 0,
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode floor)"
|
||||
);
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "trunc" }),
|
||||
-99, 12, "M12", 15, 12, 0, 0, 0, 0, 0,
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
|
||||
);
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "ceil" }),
|
||||
-99, 12, "M12", 15, 12, 0, 1, 0, 0, 0,
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
|
||||
);
|
||||
TemporalHelpers.assertPlainDateTime(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "halfExpand" }),
|
||||
-99, 12, "M12", 15, 12, 0, 1, 0, 0, 0,
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
|
||||
);
|
30
test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-direction.js
vendored
Normal file
30
test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-direction.js
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
// 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.tostring
|
||||
description: Rounding down is towards the Big Bang, not the epoch or 1 BCE
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.PlainDateTime(-99, 12, 15, 12, 0, 0, 500);
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "floor" }),
|
||||
"-000099-12-15T12:00:00",
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "trunc" }),
|
||||
"-000099-12-15T12:00:00",
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "ceil" }),
|
||||
"-000099-12-15T12:00:01",
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "halfExpand" }),
|
||||
"-000099-12-15T12:00:01",
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
|
||||
);
|
|
@ -0,0 +1,30 @@
|
|||
// 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: Rounding down is towards the Big Bang, not the epoch or 1 BCE
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.ZonedDateTime(-65_261_246_399_500_000_000n, "UTC"); // -000099-12-15T12:00:00.5Z
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "floor" }).epochNanoseconds,
|
||||
-65_261_246_400_000_000_000n, // -000099-12-15T12:00:00Z
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode floor)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "trunc" }).epochNanoseconds,
|
||||
-65_261_246_400_000_000_000n, // -000099-12-15T12:00:00Z
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "ceil" }).epochNanoseconds,
|
||||
-65_261_246_399_000_000_000n, // -000099-12-15T12:00:01Z
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.round({ smallestUnit: "second", roundingMode: "halfExpand" }).epochNanoseconds,
|
||||
-65_261_246_399_000_000_000n, // -000099-12-15T12:00:01Z
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
|
||||
);
|
30
test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-direction.js
vendored
Normal file
30
test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-direction.js
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
// 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.tostring
|
||||
description: Rounding down is towards the Big Bang, not the epoch or 1 BCE
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const instance = new Temporal.ZonedDateTime(-65_261_246_399_500_000_000n, "UTC"); // -000099-12-15T12:00:00.5Z
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "floor" }),
|
||||
"-000099-12-15T12:00:00+00:00[UTC]",
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "trunc" }),
|
||||
"-000099-12-15T12:00:00+00:00[UTC]",
|
||||
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "ceil" }),
|
||||
"-000099-12-15T12:00:01+00:00[UTC]",
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
|
||||
);
|
||||
assert.sameValue(
|
||||
instance.toString({ smallestUnit: "second", roundingMode: "halfExpand" }),
|
||||
"-000099-12-15T12:00:01+00:00[UTC]",
|
||||
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
|
||||
);
|
Loading…
Reference in New Issue