]> git.saurik.com Git - apple/javascriptcore.git/blob - tests/stress/string-iterators.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / string-iterators.js
1 // This test checks the behavior of the String iterator
2
3 var testString = "Cocoa,Cappuccino";
4 var stringIterator = testString[Symbol.iterator]();
5 var stringIteratorPrototype = stringIterator.__proto__;
6 var stringIteratorPrototypeNext = stringIteratorPrototype.next;
7
8 if (stringIterator.hasOwnProperty('next'))
9 throw "next method should exists on %StringIteratorPrototype%";
10 if (!stringIteratorPrototype.hasOwnProperty('next'))
11 throw "next method should exists on %StringIteratorPrototype%";
12
13 var iterator = testString[Symbol.iterator]();
14 var i = 0;
15 while (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
24 if (testString.length !== i)
25 throw "Error: bad value: " + i;
26
27 function 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"
52 var testString = "𠮷野家";
53 var expected = [
54 String.fromCharCode(0xD842, 0xDFB7),
55 String.fromCharCode(0x91CE),
56 String.fromCharCode(0x5BB6),
57 ];
58 testSurrogatePair(testString, expected, 4);
59
60 var testString = "A\uD842";
61 var expected = [
62 String.fromCharCode(0x0041),
63 String.fromCharCode(0xD842),
64 ];
65 testSurrogatePair(testString, expected, 2);
66
67 var testString = "A\uD842A";
68 var expected = [
69 String.fromCharCode(0x0041),
70 String.fromCharCode(0xD842),
71 String.fromCharCode(0x0041),
72 ];
73 testSurrogatePair(testString, expected, 3);
74
75 var testString = "A\uD842\uDFB7";
76 var expected = [
77 String.fromCharCode(0x0041),
78 String.fromCharCode(0xD842, 0xDFB7),
79 ];
80 testSurrogatePair(testString, expected, 3);
81
82 var testString = "\uD842A\uDFB7";
83 var expected = [
84 String.fromCharCode(0xD842),
85 String.fromCharCode(0x0041),
86 String.fromCharCode(0xDFB7),
87 ];
88 testSurrogatePair(testString, expected, 3);
89
90 var testString = "\uDFB7\uD842A";
91 var expected = [
92 String.fromCharCode(0xDFB7),
93 String.fromCharCode(0xD842),
94 String.fromCharCode(0x0041),
95 ];
96 testSurrogatePair(testString, expected, 3);
97
98 var string1 = "Cocoa";
99 var string1Iterator = string1[Symbol.iterator]();
100 var index = 0;
101 while (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 }
110 if (index !== 5)
111 throw "Error: bad index: " + index;
112
113 function increment(iter) {
114 return stringIteratorPrototypeNext.call(iter);
115 }
116 var string1 = "Cocoa";
117 var string2 = "Cocoa";
118 var string1Iterator = string1[Symbol.iterator]();
119 var string2Iterator = string2[Symbol.iterator]();
120 for (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
127 var string1 = "Cappuccino";
128 var string1Iterator = string1[Symbol.iterator]();
129
130 var value = string1Iterator.next().value;
131 if (value !== "C")
132 throw "Error: bad value: " + value;
133 var value = string1Iterator.next().value;
134 if (value !== "a")
135 throw "Error: bad value: " + value;
136 var value = string1Iterator.next().value;
137 if (value !== "p")
138 throw "Error: bad value: " + value;
139 var value = stringIteratorPrototypeNext.call(string1Iterator).value;
140 if (value !== "p")
141 throw "Error: bad value: " + value;
142 var value = stringIteratorPrototypeNext.call(string1Iterator).value;
143 if (value !== "u")
144 throw "Error: bad value: " + value;
145 var value = stringIteratorPrototypeNext.call(string1Iterator).value;
146 if (value !== "c")
147 throw "Error: bad value: " + value;
148 var value = stringIteratorPrototypeNext.call(string1Iterator).value;
149 if (value !== "c")
150 throw "Error: bad value: " + value;
151 var value = stringIteratorPrototypeNext.call(string1Iterator).value;
152 if (value !== "i")
153 throw "Error: bad value: " + value;
154 var value = stringIteratorPrototypeNext.call(string1Iterator).value;
155 if (value !== "n")
156 throw "Error: bad value: " + value;
157 var value = stringIteratorPrototypeNext.call(string1Iterator).value;
158 if (value !== "o")
159 throw "Error: bad value: " + value;
160 var value = stringIteratorPrototypeNext.call(string1Iterator).value;
161 if (value !== undefined)
162 throw "Error: bad value: " + value;
163
164 var primitives = [
165 "string",
166 42,
167 0.03,
168 false,
169 true,
170 Symbol("Cocoa"),
171 null,
172 undefined
173 ];
174 for (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
190 var 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 ];
201 for (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 }