Skip to content

Commit

Permalink
Temporal: Move some round() and total() tests out of staging
Browse files Browse the repository at this point in the history
Including tests for every possible combination of largest and smallest
unit, for each type of relativeTo (undefined, PlainDate, ZonedDateTime).
  • Loading branch information
ptomato committed Apr 17, 2024
1 parent 4927106 commit e5c7451
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.round
description: Test for all combinations of largestUnit and smallestUnit with relativeTo
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const duration = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
const plainRelativeTo = new Temporal.PlainDate(2000, 1, 1);
const zonedRelativeTo = new Temporal.ZonedDateTime(63072000_000_000_000n /* = 1972-01-01T00Z */, "UTC");

const exactResults = {
years: {
years: [6],
months: [5, 6],
weeks: [5, 6, 1],
days: [5, 6, 0, 10],
hours: [5, 6, 0, 10, 5],
minutes: [5, 6, 0, 10, 5, 5],
seconds: [5, 6, 0, 10, 5, 5, 5],
milliseconds: [5, 6, 0, 10, 5, 5, 5, 5],
microseconds: [5, 6, 0, 10, 5, 5, 5, 5, 5],
nanoseconds: [5, 6, 0, 10, 5, 5, 5, 5, 5, 5],
},
months: {
months: [0, 66],
weeks: [0, 66, 1],
days: [0, 66, 0, 10],
hours: [0, 66, 0, 10, 5],
minutes: [0, 66, 0, 10, 5, 5],
seconds: [0, 66, 0, 10, 5, 5, 5],
milliseconds: [0, 66, 0, 10, 5, 5, 5, 5],
microseconds: [0, 66, 0, 10, 5, 5, 5, 5, 5],
nanoseconds: [0, 66, 0, 10, 5, 5, 5, 5, 5, 5],
},
weeks: {
weeks: [0, 0, 288],
days: [0, 0, 288, 2],
hours: [0, 0, 288, 2, 5],
minutes: [0, 0, 288, 2, 5, 5],
seconds: [0, 0, 288, 2, 5, 5, 5],
milliseconds: [0, 0, 288, 2, 5, 5, 5, 5],
microseconds: [0, 0, 288, 2, 5, 5, 5, 5, 5],
nanoseconds: [0, 0, 288, 2, 5, 5, 5, 5, 5, 5],
},
days: {
days: [0, 0, 0, 2018],
hours: [0, 0, 0, 2018, 5],
minutes: [0, 0, 0, 2018, 5, 5],
seconds: [0, 0, 0, 2018, 5, 5, 5],
milliseconds: [0, 0, 0, 2018, 5, 5, 5, 5],
microseconds: [0, 0, 0, 2018, 5, 5, 5, 5, 5],
nanoseconds: [0, 0, 0, 2018, 5, 5, 5, 5, 5, 5],
},
hours: {
hours: [0, 0, 0, 0, 48437],
minutes: [0, 0, 0, 0, 48437, 5],
seconds: [0, 0, 0, 0, 48437, 5, 5],
milliseconds: [0, 0, 0, 0, 48437, 5, 5, 5],
microseconds: [0, 0, 0, 0, 48437, 5, 5, 5, 5],
nanoseconds: [0, 0, 0, 0, 48437, 5, 5, 5, 5, 5],
},
minutes: {
minutes: [0, 0, 0, 0, 0, 2906225],
seconds: [0, 0, 0, 0, 0, 2906225, 5],
milliseconds: [0, 0, 0, 0, 0, 2906225, 5, 5],
microseconds: [0, 0, 0, 0, 0, 2906225, 5, 5, 5],
nanoseconds: [0, 0, 0, 0, 0, 2906225, 5, 5, 5, 5],
},
seconds: {
seconds: [0, 0, 0, 0, 0, 0, 174373505],
milliseconds: [0, 0, 0, 0, 0, 0, 174373505, 5],
microseconds: [0, 0, 0, 0, 0, 0, 174373505, 5, 5],
nanoseconds: [0, 0, 0, 0, 0, 0, 174373505, 5, 5, 5],
},
milliseconds: {
milliseconds: [0, 0, 0, 0, 0, 0, 0, 174373505005],
microseconds: [0, 0, 0, 0, 0, 0, 0, 174373505005, 5],
nanoseconds: [0, 0, 0, 0, 0, 0, 0, 174373505005, 5, 5],
},
microseconds: {
microseconds: [0, 0, 0, 0, 0, 0, 0, 0, 174373505005005],
nanoseconds: [0, 0, 0, 0, 0, 0, 0, 0, 174373505005005, 5],
},
};
for (const [largestUnit, entry] of Object.entries(exactResults)) {
for (const [smallestUnit, expected] of Object.entries(entry)) {
for (const relativeTo of [plainRelativeTo, zonedRelativeTo]) {
const [y, mon = 0, w = 0, d = 0, h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
TemporalHelpers.assertDuration(
duration.round({ largestUnit, smallestUnit, relativeTo }),
y, mon, w, d, h, min, s, ms, µs, ns,
`Combination of largestUnit ${largestUnit} and smallestUnit ${smallestUnit}, relative to ${relativeTo}`
);
}
}
}

// 174373505005005005 is not a safe integer.
// ℝ(𝔽(174373505005005005)) == 174373505005004992

for (const relativeTo of [plainRelativeTo, zonedRelativeTo]) {
TemporalHelpers.assertDuration(
duration.round({ largestUnit: "nanoseconds", smallestUnit: "nanoseconds", relativeTo }),
0, 0, 0, 0, 0, 0, 0, 0, 0, 174373505005004992,
`Combination of largestUnit nanoseconds and smallestUnit nanoseconds, with precision loss, relative to ${relativeTo}`
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.round
description: Test for all combinations of largestUnit and smallestUnit without relativeTo
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const duration = new Temporal.Duration(0, 0, 0, 5, 5, 5, 5, 5, 5, 5);

const exactResults = {
days: {
days: [5],
hours: [5, 5],
minutes: [5, 5, 5],
seconds: [5, 5, 5, 5],
milliseconds: [5, 5, 5, 5, 5],
microseconds: [5, 5, 5, 5, 5, 5],
nanoseconds: [5, 5, 5, 5, 5, 5, 5],
},
hours: {
hours: [0, 125],
minutes: [0, 125, 5],
seconds: [0, 125, 5, 5],
milliseconds: [0, 125, 5, 5, 5],
microseconds: [0, 125, 5, 5, 5, 5],
nanoseconds: [0, 125, 5, 5, 5, 5, 5],
},
minutes: {
minutes: [0, 0, 7505],
seconds: [0, 0, 7505, 5],
milliseconds: [0, 0, 7505, 5, 5],
microseconds: [0, 0, 7505, 5, 5, 5],
nanoseconds: [0, 0, 7505, 5, 5, 5, 5],
},
seconds: {
seconds: [0, 0, 0, 450305],
milliseconds: [0, 0, 0, 450305, 5],
microseconds: [0, 0, 0, 450305, 5, 5],
nanoseconds: [0, 0, 0, 450305, 5, 5, 5],
},
milliseconds: {
milliseconds: [0, 0, 0, 0, 450305005],
microseconds: [0, 0, 0, 0, 450305005, 5],
nanoseconds: [0, 0, 0, 0, 450305005, 5, 5],
},
microseconds: {
microseconds: [0, 0, 0, 0, 0, 450305005005],
nanoseconds: [0, 0, 0, 0, 0, 450305005005, 5],
},
nanoseconds: {
nanoseconds: [0, 0, 0, 0, 0, 0, 450305005005005],
},
};
for (const [largestUnit, entry] of Object.entries(exactResults)) {
for (const [smallestUnit, expected] of Object.entries(entry)) {
const [d = 0, h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
TemporalHelpers.assertDuration(
duration.round({ largestUnit, smallestUnit }),
0, 0, 0, d, h, min, s, ms, µs, ns,
`Combination of largestUnit ${largestUnit} and smallestUnit ${smallestUnit}`
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.total
description: Test representative result for all units, with relativeTo
features: [Temporal]
---*/

const duration = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
const plainRelativeTo = new Temporal.PlainDate(2000, 1, 1);
const zonedRelativeTo = new Temporal.ZonedDateTime(63072000_000_000_000n /* = 1972-01-01T00Z */, "UTC");

const dayMilliseconds = 24 * 3600 * 1000;
const fullYears = 5;
const fullMonths = fullYears * 12 + 5 + 1;
const fullDays = 366 + 365 + 365 + 365 + 366 + 31 + 28 + 31 + 30 + 31 + 5 * 7 + 5;
const fullMilliseconds = fullDays * dayMilliseconds + 5 * 3600_000 + 5 * 60_000 + 5000 + 5;
const partialDayMilliseconds = fullMilliseconds - fullDays * dayMilliseconds + 0.005005;
const fractionalDay = partialDayMilliseconds / dayMilliseconds;
const partialYearDays = fullDays - (fullYears * 365 + 2);
const fractionalYear = partialYearDays / 365 + fractionalDay / 365;
const fractionalMonths = (10 /* = 2025-07-11 - 2025-07-01 */ * dayMilliseconds + partialDayMilliseconds) / (31 * dayMilliseconds);
const totalResults = {
years: fullYears + fractionalYear,
months: fullMonths + fractionalMonths,
weeks: Math.floor(fullDays / 7) + (2 + fractionalDay) / 7,
days: fullDays + fractionalDay,
hours: fullDays * 24 + partialDayMilliseconds / 3600000,
minutes: fullDays * 24 * 60 + partialDayMilliseconds / 60000,
seconds: fullDays * 24 * 60 * 60 + partialDayMilliseconds / 1000,
milliseconds: fullMilliseconds + 0.005005,
microseconds: fullMilliseconds * 1000 + 5.005,
nanoseconds: fullMilliseconds * 1000000 + 5005
};
for (const [unit, expected] of Object.entries(totalResults)) {
for (const relativeTo of [plainRelativeTo, zonedRelativeTo]) {
assert.sameValue(
duration.total({ unit, relativeTo }), expected,
`Duration.total results for ${unit} relative to ${relativeTo}`
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.total
description: Test representative result for all units, without relativeTo
features: [Temporal]
---*/

const duration = new Temporal.Duration(0, 0, 0, 5, 5, 5, 5, 5, 5, 5);

const dayMilliseconds = 24 * 3600 * 1000;
const fullDays = 5;
const fullMilliseconds = fullDays * dayMilliseconds + 5 * 3600_000 + 5 * 60_000 + 5000 + 5;
const partialDayMilliseconds = fullMilliseconds - fullDays * dayMilliseconds + 0.005005;
const fractionalDay = partialDayMilliseconds / dayMilliseconds;
const totalResults = {
days: fullDays + fractionalDay,
hours: fullDays * 24 + partialDayMilliseconds / 3600000,
minutes: fullDays * 24 * 60 + partialDayMilliseconds / 60000,
seconds: fullDays * 24 * 60 * 60 + partialDayMilliseconds / 1000,
milliseconds: fullMilliseconds + 0.005005,
microseconds: fullMilliseconds * 1000 + 5.005,
nanoseconds: fullMilliseconds * 1000000 + 5005
};
for (const [unit, expected] of Object.entries(totalResults)) {
assert.sameValue(duration.total(unit), expected, `Duration.total results for ${unit}`);
}
97 changes: 0 additions & 97 deletions test/staging/Temporal/Duration/old/round.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,103 +320,6 @@ assert.throws(RangeError, () => d.round({ largestUnit: "nanoseconds" }));
var relativeTo = Temporal.PlainDate.from("2020-01-01");
var fortyDays = Temporal.Duration.from({ days: 40 });
assert.sameValue(`${ fortyDays.round({ smallestUnit: "seconds" }) }`, "P40D");
var roundAndBalanceResults = {
years: {
years: "P6Y",
months: "P5Y6M",
weeks: "P5Y6M1W",
days: "P5Y6M10D",
hours: "P5Y6M10DT5H",
minutes: "P5Y6M10DT5H5M",
seconds: "P5Y6M10DT5H5M5S",
milliseconds: "P5Y6M10DT5H5M5.005S",
microseconds: "P5Y6M10DT5H5M5.005005S",
nanoseconds: "P5Y6M10DT5H5M5.005005005S"
},
months: {
months: "P66M",
weeks: "P66M1W",
days: "P66M10D",
hours: "P66M10DT5H",
minutes: "P66M10DT5H5M",
seconds: "P66M10DT5H5M5S",
milliseconds: "P66M10DT5H5M5.005S",
microseconds: "P66M10DT5H5M5.005005S",
nanoseconds: "P66M10DT5H5M5.005005005S"
},
weeks: {
weeks: "P288W",
days: "P288W2D",
hours: "P288W2DT5H",
minutes: "P288W2DT5H5M",
seconds: "P288W2DT5H5M5S",
milliseconds: "P288W2DT5H5M5.005S",
microseconds: "P288W2DT5H5M5.005005S",
nanoseconds: "P288W2DT5H5M5.005005005S"
},
days: {
days: "P2018D",
hours: "P2018DT5H",
minutes: "P2018DT5H5M",
seconds: "P2018DT5H5M5S",
milliseconds: "P2018DT5H5M5.005S",
microseconds: "P2018DT5H5M5.005005S",
nanoseconds: "P2018DT5H5M5.005005005S"
},
hours: {
hours: "PT48437H",
minutes: "PT48437H5M",
seconds: "PT48437H5M5S",
milliseconds: "PT48437H5M5.005S",
microseconds: "PT48437H5M5.005005S",
nanoseconds: "PT48437H5M5.005005005S"
},
minutes: {
minutes: "PT2906225M",
seconds: "PT2906225M5S",
milliseconds: "PT2906225M5.005S",
microseconds: "PT2906225M5.005005S",
nanoseconds: "PT2906225M5.005005005S"
},
seconds: {
seconds: "PT174373505S",
milliseconds: "PT174373505.005S",
microseconds: "PT174373505.005005S",
nanoseconds: "PT174373505.005005005S"
},
milliseconds: {
milliseconds: "PT174373505.005S",
microseconds: "PT174373505.005005S",
nanoseconds: "PT174373505.005005005S"
}
};
for (var [largestUnit, entry] of Object.entries(roundAndBalanceResults)) {
for (var [smallestUnit, expected] of Object.entries(entry)) {
assert.sameValue(`${ d.round({
largestUnit,
smallestUnit,
relativeTo
}) }`, expected);
}
}
var balanceLosePrecisionResults = {
microseconds: [
"microseconds",
"nanoseconds"
],
nanoseconds: ["nanoseconds"]
};

// Round may lose precision below ms
for (var [largestUnit, entry] of Object.entries(balanceLosePrecisionResults)) {
for (var smallestUnit of entry) {
assert(`${ d.round({
largestUnit,
smallestUnit,
relativeTo
}) }`.startsWith("PT174373505.005"));
}
}

// halfExpand is the default
assert.sameValue(`${ d.round({
Expand Down
Loading

0 comments on commit e5c7451

Please sign in to comment.