Skip to content

Commit

Permalink
add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jun 5, 2021
1 parent 8b56b38 commit 39919be
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 4 deletions.
64 changes: 62 additions & 2 deletions tests/pure/es.array.sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,69 @@ QUnit.test('Array#sort', assert => {
assert.throws(() => sort([1, 2, 3], null), 'throws on null');
assert.throws(() => sort([1, 2, 3], {}), 'throws on {}');

const expected = Array(516);
let array = Array(516);
assert.deepEqual(sort([1, 3, 2]), [1, 2, 3], '#1');
assert.deepEqual(sort([1, 3, 2, 11]), [1, 11, 2, 3], '#2');

let array = Array(5);
array[0] = 1;
array[2] = 3;
array[4] = 2;
let expected = Array(5);
expected[0] = 1;
expected[1] = 2;
expected[2] = 3;
assert.deepEqual(sort(array), expected, 'holes');

array = 'zyxwvutsrqponMLKJIHGFEDCBA'.split('');
expected = 'ABCDEFGHIJKLMnopqrstuvwxyz'.split('');
assert.deepEqual(sort(array), expected, 'alpha #1');

array = 'ёяюэьыъщшчцхфутсрПОНМЛКЙИЗЖЕДГВБА'.split('');
expected = 'АБВГДЕЖЗИЙКЛМНОПрстуфхцчшщъыьэюяё'.split('');
assert.deepEqual(sort(array), expected, 'alpha #2');

array = [undefined, 1];
assert.notThrows(() => sort(array, () => { throw 1; }), 'undefined #1');
assert.deepEqual(array, [1, undefined], 'undefined #2');

const object = {
valueOf: () => 1,
toString: () => -1,
};

array = {
0: undefined,
1: 2,
2: 1,
3: 'X',
4: -1,
5: 'a',
6: true,
7: object,
8: NaN,
10: Infinity,
length: 11,
};

expected = {
0: -1,
1: object,
2: 1,
3: 2,
4: Infinity,
5: NaN,
6: 'X',
7: 'a',
8: true,
9: undefined,
length: 11,
};

assert.deepEqual(sort(array), expected, 'custom generic');

let index, mod, code, chr, value;
expected = Array(516);
array = Array(516);

for (index = 0; index < 516; index++) {
mod = index % 4;
Expand Down
64 changes: 62 additions & 2 deletions tests/tests/es.array.sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,69 @@ QUnit.test('Array#sort', assert => {
assert.looksNative(sort);
assert.nonEnumerable(Array.prototype, 'sort');

const expected = Array(516);
let array = Array(516);
assert.deepEqual([1, 3, 2].sort(), [1, 2, 3], '#1');
assert.deepEqual([1, 3, 2, 11].sort(), [1, 11, 2, 3], '#2');

let array = Array(5);
array[0] = 1;
array[2] = 3;
array[4] = 2;
let expected = Array(5);
expected[0] = 1;
expected[1] = 2;
expected[2] = 3;
assert.deepEqual(array.sort(), expected, 'holes');

array = 'zyxwvutsrqponMLKJIHGFEDCBA'.split('');
expected = 'ABCDEFGHIJKLMnopqrstuvwxyz'.split('');
assert.deepEqual(array.sort(), expected, 'alpha #1');

array = 'ёяюэьыъщшчцхфутсрПОНМЛКЙИЗЖЕДГВБА'.split('');
expected = 'АБВГДЕЖЗИЙКЛМНОПрстуфхцчшщъыьэюяё'.split('');
assert.deepEqual(array.sort(), expected, 'alpha #2');

array = [undefined, 1];
assert.notThrows(() => array.sort(() => { throw 1; }), 'undefined #1');
assert.deepEqual(array, [1, undefined], 'undefined #2');

const object = {
valueOf: () => 1,
toString: () => -1,
};

array = {
0: undefined,
1: 2,
2: 1,
3: 'X',
4: -1,
5: 'a',
6: true,
7: object,
8: NaN,
10: Infinity,
length: 11,
};

expected = {
0: -1,
1: object,
2: 1,
3: 2,
4: Infinity,
5: NaN,
6: 'X',
7: 'a',
8: true,
9: undefined,
length: 11,
};

assert.deepEqual(sort.call(array), expected, 'custom generic');

let index, mod, code, chr, value;
expected = Array(516);
array = Array(516);

for (index = 0; index < 516; index++) {
mod = index % 4;
Expand Down

0 comments on commit 39919be

Please sign in to comment.