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