OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 | 2 |
3 // Check that we can traverse very deep stacks of ConsStrings using | 3 // Check that we can traverse very deep stacks of ConsStrings using |
4 // StringInputBuffer. Check that Get(int) works on very deep stacks | 4 // StringInputBuffer. Check that Get(int) works on very deep stacks |
5 // of ConsStrings. These operations may not be very fast, but they | 5 // of ConsStrings. These operations may not be very fast, but they |
6 // should be possible without getting errors due to too deep recursion. | 6 // should be possible without getting errors due to too deep recursion. |
7 | 7 |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include "v8.h" | 10 #include "v8.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 } | 55 } |
56 switch (gen() % 4) { | 56 switch (gen() % 4) { |
57 case 0: { | 57 case 0: { |
58 uc16 buf[2000]; | 58 uc16 buf[2000]; |
59 for (int j = 0; j < len; j++) { | 59 for (int j = 0; j < len; j++) { |
60 buf[j] = gen() % 65536; | 60 buf[j] = gen() % 65536; |
61 } | 61 } |
62 building_blocks[i] = | 62 building_blocks[i] = |
63 Factory::NewStringFromTwoByte(Vector<const uc16>(buf, len)); | 63 Factory::NewStringFromTwoByte(Vector<const uc16>(buf, len)); |
64 for (int j = 0; j < len; j++) { | 64 for (int j = 0; j < len; j++) { |
65 StringShape shape(*building_blocks[i]); | 65 CHECK_EQ(buf[j], building_blocks[i]->Get(j)); |
66 CHECK_EQ(buf[j], building_blocks[i]->Get(shape, j)); | |
67 } | 66 } |
68 break; | 67 break; |
69 } | 68 } |
70 case 1: { | 69 case 1: { |
71 char buf[2000]; | 70 char buf[2000]; |
72 for (int j = 0; j < len; j++) { | 71 for (int j = 0; j < len; j++) { |
73 buf[j] = gen() % 128; | 72 buf[j] = gen() % 128; |
74 } | 73 } |
75 building_blocks[i] = | 74 building_blocks[i] = |
76 Factory::NewStringFromAscii(Vector<const char>(buf, len)); | 75 Factory::NewStringFromAscii(Vector<const char>(buf, len)); |
77 for (int j = 0; j < len; j++) { | 76 for (int j = 0; j < len; j++) { |
78 StringShape shape(*building_blocks[i]); | 77 CHECK_EQ(buf[j], building_blocks[i]->Get(j)); |
79 CHECK_EQ(buf[j], building_blocks[i]->Get(shape, j)); | |
80 } | 78 } |
81 break; | 79 break; |
82 } | 80 } |
83 case 2: { | 81 case 2: { |
84 class Resource: public v8::String::ExternalStringResource, | 82 class Resource: public v8::String::ExternalStringResource, |
85 public Malloced { | 83 public Malloced { |
86 public: | 84 public: |
87 explicit Resource(Vector<const uc16> string): data_(string.start()) { | 85 explicit Resource(Vector<const uc16> string): data_(string.start()) { |
88 length_ = string.length(); | 86 length_ = string.length(); |
89 } | 87 } |
90 virtual const uint16_t* data() const { return data_; } | 88 virtual const uint16_t* data() const { return data_; } |
91 virtual size_t length() const { return length_; } | 89 virtual size_t length() const { return length_; } |
92 | 90 |
93 private: | 91 private: |
94 const uc16* data_; | 92 const uc16* data_; |
95 size_t length_; | 93 size_t length_; |
96 }; | 94 }; |
97 uc16* buf = NewArray<uc16>(len); | 95 uc16* buf = NewArray<uc16>(len); |
98 for (int j = 0; j < len; j++) { | 96 for (int j = 0; j < len; j++) { |
99 buf[j] = gen() % 65536; | 97 buf[j] = gen() % 65536; |
100 } | 98 } |
101 Resource* resource = new Resource(Vector<const uc16>(buf, len)); | 99 Resource* resource = new Resource(Vector<const uc16>(buf, len)); |
102 building_blocks[i] = Factory::NewExternalStringFromTwoByte(resource); | 100 building_blocks[i] = Factory::NewExternalStringFromTwoByte(resource); |
103 for (int j = 0; j < len; j++) { | 101 for (int j = 0; j < len; j++) { |
104 StringShape shape(*building_blocks[i]); | 102 CHECK_EQ(buf[j], building_blocks[i]->Get(j)); |
105 CHECK_EQ(buf[j], building_blocks[i]->Get(shape, j)); | |
106 } | 103 } |
107 break; | 104 break; |
108 } | 105 } |
109 case 3: { | 106 case 3: { |
110 char* buf = NewArray<char>(len); | 107 char* buf = NewArray<char>(len); |
111 for (int j = 0; j < len; j++) { | 108 for (int j = 0; j < len; j++) { |
112 buf[j] = gen() % 128; | 109 buf[j] = gen() % 128; |
113 } | 110 } |
114 building_blocks[i] = | 111 building_blocks[i] = |
115 Factory::NewStringFromAscii(Vector<const char>(buf, len)); | 112 Factory::NewStringFromAscii(Vector<const char>(buf, len)); |
116 for (int j = 0; j < len; j++) { | 113 for (int j = 0; j < len; j++) { |
117 StringShape shape(*building_blocks[i]); | 114 CHECK_EQ(buf[j], building_blocks[i]->Get(j)); |
118 CHECK_EQ(buf[j], building_blocks[i]->Get(shape, j)); | |
119 } | 115 } |
120 DeleteArray<char>(buf); | 116 DeleteArray<char>(buf); |
121 break; | 117 break; |
122 } | 118 } |
123 } | 119 } |
124 } | 120 } |
125 } | 121 } |
126 | 122 |
127 | 123 |
128 static Handle<String> ConstructLeft( | 124 static Handle<String> ConstructLeft( |
129 Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS], | 125 Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS], |
130 int depth) { | 126 int depth) { |
131 Handle<String> answer = Factory::NewStringFromAscii(CStrVector("")); | 127 Handle<String> answer = Factory::NewStringFromAscii(CStrVector("")); |
132 for (int i = 0; i < depth; i++) { | 128 for (int i = 0; i < depth; i++) { |
133 answer = Factory::NewConsString( | 129 answer = Factory::NewConsString( |
134 answer, | 130 answer, |
135 StringShape(*answer), | 131 building_blocks[i % NUMBER_OF_BUILDING_BLOCKS]); |
136 building_blocks[i % NUMBER_OF_BUILDING_BLOCKS], | |
137 StringShape(*building_blocks[i % NUMBER_OF_BUILDING_BLOCKS])); | |
138 } | 132 } |
139 return answer; | 133 return answer; |
140 } | 134 } |
141 | 135 |
142 | 136 |
143 static Handle<String> ConstructRight( | 137 static Handle<String> ConstructRight( |
144 Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS], | 138 Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS], |
145 int depth) { | 139 int depth) { |
146 Handle<String> answer = Factory::NewStringFromAscii(CStrVector("")); | 140 Handle<String> answer = Factory::NewStringFromAscii(CStrVector("")); |
147 for (int i = depth - 1; i >= 0; i--) { | 141 for (int i = depth - 1; i >= 0; i--) { |
148 answer = Factory::NewConsString( | 142 answer = Factory::NewConsString( |
149 building_blocks[i % NUMBER_OF_BUILDING_BLOCKS], | 143 building_blocks[i % NUMBER_OF_BUILDING_BLOCKS], |
150 StringShape(*building_blocks[i % NUMBER_OF_BUILDING_BLOCKS]), | 144 answer); |
151 answer, | |
152 StringShape(*answer)); | |
153 } | 145 } |
154 return answer; | 146 return answer; |
155 } | 147 } |
156 | 148 |
157 | 149 |
158 static Handle<String> ConstructBalancedHelper( | 150 static Handle<String> ConstructBalancedHelper( |
159 Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS], | 151 Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS], |
160 int from, | 152 int from, |
161 int to) { | 153 int to) { |
162 ASSERT(to > from); | 154 ASSERT(to > from); |
163 if (to - from == 1) { | 155 if (to - from == 1) { |
164 return building_blocks[from % NUMBER_OF_BUILDING_BLOCKS]; | 156 return building_blocks[from % NUMBER_OF_BUILDING_BLOCKS]; |
165 } | 157 } |
166 if (to - from == 2) { | 158 if (to - from == 2) { |
167 return Factory::NewConsString( | 159 return Factory::NewConsString( |
168 building_blocks[from % NUMBER_OF_BUILDING_BLOCKS], | 160 building_blocks[from % NUMBER_OF_BUILDING_BLOCKS], |
169 StringShape(*building_blocks[from % NUMBER_OF_BUILDING_BLOCKS]), | 161 building_blocks[(from+1) % NUMBER_OF_BUILDING_BLOCKS]); |
170 building_blocks[(from+1) % NUMBER_OF_BUILDING_BLOCKS], | |
171 StringShape(*building_blocks[(from+1) % NUMBER_OF_BUILDING_BLOCKS])); | |
172 } | 162 } |
173 Handle<String> part1 = | 163 Handle<String> part1 = |
174 ConstructBalancedHelper(building_blocks, from, from + ((to - from) / 2)); | 164 ConstructBalancedHelper(building_blocks, from, from + ((to - from) / 2)); |
175 Handle<String> part2 = | 165 Handle<String> part2 = |
176 ConstructBalancedHelper(building_blocks, from + ((to - from) / 2), to); | 166 ConstructBalancedHelper(building_blocks, from + ((to - from) / 2), to); |
177 return Factory::NewConsString( | 167 return Factory::NewConsString(part1, part2); |
178 part1, | |
179 StringShape(*part1), | |
180 part2, | |
181 StringShape(*part2)); | |
182 } | 168 } |
183 | 169 |
184 | 170 |
185 static Handle<String> ConstructBalanced( | 171 static Handle<String> ConstructBalanced( |
186 Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS]) { | 172 Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS]) { |
187 return ConstructBalancedHelper(building_blocks, 0, DEEP_DEPTH); | 173 return ConstructBalancedHelper(building_blocks, 0, DEEP_DEPTH); |
188 } | 174 } |
189 | 175 |
190 | 176 |
191 static StringInputBuffer buffer; | 177 static StringInputBuffer buffer; |
(...skipping 17 matching lines...) Expand all Loading... |
209 static void TraverseFirst(Handle<String> s1, Handle<String> s2, int chars) { | 195 static void TraverseFirst(Handle<String> s1, Handle<String> s2, int chars) { |
210 int i = 0; | 196 int i = 0; |
211 buffer.Reset(*s1); | 197 buffer.Reset(*s1); |
212 StringInputBuffer buffer2(*s2); | 198 StringInputBuffer buffer2(*s2); |
213 while (buffer.has_more() && i < chars) { | 199 while (buffer.has_more() && i < chars) { |
214 CHECK(buffer2.has_more()); | 200 CHECK(buffer2.has_more()); |
215 uint16_t c = buffer.GetNext(); | 201 uint16_t c = buffer.GetNext(); |
216 CHECK_EQ(c, buffer2.GetNext()); | 202 CHECK_EQ(c, buffer2.GetNext()); |
217 i++; | 203 i++; |
218 } | 204 } |
219 s1->Get(StringShape(*s1), s1->length() - 1); | 205 s1->Get(s1->length() - 1); |
220 s2->Get(StringShape(*s2), s2->length() - 1); | 206 s2->Get(s2->length() - 1); |
221 } | 207 } |
222 | 208 |
223 | 209 |
224 TEST(Traverse) { | 210 TEST(Traverse) { |
225 printf("TestTraverse\n"); | 211 printf("TestTraverse\n"); |
226 InitializeVM(); | 212 InitializeVM(); |
227 v8::HandleScope scope; | 213 v8::HandleScope scope; |
228 Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS]; | 214 Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS]; |
229 InitializeBuildingBlocks(building_blocks); | 215 InitializeBuildingBlocks(building_blocks); |
230 Handle<String> flat = ConstructBalanced(building_blocks); | 216 Handle<String> flat = ConstructBalanced(building_blocks); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 ASSERT(to > from); | 278 ASSERT(to > from); |
293 if (to - from <= 1) | 279 if (to - from <= 1) |
294 return SliceOf(building_blocks[from % NUMBER_OF_BUILDING_BLOCKS]); | 280 return SliceOf(building_blocks[from % NUMBER_OF_BUILDING_BLOCKS]); |
295 if (to - from == 2) { | 281 if (to - from == 2) { |
296 Handle<String> lhs = building_blocks[from % NUMBER_OF_BUILDING_BLOCKS]; | 282 Handle<String> lhs = building_blocks[from % NUMBER_OF_BUILDING_BLOCKS]; |
297 if (gen() % 2 == 0) | 283 if (gen() % 2 == 0) |
298 lhs = SliceOf(lhs); | 284 lhs = SliceOf(lhs); |
299 Handle<String> rhs = building_blocks[(from+1) % NUMBER_OF_BUILDING_BLOCKS]; | 285 Handle<String> rhs = building_blocks[(from+1) % NUMBER_OF_BUILDING_BLOCKS]; |
300 if (gen() % 2 == 0) | 286 if (gen() % 2 == 0) |
301 rhs = SliceOf(rhs); | 287 rhs = SliceOf(rhs); |
302 return Factory::NewConsString(lhs, | 288 return Factory::NewConsString(lhs, rhs); |
303 StringShape(*lhs), | |
304 rhs, | |
305 StringShape(*rhs)); | |
306 } | 289 } |
307 Handle<String> part1 = | 290 Handle<String> part1 = |
308 ConstructBalancedHelper(building_blocks, from, from + ((to - from) / 2)); | 291 ConstructBalancedHelper(building_blocks, from, from + ((to - from) / 2)); |
309 Handle<String> part2 = | 292 Handle<String> part2 = |
310 ConstructBalancedHelper(building_blocks, from + ((to - from) / 2), to); | 293 ConstructBalancedHelper(building_blocks, from + ((to - from) / 2), to); |
311 Handle<String> branch = Factory::NewConsString(part1, | 294 Handle<String> branch = Factory::NewConsString(part1, part2); |
312 StringShape(*part1), | |
313 part2, | |
314 StringShape(*part2)); | |
315 if (gen() % 2 == 0) | 295 if (gen() % 2 == 0) |
316 return branch; | 296 return branch; |
317 return(SliceOf(branch)); | 297 return(SliceOf(branch)); |
318 } | 298 } |
319 | 299 |
320 | 300 |
321 TEST(Slice) { | 301 TEST(Slice) { |
322 printf("TestSlice\n"); | 302 printf("TestSlice\n"); |
323 InitializeVM(); | 303 InitializeVM(); |
324 v8::HandleScope scope; | 304 v8::HandleScope scope; |
(...skipping 19 matching lines...) Expand all Loading... |
344 v8::HandleScope scope; | 324 v8::HandleScope scope; |
345 | 325 |
346 char* foo = NewArray<char>(DEEP_ASCII_DEPTH); | 326 char* foo = NewArray<char>(DEEP_ASCII_DEPTH); |
347 for (int i = 0; i < DEEP_ASCII_DEPTH; i++) { | 327 for (int i = 0; i < DEEP_ASCII_DEPTH; i++) { |
348 foo[i] = "foo "[i % 4]; | 328 foo[i] = "foo "[i % 4]; |
349 } | 329 } |
350 Handle<String> string = | 330 Handle<String> string = |
351 Factory::NewStringFromAscii(Vector<const char>(foo, DEEP_ASCII_DEPTH)); | 331 Factory::NewStringFromAscii(Vector<const char>(foo, DEEP_ASCII_DEPTH)); |
352 Handle<String> foo_string = Factory::NewStringFromAscii(CStrVector("foo")); | 332 Handle<String> foo_string = Factory::NewStringFromAscii(CStrVector("foo")); |
353 for (int i = 0; i < DEEP_ASCII_DEPTH; i += 10) { | 333 for (int i = 0; i < DEEP_ASCII_DEPTH; i += 10) { |
354 string = Factory::NewConsString(string, | 334 string = Factory::NewConsString(string, foo_string); |
355 StringShape(*string), | |
356 foo_string, | |
357 StringShape(*foo_string)); | |
358 } | 335 } |
359 Handle<String> flat_string = Factory::NewConsString(string, | 336 Handle<String> flat_string = Factory::NewConsString(string, foo_string); |
360 StringShape(*string), | |
361 foo_string, | |
362 StringShape(*foo_string)); | |
363 FlattenString(flat_string); | 337 FlattenString(flat_string); |
364 | 338 |
365 for (int i = 0; i < 500; i++) { | 339 for (int i = 0; i < 500; i++) { |
366 TraverseFirst(flat_string, string, DEEP_ASCII_DEPTH); | 340 TraverseFirst(flat_string, string, DEEP_ASCII_DEPTH); |
367 } | 341 } |
368 DeleteArray<char>(foo); | 342 DeleteArray<char>(foo); |
369 } | 343 } |
370 | 344 |
371 | 345 |
372 TEST(Utf8Conversion) { | 346 TEST(Utf8Conversion) { |
(...skipping 28 matching lines...) Expand all Loading... |
401 int written = mixed->WriteUtf8(buffer, i); | 375 int written = mixed->WriteUtf8(buffer, i); |
402 CHECK_EQ(lengths[i], written); | 376 CHECK_EQ(lengths[i], written); |
403 // Check that the contents are correct | 377 // Check that the contents are correct |
404 for (int j = 0; j < lengths[i]; j++) | 378 for (int j = 0; j < lengths[i]; j++) |
405 CHECK_EQ(as_utf8[j], static_cast<unsigned char>(buffer[j])); | 379 CHECK_EQ(as_utf8[j], static_cast<unsigned char>(buffer[j])); |
406 // Check that the rest of the buffer hasn't been touched | 380 // Check that the rest of the buffer hasn't been touched |
407 for (int j = lengths[i]; j < 11; j++) | 381 for (int j = lengths[i]; j < 11; j++) |
408 CHECK_EQ(kNoChar, buffer[j]); | 382 CHECK_EQ(kNoChar, buffer[j]); |
409 } | 383 } |
410 } | 384 } |
OLD | NEW |