From d9616ed91f1b76a1c2bea0c15efb89a9053faef0 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Fri, 8 Apr 2022 16:30:39 -0700 Subject: [PATCH] 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. --- .../prototype/round/rounding-direction.js | 30 ++++++++++++++++++ .../prototype/toString/rounding-direction.js | 30 ++++++++++++++++++ .../prototype/round/rounding-direction.js | 31 +++++++++++++++++++ .../prototype/toString/rounding-direction.js | 30 ++++++++++++++++++ .../prototype/round/rounding-direction.js | 30 ++++++++++++++++++ .../prototype/toString/rounding-direction.js | 30 ++++++++++++++++++ 6 files changed, 181 insertions(+) create mode 100644 test/built-ins/Temporal/Instant/prototype/round/rounding-direction.js create mode 100644 test/built-ins/Temporal/Instant/prototype/toString/rounding-direction.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/round/rounding-direction.js create mode 100644 test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-direction.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/round/rounding-direction.js create mode 100644 test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-direction.js diff --git a/test/built-ins/Temporal/Instant/prototype/round/rounding-direction.js b/test/built-ins/Temporal/Instant/prototype/round/rounding-direction.js new file mode 100644 index 00000000000..88a6abba4c2 --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/round/rounding-direction.js @@ -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)" +); diff --git a/test/built-ins/Temporal/Instant/prototype/toString/rounding-direction.js b/test/built-ins/Temporal/Instant/prototype/toString/rounding-direction.js new file mode 100644 index 00000000000..f5185d9331d --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/toString/rounding-direction.js @@ -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)" +); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/round/rounding-direction.js b/test/built-ins/Temporal/PlainDateTime/prototype/round/rounding-direction.js new file mode 100644 index 00000000000..5e0e1eaeca4 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/round/rounding-direction.js @@ -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)" +); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-direction.js b/test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-direction.js new file mode 100644 index 00000000000..a4d7dec32d7 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/toString/rounding-direction.js @@ -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)" +); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/round/rounding-direction.js b/test/built-ins/Temporal/ZonedDateTime/prototype/round/rounding-direction.js new file mode 100644 index 00000000000..676841ed26d --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/round/rounding-direction.js @@ -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)" +); diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-direction.js b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-direction.js new file mode 100644 index 00000000000..b376e075bb8 --- /dev/null +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/toString/rounding-direction.js @@ -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)" +);