Skip to content

Commit f527f86

Browse files
committed
Thorough tests for ISO-like calendar math
Splits test/thorough/gregorian.mjs into two new test files: - calendarisolike.mjs tests that the year and month differences for the four ISO-like calendars are equal to the same differences calculated in the ISO calendar. - calendardaymath.mjs tests that week and day differences for all non- ISO calendars (except, temporarily, 'chinese' and 'dangi') are equal to the same differences calculated in the ISO calendar. (Days are always equivalent, and weeks are always 7 days in all currently supported calendars.) This is in order to test the simplest invariants in other non-ISO calendars and not just 'gregory'.
1 parent 2cee76a commit f527f86

File tree

4 files changed

+105
-44
lines changed

4 files changed

+105
-44
lines changed

polyfill/test/thorough/all.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ cd $(dirname $0)
1515
interpreter=${1-node}
1616
there_were_errors=0
1717
for test in \
18+
calendardaymath \
19+
calendarisolike \
1820
dateaddition \
1921
datedifference \
2022
datetimeaddition \
2123
datetimedifference \
2224
datetimerounding \
2325
durationaddition \
24-
gregorian \
2526
instantaddition \
2627
instantdifference \
2728
instantrounding \
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {
2+
assertDurationsEqual,
3+
assertTemporalEqual,
4+
getProgressBar,
5+
makeDateCases,
6+
temporalImpl as T,
7+
time
8+
} from './support.mjs';
9+
10+
const largestUnits = [{ largestUnit: 'weeks' }, { largestUnit: 'days' }];
11+
// 'chinese' and 'dangi' are temporarily omitted because there are some dates
12+
// that the current version of ICU4C can't handle well enough to calculate the
13+
// snapshots
14+
const calendars = [
15+
'buddhist',
16+
'coptic',
17+
'ethioaa',
18+
'ethiopic',
19+
'gregory',
20+
'hebrew',
21+
'indian',
22+
'islamic-civil',
23+
'islamic-tbla',
24+
'islamic-umalqura',
25+
'japanese',
26+
'persian',
27+
'roc'
28+
];
29+
30+
const interestingCases = makeDateCases();
31+
const total = calendars.length * ((interestingCases.length * (interestingCases.length - 1)) / 2);
32+
33+
await time((start) => {
34+
const progress = getProgressBar(start, total);
35+
36+
for (const calendar of calendars) {
37+
for (const [oneISO, str1] of interestingCases) {
38+
const one = oneISO.withCalendar(calendar);
39+
for (const [twoISO, str2] of interestingCases) {
40+
if (T.PlainDate.compare(oneISO, twoISO) === 1) continue;
41+
progress.tick(1, { test: `${calendar} ${str1} : ${str2}` });
42+
const two = twoISO.withCalendar(calendar);
43+
for (const opts of largestUnits) {
44+
const dif = oneISO.until(twoISO, opts);
45+
assertDurationsEqual(twoISO.since(oneISO, opts), dif, `${str1} and ${str2} symmetric in ${opts.largestUnit}`);
46+
assertDurationsEqual(one.until(two, opts), dif, `${str1} until ${str2} ${opts.largestUnit}`);
47+
assertDurationsEqual(one.since(two, opts), dif.negated(), `${str1} since ${str2} ${opts.largestUnit}`);
48+
assertDurationsEqual(two.until(one, opts), dif.negated(), `${str2} until ${str1} ${opts.largestUnit}`);
49+
assertDurationsEqual(two.since(one, opts), dif, `${str2} since ${str1} ${opts.largestUnit}`);
50+
assertTemporalEqual(one.add(dif), two, `${str1} + ${dif} = ${str2}`);
51+
assertTemporalEqual(one.subtract(dif.negated()), two, `${str1} - -${dif} = ${str2}`);
52+
assertTemporalEqual(two.add(dif.negated()), one, `${str2} + -${dif} = ${str1}`);
53+
assertTemporalEqual(two.subtract(dif), one, `${str2} - ${dif} = ${str1}`);
54+
}
55+
}
56+
}
57+
}
58+
59+
return total;
60+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
assertDurationsEqual,
3+
assertTemporalEqual,
4+
getProgressBar,
5+
makeDateCases,
6+
temporalImpl as T,
7+
time
8+
} from './support.mjs';
9+
10+
const largestUnits = [{ largestUnit: 'years' }, { largestUnit: 'months' }];
11+
const calendars = ['buddhist', 'gregory', 'japanese', 'roc'];
12+
13+
const interestingCases = makeDateCases();
14+
const total = calendars.length * ((interestingCases.length * (interestingCases.length - 1)) / 2);
15+
16+
await time((start) => {
17+
const progress = getProgressBar(start, total);
18+
19+
for (const calendar of calendars) {
20+
for (const [oneISO, str1] of interestingCases) {
21+
const one = oneISO.withCalendar(calendar);
22+
for (const [twoISO, str2] of interestingCases) {
23+
if (T.PlainDate.compare(oneISO, twoISO) === 1) continue;
24+
progress.tick(1, { test: `${calendar} ${str1} : ${str2}` });
25+
const two = twoISO.withCalendar(calendar);
26+
for (const opts of largestUnits) {
27+
const dif1 = oneISO.until(twoISO, opts);
28+
const dif2 = twoISO.since(oneISO, opts);
29+
assertDurationsEqual(one.until(two, opts), dif1, `${str1} until ${str2} ${opts.largestUnit}`);
30+
assertDurationsEqual(one.since(two, opts), dif1.negated(), `${str1} since ${str2} ${opts.largestUnit}`);
31+
assertDurationsEqual(two.until(one, opts), dif2.negated(), `${str2} until ${str1} ${opts.largestUnit}`);
32+
assertDurationsEqual(two.since(one, opts), dif2, `${str2} since ${str1} ${opts.largestUnit}`);
33+
assertTemporalEqual(one.add(dif1), two, `${str1} + ${dif1} = ${str2}`);
34+
assertTemporalEqual(one.subtract(dif1.negated()), two, `${str1} - -${dif1} = ${str2}`);
35+
assertTemporalEqual(two.add(dif2.negated()), one, `${str2} + -${dif2} = ${str1}`);
36+
assertTemporalEqual(two.subtract(dif2), one, `${str2} - ${dif2} = ${str1}`);
37+
}
38+
}
39+
}
40+
}
41+
42+
return total;
43+
});

polyfill/test/thorough/gregorian.mjs

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)