aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/lib.test.ts89
-rw-r--r--tests/rbtree.test.ts4
2 files changed, 50 insertions, 43 deletions
diff --git a/tests/lib.test.ts b/tests/lib.test.ts
index 439703f..407d1d1 100644
--- a/tests/lib.test.ts
+++ b/tests/lib.test.ts
@@ -1,18 +1,21 @@
import {
moduloPositive, intToOrdinal, getNumTrailingZeros,
gregorianToJdn, julianToJdn, jdnToGregorian, jdnToJulian, gregorianToJulian, julianToGregorian, getDaysInMonth,
- YearDate, CalDate, boundedDateToStr, eventDatesToStr, HistEvent,
+ YearDate, CalDate, dateToDisplayStr, boundedDateToStr, HistEvent,
queryServer, jsonToHistDate, jsonToHistEvent,
DAY_SCALE, MONTH_SCALE, stepDate, inDateScale, getScaleRatio, getUnitDiff,
getEventPrecision, dateToUnit, dateToScaleDate,
DateRangeTree,
} from '/src/lib.ts'
+// ========== General utility functions ==========
+
test('moduloPositive', () => {
expect(moduloPositive(4, 2)).toBe(0)
expect(moduloPositive(5, 3)).toBe(2)
expect(moduloPositive(-5, 3)).toBe(1)
})
+
test('intToOrdinal', () => {
expect(intToOrdinal(1)).toBe('1st')
expect(intToOrdinal(3)).toBe('3rd')
@@ -20,6 +23,7 @@ test('intToOrdinal', () => {
expect(intToOrdinal(294)).toBe('294th')
expect(intToOrdinal(10301)).toBe('10301st')
})
+
test('getNumTrailingZeros', () => {
expect(getNumTrailingZeros(1)).toBe(0)
expect(getNumTrailingZeros(20)).toBe(1)
@@ -29,40 +33,50 @@ test('getNumTrailingZeros', () => {
expect(getNumTrailingZeros(1e20)).toBe(20)
})
+// ========== For calendar conversion ==========
+
test('gregorianToJdn', () => {
expect(gregorianToJdn(2010, 11, 3)).toBe(2455504)
expect(gregorianToJdn(-4714, 11, 24)).toBe(0)
expect(gregorianToJdn(-1, 1, 1)).toBe(1721060)
})
+
test('julianToJdn', () => {
expect(julianToJdn(2010, 11, 3)).toBe(2455517)
expect(julianToJdn(-4713, 1, 1)).toBe(0)
expect(julianToJdn(-1, 1, 1)).toBe(1721058)
})
+
test('jdnToGregorian', () => {
expect(jdnToGregorian(2455504)).toEqual([2010, 11, 3])
expect(jdnToGregorian(0)).toEqual([-4714, 11, 24])
expect(jdnToGregorian(1721060)).toEqual([-1, 1, 1])
})
+
test('jdnToJulian', () => {
expect(jdnToJulian(2455517)).toEqual([2010, 11, 3])
expect(jdnToJulian(0)).toEqual([-4713, 1, 1])
expect(jdnToJulian(1721058)).toEqual([-1, 1, 1])
})
+
test('gregorianToJulian', () => {
expect(gregorianToJulian(2022, 9, 30)).toEqual([2022, 9, 17])
expect(gregorianToJulian(1616, 5, 3)).toEqual([1616, 4, 23])
})
+
test('julianToGregorian', () => {
expect(julianToGregorian(2022, 9, 17)).toEqual([2022, 9, 30])
expect(julianToGregorian(1616, 4, 23)).toEqual([1616, 5, 3])
})
+
test('getDaysInMonth', () => {
expect(getDaysInMonth(2022, 12)).toBe(31)
expect(getDaysInMonth(2022, 2)).toBe(28)
expect(getDaysInMonth(2000, 2)).toBe(29)
})
+// ========== For date representation ==========
+
describe('YearDate', () => {
test('cmp', () => {
expect((new YearDate(-5000)).equals(new YearDate(-5000))).toBe(true)
@@ -75,6 +89,7 @@ describe('YearDate', () => {
expect((new YearDate(-5000)).getYearDiff(new YearDate(-6000))).toBe(1000)
})
})
+
describe('CalDate', () => {
test('cmp', () => {
expect((new CalDate(2000, 1, 1)).equals(new CalDate(2000, 1, 1))).toBe(true)
@@ -90,21 +105,23 @@ describe('CalDate', () => {
expect((new CalDate(-1, 10, 3)).getYearDiff(new CalDate(1, 1, 1))).toBe(1)
})
})
+
test('toDisplayString', () => {
- expect(new YearDate(-14_000_000_000).toDisplayString()).toBe('14 billion years ago')
- expect(new YearDate(-14_300_000_000).toDisplayString()).toBe('14.3 billion years ago')
- expect(new YearDate( -1_230_000).toDisplayString()).toBe('1.23 million years ago')
- expect(new YearDate( -1_234_567).toDisplayString()).toBe('1.235 million years ago')
- expect(new YearDate( -123_456).toDisplayString()).toBe('123 thousand years ago')
- expect(new YearDate( -9_999).toDisplayString()).toBe('9,999 BC')
- expect(new YearDate( -200).toDisplayString()).toBe('200 BC')
- expect(new YearDate( 1).toDisplayString()).toBe('1 AD')
- expect(new YearDate( 1500).toDisplayString()).toBe('1500')
- expect(new CalDate(2000, 10, 3).toDisplayString()).toBe('3rd Oct 2000')
- expect(new CalDate(-2000, 1, 1).toDisplayString()).toBe('1st Jan 2000 BC')
- expect(new CalDate(1610, 8, 6, false).toDisplayString()).toBe('6th Aug 1610 (OS)')
- expect(new CalDate(-100, 2, 2, false).toDisplayString()).toBe('2nd Feb 100 BC (OS)')
+ expect(dateToDisplayStr(new YearDate(-14_000_000_000))).toBe('14 billion years ago')
+ expect(dateToDisplayStr(new YearDate(-14_300_000_000))).toBe('14.3 billion years ago')
+ expect(dateToDisplayStr(new YearDate( -1_230_000))).toBe('1.23 million years ago')
+ expect(dateToDisplayStr(new YearDate( -1_234_567))).toBe('1.235 million years ago')
+ expect(dateToDisplayStr(new YearDate( -123_456))).toBe('123 thousand years ago')
+ expect(dateToDisplayStr(new YearDate( -9_999))).toBe('9,999 BC')
+ expect(dateToDisplayStr(new YearDate( -200))).toBe('200 BC')
+ expect(dateToDisplayStr(new YearDate( 1))).toBe('1 AD')
+ expect(dateToDisplayStr(new YearDate( 1500))).toBe('1500')
+ expect(dateToDisplayStr(new CalDate(2000, 10, 3))).toBe('3rd Oct 2000')
+ expect(dateToDisplayStr(new CalDate(-2000, 1, 1))).toBe('1st Jan 2000 BC')
+ expect(dateToDisplayStr(new CalDate(1610, 8, 6, false))).toBe('6th Aug 1610 (O.S.)')
+ expect(dateToDisplayStr(new CalDate(-100, 2, 2, false))).toBe('2nd Feb 100 BC (O.S.)')
})
+
test('boundedDateToStr', () => {
// Start and end N billion/million/thousand years ago
expect(boundedDateToStr(new YearDate(-1e9), new YearDate(-1e9))).toBe('1 billion years ago')
@@ -126,39 +143,13 @@ test('boundedDateToStr', () => {
expect(boundedDateToStr(new CalDate(100, 1, 2), new CalDate(100, 10, 3))).toBe('2nd Jan to 3rd Oct 100 AD')
expect(boundedDateToStr(new CalDate(100, 1, 2), new CalDate(100, 1, 3))).toBe('2nd to 3rd Jan 100 AD')
expect(boundedDateToStr(new CalDate(100, 1, 1), new CalDate(100, 1, 31))).toBe('Jan 100 AD')
- expect(boundedDateToStr(new CalDate(100, 1, 1, false), new CalDate(100, 1, 31, false))).toBe('Jan 100 AD (OS)')
+ expect(boundedDateToStr(new CalDate(100, 1, 1, false), new CalDate(100, 1, 31, false))).toBe('Jan 100 AD (O.S.)')
// Other
expect(boundedDateToStr(new CalDate(10, 1, 2), null)).toBe('2nd Jan 10 AD')
expect(boundedDateToStr(new YearDate(-1e7), new CalDate(1610, 3, 2))).toBe('10 million years ago to 2nd Mar 1610')
})
-test('eventDatesToStr', () => {
- // Year-based start and end
- expect(eventDatesToStr(new YearDate(100), new YearDate(500), new YearDate(600)))
- .toEqual(['100 to 500 AD', '600 AD'])
- expect(eventDatesToStr(new YearDate(-2e6), new YearDate(-1e6 - 1), new YearDate(1)))
- .toEqual(['About 2 million years ago', '1 AD'])
- expect(eventDatesToStr(new YearDate(-3e9), null, new YearDate(-1e9), null))
- .toBe('3 to 1 billion years ago')
- expect(eventDatesToStr(new YearDate(-5e6), new YearDate(-5e6), new YearDate(-2.2e6), null))
- .toEqual('5 to 2.2 million years ago')
- expect(eventDatesToStr(new YearDate(1), new YearDate(1000), new YearDate(2001), new YearDate(3000)))
- .toEqual('1st to 3rd millenium')
- expect(eventDatesToStr(new YearDate(-1099), new YearDate(-1000), new YearDate(-499), new YearDate(-400)))
- .toEqual('11th to 5th century BC')
- expect(eventDatesToStr(new YearDate(13), null, new YearDate(300), new YearDate(300)))
- .toEqual('13 to 300 AD')
- // Calendar-based start and end
- expect(eventDatesToStr(new CalDate(1, 1, 1), null, new CalDate(1, 3, 2), null))
- .toEqual('1st Jan to 2nd Mar 1 AD')
- expect(eventDatesToStr(new CalDate(1, 1, 1), null, new CalDate(1, 1, 2), null))
- .toEqual('1st to 2nd Jan 1 AD')
- expect(eventDatesToStr(new CalDate(1670, 9, 1), new CalDate(1670, 9, 30),
- new CalDate(1670, 10, 1), new CalDate(1670, 10, 31)))
- .toEqual('Sep to Oct 1670')
- // Other
- expect(eventDatesToStr(new CalDate(10, 1, 2), null, null, null)).toBe('2nd Jan 10 AD')
- expect(eventDatesToStr(new YearDate(1000), null, new YearDate(1000), null)).toBe('1000 AD')
-})
+
+// ========== For server requests ==========
test('queryServer', async () => {
const oldFetch = fetch
@@ -167,10 +158,12 @@ test('queryServer', async () => {
expect(json).toEqual({test: 'value'})
fetch = oldFetch
})
+
test('jsonToHistDate', () => {
expect(jsonToHistDate({gcal: true, year: 1000, month: 1, day: 10})).toEqual(new CalDate(1000, 1, 10))
expect(jsonToHistDate({gcal: null, year: -5000, month: 1, day: 1})).toEqual(new YearDate(-5000))
})
+
test('jsonToHistEvent', () => {
const jsonEvent = {
id: 3,
@@ -196,6 +189,8 @@ test('jsonToHistEvent', () => {
});
})
+// ========== For dates in a timeline ==========
+
test('stepDate', () => {
expect(stepDate(new CalDate(2000, 1, 1), DAY_SCALE)).toEqual(new CalDate(2000, 1, 2))
expect(stepDate(new CalDate(2000, 1, 2), DAY_SCALE, {forward: false, count: 10})).toEqual(new CalDate(1999, 12, 23))
@@ -203,6 +198,7 @@ test('stepDate', () => {
expect(stepDate(new CalDate(2000, 1, 3), 1, {count: 10})).toEqual(new CalDate(2010, 1, 3))
expect(stepDate(new YearDate(-5000), 1e3, {forward: false, count: 6})).toEqual(new YearDate(-11000))
})
+
test('inDateScale', () => {
expect(inDateScale(new CalDate(100, 2, 3), DAY_SCALE)).toBe(true)
expect(inDateScale(new CalDate(100, 2, 3), MONTH_SCALE)).toBe(false)
@@ -212,18 +208,21 @@ test('inDateScale', () => {
expect(inDateScale(new YearDate(-5000), 1e3)).toBe(true)
expect(inDateScale(new YearDate(-5100), 1e3)).toBe(false)
})
+
test('getScaleRatio', () => {
expect(getScaleRatio(DAY_SCALE, MONTH_SCALE)).toBe(31)
expect(getScaleRatio(MONTH_SCALE, 1)).toBe(12)
expect(getScaleRatio(MONTH_SCALE, 10)).toBe(120)
expect(getScaleRatio(200, 10)).toBeCloseTo(1/20, 5)
})
+
test('getUnitDiff', () => {
expect(getUnitDiff(new CalDate(2000, 1, 1), (new CalDate(2000, 2, 2)), DAY_SCALE)).toBe(32)
expect(getUnitDiff(new CalDate(2000, 10, 10), (new CalDate(2001, 11, 2)), MONTH_SCALE)).toBe(13)
expect(getUnitDiff(new CalDate(-1, 1, 10), (new CalDate(10, 11, 2)), 1)).toBe(10)
expect(getUnitDiff(new YearDate(-5000), (new YearDate(-6500)), 10)).toBe(150)
})
+
test('getEventPrecision', () => {
expect(getEventPrecision(new HistEvent(1, 'one', new YearDate(-5000), new YearDate(-4991)))).toBe(10)
expect(getEventPrecision(new HistEvent(1, 'one', new YearDate(-5000), new YearDate(-4990)))).toBe(100)
@@ -231,6 +230,7 @@ test('getEventPrecision', () => {
expect(getEventPrecision(new HistEvent(1, 'one', new CalDate(1, 2, 3), new CalDate(1, 2, 25)))).toBe(MONTH_SCALE)
expect(getEventPrecision(new HistEvent(1, 'one', new CalDate(1, 2, 3), new CalDate(1, 2, 3)))).toBe(DAY_SCALE)
})
+
test('dateToUnit', () => {
expect(dateToUnit(new CalDate(2013), 1e3)).toBe(2)
expect(dateToUnit(new CalDate(2013), 100)).toBe(20)
@@ -239,6 +239,7 @@ test('dateToUnit', () => {
expect(dateToUnit(new CalDate(1911, 12, 3), MONTH_SCALE)).toBe(gregorianToJdn(1911, 12, 1))
expect(dateToUnit(new CalDate(1911, 12, 3, false), DAY_SCALE)).toBe(julianToJdn(1911, 12, 3))
})
+
test('dateToScaleDate', () => {
expect(dateToScaleDate(new CalDate(2013, 10, 3), DAY_SCALE)).toEqual(new CalDate(2013, 10, 3))
expect(dateToScaleDate(new CalDate(2013, 10, 3, false), MONTH_SCALE)).toEqual(new CalDate(2013, 10, 1))
@@ -248,6 +249,8 @@ test('dateToScaleDate', () => {
expect(dateToScaleDate(new YearDate(-1222333), 1e6)).toEqual(new YearDate(-2000000))
})
+// ========== For DateRangeTree ==========
+
test('DateRangeTree', () => {
const ranges = new DateRangeTree()
ranges.add([new CalDate(100, 1, 1), new CalDate(200, 1, 1)])
diff --git a/tests/rbtree.test.ts b/tests/rbtree.test.ts
index 5b2bc34..edb3135 100644
--- a/tests/rbtree.test.ts
+++ b/tests/rbtree.test.ts
@@ -3,6 +3,7 @@ import {RBTree, Iterator, rbtree_shallow_copy} from '/src/rbtree.ts'
function cmpInt(a: int, b: int){
return a - b;
}
+
function getIteratorEls<T>(itr: Iterator<T>): T[]{
let els: T[] = [];
if (itr.data() != null){
@@ -14,6 +15,7 @@ function getIteratorEls<T>(itr: Iterator<T>): T[]{
}
return els;
}
+
function getIteratorElsRev<T>(itr: Iterator<T>): T[]{
let els: T[] = [];
if (itr.data() != null){
@@ -40,6 +42,7 @@ test('insert and remove', () => {
expect(tree.min()).toBe(-1);
expect(tree.max()).toBe(20);
});
+
test('iteration', () => {
let vals = [10, 10, 20, 5, -1];
let tree = new RBTree(cmpInt);
@@ -51,6 +54,7 @@ test('iteration', () => {
sorted.reverse()
expect(getIteratorElsRev(tree.iterator())).toEqual(sorted);
});
+
test('find', () => {
let tree = new RBTree(cmpInt);
tree.insert(1);