Update test and snippet description with easier to understand examples

This commit is contained in:
simov
2018-02-07 23:08:16 +02:00
parent bc6c8633c1
commit b82e4c5cf1
2 changed files with 90 additions and 16 deletions

View File

@ -10,16 +10,44 @@ test('Testing stableSort', (t) => {
//t.false(stableSort(args..), 'Expected');
//t.throws(stableSort(args..), 'Expected');
// test if js engine's Array#sort implementation is stable
// https://gist.github.com/leeoniya/5816476
const str = 'abcdefghijklmnopqrstuvwxyz';
const compare = (a, b) => ~~(str.indexOf(b) / 2.3) - ~~(str.indexOf(a) / 2.3);
// sorted by weight
const input = [
{ height: 100, weight: 80 },
{ height: 90, weight: 90 },
{ height: 70, weight: 95 },
{ height: 100, weight: 100 },
{ height: 80, weight: 110 },
{ height: 110, weight: 115 },
{ height: 100, weight: 120 },
{ height: 70, weight: 125 },
{ height: 70, weight: 130 },
{ height: 100, weight: 135 },
{ height: 75, weight: 140 },
{ height: 70, weight: 140 }
]
const input = str.split('');
const output = stableSort(input, compare);
// sorted by height (using stableSort)
const target = [
{ height: 70, weight: 95 },
{ height: 70, weight: 125 },
{ height: 70, weight: 130 },
{ height: 70, weight: 140 },
{ height: 75, weight: 140 },
{ height: 80, weight: 110 },
{ height: 90, weight: 90 },
{ height: 100, weight: 80 },
{ height: 100, weight: 100 },
{ height: 100, weight: 120 },
{ height: 100, weight: 135 },
{ height: 110, weight: 115 }
]
t.equal(output.join(''), 'xyzvwtursopqmnklhijfgdeabc');
t.notDeepEqual(input, output);
const compare = (a, b) => a.height - b.height;
// stable
t.deepEqual(stableSort(input, compare), target);
// unstable
t.notDeepEqual(input.sort(compare), target);
t.end();
});