Skip to content

Commit ebe75dc

Browse files
committed
Temporal: Update duration tests to cover the largest possible ms, µs, and ns values
1 parent 8634bea commit ebe75dc

4 files changed

Lines changed: 98 additions & 18 deletions

File tree

test/built-ins/Temporal/Duration/prototype/add/result-out-of-range-1.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,26 @@ description: >
88
features: [Temporal]
99
---*/
1010

11-
// Largest temporal unit is "second".
12-
const duration = Temporal.Duration.from({seconds: Number.MAX_SAFE_INTEGER});
11+
const maxSec = Number.MAX_SAFE_INTEGER;
12+
const maxMs = 9_007_199_254_740_991_487;
13+
const maxUs = 9_007_199_254_740_991_475_711;
14+
const maxNs = 9_007_199_254_740_991_463_129_087;
15+
16+
const durations = [
17+
Temporal.Duration.from({seconds: maxSec}),
18+
Temporal.Duration.from({milliseconds: maxMs}),
19+
Temporal.Duration.from({microseconds: maxUs}),
20+
Temporal.Duration.from({nanoseconds: maxNs}),
21+
Temporal.Duration.from({seconds: -maxSec}),
22+
Temporal.Duration.from({milliseconds: -maxMs}),
23+
Temporal.Duration.from({microseconds: -maxUs}),
24+
Temporal.Duration.from({nanoseconds: -maxNs}),
25+
]
26+
27+
for (let duration of durations) {
28+
assert.throws(RangeError, () => {
29+
duration.add(duration);
30+
});
31+
}
32+
1333

14-
assert.throws(RangeError, () => {
15-
duration.add(duration);
16-
});

test/built-ins/Temporal/Duration/prototype/round/out-of-range-when-converting-from-normalized-duration.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,44 @@ description: >
99
features: [Temporal]
1010
---*/
1111

12-
const d = new Temporal.Duration(0, 0, 0, 0, 0, 0, /* s = */ Number.MAX_SAFE_INTEGER, 0, 0, /* ns = */ 999_999_999);
13-
assert.throws(RangeError, () => d.round({
12+
/*
13+
const maxMs = 9_007_199_254_740_991_487;
14+
const maxUs = 9_007_199_254_740_991_475_711;
15+
const maxNs = 9_007_199_254_740_991_463_129_087;
16+
*/
17+
18+
const ms = new Temporal.Duration(0, 0, 0, 0, 0, 0, /* s = */ Number.MAX_SAFE_INTEGER, /* ms = */ 488, 0, 0);
19+
assert.throws(RangeError, () => ms.round({
20+
largestUnit: "nanoseconds",
21+
roundingIncrement: 1,
22+
}), "nanoseconds component after balancing as a float64-representable integer is out of range (maximum milliseconds)");
23+
24+
const us = new Temporal.Duration(0, 0, 0, 0, 0, 0, /* s = */ Number.MAX_SAFE_INTEGER, 0, /* us = */ 475_712, 0);
25+
assert.throws(RangeError, () => ms.round({
26+
largestUnit: "nanoseconds",
27+
roundingIncrement: 1,
28+
}), "nanoseconds component after balancing as a float64-representable integer is out of range (maximum microseconds)");
29+
30+
const ns = new Temporal.Duration(0, 0, 0, 0, 0, 0, /* s = */ Number.MAX_SAFE_INTEGER, 0, 0, /* ns = */ 463_129_088);
31+
assert.throws(RangeError, () => ns.round({
32+
largestUnit: "nanoseconds",
33+
roundingIncrement: 1,
34+
}), "nanoseconds component after balancing as a float64-representable integer is out of range (maximum nanoseconds)");
35+
36+
const msMin = new Temporal.Duration(0, 0, 0, 0, 0, 0, /* s = */ -Number.MAX_SAFE_INTEGER, /* ms = */ -487, 0, 0);
37+
assert.throws(RangeError, () => msMin.round({
38+
largestUnit: "nanoseconds",
39+
roundingIncrement: 1,
40+
}), "nanoseconds component after balancing as a float64-representable integer is out of range (minimum milliseconds)");
41+
42+
const usMin = new Temporal.Duration(0, 0, 0, 0, 0, 0, /* s = */ -Number.MAX_SAFE_INTEGER, 0, /* us = */ -475_711, 0);
43+
assert.throws(RangeError, () => usMin.round({
44+
largestUnit: "nanoseconds",
45+
roundingIncrement: 1,
46+
}), "nanoseconds component after balancing as a float64-representable integer is out of range (minimum microseconds)");
47+
48+
const nsMin = new Temporal.Duration(0, 0, 0, 0, 0, 0, /* s = */ -Number.MAX_SAFE_INTEGER, 0, 0, /* ns = */ -463_129_088);
49+
assert.throws(RangeError, () => nsMin.round({
1450
largestUnit: "nanoseconds",
1551
roundingIncrement: 1,
16-
}), "nanoseconds component after balancing as a float64-representable integer is out of range");
52+
}), "nanoseconds component after balancing as a float64-representable integer is out of range (minimum nanoseconds)");

test/built-ins/Temporal/Duration/prototype/round/total-duration-nanoseconds-too-large-with-zoned-datetime.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ description: >
88
features: [Temporal]
99
---*/
1010

11-
var duration = Temporal.Duration.from({
12-
seconds: Number.MAX_SAFE_INTEGER,
13-
});
11+
const maxMs = 9_007_199_254_740_991_487;
12+
const maxUs = 9_007_199_254_740_991_475_711;
13+
const maxNs = 9_007_199_254_740_991_463_129_087;
14+
15+
const durations = [
16+
Temporal.Duration.from({ seconds: Number.MAX_SAFE_INTEGER }),
17+
Temporal.Duration.from({ milliseconds: maxMs }),
18+
Temporal.Duration.from({ microseconds: maxUs }),
19+
Temporal.Duration.from({ nanoseconds: maxNs }),
20+
Temporal.Duration.from({ seconds: -Number.MAX_SAFE_INTEGER }),
21+
Temporal.Duration.from({ milliseconds: -maxMs }),
22+
Temporal.Duration.from({ microseconds: -maxUs }),
23+
Temporal.Duration.from({ nanoseconds: -maxNs }),
24+
];
1425

1526
var zonedDateTime = new Temporal.ZonedDateTime(0n, "UTC");
1627

@@ -20,4 +31,6 @@ var options = {
2031
relativeTo: zonedDateTime,
2132
};
2233

23-
assert.throws(RangeError, () => duration.round(options));
34+
for (let duration of durations) {
35+
assert.throws(RangeError, () => duration.round(options));
36+
}

test/built-ins/Temporal/Duration/prototype/subtract/result-out-of-range-1.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,24 @@ description: >
88
features: [Temporal]
99
---*/
1010

11-
// Largest temporal unit is "second".
12-
const duration1 = Temporal.Duration.from({seconds: Number.MAX_SAFE_INTEGER});
13-
const duration2 = Temporal.Duration.from({seconds: -Number.MAX_SAFE_INTEGER});
11+
const maxSec = Number.MAX_SAFE_INTEGER;
12+
const maxMs = 9_007_199_254_740_991_487;
13+
const maxUs = 9_007_199_254_740_991_475_711;
14+
const maxNs = 9_007_199_254_740_991_463_129_087;
1415

15-
assert.throws(RangeError, () => {
16-
duration1.subtract(duration2);
17-
});
16+
const durations = [
17+
Temporal.Duration.from({seconds: maxSec}),
18+
Temporal.Duration.from({milliseconds: maxMs}),
19+
Temporal.Duration.from({microseconds: maxUs}),
20+
Temporal.Duration.from({nanoseconds: maxNs}),
21+
Temporal.Duration.from({seconds: -maxSec}),
22+
Temporal.Duration.from({milliseconds: -maxMs}),
23+
Temporal.Duration.from({microseconds: -maxUs}),
24+
Temporal.Duration.from({nanoseconds: -maxNs}),
25+
];
26+
27+
for (let duration of durations) {
28+
assert.throws(RangeError, () => {
29+
duration.subtract(duration.negated());
30+
}, `subtracting the negation of a large duration from the duration is out of bounds: ${duration}`);
31+
}

0 commit comments

Comments
 (0)