Skip to content

Commit 4b6329c

Browse files
committed
Restructured tests a bit
1 parent 7f356ff commit 4b6329c

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

src/__tests__/merge-insertion.test.ts

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import mergeInsertionSort, { _binInsertIdx, _groupSizes, _makeGroups, Comparable
1818
fisherYates, mergeInsertionMaxComparisons, permutations, xorshift32 } from '../merge-insertion'
1919
import { describe, expect, test } from '@jest/globals'
2020

21+
const comp :Comparator<string> = ([a,b]) => Promise.resolve(a>b?0:1)
22+
2123
test('smoke', async () => {
2224
expect( await mergeInsertionSort(['D','E','A','C','F','B','G'],
23-
testComp(([a,b]) => Promise.resolve(a>b?0:1), mergeInsertionMaxComparisons(7))) )
25+
testComp(comp, mergeInsertionMaxComparisons(7))) )
2426
.toStrictEqual(['A','B','C','D','E','F','G'])
2527
})
2628

@@ -49,7 +51,6 @@ test('_makeGroups', () => {
4951
})
5052

5153
test('_binInsertIdx', async () => {
52-
const comp :Comparator<string> = ([a,b]) => Promise.resolve(a > b ? 0 : 1)
5354
// A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
5455
const a = ['B','D','F','H','J','L','N','P','R','T','V','X','Z']
5556

@@ -240,13 +241,13 @@ function testComp<T extends Comparable>(comp :Comparator<T>, maxCalls :number, l
240241

241242
test('testComp', async () => {
242243
const log :[string,string][] = []
243-
const comp = testComp(async _ab=>0, 2, log)
244-
await comp(['x','y'])
245-
await comp(['x','z'])
246-
await expect( comp(['x','x']) ).rejects.toThrow('may not be equal')
247-
await expect( comp(['y','x']) ).rejects.toThrow('duplicate comparison')
248-
await expect( comp(['x','y']) ).rejects.toThrow('duplicate comparison')
249-
await expect( comp(['x','z']) ).rejects.toThrow('duplicate comparison')
244+
const c = testComp(async _ab=>0, 2, log)
245+
await c(['x','y'])
246+
await c(['x','z'])
247+
await expect( c(['x','x']) ).rejects.toThrow('may not be equal')
248+
await expect( c(['y','x']) ).rejects.toThrow('duplicate comparison')
249+
await expect( c(['x','y']) ).rejects.toThrow('duplicate comparison')
250+
await expect( c(['x','z']) ).rejects.toThrow('duplicate comparison')
250251
expect(log).toStrictEqual([['x','y'],['x','z']])
251252
//TODO: Reenable: await expect( comp(['i','j']) ).rejects.toThrow('too many')
252253
})
@@ -280,15 +281,16 @@ describe('fisherYates', () => {
280281
})
281282
})
282283

283-
test('mergeInsertionSort', async () => {
284-
const comp :Comparator<string> = ([a,b]) => Promise.resolve(a>b?0:1)
285-
await expect( mergeInsertionSort(['A','B','B'], comp) ).rejects.toThrow('duplicate')
284+
describe('mergeInsertionSort', () => {
285+
286+
test('errors', async () => {
287+
await expect( mergeInsertionSort(['A','B','B'], comp) ).rejects.toThrow('duplicate')
288+
})
286289

287-
// Test many array lengths
288-
for (let len=0; len<100; len++) {
290+
const lengthTable = Array.from({ length: 101 }, (_,i) => ({ len: i }))
291+
test.each(lengthTable)('array length $len', async ({len}) => {
289292
const array :Readonly<string[]> = Array.from({ length: len }, (_,i) => String.fromCharCode(65 + i))
290293
const a = Array.from(array)
291-
//try { //TODO: use describe() or perhaps test.each to add context to test cases
292294
// in order array
293295
expect( await mergeInsertionSort(a, testComp(comp, mergeInsertionMaxComparisons(len))) ).toStrictEqual(array)
294296
// reverse order
@@ -299,24 +301,21 @@ test('mergeInsertionSort', async () => {
299301
fisherYates(a)
300302
expect( await mergeInsertionSort(a, testComp(comp, mergeInsertionMaxComparisons(len))) ).toStrictEqual(array)
301303
}
302-
//} catch (ex) {
303-
// // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
304-
// throw new Error(`${ex} on ${a}`)
305-
//}
306-
}
304+
})
307305

308-
// Test all permutations
309306
// 6! = 720, 7! = 5040, 8! = 40320, 9! = 362880 - already takes a fair amount of time, so don't increase this!
310-
for (let len=0; len<8; len++) {
307+
const permTable = Array.from({ length: 8 }, (_,i) => ({ len: i }))
308+
test.each(permTable)('all permutations of length $len', async ({len}) => {
311309
const array :Readonly<string[]> = Array.from({ length: len }, (_,i) => String.fromCharCode(65 + i))
312310
for (const perm of permutations(array))
313-
// try {
314-
expect( await mergeInsertionSort(perm, testComp(comp, mergeInsertionMaxComparisons(len))) ).toStrictEqual(array)
315-
// } catch (ex) {
316-
// // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
317-
// throw new Error(`${ex} on ${perm}`)
318-
//}
319-
}
311+
try {
312+
expect( await mergeInsertionSort(perm, testComp(comp, mergeInsertionMaxComparisons(len))) ).toStrictEqual(array)
313+
} catch (ex) {
314+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
315+
throw new Error(`${ex} on ${perm.join('')}`)
316+
}
317+
})
318+
320319
})
321320

322321
test('mergeInsertionMaxComparisons', () => {

0 commit comments

Comments
 (0)