|
4 | 4 | // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
|
5 | 5 | //-------------------------------------------------------------------------------------------------------
|
6 | 6 |
|
7 |
| -const array = new Uint32Array([3, 1, 2]); |
| 7 | +// @ts-check |
| 8 | +/// <reference path="../UnitTestFramework/UnitTestFramework.js" /> |
8 | 9 |
|
9 |
| -// May not throw; See https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort |
10 |
| -array.sort(undefined); |
| 10 | +WScript.LoadScriptFile("../UnitTestFramework/UnitTestFramework.js"); |
11 | 11 |
|
12 |
| -print("pass"); |
| 12 | +const tests = [ |
| 13 | + { |
| 14 | + name: "Uint8Array.prototype.sort basic properties", |
| 15 | + body() { |
| 16 | + assert.areEqual(1, Uint8Array.prototype.sort.length, "Uint8Array.prototype.sort should have length of 1"); |
| 17 | + assert.areEqual("sort", Uint8Array.prototype.sort.name, "Uint8Array.prototype.sort should have name 'sort'"); |
| 18 | + const desc = Object.getOwnPropertyDescriptor(Uint8Array.prototype.__proto__, "sort"); |
| 19 | + assert.isTrue(desc.writable, "Uint8Array.prototype.sort.writable === true"); |
| 20 | + assert.isFalse(desc.enumerable, "Uint8Array.prototype.sort.enumerable === false"); |
| 21 | + assert.isTrue(desc.configurable, "Uint8Array.prototype.sort.configurable === true"); |
| 22 | + assert.areEqual('function', typeof desc.value, "typeof Uint8Array.prototype.sort === 'function'"); |
| 23 | + assert.throws(() => { [].sort("not a function"); }, TypeError); |
| 24 | + assert.throws(() => { [].sort(null); }, TypeError); |
| 25 | + assert.doesNotThrow(() => { [].sort(undefined); }, "Uint8Array.prototype.sort with undefined sort parameter does not throw"); |
| 26 | + } |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "Uint8Array.prototype.sort basic sort cases with arrays of numbers", |
| 30 | + body() { |
| 31 | + const arrayOne = [120, 5, 8, 4, 6, 9, 9, 10, 2, 3]; |
| 32 | + assert.areEqual([10, 120, 2, 3, 4, 5, 6, 8, 9, 9], arrayOne.sort(undefined), "Uint8Array.sort with default comparator should sort based on string ordering"); |
| 33 | + const result = [2, 3, 4, 5, 6, 8, 9, 9, 10, 120]; |
| 34 | + assert.areEqual(result, arrayOne.sort((x, y) => x - y), "Uint8Array.sort with numerical comparator should sort numerically"); |
| 35 | + assert.areEqual(result, arrayOne, "Uint8Array.sort should sort original array as well as return value"); |
| 36 | + assert.areEqual(result, arrayOne.sort((x, y) => { return (x > y) ? 1 : ((x < y) ? -1 : 0); }), "1/-1/0 numerical comparison should be the same as x - y"); |
| 37 | + const arrayTwo = [25, 8, 7, 41]; |
| 38 | + assert.areEqual([41, 25, 8, 7], arrayTwo.sort((a, b) => b - a), "Uint8Array.sort with (a,b)=>b-a should sort descending"); |
| 39 | + assert.areEqual([7, 8, 25, 41], arrayTwo.sort((a, b) => a - b), "Uint8Array.sort with (a,b)=>a-b should sort ascending"); |
| 40 | + assert.areEqual([1, 1.2, 4, 4.8, 12], [1, 1.2, 12, 4.8, 4].sort((a, b) => a - b), "Uint8Array.sort with numerical comparator handles floats correctly"); |
| 41 | + } |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Uint8Array.prototype.sort with a compare function with side effects", |
| 45 | + body() { |
| 46 | + let xyz = 5; |
| 47 | + function setXyz() { xyz = 10; return 0; } |
| 48 | + [].sort(setXyz); |
| 49 | + assert.areEqual(5, xyz, "Uint8Array.sort does not call compare function when length is 0"); |
| 50 | + [1].sort(setXyz) |
| 51 | + assert.areEqual(5, xyz, "Uint8Array.sort does not call compare function when length is 1"); |
| 52 | + [undefined, undefined, undefined, undefined].sort(setXyz); |
| 53 | + assert.areEqual(5, xyz, "Uint8Array.sort does not call compare function when all elements are undefined"); |
| 54 | + [5, undefined, , undefined].sort(setXyz); |
| 55 | + assert.areEqual(5, xyz, "Uint8Array.sort does not call compare function when only one element is defined"); |
| 56 | + [1, 2, undefined].sort(setXyz); |
| 57 | + assert.areEqual(10, xyz, "Uint8Array.sort calls compare function if there is > 1 defined element"); |
| 58 | + } |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "Uint8Array.prototype.sort default comparison should not call valueOf", |
| 62 | + body() { |
| 63 | + valueOf = false; |
| 64 | + const arr = [{ |
| 65 | + valueOf() { valueOf = true; return 0; } |
| 66 | + }, 1, 1, 1,]; |
| 67 | + arr.sort(); |
| 68 | + assert.isFalse(valueOf); |
| 69 | + } |
| 70 | + } |
| 71 | +]; |
| 72 | + |
| 73 | +//assert.areEqual does not work for directly comparing sparse arrays |
| 74 | +function compareSparseArrays(arrayOne, arrayTwo, message) { |
| 75 | + const len = arrayOne.length; |
| 76 | + assert.areEqual(len, arrayTwo.length, message + " lengths are not the same"); |
| 77 | + for (let i = 0; i < len; ++i) { |
| 78 | + assert.areEqual(arrayOne[i], arrayTwo[i], message + " property " + i + " is not the same"); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" }); |
0 commit comments