MCPcopy Index your code
hub / github.com/nodejs/node / TestNumberSort

Function TestNumberSort

deps/v8/test/mjsunit/array-sort.js:12–53  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10
11// Test counter-intuitive default number sorting.
12function TestNumberSort() {
13 var a = [ 200, 45, 7 ];
14
15 // Default sort converts each element to string and orders
16 // lexicographically.
17 a.sort();
18 assertArrayEquals([ 200, 45, 7 ], a);
19 // Sort numbers by value using a compare functions.
20 a.sort(function(x, y) { return x - y; });
21 assertArrayEquals([ 7, 45, 200 ], a);
22
23 // Default sort on negative numbers.
24 a = [-12345,-123,-1234,-123456];
25 a.sort();
26 assertArrayEquals([-123,-1234,-12345,-123456], a);
27
28 // Default sort on negative and non-negative numbers.
29 a = [123456,0,-12345,-123,123,1234,-1234,0,12345,-123456];
30 a.sort();
31 assertArrayEquals([-123,-1234,-12345,-123456,0,0,123,1234,12345,123456], a);
32
33 // Tricky case avoiding integer overflow in Runtime_SmiLexicographicCompare.
34 a = [9, 1000000000].sort();
35 assertArrayEquals([1000000000, 9], a);
36 a = [1000000000, 1].sort();
37 assertArrayEquals([1, 1000000000], a);
38 a = [1000000000, 0].sort();
39 assertArrayEquals([0, 1000000000], a);
40
41 // One string is a prefix of the other.
42 a = [1230, 123].sort();
43 assertArrayEquals([123, 1230], a);
44 a = [1231, 123].sort();
45 assertArrayEquals([123, 1231], a);
46
47 // Default sort on Smis and non-Smis.
48 a = [1000000000, 10000000000, 1000000001, -1000000000, -10000000000, -1000000001];
49 a.sort();
50 assertArrayEquals([-1000000000, -10000000000, -1000000001, 1000000000, 10000000000, 1000000001], a);
51
52 // Other cases are tested implicitly in TestSmiLexicographicCompare.
53}
54
55TestNumberSort();
56

Callers 1

array-sort.jsFile · 0.85

Calls 2

assertArrayEqualsFunction · 0.85
sortMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…