forked from tc39/test262
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
6 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
test/built-ins/Temporal/Instant/prototype/round/rounding-direction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)" | ||
); |
30 changes: 30 additions & 0 deletions
30
test/built-ins/Temporal/Instant/prototype/toString/rounding-direction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)" | ||
); |
31 changes: 31 additions & 0 deletions
31
test/built-ins/Temporal/PlainDateTime/prototype/round/rounding-direction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 30 additions & 0 deletions
30
test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-direction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)" | ||
); |
30 changes: 30 additions & 0 deletions
30
test/built-ins/Temporal/ZonedDateTime/prototype/round/rounding-direction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 30 additions & 0 deletions
30
test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-direction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)" | ||
); |