| 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 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 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 /** | 28 /** |
| 29 * @fileoverview Check all sorts of borderline cases for charCodeAt. | 29 * @fileoverview Check all sorts of borderline cases for charCodeAt. |
| 30 */ | 30 */ |
| 31 | 31 |
| 32 function Cons() { | 32 function Cons() { |
| 33 return "Te" + "st"; | 33 return "Te" + "st testing 123"; |
| 34 } | 34 } |
| 35 | 35 |
| 36 | 36 |
| 37 function Deep() { | 37 function Deep() { |
| 38 var a = "T"; | 38 var a = "T"; |
| 39 a += "e"; | 39 a += "e"; |
| 40 a += "s"; | 40 a += "s"; |
| 41 a += "t"; | 41 a += "ting testing 123"; |
| 42 return a; | 42 return a; |
| 43 } | 43 } |
| 44 | 44 |
| 45 | 45 |
| 46 function Slice() { | 46 function Slice() { |
| 47 return "testing Testing".substring(8, 12); | 47 return "testing Testing testing 123456789012345".substring(8, 22); |
| 48 } | 48 } |
| 49 | 49 |
| 50 | 50 |
| 51 function Flat() { | 51 function Flat() { |
| 52 return "Test"; | 52 return "Testing testing 123"; |
| 53 } | 53 } |
| 54 | 54 |
| 55 function Cons16() { | 55 function Cons16() { |
| 56 return "Te" + "\u1234t"; | 56 return "Te" + "\u1234t testing 123"; |
| 57 } | 57 } |
| 58 | 58 |
| 59 | 59 |
| 60 function Deep16() { | 60 function Deep16() { |
| 61 var a = "T"; | 61 var a = "T"; |
| 62 a += "e"; | 62 a += "e"; |
| 63 a += "\u1234"; | 63 a += "\u1234"; |
| 64 a += "t"; | 64 a += "ting testing 123"; |
| 65 return a; | 65 return a; |
| 66 } | 66 } |
| 67 | 67 |
| 68 | 68 |
| 69 function Slice16Beginning() { | 69 function Slice16Beginning() { |
| 70 return "Te\u1234t test".substring(0, 4); | 70 return "Te\u1234t testing testing 123".substring(0, 14); |
| 71 } | 71 } |
| 72 | 72 |
| 73 | 73 |
| 74 function Slice16Middle() { | 74 function Slice16Middle() { |
| 75 return "test Te\u1234t test".substring(5, 9); | 75 return "test Te\u1234t testing testing 123".substring(5, 19); |
| 76 } | 76 } |
| 77 | 77 |
| 78 | 78 |
| 79 function Slice16End() { | 79 function Slice16End() { |
| 80 return "test Te\u1234t".substring(5, 9); | 80 return "test Te\u1234t".substring(5, 9); |
| 81 } | 81 } |
| 82 | 82 |
| 83 | 83 |
| 84 function Flat16() { | 84 function Flat16() { |
| 85 return "Te\u1234t"; | 85 return "Te\u1234ting testing 123"; |
| 86 } | 86 } |
| 87 | 87 |
| 88 | 88 |
| 89 function Thing() { | 89 function Thing() { |
| 90 } | 90 } |
| 91 | 91 |
| 92 | 92 |
| 93 function NotAString() { | 93 function NotAString() { |
| 94 var n = new Thing(); | 94 var n = new Thing(); |
| 95 n.toString = function() { return "Test"; }; | 95 n.toString = function() { return "Test"; }; |
| 96 n.charCodeAt = String.prototype.charCodeAt; | 96 n.charCodeAt = String.prototype.charCodeAt; |
| 97 return n; | 97 return n; |
| 98 } | 98 } |
| 99 | 99 |
| 100 | 100 |
| 101 function NotAString16() { | 101 function NotAString16() { |
| 102 var n = new Thing(); | 102 var n = new Thing(); |
| 103 n.toString = function() { return "Te\u1234t"; }; | 103 n.toString = function() { return "Te\u1234t"; }; |
| 104 n.charCodeAt = String.prototype.charCodeAt; | 104 n.charCodeAt = String.prototype.charCodeAt; |
| 105 return n; | 105 return n; |
| 106 } | 106 } |
| 107 | 107 |
| 108 | 108 |
| 109 function TestStringType(generator, sixteen) { | 109 function TestStringType(generator, sixteen) { |
| 110 var g = generator; | 110 var g = generator; |
| 111 assertTrue(isNaN(g().charCodeAt(-1e19))); | 111 var len = g().toString().length; |
| 112 assertTrue(isNaN(g().charCodeAt(-0x80000001))); | 112 var t = sixteen ? "t" : "f" |
| 113 assertTrue(isNaN(g().charCodeAt(-0x80000000))); | 113 t += generator.name; |
| 114 assertTrue(isNaN(g().charCodeAt(-0x40000000))); | 114 assertTrue(isNaN(g().charCodeAt(-1e19)), 1 + t); |
| 115 assertTrue(isNaN(g().charCodeAt(-1))); | 115 assertTrue(isNaN(g().charCodeAt(-0x80000001)), 2 + t); |
| 116 assertTrue(isNaN(g().charCodeAt(4))); | 116 assertTrue(isNaN(g().charCodeAt(-0x80000000)), 3 + t); |
| 117 assertTrue(isNaN(g().charCodeAt(5))); | 117 assertTrue(isNaN(g().charCodeAt(-0x40000000)), 4 + t); |
| 118 assertTrue(isNaN(g().charCodeAt(0x3fffffff))); | 118 assertTrue(isNaN(g().charCodeAt(-1)), 5 + t); |
| 119 assertTrue(isNaN(g().charCodeAt(0x7fffffff))); | 119 assertTrue(isNaN(g().charCodeAt(len)), 6 + t); |
| 120 assertTrue(isNaN(g().charCodeAt(0x80000000))); | 120 assertTrue(isNaN(g().charCodeAt(len + 1)), 7 + t); |
| 121 assertTrue(isNaN(g().charCodeAt(1e9))); | 121 assertTrue(isNaN(g().charCodeAt(0x3fffffff)), 8 + t); |
| 122 assertEquals(84, g().charCodeAt(0)); | 122 assertTrue(isNaN(g().charCodeAt(0x7fffffff)), 9 + t); |
| 123 assertEquals(84, g().charCodeAt("test")); | 123 assertTrue(isNaN(g().charCodeAt(0x80000000)), 10 + t); |
| 124 assertEquals(84, g().charCodeAt("")); | 124 assertTrue(isNaN(g().charCodeAt(1e9)), 11 + t); |
| 125 assertEquals(84, g().charCodeAt(null)); | 125 assertEquals(84, g().charCodeAt(0), 12 + t); |
| 126 assertEquals(84, g().charCodeAt(undefined)); | 126 assertEquals(84, g().charCodeAt("test"), 13 + t); |
| 127 assertEquals(84, g().charCodeAt()); | 127 assertEquals(84, g().charCodeAt(""), 14 + t); |
| 128 assertEquals(84, g().charCodeAt(void 0)); | 128 assertEquals(84, g().charCodeAt(null), 15 + t); |
| 129 assertEquals(84, g().charCodeAt(false)); | 129 assertEquals(84, g().charCodeAt(undefined), 16 + t); |
| 130 assertEquals(101, g().charCodeAt(true)); | 130 assertEquals(84, g().charCodeAt(), 17 + t); |
| 131 assertEquals(101, g().charCodeAt(1)); | 131 assertEquals(84, g().charCodeAt(void 0), 18 + t); |
| 132 assertEquals(sixteen ? 0x1234 : 115, g().charCodeAt(2)); | 132 assertEquals(84, g().charCodeAt(false), 19 + t); |
| 133 assertEquals(116, g().charCodeAt(3)); | 133 assertEquals(101, g().charCodeAt(true), 20 + t); |
| 134 assertEquals(101, g().charCodeAt(1.1)); | 134 assertEquals(101, g().charCodeAt(1), 21 + t); |
| 135 assertEquals(sixteen ? 0x1234 : 115, g().charCodeAt(2.1718)); | 135 assertEquals(sixteen ? 0x1234 : 115, g().charCodeAt(2), 22 + t); |
| 136 assertEquals(116, g().charCodeAt(3.14159)); | 136 assertEquals(116, g().charCodeAt(3), 23 + t); |
| 137 assertEquals(101, g().charCodeAt(1.1), 24 + t); |
| 138 assertEquals(sixteen ? 0x1234 : 115, g().charCodeAt(2.1718), 25 + t); |
| 139 assertEquals(116, g().charCodeAt(3.14159), 26 + t); |
| 137 } | 140 } |
| 138 | 141 |
| 139 | 142 |
| 140 TestStringType(Cons, false); | 143 TestStringType(Cons, false); |
| 141 TestStringType(Deep, false); | 144 TestStringType(Deep, false); |
| 142 TestStringType(Slice, false); | 145 TestStringType(Slice, false); |
| 143 TestStringType(Flat, false); | 146 TestStringType(Flat, false); |
| 144 TestStringType(NotAString, false); | 147 TestStringType(NotAString, false); |
| 145 TestStringType(Cons16, true); | 148 TestStringType(Cons16, true); |
| 146 TestStringType(Deep16, true); | 149 TestStringType(Deep16, true); |
| 147 TestStringType(Slice16Beginning, true); | 150 TestStringType(Slice16Beginning, true); |
| 148 TestStringType(Slice16Middle, true); | 151 TestStringType(Slice16Middle, true); |
| 149 TestStringType(Slice16End, true); | 152 TestStringType(Slice16End, true); |
| 150 TestStringType(Flat16, true); | 153 TestStringType(Flat16, true); |
| 151 TestStringType(NotAString16, true); | 154 TestStringType(NotAString16, true); |
| 152 | 155 |
| 153 | 156 |
| 154 function StupidThing() { | 157 function StupidThing() { |
| 155 // Doesn't return a string from toString! | 158 // Doesn't return a string from toString! |
| 156 this.toString = function() { return 42; } | 159 this.toString = function() { return 42; } |
| 157 this.charCodeAt = String.prototype.charCodeAt; | 160 this.charCodeAt = String.prototype.charCodeAt; |
| 158 } | 161 } |
| 159 | 162 |
| 160 assertEquals(52, new StupidThing().charCodeAt(0)); | 163 assertEquals(52, new StupidThing().charCodeAt(0), 27); |
| 161 assertEquals(50, new StupidThing().charCodeAt(1)); | 164 assertEquals(50, new StupidThing().charCodeAt(1), 28); |
| 162 assertTrue(isNaN(new StupidThing().charCodeAt(2))); | 165 assertTrue(isNaN(new StupidThing().charCodeAt(2)), 29); |
| 163 assertTrue(isNaN(new StupidThing().charCodeAt(-1))); | 166 assertTrue(isNaN(new StupidThing().charCodeAt(-1)), 30); |
| 164 | 167 |
| 165 | 168 |
| 166 // Medium (>255) and long (>65535) strings. | 169 // Medium (>255) and long (>65535) strings. |
| 167 | 170 |
| 168 var medium = "12345678"; | 171 var medium = "12345678"; |
| 169 medium += medium; // 16. | 172 medium += medium; // 16. |
| 170 medium += medium; // 32. | 173 medium += medium; // 32. |
| 171 medium += medium; // 64. | 174 medium += medium; // 64. |
| 172 medium += medium; // 128. | 175 medium += medium; // 128. |
| 173 medium += medium; // 256. | 176 medium += medium; // 256. |
| 174 | 177 |
| 175 var long = medium; | 178 var long = medium; |
| 176 long += long + long + long; // 1024. | 179 long += long + long + long; // 1024. |
| 177 long += long + long + long; // 4096. | 180 long += long + long + long; // 4096. |
| 178 long += long + long + long; // 16384. | 181 long += long + long + long; // 16384. |
| 179 long += long + long + long; // 65536. | 182 long += long + long + long; // 65536. |
| 180 | 183 |
| 181 assertTrue(isNaN(medium.charCodeAt(-1))); | 184 assertTrue(isNaN(medium.charCodeAt(-1)), 31); |
| 182 assertEquals(49, medium.charCodeAt(0)); | 185 assertEquals(49, medium.charCodeAt(0), 32); |
| 183 assertEquals(56, medium.charCodeAt(255)); | 186 assertEquals(56, medium.charCodeAt(255), 33); |
| 184 assertTrue(isNaN(medium.charCodeAt(256))); | 187 assertTrue(isNaN(medium.charCodeAt(256)), 34); |
| 185 | 188 |
| 186 assertTrue(isNaN(long.charCodeAt(-1))); | 189 assertTrue(isNaN(long.charCodeAt(-1)), 35); |
| 187 assertEquals(49, long.charCodeAt(0)); | 190 assertEquals(49, long.charCodeAt(0), 36); |
| 188 assertEquals(56, long.charCodeAt(65535)); | 191 assertEquals(56, long.charCodeAt(65535), 37); |
| 189 assertTrue(isNaN(long.charCodeAt(65536))); | 192 assertTrue(isNaN(long.charCodeAt(65536)), 38); |
| OLD | NEW |