Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(825)

Side by Side Diff: test/cctest/test-utils.cc

Issue 433813002: Add iteration macros. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: No rvalues with MSVC Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/utils.h ('k') | tools/presubmit.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include <stdlib.h> 28 #include <stdlib.h>
29 29
30 #include <deque>
31 #include <list>
32
30 #include "src/v8.h" 33 #include "src/v8.h"
31 34
32 #include "src/base/platform/platform.h" 35 #include "src/base/platform/platform.h"
36 #include "src/ostreams.h"
33 #include "src/utils-inl.h" 37 #include "src/utils-inl.h"
34 #include "test/cctest/cctest.h" 38 #include "test/cctest/cctest.h"
35 39
36 using namespace v8::internal; 40 using namespace v8::internal;
37 41
38 42
39 TEST(Utils1) { 43 TEST(Utils1) {
40 CHECK_EQ(-1000000, FastD2I(-1000000.0)); 44 CHECK_EQ(-1000000, FastD2I(-1000000.0));
41 CHECK_EQ(-1, FastD2I(-1.0)); 45 CHECK_EQ(-1, FastD2I(-1.0));
42 CHECK_EQ(0, FastD2I(0.0)); 46 CHECK_EQ(0, FastD2I(0.0));
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 TEST(SequenceCollectorRegression) { 215 TEST(SequenceCollectorRegression) {
212 SequenceCollector<char> collector(16); 216 SequenceCollector<char> collector(16);
213 collector.StartSequence(); 217 collector.StartSequence();
214 collector.Add('0'); 218 collector.Add('0');
215 collector.AddBlock( 219 collector.AddBlock(
216 i::Vector<const char>("12345678901234567890123456789012", 32)); 220 i::Vector<const char>("12345678901234567890123456789012", 32));
217 i::Vector<char> seq = collector.EndSequence(); 221 i::Vector<char> seq = collector.EndSequence();
218 CHECK_EQ(0, strncmp("0123456789012345678901234567890123", 222 CHECK_EQ(0, strncmp("0123456789012345678901234567890123",
219 seq.start(), seq.length())); 223 seq.start(), seq.length()));
220 } 224 }
225
226
227 // The tests for the iteration macros below are slightly modified examples taken
228 // from the Boost docs at http://www.boost.org/doc/libs/release/libs/foreach/.
229
230 // Iterate over an STL container.
231 TEST(ForeachSTL) {
232 OStringStream os;
233 std::list<int> list_int;
234 list_int.push_back(11);
235 list_int.push_back(22);
236 list_int.push_back(33);
237
238 V8_FOREACH_C(int i, list_int) { os << i << " "; }
239 V8_FOREACH(int i, list_int) { os << i << " "; }
240
241 CHECK_EQ("11 22 33 11 22 33 ", os.c_str());
242 }
243
244
245 // Iteration over arrays is not supported yet.
246 #if 0
247 // Iterate over an array, with covariance (i.e., the type of the iteration
248 // variable is not exactly the same as the element type of the container).
249 TEST(ForeachArrayCovariance) {
250 OStringStream os;
251 short array_short[] = {111, 222, 333}; // NOLINT
252
253 V8_FOREACH(int i, array_short) {
254 // The short was implicitly converted to an int
255 os << i << " ";
256 }
257
258 CHECK_EQ("111 222 333 ", os.c_str());
259 }
260 #endif
261
262
263 // Predeclare the loop variable, and use continue + break in the loop body.
264 TEST(ForeachContinueBreak) {
265 OStringStream os;
266 std::deque<int> deque_int;
267 deque_int.push_back(1111);
268 deque_int.push_back(2222);
269 deque_int.push_back(3333);
270 deque_int.push_back(4444);
271 deque_int.push_back(5555);
272
273 int i = 0;
274 V8_FOREACH_C(i, deque_int) {
275 if (i == 2222) continue;
276 if (i == 4444) break;
277 os << i << " ";
278 }
279
280 CHECK_EQ("1111 3333 ", os.c_str());
281 }
282
283
284 // Iteration over arrays is not supported yet.
285 #if 0
286 // Iterate over a sequence by reference, and modify the underlying sequence.
287 TEST(ForeachModify) {
288 short array_short[] = {1, 2, 3}; // NOLINT
289
290 V8_FOREACH(short& i, array_short) { ++i; } // NOLINT
291
292 CHECK_EQ(2, array_short[0]);
293 CHECK_EQ(3, array_short[1]);
294 CHECK_EQ(4, array_short[2]);
295 }
296 #endif
297
298
299 // Iterate over a vector of vectors with nested V8_FOREACH_ loops. In this
300 // example, notice that braces around the loop body are not necessary:
301 TEST(ForeachNested) {
302 std::vector<int> row0;
303 row0.push_back(11);
304 std::vector<int> row1;
305 row1.push_back(22);
306 row1.push_back(33);
307 std::vector<std::vector<int> > matrix_int;
308 matrix_int.push_back(row0);
309 matrix_int.push_back(row1);
310
311 V8_FOREACH(std::vector<int>& row, matrix_int)
312 V8_FOREACH(int& i, row)
313 ++i;
314
315 CHECK_EQ(12, matrix_int[0][0]);
316 CHECK_EQ(23, matrix_int[1][0]);
317 CHECK_EQ(34, matrix_int[1][1]);
318
319 OStringStream os;
320
321 V8_FOREACH_C_REV(const std::vector<int>& row, matrix_int)
322 V8_FOREACH_C_REV(const int& i, row)
323 os << i << " ";
324
325 CHECK_EQ("34 23 12 ", os.c_str());
326 }
327
328
329 static int get_vector_float_counter = 0;
330
331 std::vector<float> get_vector_float() {
332 get_vector_float_counter++;
333 std::vector<float> v;
334 v.push_back(1.5);
335 v.push_back(2.5);
336 v.push_back(3.5);
337 return v;
338 }
339
340
341 #if !V8_FOREACH_RVALUE_BROKEN
342 // Iterate over an expression that returns a sequence by value (i.e. an rvalue).
343 TEST(ForeachRvalue) {
344 OStringStream os;
345
346 V8_FOREACH(float f, get_vector_float()) { os << f << " "; }
347
348 CHECK_EQ(1, get_vector_float_counter);
349 CHECK_EQ("1.5 2.5 3.5 ", os.c_str());
350 }
351 #endif
352
353
354 // Iterate in reverse.
355 TEST(ForeachReverse) {
356 OStringStream os;
357 std::list<int> list_int;
358 list_int.push_back(12);
359 list_int.push_back(34);
360 list_int.push_back(56);
361
362 V8_FOREACH_C_REV(int i, list_int) { os << i << " "; }
363 V8_FOREACH_REV(int i, list_int) { os << i << " "; }
364
365 CHECK_EQ("56 34 12 56 34 12 ", os.c_str());
366 }
367
368
369 struct Foo {
370 Foo(int aa, int bb, int cc) : a(aa), b(bb), c(cc) {}
371 Foo(const Foo& f) {
372 a = f.a;
373 b = f.b;
374 c = f.c;
375 copy_constructor_count++;
376 }
377
378 static int copy_constructor_count;
379
380 int a, b, c;
381 };
382
383
384 int Foo::copy_constructor_count = 0;
385
386
387 // Check that V8_FOREACH_ does not copy the container.
388 TEST(ForeachCopy) {
389 OStringStream os;
390
391 std::list<Foo> list_foo;
392 list_foo.push_back(Foo(1, 2, 3));
393 list_foo.push_back(Foo(4, 5, 6));
394 list_foo.push_back(Foo(7, 8, 9));
395
396 CHECK_EQ(3, Foo::copy_constructor_count);
397
398 V8_FOREACH(Foo f, list_foo)
399 os << f.a << f.b << f.c << " ";
400
401 CHECK_EQ("123 456 789 ", os.c_str());
402 CHECK_EQ(6, Foo::copy_constructor_count);
403
404 V8_FOREACH(const Foo& f, list_foo)
405 os << f.a << f.b << f.c << " ";
406
407 CHECK_EQ("123 456 789 123 456 789 ", os.c_str());
408 CHECK_EQ(6, Foo::copy_constructor_count);
409 }
OLDNEW
« no previous file with comments | « src/utils.h ('k') | tools/presubmit.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698