OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 a = [1230, 123].sort(); | 63 a = [1230, 123].sort(); |
64 assertArrayEquals([123, 1230], a); | 64 assertArrayEquals([123, 1230], a); |
65 a = [1231, 123].sort(); | 65 a = [1231, 123].sort(); |
66 assertArrayEquals([123, 1231], a); | 66 assertArrayEquals([123, 1231], a); |
67 | 67 |
68 // Default sort on Smis and non-Smis. | 68 // Default sort on Smis and non-Smis. |
69 a = [1000000000, 10000000000, 1000000001, -1000000000, -10000000000, -10000000
01]; | 69 a = [1000000000, 10000000000, 1000000001, -1000000000, -10000000000, -10000000
01]; |
70 a.sort(); | 70 a.sort(); |
71 assertArrayEquals([-1000000000, -10000000000, -1000000001, 1000000000, 1000000
0000, 1000000001], a); | 71 assertArrayEquals([-1000000000, -10000000000, -1000000001, 1000000000, 1000000
0000, 1000000001], a); |
72 | 72 |
| 73 // Other cases are tested implicitly in TestSmiLexicographicCompare. |
| 74 } |
73 | 75 |
74 for (var xb = 1; xb <= 1000 * 1000 * 1000; xb *= 10) { | 76 TestNumberSort(); |
| 77 |
| 78 function TestSmiLexicographicCompare() { |
| 79 |
| 80 assertFalse(%_IsSmi(2147483648), 'Update test for >32 bit Smi'); |
| 81 |
| 82 // Collect a list of interesting Smis. |
| 83 var seen = {}; |
| 84 var smis = []; |
| 85 function add(x) { |
| 86 if (x | 0 == x) { |
| 87 x = x | 0; // Canonicalizes to Smi if 32-bit signed and fits in Smi. |
| 88 } |
| 89 if (%_IsSmi(x) && !seen[x]) { |
| 90 seen[x] = 1; |
| 91 smis.push(x); |
| 92 } |
| 93 } |
| 94 function addSigned(x) { |
| 95 add(x); |
| 96 add(-x); |
| 97 } |
| 98 |
| 99 var BIGGER_THAN_ANY_SMI = 10 * 1000 * 1000 * 1000; |
| 100 for (var xb = 1; xb <= BIGGER_THAN_ANY_SMI; xb *= 10) { |
75 for (var xf = 0; xf <= 9; xf++) { | 101 for (var xf = 0; xf <= 9; xf++) { |
76 for (var xo = -1; xo <= 1; xo++) { | 102 for (var xo = -1; xo <= 1; xo++) { |
77 for (var yb = 1; yb <= 1000 * 1000 * 1000; yb *= 10) { | 103 addSigned(xb * xf + xo); |
78 for (var yf = 0; yf <= 9; yf++) { | |
79 for (var yo = -1; yo <= 1; yo++) { | |
80 var x = xb * xf + xo; | |
81 var y = yb * yf + yo; | |
82 if (!%_IsSmi(x)) continue; | |
83 if (!%_IsSmi(y)) continue; | |
84 var lex = %SmiLexicographicCompare(x, y); | |
85 if (lex < 0) lex = -1; | |
86 if (lex > 0) lex = 1; | |
87 assertEquals(lex, (x == y) ? 0 : ((x + "") < (y + "") ? -1 : 1), x
+ " < " + y); | |
88 } | |
89 } | |
90 } | |
91 } | 104 } |
92 } | 105 } |
93 } | 106 } |
| 107 |
| 108 for (var yb = 1; yb <= BIGGER_THAN_ANY_SMI; yb *= 2) { |
| 109 for (var yo = -2; yo <= 2; yo++) { |
| 110 addSigned(yb + yo); |
| 111 } |
| 112 } |
| 113 |
| 114 for (var i = 0; i < smis.length; i++) { |
| 115 for (var j = 0; j < smis.length; j++) { |
| 116 var x = smis[i]; |
| 117 var y = smis[j]; |
| 118 var lex = %SmiLexicographicCompare(x, y); |
| 119 var expected = (x == y) ? 0 : ((x + "") < (y + "") ? -1 : 1); |
| 120 assertEquals(lex, expected, x + " < " + y); |
| 121 } |
| 122 } |
94 } | 123 } |
95 | 124 |
96 TestNumberSort(); | 125 TestSmiLexicographicCompare(); |
97 | 126 |
98 | 127 |
99 // Test lexicographical string sorting. | 128 // Test lexicographical string sorting. |
100 function TestStringSort() { | 129 function TestStringSort() { |
101 var a = [ "cc", "c", "aa", "a", "bb", "b", "ab", "ac" ]; | 130 var a = [ "cc", "c", "aa", "a", "bb", "b", "ab", "ac" ]; |
102 a.sort(); | 131 a.sort(); |
103 assertArrayEquals([ "a", "aa", "ab", "ac", "b", "bb", "c", "cc" ], a); | 132 assertArrayEquals([ "a", "aa", "ab", "ac", "b", "bb", "c", "cc" ], a); |
104 } | 133 } |
105 | 134 |
106 TestStringSort(); | 135 TestStringSort(); |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 return {__proto__: o.prototype, val: v}; | 396 return {__proto__: o.prototype, val: v}; |
368 } | 397 } |
369 var arr = [o(1), o(2), o(4), o(8), o(16), o(32), o(64), o(128), o(256), o(-0)]; | 398 var arr = [o(1), o(2), o(4), o(8), o(16), o(32), o(64), o(128), o(256), o(-0)]; |
370 var global = this; | 399 var global = this; |
371 function cmpTest(a, b) { | 400 function cmpTest(a, b) { |
372 assertEquals(global, this); | 401 assertEquals(global, this); |
373 assertTrue(a instanceof o); | 402 assertTrue(a instanceof o); |
374 assertTrue(b instanceof o); | 403 assertTrue(b instanceof o); |
375 return a.val - b.val; | 404 return a.val - b.val; |
376 } | 405 } |
377 arr.sort(cmpTest); | 406 arr.sort(cmpTest); |
OLD | NEW |