add compare method to SortKey and add tests

This commit is contained in:
Bruno Windels 2019-02-24 19:24:43 +01:00
parent d5b88cbbe6
commit 4d28448920

View file

@ -85,6 +85,15 @@ export default class SortKey {
return minKey; return minKey;
} }
compare(otherKey) {
const gapDiff = this.gapKey - otherKey.gapKey;
if (gapDiff === 0) {
return this.eventKey - otherKey.eventKey;
} else {
return gapDiff;
}
}
toString() { toString() {
return `[${this.gapKey}/${this.eventKey}]`; return `[${this.gapKey}/${this.eventKey}]`;
} }
@ -140,8 +149,7 @@ export function tests() {
a.eventKey = 1; a.eventKey = 1;
b.gapKey = 1; b.gapKey = 1;
b.eventKey = 100000; b.eventKey = 100000;
assert(b < a); assert(a.compare(b) > 0);
assert(a > b);
}, },
test_cmp_eventkey_second(assert) { test_cmp_eventkey_second(assert) {
@ -151,12 +159,11 @@ export function tests() {
a.eventKey = 100000; a.eventKey = 100000;
b.gapKey = 1; b.gapKey = 1;
b.eventKey = 2; b.eventKey = 2;
assert(b.buffer < a.buffer); assert(a.compare(b) > 0);
assert(a.buffer > b.buffer);
}, },
test_cmp_max_larger_than_min(assert) { test_cmp_max_larger_than_min(assert) {
assert(SortKey.minKey < SortKey.maxKey); assert(SortKey.minKey.compare(SortKey.maxKey) < 0);
}, },
test_cmp_gapkey_first_large(assert) { test_cmp_gapkey_first_large(assert) {
@ -171,4 +178,4 @@ export function tests() {
} }
}; };
} }
//#endif //#endif