mirror of
https://github.com/tc39/test262.git
synced 2025-07-23 05:55:36 +02:00
test for all overflow/underflow scenarios
This commit is contained in:
parent
6c35fba9b2
commit
6c60904d42
@ -40,21 +40,22 @@ class TimeZone extends Temporal.TimeZone {
|
|||||||
#count = 0;
|
#count = 0;
|
||||||
#nanoseconds;
|
#nanoseconds;
|
||||||
|
|
||||||
constructor(nanoseconds) {
|
constructor(todayEpochNanoseconds, tomorrowEpochNanoseconds) {
|
||||||
super("UTC");
|
super("UTC");
|
||||||
this.#nanoseconds = nanoseconds;
|
this.#nanoseconds = [todayEpochNanoseconds, tomorrowEpochNanoseconds];
|
||||||
}
|
}
|
||||||
getPossibleInstantsFor(dateTime) {
|
getPossibleInstantsFor(dateTime) {
|
||||||
if (++this.#count === 2) {
|
const nanoseconds = this.#nanoseconds[this.#count++];
|
||||||
return [new Temporal.Instant(this.#nanoseconds)];
|
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)) {
|
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 zoned = new Temporal.ZonedDateTime(epochNanoseconds, timeZone);
|
||||||
let result = zoned.round({ smallestUnit: "days", roundingMode });
|
let result = zoned.round({ smallestUnit: "days", roundingMode });
|
||||||
assert.sameValue(result.epochNanoseconds, expected);
|
assert.sameValue(result.epochNanoseconds, expected);
|
||||||
@ -63,14 +64,14 @@ function test(epochNanoseconds, tomorrowEpochNanoseconds, testCases) {
|
|||||||
|
|
||||||
const oneDay = 24n * 60n * 60n * 1000n * 1000n * 1000n;
|
const oneDay = 24n * 60n * 60n * 1000n * 1000n * 1000n;
|
||||||
|
|
||||||
test(3n, 10n, {
|
test(3n, undefined, 10n, {
|
||||||
ceil: 10n, // end-of-day according to TimeZone protocol
|
ceil: 10n, // end-of-day according to TimeZone protocol
|
||||||
floor: 0n,
|
floor: 0n,
|
||||||
trunc: 0n,
|
trunc: 0n,
|
||||||
halfExpand: 0n,
|
halfExpand: 0n,
|
||||||
});
|
});
|
||||||
|
|
||||||
test(-3n, 10n, {
|
test(-3n, undefined, 10n, {
|
||||||
ceil: 10n, // end-of-day according to TimeZone protocol
|
ceil: 10n, // end-of-day according to TimeZone protocol
|
||||||
floor: -oneDay,
|
floor: -oneDay,
|
||||||
trunc: -oneDay,
|
trunc: -oneDay,
|
||||||
@ -78,16 +79,19 @@ test(-3n, 10n, {
|
|||||||
});
|
});
|
||||||
|
|
||||||
assert.throws(RangeError, () => {
|
assert.throws(RangeError, () => {
|
||||||
test(-3n, -10n, {
|
test(-3n, 0n, 10n, { ceil: undefined });
|
||||||
ceil: oneDay,
|
}, "instant is before TimeZone protocol's start-of-day");
|
||||||
floor: 0n,
|
|
||||||
trunc: 0n,
|
assert.throws(RangeError, () => {
|
||||||
halfExpand: 0n,
|
test(-3n, undefined, -10n, { ceil: undefined });
|
||||||
});
|
}, "instant is after TimeZone protocol's end-of-day");
|
||||||
}, "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 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
|
ceil: /*INT64_MAX=*/ 9223372036854775807n, // end-of-day according to TimeZone protocol
|
||||||
floor: 0n,
|
floor: 0n,
|
||||||
trunc: 0n,
|
trunc: 0n,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user