The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / mjsunit / string-charcodeat.js @ 40c0f755

History | View | Annotate | Download (5.27 KB)

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() {
33
  return "Te" + "st";
34
}
35

    
36

    
37
function Deep() {
38
  var a = "T";
39
  a += "e";
40
  a += "s";
41
  a += "t";
42
  return a;
43
}
44

    
45

    
46
function Slice() {
47
  return "testing Testing".substring(8, 12);
48
}
49

    
50

    
51
function Flat() {
52
  return "Test";
53
}
54

    
55
function Cons16() {
56
  return "Te" + "\u1234t";
57
}
58

    
59

    
60
function Deep16() {
61
  var a = "T";
62
  a += "e";
63
  a += "\u1234";
64
  a += "t";
65
  return a;
66
}
67

    
68

    
69
function Slice16Beginning() {
70
  return "Te\u1234t test".substring(0, 4);
71
}
72

    
73

    
74
function Slice16Middle() {
75
  return "test Te\u1234t test".substring(5, 9);
76
}
77

    
78

    
79
function Slice16End() {
80
  return "test Te\u1234t".substring(5, 9);
81
}
82

    
83

    
84
function Flat16() {
85
  return "Te\u1234t";
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;
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

    
140
TestStringType(Cons, false);
141
TestStringType(Deep, false);
142
TestStringType(Slice, false);
143
TestStringType(Flat, false);
144
TestStringType(NotAString, false);
145
TestStringType(Cons16, true);
146
TestStringType(Deep16, true);
147
TestStringType(Slice16Beginning, true);
148
TestStringType(Slice16Middle, true);
149
TestStringType(Slice16End, true);
150
TestStringType(Flat16, true);
151
TestStringType(NotAString16, true);
152

    
153

    
154
function StupidThing() {
155
  // Doesn't return a string from toString!
156
  this.toString = function() { return 42; }
157
  this.charCodeAt = String.prototype.charCodeAt;
158
}
159

    
160
assertEquals(52, new StupidThing().charCodeAt(0));
161
assertEquals(50, new StupidThing().charCodeAt(1));
162
assertTrue(isNaN(new StupidThing().charCodeAt(2)));
163
assertTrue(isNaN(new StupidThing().charCodeAt(-1)));
164

    
165

    
166
// Medium (>255) and long (>65535) strings.
167

    
168
var medium = "12345678";
169
medium += medium; // 16.
170
medium += medium; // 32.
171
medium += medium; // 64.
172
medium += medium; // 128.
173
medium += medium; // 256.
174

    
175
var long = medium;
176
long += long + long + long;     // 1024.
177
long += long + long + long;     // 4096.
178
long += long + long + long;     // 16384.
179
long += long + long + long;     // 65536.
180

    
181
assertTrue(isNaN(medium.charCodeAt(-1)));
182
assertEquals(49, medium.charCodeAt(0));
183
assertEquals(56, medium.charCodeAt(255));
184
assertTrue(isNaN(medium.charCodeAt(256)));
185

    
186
assertTrue(isNaN(long.charCodeAt(-1)));
187
assertEquals(49, long.charCodeAt(0));
188
assertEquals(56, long.charCodeAt(65535));
189
assertTrue(isNaN(long.charCodeAt(65536)));