OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 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 |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
| 28 // Flags: --allow-natives-syntax |
| 29 |
28 // Test array sort. | 30 // Test array sort. |
29 | 31 |
30 // Test counter-intuitive default number sorting. | 32 // Test counter-intuitive default number sorting. |
31 function TestNumberSort() { | 33 function TestNumberSort() { |
32 var a = [ 200, 45, 7 ]; | 34 var a = [ 200, 45, 7 ]; |
33 | 35 |
34 // Default sort converts each element to string and orders | 36 // Default sort converts each element to string and orders |
35 // lexicographically. | 37 // lexicographically. |
36 a.sort(); | 38 a.sort(); |
37 assertArrayEquals([ 200, 45, 7 ], a); | 39 assertArrayEquals([ 200, 45, 7 ], a); |
38 // Sort numbers by value using a compare functions. | 40 // Sort numbers by value using a compare functions. |
39 a.sort(function(x, y) { return x - y; }); | 41 a.sort(function(x, y) { return x - y; }); |
40 assertArrayEquals([ 7, 45, 200 ], a); | 42 assertArrayEquals([ 7, 45, 200 ], a); |
41 | 43 |
42 // Default sort on negative numbers. | 44 // Default sort on negative numbers. |
43 a = [-12345,-123,-1234,-123456]; | 45 a = [-12345,-123,-1234,-123456]; |
44 a.sort(); | 46 a.sort(); |
45 assertArrayEquals([-123,-1234,-12345,-123456], a); | 47 assertArrayEquals([-123,-1234,-12345,-123456], a); |
46 | 48 |
47 // Default sort on negative and non-negative numbers. | 49 // Default sort on negative and non-negative numbers. |
48 a = [123456,0,-12345,-123,123,1234,-1234,0,12345,-123456]; | 50 a = [123456,0,-12345,-123,123,1234,-1234,0,12345,-123456]; |
49 a.sort(); | 51 a.sort(); |
50 assertArrayEquals([-123,-1234,-12345,-123456,0,0,123,1234,12345,123456], a); | 52 assertArrayEquals([-123,-1234,-12345,-123456,0,0,123,1234,12345,123456], a); |
51 | 53 |
| 54 // Tricky case avoiding integer overflow in Runtime_SmiLexicographicCompare. |
| 55 a = [9, 1000000000].sort(); |
| 56 assertArrayEquals([1000000000, 9], a); |
| 57 a = [1000000000, 1].sort(); |
| 58 assertArrayEquals([1, 1000000000], a); |
| 59 a = [1000000000, 0].sort(); |
| 60 assertArrayEquals([0, 1000000000], a); |
| 61 |
| 62 // One string is a prefix of the other. |
| 63 a = [1230, 123].sort(); |
| 64 assertArrayEquals([123, 1230], a); |
| 65 a = [1231, 123].sort(); |
| 66 assertArrayEquals([123, 1231], a); |
| 67 |
52 // Default sort on Smis and non-Smis. | 68 // Default sort on Smis and non-Smis. |
53 a = [1000000000, 10000000000, 1000000001, -1000000000, -10000000000, -10000000
01]; | 69 a = [1000000000, 10000000000, 1000000001, -1000000000, -10000000000, -10000000
01]; |
54 a.sort(); | 70 a.sort(); |
55 assertArrayEquals([-1000000000, -10000000000, -1000000001, 1000000000, 1000000
0000, 1000000001], a); | 71 assertArrayEquals([-1000000000, -10000000000, -1000000001, 1000000000, 1000000
0000, 1000000001], a); |
| 72 |
| 73 |
| 74 for (var xb = 1; xb <= 1000 * 1000 * 1000; xb *= 10) { |
| 75 for (var xf = 0; xf <= 9; xf++) { |
| 76 for (var xo = -1; xo <= 1; xo++) { |
| 77 for (var yb = 1; yb <= 1000 * 1000 * 1000; yb *= 10) { |
| 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 } |
| 92 } |
| 93 } |
56 } | 94 } |
57 | 95 |
58 TestNumberSort(); | 96 TestNumberSort(); |
59 | 97 |
60 | 98 |
61 // Test lexicographical string sorting. | 99 // Test lexicographical string sorting. |
62 function TestStringSort() { | 100 function TestStringSort() { |
63 var a = [ "cc", "c", "aa", "a", "bb", "b", "ab", "ac" ]; | 101 var a = [ "cc", "c", "aa", "a", "bb", "b", "ab", "ac" ]; |
64 a.sort(); | 102 a.sort(); |
65 assertArrayEquals([ "a", "aa", "ab", "ac", "b", "bb", "c", "cc" ], a); | 103 assertArrayEquals([ "a", "aa", "ab", "ac", "b", "bb", "c", "cc" ], a); |
66 } | 104 } |
67 | 105 |
68 TestStringSort(); | 106 TestStringSort(); |
69 | 107 |
70 | 108 |
71 // Test object sorting. Calls toString on each element and sorts | 109 // Test object sorting. Calls toString on each element and sorts |
72 // lexicographically. | 110 // lexicographically. |
73 function TestObjectSort() { | 111 function TestObjectSort() { |
74 var obj0 = { toString: function() { return "a"; } }; | 112 var obj0 = { toString: function() { return "a"; } }; |
75 var obj1 = { toString: function() { return "b"; } }; | 113 var obj1 = { toString: function() { return "b"; } }; |
76 var obj2 = { toString: function() { return "c"; } }; | 114 var obj2 = { toString: function() { return "c"; } }; |
77 var a = [ obj2, obj0, obj1 ]; | 115 var a = [ obj2, obj0, obj1 ]; |
78 a.sort(); | 116 a.sort(); |
79 assertArrayEquals([ obj0, obj1, obj2 ], a); | 117 assertArrayEquals([ obj0, obj1, obj2 ], a); |
80 } | 118 } |
81 | 119 |
82 TestObjectSort(); | 120 TestObjectSort(); |
OLD | NEW |