]> git.saurik.com Git - apple/javascriptcore.git/blame - tests/stress/string-iterators.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / string-iterators.js
CommitLineData
ed1e77d3
A
1// This test checks the behavior of the String iterator
2
3var testString = "Cocoa,Cappuccino";
4var stringIterator = testString[Symbol.iterator]();
5var stringIteratorPrototype = stringIterator.__proto__;
6var stringIteratorPrototypeNext = stringIteratorPrototype.next;
7
8if (stringIterator.hasOwnProperty('next'))
9 throw "next method should exists on %StringIteratorPrototype%";
10if (!stringIteratorPrototype.hasOwnProperty('next'))
11 throw "next method should exists on %StringIteratorPrototype%";
12
13var iterator = testString[Symbol.iterator]();
14var i = 0;
15while (true) {
16 var {done, value} = iterator.next();
17 if (done)
18 break;
19 if (value !== testString[i])
20 throw "Error: bad value: " + value;
21 i++;
22}
23
24if (testString.length !== i)
25 throw "Error: bad value: " + i;
26
27function testSurrogatePair(testString, expected, numberOfElements) {
28 if (testString.length !== numberOfElements)
29 throw "Error: bad value: " + testString.length;
30
31 var iterator = testString[Symbol.iterator]();
32 var i = 0;
33 while (true) {
34 var {done, value} = iterator.next();
35 if (done)
36 break;
37 if (value !== expected[i])
38 throw "Error: bad value: " + value;
39 i++;
40 }
41
42 if (i !== expected.length)
43 throw "Error: bad value: " + i;
44
45 for (var codePoint of testString) {
46 if (value !== expected[i])
47 throw "Error: bad value: " + value;
48 }
49}
50
51// "\uD842\uDFB7\u91ce\u5bb6"
52var testString = "𠮷野家";
53var expected = [
54 String.fromCharCode(0xD842, 0xDFB7),
55 String.fromCharCode(0x91CE),
56 String.fromCharCode(0x5BB6),
57];
58testSurrogatePair(testString, expected, 4);
59
60var testString = "A\uD842";
61var expected = [
62 String.fromCharCode(0x0041),
63 String.fromCharCode(0xD842),
64];
65testSurrogatePair(testString, expected, 2);
66
67var testString = "A\uD842A";
68var expected = [
69 String.fromCharCode(0x0041),
70 String.fromCharCode(0xD842),
71 String.fromCharCode(0x0041),
72];
73testSurrogatePair(testString, expected, 3);
74
75var testString = "A\uD842\uDFB7";
76var expected = [
77 String.fromCharCode(0x0041),
78 String.fromCharCode(0xD842, 0xDFB7),
79];
80testSurrogatePair(testString, expected, 3);
81
82var testString = "\uD842A\uDFB7";
83var expected = [
84 String.fromCharCode(0xD842),
85 String.fromCharCode(0x0041),
86 String.fromCharCode(0xDFB7),
87];
88testSurrogatePair(testString, expected, 3);
89
90var testString = "\uDFB7\uD842A";
91var expected = [
92 String.fromCharCode(0xDFB7),
93 String.fromCharCode(0xD842),
94 String.fromCharCode(0x0041),
95];
96testSurrogatePair(testString, expected, 3);
97
98var string1 = "Cocoa";
99var string1Iterator = string1[Symbol.iterator]();
100var index = 0;
101while (true) {
102 var result = stringIteratorPrototypeNext.call(string1Iterator);
103 var value = result.value;
104 if (result.done) {
105 break;
106 }
107 if (value !== string1[index++])
108 throw "Error: bad value: " + value;
109}
110if (index !== 5)
111 throw "Error: bad index: " + index;
112
113function increment(iter) {
114 return stringIteratorPrototypeNext.call(iter);
115}
116var string1 = "Cocoa";
117var string2 = "Cocoa";
118var string1Iterator = string1[Symbol.iterator]();
119var string2Iterator = string2[Symbol.iterator]();
120for (var i = 0; i < 3; ++i) {
121 var value1 = increment(string1Iterator).value;
122 var value2 = increment(string2Iterator).value;
123 if (value1 !== value2)
124 throw "Error: bad value: " + value1 + " " + value2;
125}
126
127var string1 = "Cappuccino";
128var string1Iterator = string1[Symbol.iterator]();
129
130var value = string1Iterator.next().value;
131if (value !== "C")
132 throw "Error: bad value: " + value;
133var value = string1Iterator.next().value;
134if (value !== "a")
135 throw "Error: bad value: " + value;
136var value = string1Iterator.next().value;
137if (value !== "p")
138 throw "Error: bad value: " + value;
139var value = stringIteratorPrototypeNext.call(string1Iterator).value;
140if (value !== "p")
141 throw "Error: bad value: " + value;
142var value = stringIteratorPrototypeNext.call(string1Iterator).value;
143if (value !== "u")
144 throw "Error: bad value: " + value;
145var value = stringIteratorPrototypeNext.call(string1Iterator).value;
146if (value !== "c")
147 throw "Error: bad value: " + value;
148var value = stringIteratorPrototypeNext.call(string1Iterator).value;
149if (value !== "c")
150 throw "Error: bad value: " + value;
151var value = stringIteratorPrototypeNext.call(string1Iterator).value;
152if (value !== "i")
153 throw "Error: bad value: " + value;
154var value = stringIteratorPrototypeNext.call(string1Iterator).value;
155if (value !== "n")
156 throw "Error: bad value: " + value;
157var value = stringIteratorPrototypeNext.call(string1Iterator).value;
158if (value !== "o")
159 throw "Error: bad value: " + value;
160var value = stringIteratorPrototypeNext.call(string1Iterator).value;
161if (value !== undefined)
162 throw "Error: bad value: " + value;
163
164var primitives = [
165 "string",
166 42,
167 0.03,
168 false,
169 true,
170 Symbol("Cocoa"),
171 null,
172 undefined
173];
174for (var primitive of primitives) {
175 var didThrow = null;
176 try {
177 stringIteratorPrototypeNext.call(primitive);
178 } catch (e) {
179 didThrow = e;
180 }
181 if (!didThrow)
182 throw "Error: no error thrown";
183 var message = 'TypeError: %StringIteratorPrototype%.next requires that |this| be a String Iterator instance';
184 if (primitive == null)
185 message = 'TypeError: %StringIteratorPrototype%.next requires that |this| not be null or undefined'
186 if (String(didThrow) !== message)
187 throw "Error: bad error thrown: " + didThrow;
188}
189
190var nonRelatedObjects = [
191 {},
192 [],
193 new Date(),
194 new Error(),
195 Object(Symbol()),
196 new String("Cappuccino"),
197 new Number(42),
198 new Boolean(false),
199 function () { },
200];
201for (var object of nonRelatedObjects) {
202 var didThrow = null;
203 try {
204 stringIteratorPrototypeNext.call(object);
205 } catch (e) {
206 didThrow = e;
207 }
208 if (!didThrow)
209 throw "Error: no error thrown";
210 if (String(didThrow) !== 'TypeError: %StringIteratorPrototype%.next requires that |this| be a String Iterator instance')
211 throw "Error: bad error thrown: " + didThrow;
212}