Skip to content

Commit

Permalink
test for all overflow/underflow scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw authored and ptomato committed Apr 9, 2024
1 parent 6c35fba commit 6c60904
Showing 1 changed file with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,22 @@ class TimeZone extends Temporal.TimeZone {
#count = 0;
#nanoseconds;

constructor(nanoseconds) {
constructor(todayEpochNanoseconds, tomorrowEpochNanoseconds) {
super("UTC");
this.#nanoseconds = nanoseconds;
this.#nanoseconds = [todayEpochNanoseconds, tomorrowEpochNanoseconds];
}
getPossibleInstantsFor(dateTime) {
if (++this.#count === 2) {
return [new Temporal.Instant(this.#nanoseconds)];
const nanoseconds = this.#nanoseconds[this.#count++];
if (nanoseconds === undefined) {
return super.getPossibleInstantsFor(dateTime);
}
return super.getPossibleInstantsFor(dateTime);
return [new Temporal.Instant(nanoseconds)];
}
}

function test(epochNanoseconds, tomorrowEpochNanoseconds, testCases) {
function test(epochNanoseconds, todayEpochNanoseconds, tomorrowEpochNanoseconds, testCases) {
for (let [roundingMode, expected] of Object.entries(testCases)) {
let timeZone = new TimeZone(tomorrowEpochNanoseconds);
let timeZone = new TimeZone(todayEpochNanoseconds, tomorrowEpochNanoseconds);
let zoned = new Temporal.ZonedDateTime(epochNanoseconds, timeZone);
let result = zoned.round({ smallestUnit: "days", roundingMode });
assert.sameValue(result.epochNanoseconds, expected);
Expand All @@ -63,31 +64,34 @@ function test(epochNanoseconds, tomorrowEpochNanoseconds, testCases) {

const oneDay = 24n * 60n * 60n * 1000n * 1000n * 1000n;

test(3n, 10n, {
test(3n, undefined, 10n, {
ceil: 10n, // end-of-day according to TimeZone protocol
floor: 0n,
trunc: 0n,
halfExpand: 0n,
});

test(-3n, 10n, {
test(-3n, undefined, 10n, {
ceil: 10n, // end-of-day according to TimeZone protocol
floor: -oneDay,
trunc: -oneDay,
halfExpand: 10n, // end-of-day according to TimeZone protocol
});

assert.throws(RangeError, () => {
test(-3n, -10n, {
ceil: oneDay,
floor: 0n,
trunc: 0n,
halfExpand: 0n,
});
}, "instant is after TimeZone protocol's end-of-day")
test(-3n, 0n, 10n, { ceil: undefined });
}, "instant is before TimeZone protocol's start-of-day");

assert.throws(RangeError, () => {
test(-3n, undefined, -10n, { ceil: undefined });
}, "instant is after TimeZone protocol's end-of-day");

assert.throws(RangeError, () => {
test(0n, 0n, 0n, { ceil: undefined });
}, "instant is within zero-duration day");

// Test values at int64 boundaries.
test(3n, /*INT64_MAX=*/ 9223372036854775807n, {
test(3n, undefined, /*INT64_MAX=*/ 9223372036854775807n, {
ceil: /*INT64_MAX=*/ 9223372036854775807n, // end-of-day according to TimeZone protocol
floor: 0n,
trunc: 0n,
Expand Down

0 comments on commit 6c60904

Please sign in to comment.