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

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

Issue 153913002: A64: Synchronize with r16756. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/a64
Patch Set: Created 6 years, 10 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 | « test/cctest/test-heap.cc ('k') | test/mjsunit/compiler/rotate.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 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 #include <stdlib.h>
29
30 #include "v8.h"
31
32 #include "factory.h"
33 #include "global-handles.h"
34 #include "unique.h"
35 #include "cctest.h"
36
37 using namespace v8::internal;
38
39 template <class T, class U>
40 void CheckHashCodeEqual(Unique<T> a, Unique<U> b) {
41 int64_t hasha = static_cast<int64_t>(a.Hashcode());
42 int64_t hashb = static_cast<int64_t>(b.Hashcode());
43 CHECK_NE(static_cast<int64_t>(0), hasha);
44 CHECK_NE(static_cast<int64_t>(0), hashb);
45 CHECK_EQ(hasha, hashb);
46 }
47
48
49 template <class T, class U>
50 void CheckHashCodeNotEqual(Unique<T> a, Unique<U> b) {
51 int64_t hasha = static_cast<int64_t>(a.Hashcode());
52 int64_t hashb = static_cast<int64_t>(b.Hashcode());
53 CHECK_NE(static_cast<int64_t>(0), hasha);
54 CHECK_NE(static_cast<int64_t>(0), hashb);
55 CHECK_NE(hasha, hashb);
56 }
57
58
59 TEST(UniqueCreate) {
60 CcTest::InitializeVM();
61 Isolate* isolate = Isolate::Current();
62 Factory* factory = isolate->factory();
63 HandleScope sc(isolate);
64
65 Handle<String> A = factory->InternalizeUtf8String("A");
66 Unique<String> HA(A);
67
68 CHECK(*HA.handle() == *A);
69 CHECK_EQ(*A, *HA.handle());
70
71 Unique<String> HA2(A);
72
73 CheckHashCodeEqual(HA, HA2);
74 CHECK(HA == HA2);
75 CHECK_EQ(*HA.handle(), *HA2.handle());
76
77 CHECK(HA2 == HA);
78 CHECK_EQ(*HA2.handle(), *HA.handle());
79
80 Handle<String> B = factory->InternalizeUtf8String("B");
81 Unique<String> HB(B);
82
83 CheckHashCodeNotEqual(HA, HB);
84 CHECK(HA != HB);
85 CHECK_NE(*HA.handle(), *HB.handle());
86
87 CHECK(HB != HA);
88 CHECK_NE(*HB.handle(), *HA.handle());
89
90 // TODO(titzer): check that Unique properly survives a GC.
91 }
92
93
94 TEST(UniqueSubsume) {
95 CcTest::InitializeVM();
96 Isolate* isolate = Isolate::Current();
97 Factory* factory = isolate->factory();
98 HandleScope sc(isolate);
99
100 Handle<String> A = factory->InternalizeUtf8String("A");
101 Unique<String> HA(A);
102
103 CHECK(*HA.handle() == *A);
104 CHECK_EQ(*A, *HA.handle());
105
106 Unique<Object> HO = HA; // Here comes the subsumption, boys.
107
108 CheckHashCodeEqual(HA, HO);
109 CHECK(HA == HO);
110 CHECK_EQ(*HA.handle(), *HO.handle());
111
112 CHECK(HO == HA);
113 CHECK_EQ(*HO.handle(), *HA.handle());
114 }
115
116
117 TEST(UniqueSet_Add) {
118 CcTest::InitializeVM();
119 Isolate* isolate = Isolate::Current();
120 Factory* factory = isolate->factory();
121 HandleScope sc(isolate);
122
123 Unique<String> A(factory->InternalizeUtf8String("A"));
124 Unique<String> B(factory->InternalizeUtf8String("B"));
125 Unique<String> C(factory->InternalizeUtf8String("C"));
126
127 Zone zone(isolate);
128
129 UniqueSet<String>* set = new(&zone) UniqueSet<String>();
130
131 CHECK_EQ(0, set->size());
132 set->Add(A, &zone);
133 CHECK_EQ(1, set->size());
134 set->Add(A, &zone);
135 CHECK_EQ(1, set->size());
136 set->Add(B, &zone);
137 CHECK_EQ(2, set->size());
138 set->Add(C, &zone);
139 CHECK_EQ(3, set->size());
140 set->Add(C, &zone);
141 CHECK_EQ(3, set->size());
142 set->Add(B, &zone);
143 CHECK_EQ(3, set->size());
144 set->Add(A, &zone);
145 CHECK_EQ(3, set->size());
146 }
147
148
149 TEST(UniqueSet_Contains) {
150 CcTest::InitializeVM();
151 Isolate* isolate = Isolate::Current();
152 Factory* factory = isolate->factory();
153 HandleScope sc(isolate);
154
155 Unique<String> A(factory->InternalizeUtf8String("A"));
156 Unique<String> B(factory->InternalizeUtf8String("B"));
157 Unique<String> C(factory->InternalizeUtf8String("C"));
158
159 Zone zone(isolate);
160
161 UniqueSet<String>* set = new(&zone) UniqueSet<String>();
162
163 CHECK_EQ(0, set->size());
164 set->Add(A, &zone);
165 CHECK(set->Contains(A));
166 CHECK(!set->Contains(B));
167 CHECK(!set->Contains(C));
168
169 set->Add(A, &zone);
170 CHECK(set->Contains(A));
171 CHECK(!set->Contains(B));
172 CHECK(!set->Contains(C));
173
174 set->Add(B, &zone);
175 CHECK(set->Contains(A));
176 CHECK(set->Contains(B));
177
178 set->Add(C, &zone);
179 CHECK(set->Contains(A));
180 CHECK(set->Contains(B));
181 CHECK(set->Contains(C));
182 }
183
184
185 TEST(UniqueSet_At) {
186 CcTest::InitializeVM();
187 Isolate* isolate = Isolate::Current();
188 Factory* factory = isolate->factory();
189 HandleScope sc(isolate);
190
191 Unique<String> A(factory->InternalizeUtf8String("A"));
192 Unique<String> B(factory->InternalizeUtf8String("B"));
193 Unique<String> C(factory->InternalizeUtf8String("C"));
194
195 Zone zone(isolate);
196
197 UniqueSet<String>* set = new(&zone) UniqueSet<String>();
198
199 CHECK_EQ(0, set->size());
200 set->Add(A, &zone);
201 CHECK(A == set->at(0));
202
203 set->Add(A, &zone);
204 CHECK(A == set->at(0));
205
206 set->Add(B, &zone);
207 CHECK(A == set->at(0) || B == set->at(0));
208 CHECK(A == set->at(1) || B == set->at(1));
209
210 set->Add(C, &zone);
211 CHECK(A == set->at(0) || B == set->at(0) || C == set->at(0));
212 CHECK(A == set->at(1) || B == set->at(1) || C == set->at(1));
213 CHECK(A == set->at(2) || B == set->at(2) || C == set->at(2));
214 }
215
216
217 template <class T>
218 static void CHECK_SETS(
219 UniqueSet<T>* set1, UniqueSet<T>* set2, bool expected) {
220 CHECK(set1->Equals(set1));
221 CHECK(set2->Equals(set2));
222 CHECK(expected == set1->Equals(set2));
223 CHECK(expected == set2->Equals(set1));
224 }
225
226
227 TEST(UniqueSet_Equals) {
228 CcTest::InitializeVM();
229 Isolate* isolate = Isolate::Current();
230 Factory* factory = isolate->factory();
231 HandleScope sc(isolate);
232
233 Unique<String> A(factory->InternalizeUtf8String("A"));
234 Unique<String> B(factory->InternalizeUtf8String("B"));
235 Unique<String> C(factory->InternalizeUtf8String("C"));
236
237 Zone zone(isolate);
238
239 UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
240 UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
241
242 CHECK_SETS(set1, set2, true);
243
244 set1->Add(A, &zone);
245
246 CHECK_SETS(set1, set2, false);
247
248 set2->Add(A, &zone);
249
250 CHECK_SETS(set1, set2, true);
251
252 set1->Add(B, &zone);
253
254 CHECK_SETS(set1, set2, false);
255
256 set2->Add(C, &zone);
257
258 CHECK_SETS(set1, set2, false);
259
260 set1->Add(C, &zone);
261
262 CHECK_SETS(set1, set2, false);
263
264 set2->Add(B, &zone);
265
266 CHECK_SETS(set1, set2, true);
267 }
268
269
270 TEST(UniqueSet_IsSubset1) {
271 CcTest::InitializeVM();
272 Isolate* isolate = Isolate::Current();
273 Factory* factory = isolate->factory();
274 HandleScope sc(isolate);
275
276 Unique<String> A(factory->InternalizeUtf8String("A"));
277 Unique<String> B(factory->InternalizeUtf8String("B"));
278 Unique<String> C(factory->InternalizeUtf8String("C"));
279
280 Zone zone(isolate);
281
282 UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
283 UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
284
285 CHECK(set1->IsSubset(set2));
286 CHECK(set2->IsSubset(set1));
287
288 set1->Add(A, &zone);
289
290 CHECK(!set1->IsSubset(set2));
291 CHECK(set2->IsSubset(set1));
292
293 set2->Add(B, &zone);
294
295 CHECK(!set1->IsSubset(set2));
296 CHECK(!set2->IsSubset(set1));
297
298 set2->Add(A, &zone);
299
300 CHECK(set1->IsSubset(set2));
301 CHECK(!set2->IsSubset(set1));
302
303 set1->Add(B, &zone);
304
305 CHECK(set1->IsSubset(set2));
306 CHECK(set2->IsSubset(set1));
307 }
308
309
310 TEST(UniqueSet_IsSubset2) {
311 CcTest::InitializeVM();
312 Isolate* isolate = Isolate::Current();
313 Factory* factory = isolate->factory();
314 HandleScope sc(isolate);
315
316 Unique<String> A(factory->InternalizeUtf8String("A"));
317 Unique<String> B(factory->InternalizeUtf8String("B"));
318 Unique<String> C(factory->InternalizeUtf8String("C"));
319 Unique<String> D(factory->InternalizeUtf8String("D"));
320 Unique<String> E(factory->InternalizeUtf8String("E"));
321 Unique<String> F(factory->InternalizeUtf8String("F"));
322 Unique<String> G(factory->InternalizeUtf8String("G"));
323
324 Zone zone(isolate);
325
326 UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
327 UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
328
329 set1->Add(A, &zone);
330 set1->Add(C, &zone);
331 set1->Add(E, &zone);
332
333 set2->Add(A, &zone);
334 set2->Add(B, &zone);
335 set2->Add(C, &zone);
336 set2->Add(D, &zone);
337 set2->Add(E, &zone);
338 set2->Add(F, &zone);
339
340 CHECK(set1->IsSubset(set2));
341 CHECK(!set2->IsSubset(set1));
342
343 set1->Add(G, &zone);
344
345 CHECK(!set1->IsSubset(set2));
346 CHECK(!set2->IsSubset(set1));
347 }
348
349
350 template <class T>
351 static UniqueSet<T>* MakeSet(Zone* zone, int which, Unique<T>* elements) {
352 UniqueSet<T>* set = new(zone) UniqueSet<T>();
353 for (int i = 0; i < 32; i++) {
354 if ((which & (1 << i)) != 0) set->Add(elements[i], zone);
355 }
356 return set;
357 }
358
359
360 TEST(UniqueSet_IsSubsetExhaustive) {
361 const int kSetSize = 6;
362
363 CcTest::InitializeVM();
364 Isolate* isolate = Isolate::Current();
365 Factory* factory = isolate->factory();
366 HandleScope sc(isolate);
367
368 Zone zone(isolate);
369
370 Unique<String> A(factory->InternalizeUtf8String("A"));
371 Unique<String> B(factory->InternalizeUtf8String("B"));
372 Unique<String> C(factory->InternalizeUtf8String("C"));
373 Unique<String> D(factory->InternalizeUtf8String("D"));
374 Unique<String> E(factory->InternalizeUtf8String("E"));
375 Unique<String> F(factory->InternalizeUtf8String("F"));
376 Unique<String> G(factory->InternalizeUtf8String("G"));
377
378 Unique<String> elements[] = {
379 A, B, C, D, E, F, G
380 };
381
382 // Exhaustively test all sets with <= 6 elements.
383 for (int i = 0; i < (1 << kSetSize); i++) {
384 for (int j = 0; j < (1 << kSetSize); j++) {
385 UniqueSet<String>* set1 = MakeSet(&zone, i, elements);
386 UniqueSet<String>* set2 = MakeSet(&zone, j, elements);
387
388 CHECK(((i & j) == i) == set1->IsSubset(set2));
389 }
390 }
391 }
392
393
394 TEST(UniqueSet_Intersect1) {
395 CcTest::InitializeVM();
396 Isolate* isolate = Isolate::Current();
397 Factory* factory = isolate->factory();
398 HandleScope sc(isolate);
399
400 Unique<String> A(factory->InternalizeUtf8String("A"));
401 Unique<String> B(factory->InternalizeUtf8String("B"));
402 Unique<String> C(factory->InternalizeUtf8String("C"));
403
404 Zone zone(isolate);
405
406 UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
407 UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
408 UniqueSet<String>* result;
409
410 CHECK(set1->IsSubset(set2));
411 CHECK(set2->IsSubset(set1));
412
413 set1->Add(A, &zone);
414
415 result = set1->Intersect(set2, &zone);
416
417 CHECK_EQ(0, result->size());
418 CHECK(set2->Equals(result));
419
420 set2->Add(A, &zone);
421
422 result = set1->Intersect(set2, &zone);
423
424 CHECK_EQ(1, result->size());
425 CHECK(set1->Equals(result));
426 CHECK(set2->Equals(result));
427
428 set2->Add(B, &zone);
429 set2->Add(C, &zone);
430
431 result = set1->Intersect(set2, &zone);
432
433 CHECK_EQ(1, result->size());
434 CHECK(set1->Equals(result));
435 }
436
437
438 TEST(UniqueSet_IntersectExhaustive) {
439 const int kSetSize = 6;
440
441 CcTest::InitializeVM();
442 Isolate* isolate = Isolate::Current();
443 Factory* factory = isolate->factory();
444 HandleScope sc(isolate);
445
446 Zone zone(isolate);
447
448 Unique<String> A(factory->InternalizeUtf8String("A"));
449 Unique<String> B(factory->InternalizeUtf8String("B"));
450 Unique<String> C(factory->InternalizeUtf8String("C"));
451 Unique<String> D(factory->InternalizeUtf8String("D"));
452 Unique<String> E(factory->InternalizeUtf8String("E"));
453 Unique<String> F(factory->InternalizeUtf8String("F"));
454 Unique<String> G(factory->InternalizeUtf8String("G"));
455
456 Unique<String> elements[] = {
457 A, B, C, D, E, F, G
458 };
459
460 // Exhaustively test all sets with <= 6 elements.
461 for (int i = 0; i < (1 << kSetSize); i++) {
462 for (int j = 0; j < (1 << kSetSize); j++) {
463 UniqueSet<String>* set1 = MakeSet(&zone, i, elements);
464 UniqueSet<String>* set2 = MakeSet(&zone, j, elements);
465
466 UniqueSet<String>* result = set1->Intersect(set2, &zone);
467 UniqueSet<String>* expected = MakeSet(&zone, i & j, elements);
468
469 CHECK(result->Equals(expected));
470 CHECK(expected->Equals(result));
471 }
472 }
473 }
474
475
476 TEST(UniqueSet_Union1) {
477 CcTest::InitializeVM();
478 Isolate* isolate = Isolate::Current();
479 Factory* factory = isolate->factory();
480 HandleScope sc(isolate);
481
482 Unique<String> A(factory->InternalizeUtf8String("A"));
483 Unique<String> B(factory->InternalizeUtf8String("B"));
484 Unique<String> C(factory->InternalizeUtf8String("C"));
485
486 Zone zone(isolate);
487
488 UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
489 UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
490 UniqueSet<String>* result;
491
492 CHECK(set1->IsSubset(set2));
493 CHECK(set2->IsSubset(set1));
494
495 set1->Add(A, &zone);
496
497 result = set1->Union(set2, &zone);
498
499 CHECK_EQ(1, result->size());
500 CHECK(set1->Equals(result));
501
502 set2->Add(A, &zone);
503
504 result = set1->Union(set2, &zone);
505
506 CHECK_EQ(1, result->size());
507 CHECK(set1->Equals(result));
508 CHECK(set2->Equals(result));
509
510 set2->Add(B, &zone);
511 set2->Add(C, &zone);
512
513 result = set1->Union(set2, &zone);
514
515 CHECK_EQ(3, result->size());
516 CHECK(set2->Equals(result));
517 }
518
519
520 TEST(UniqueSet_UnionExhaustive) {
521 const int kSetSize = 6;
522
523 CcTest::InitializeVM();
524 Isolate* isolate = Isolate::Current();
525 Factory* factory = isolate->factory();
526 HandleScope sc(isolate);
527
528 Zone zone(isolate);
529
530 Unique<String> A(factory->InternalizeUtf8String("A"));
531 Unique<String> B(factory->InternalizeUtf8String("B"));
532 Unique<String> C(factory->InternalizeUtf8String("C"));
533 Unique<String> D(factory->InternalizeUtf8String("D"));
534 Unique<String> E(factory->InternalizeUtf8String("E"));
535 Unique<String> F(factory->InternalizeUtf8String("F"));
536 Unique<String> G(factory->InternalizeUtf8String("G"));
537
538 Unique<String> elements[] = {
539 A, B, C, D, E, F, G
540 };
541
542 // Exhaustively test all sets with <= 6 elements.
543 for (int i = 0; i < (1 << kSetSize); i++) {
544 for (int j = 0; j < (1 << kSetSize); j++) {
545 UniqueSet<String>* set1 = MakeSet(&zone, i, elements);
546 UniqueSet<String>* set2 = MakeSet(&zone, j, elements);
547
548 UniqueSet<String>* result = set1->Union(set2, &zone);
549 UniqueSet<String>* expected = MakeSet(&zone, i | j, elements);
550
551 CHECK(result->Equals(expected));
552 CHECK(expected->Equals(result));
553 }
554 }
555 }
556
OLDNEW
« no previous file with comments | « test/cctest/test-heap.cc ('k') | test/mjsunit/compiler/rotate.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698