Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | |
| 28 | /** |
| 29 | * @fileoverview Check all sorts of borderline cases for charCodeAt. |
| 30 | */ |
| 31 | |
| 32 | function Cons() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 33 | return "Te" + "st testing 123"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | |
| 37 | function Deep() { |
| 38 | var a = "T"; |
| 39 | a += "e"; |
| 40 | a += "s"; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 41 | a += "ting testing 123"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | return a; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | function Slice() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 47 | return "testing Testing testing 123456789012345".substring(8, 22); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
| 51 | function Flat() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 52 | return "Testing testing 123"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | function Cons16() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 56 | return "Te" + "\u1234t testing 123"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
| 60 | function Deep16() { |
| 61 | var a = "T"; |
| 62 | a += "e"; |
| 63 | a += "\u1234"; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 64 | a += "ting testing 123"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 65 | return a; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | function Slice16Beginning() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 70 | return "Te\u1234t testing testing 123".substring(0, 14); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | function Slice16Middle() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 75 | return "test Te\u1234t testing testing 123".substring(5, 19); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | function Slice16End() { |
| 80 | return "test Te\u1234t".substring(5, 9); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | function Flat16() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 85 | return "Te\u1234ting testing 123"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
| 89 | function Thing() { |
| 90 | } |
| 91 | |
| 92 | |
| 93 | function NotAString() { |
| 94 | var n = new Thing(); |
| 95 | n.toString = function() { return "Test"; }; |
| 96 | n.charCodeAt = String.prototype.charCodeAt; |
| 97 | return n; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | function NotAString16() { |
| 102 | var n = new Thing(); |
| 103 | n.toString = function() { return "Te\u1234t"; }; |
| 104 | n.charCodeAt = String.prototype.charCodeAt; |
| 105 | return n; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | function TestStringType(generator, sixteen) { |
| 110 | var g = generator; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 111 | var len = g().toString().length; |
| 112 | var t = sixteen ? "t" : "f" |
| 113 | t += generator.name; |
| 114 | assertTrue(isNaN(g().charCodeAt(-1e19)), 1 + t); |
| 115 | assertTrue(isNaN(g().charCodeAt(-0x80000001)), 2 + t); |
| 116 | assertTrue(isNaN(g().charCodeAt(-0x80000000)), 3 + t); |
| 117 | assertTrue(isNaN(g().charCodeAt(-0x40000000)), 4 + t); |
| 118 | assertTrue(isNaN(g().charCodeAt(-1)), 5 + t); |
| 119 | assertTrue(isNaN(g().charCodeAt(len)), 6 + t); |
| 120 | assertTrue(isNaN(g().charCodeAt(len + 1)), 7 + t); |
| 121 | assertTrue(isNaN(g().charCodeAt(0x3fffffff)), 8 + t); |
| 122 | assertTrue(isNaN(g().charCodeAt(0x7fffffff)), 9 + t); |
| 123 | assertTrue(isNaN(g().charCodeAt(0x80000000)), 10 + t); |
| 124 | assertTrue(isNaN(g().charCodeAt(1e9)), 11 + t); |
| 125 | assertEquals(84, g().charCodeAt(0), 12 + t); |
| 126 | assertEquals(84, g().charCodeAt("test"), 13 + t); |
| 127 | assertEquals(84, g().charCodeAt(""), 14 + t); |
| 128 | assertEquals(84, g().charCodeAt(null), 15 + t); |
| 129 | assertEquals(84, g().charCodeAt(undefined), 16 + t); |
| 130 | assertEquals(84, g().charCodeAt(), 17 + t); |
| 131 | assertEquals(84, g().charCodeAt(void 0), 18 + t); |
| 132 | assertEquals(84, g().charCodeAt(false), 19 + t); |
| 133 | assertEquals(101, g().charCodeAt(true), 20 + t); |
| 134 | assertEquals(101, g().charCodeAt(1), 21 + t); |
| 135 | assertEquals(sixteen ? 0x1234 : 115, g().charCodeAt(2), 22 + t); |
| 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); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | TestStringType(Cons, false); |
| 144 | TestStringType(Deep, false); |
| 145 | TestStringType(Slice, false); |
| 146 | TestStringType(Flat, false); |
| 147 | TestStringType(NotAString, false); |
| 148 | TestStringType(Cons16, true); |
| 149 | TestStringType(Deep16, true); |
| 150 | TestStringType(Slice16Beginning, true); |
| 151 | TestStringType(Slice16Middle, true); |
| 152 | TestStringType(Slice16End, true); |
| 153 | TestStringType(Flat16, true); |
| 154 | TestStringType(NotAString16, true); |
| 155 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 156 | |
| 157 | function ConsNotSmiIndex() { |
| 158 | var str = Cons(); |
| 159 | assertTrue(isNaN(str.charCodeAt(0x7fffffff))); |
| 160 | } |
| 161 | |
| 162 | for (var i = 0; i < 100000; i++) { |
| 163 | ConsNotSmiIndex(); |
| 164 | } |
| 165 | |
| 166 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 167 | for (var i = 0; i != 10; i++) { |
| 168 | assertEquals(101, Cons16().charCodeAt(1.1)); |
| 169 | assertEquals('e', Cons16().charAt(1.1)); |
| 170 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | |
| 172 | function StupidThing() { |
| 173 | // Doesn't return a string from toString! |
| 174 | this.toString = function() { return 42; } |
| 175 | this.charCodeAt = String.prototype.charCodeAt; |
| 176 | } |
| 177 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 178 | assertEquals(52, new StupidThing().charCodeAt(0), 27); |
| 179 | assertEquals(50, new StupidThing().charCodeAt(1), 28); |
| 180 | assertTrue(isNaN(new StupidThing().charCodeAt(2)), 29); |
| 181 | assertTrue(isNaN(new StupidThing().charCodeAt(-1)), 30); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | |
| 183 | |
| 184 | // Medium (>255) and long (>65535) strings. |
| 185 | |
| 186 | var medium = "12345678"; |
| 187 | medium += medium; // 16. |
| 188 | medium += medium; // 32. |
| 189 | medium += medium; // 64. |
| 190 | medium += medium; // 128. |
| 191 | medium += medium; // 256. |
| 192 | |
| 193 | var long = medium; |
| 194 | long += long + long + long; // 1024. |
| 195 | long += long + long + long; // 4096. |
| 196 | long += long + long + long; // 16384. |
| 197 | long += long + long + long; // 65536. |
| 198 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 199 | assertTrue(isNaN(medium.charCodeAt(-1)), 31); |
| 200 | assertEquals(49, medium.charCodeAt(0), 32); |
| 201 | assertEquals(56, medium.charCodeAt(255), 33); |
| 202 | assertTrue(isNaN(medium.charCodeAt(256)), 34); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 204 | assertTrue(isNaN(long.charCodeAt(-1)), 35); |
| 205 | assertEquals(49, long.charCodeAt(0), 36); |
| 206 | assertEquals(56, long.charCodeAt(65535), 37); |
| 207 | assertTrue(isNaN(long.charCodeAt(65536)), 38); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 208 | |
| 209 | |
| 210 | // Test crankshaft code when the function is set directly on the |
| 211 | // string prototype object instead of the hidden prototype object. |
| 212 | // See http://code.google.com/p/v8/issues/detail?id=1070 |
| 213 | |
| 214 | String.prototype.x = String.prototype.charCodeAt; |
| 215 | |
| 216 | function directlyOnPrototype() { |
| 217 | assertEquals(97, "a".x(0)); |
| 218 | assertEquals(98, "b".x(0)); |
| 219 | assertEquals(99, "c".x(0)); |
| 220 | assertEquals(97, "a".x(0)); |
| 221 | assertEquals(98, "b".x(0)); |
| 222 | assertEquals(99, "c".x(0)); |
| 223 | } |
| 224 | |
| 225 | for (var i = 0; i < 10000; i++) { |
| 226 | directlyOnPrototype(); |
| 227 | } |