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

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

Issue 23609020: First implementation of HUnique<T> and HUniqueSet<T>, which is supposed to replace UniqueValueId. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Rename HUnique to Unique. Created 7 years, 3 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/cctest.gyp ('k') | tools/gyp/v8.gyp » ('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 TEST(UniqueCreate) {
40 CcTest::InitializeVM();
41 Isolate* isolate = Isolate::Current();
42 Factory* factory = isolate->factory();
43 HandleScope sc(isolate);
44
45 Handle<String> A = factory->InternalizeUtf8String("A");
46 Unique<String> HA(A);
47
48 CHECK_NE(0, HA.Hashcode());
49 CHECK(*HA.handle() == *A);
50 CHECK_EQ(*A, *HA.handle());
51
52 Unique<String> HA2(A);
53
54 CHECK_EQ(HA.Hashcode(), HA2.Hashcode());
55 CHECK(HA == HA2);
56 CHECK_EQ(*HA.handle(), *HA2.handle());
57
58 CHECK_EQ(HA2.Hashcode(), HA.Hashcode());
59 CHECK(HA2 == HA);
60 CHECK_EQ(*HA2.handle(), *HA.handle());
61
62 Handle<String> B = factory->InternalizeUtf8String("B");
63 Unique<String> HB(B);
64
65 CHECK_NE(HA.Hashcode(), HB.Hashcode());
66 CHECK(HA != HB);
67 CHECK_NE(*HA.handle(), *HB.handle());
68
69 CHECK_NE(HB.Hashcode(), HA.Hashcode());
70 CHECK(HB != HA);
71 CHECK_NE(*HB.handle(), *HA.handle());
72
73 // TODO(titzer): check that Unique properly survives a GC.
74 }
75
76
77 TEST(UniqueSubsume) {
78 CcTest::InitializeVM();
79 Isolate* isolate = Isolate::Current();
80 Factory* factory = isolate->factory();
81 HandleScope sc(isolate);
82
83 Handle<String> A = factory->InternalizeUtf8String("A");
84 Unique<String> HA(A);
85
86 CHECK_NE(0, HA.Hashcode());
87 CHECK(*HA.handle() == *A);
88 CHECK_EQ(*A, *HA.handle());
89
90 Unique<Object> HO = HA; // Here comes the subsumption, boys.
91
92 CHECK_EQ(HA.Hashcode(), HO.Hashcode());
93 CHECK(HA == HO);
94 CHECK_EQ(*HA.handle(), *HO.handle());
95
96 CHECK_EQ(HO.Hashcode(), HA.Hashcode());
97 CHECK(HO == HA);
98 CHECK_EQ(*HO.handle(), *HA.handle());
99 }
100
101
102 TEST(UniqueSet_Add) {
103 CcTest::InitializeVM();
104 Isolate* isolate = Isolate::Current();
105 Factory* factory = isolate->factory();
106 HandleScope sc(isolate);
107
108 Unique<String> A(factory->InternalizeUtf8String("A"));
109 Unique<String> B(factory->InternalizeUtf8String("B"));
110 Unique<String> C(factory->InternalizeUtf8String("C"));
111
112 Zone zone(isolate);
113
114 UniqueSet<String>* set = new(&zone) UniqueSet<String>();
115
116 CHECK_EQ(0, set->size());
117 set->Add(A, &zone);
118 CHECK_EQ(1, set->size());
119 set->Add(A, &zone);
120 CHECK_EQ(1, set->size());
121 set->Add(B, &zone);
122 CHECK_EQ(2, set->size());
123 set->Add(C, &zone);
124 CHECK_EQ(3, set->size());
125 set->Add(C, &zone);
126 CHECK_EQ(3, set->size());
127 set->Add(B, &zone);
128 CHECK_EQ(3, set->size());
129 set->Add(A, &zone);
130 CHECK_EQ(3, set->size());
131 }
132
133
134 template <class T>
135 static void CHECK_SETS(
136 UniqueSet<T>* set1, UniqueSet<T>* set2, bool expected) {
137 CHECK(set1->Equals(set1));
138 CHECK(set2->Equals(set2));
139 CHECK(expected == set1->Equals(set2));
140 CHECK(expected == set2->Equals(set1));
141 }
142
143
144 TEST(UniqueSet_Equals) {
145 CcTest::InitializeVM();
146 Isolate* isolate = Isolate::Current();
147 Factory* factory = isolate->factory();
148 HandleScope sc(isolate);
149
150 Unique<String> A(factory->InternalizeUtf8String("A"));
151 Unique<String> B(factory->InternalizeUtf8String("B"));
152 Unique<String> C(factory->InternalizeUtf8String("C"));
153
154 Zone zone(isolate);
155
156 UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
157 UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
158
159 CHECK_SETS(set1, set2, true);
160
161 set1->Add(A, &zone);
162
163 CHECK_SETS(set1, set2, false);
164
165 set2->Add(A, &zone);
166
167 CHECK_SETS(set1, set2, true);
168
169 set1->Add(B, &zone);
170
171 CHECK_SETS(set1, set2, false);
172
173 set2->Add(C, &zone);
174
175 CHECK_SETS(set1, set2, false);
176
177 set1->Add(C, &zone);
178
179 CHECK_SETS(set1, set2, false);
180
181 set2->Add(B, &zone);
182
183 CHECK_SETS(set1, set2, true);
184 }
185
186
187 TEST(UniqueSet_IsSubset1) {
188 CcTest::InitializeVM();
189 Isolate* isolate = Isolate::Current();
190 Factory* factory = isolate->factory();
191 HandleScope sc(isolate);
192
193 Unique<String> A(factory->InternalizeUtf8String("A"));
194 Unique<String> B(factory->InternalizeUtf8String("B"));
195 Unique<String> C(factory->InternalizeUtf8String("C"));
196
197 Zone zone(isolate);
198
199 UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
200 UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
201
202 CHECK(set1->IsSubset(set2));
203 CHECK(set2->IsSubset(set1));
204
205 set1->Add(A, &zone);
206
207 CHECK(!set1->IsSubset(set2));
208 CHECK(set2->IsSubset(set1));
209
210 set2->Add(B, &zone);
211
212 CHECK(!set1->IsSubset(set2));
213 CHECK(!set2->IsSubset(set1));
214
215 set2->Add(A, &zone);
216
217 CHECK(set1->IsSubset(set2));
218 CHECK(!set2->IsSubset(set1));
219
220 set1->Add(B, &zone);
221
222 CHECK(set1->IsSubset(set2));
223 CHECK(set2->IsSubset(set1));
224 }
225
226
227 TEST(UniqueSet_IsSubset2) {
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 Unique<String> D(factory->InternalizeUtf8String("D"));
237 Unique<String> E(factory->InternalizeUtf8String("E"));
238 Unique<String> F(factory->InternalizeUtf8String("F"));
239 Unique<String> G(factory->InternalizeUtf8String("G"));
240
241 Zone zone(isolate);
242
243 UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
244 UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
245
246 set1->Add(A, &zone);
247 set1->Add(C, &zone);
248 set1->Add(E, &zone);
249
250 set2->Add(A, &zone);
251 set2->Add(B, &zone);
252 set2->Add(C, &zone);
253 set2->Add(D, &zone);
254 set2->Add(E, &zone);
255 set2->Add(F, &zone);
256
257 CHECK(set1->IsSubset(set2));
258 CHECK(!set2->IsSubset(set1));
259
260 set1->Add(G, &zone);
261
262 CHECK(!set1->IsSubset(set2));
263 CHECK(!set2->IsSubset(set1));
264 }
265
266
267 template <class T>
268 static UniqueSet<T>* MakeSet(Zone* zone, int which, Unique<T>* elements) {
269 UniqueSet<T>* set = new(zone) UniqueSet<T>();
270 for (int i = 0; i < 32; i++) {
271 if ((which & (1 << i)) != 0) set->Add(elements[i], zone);
272 }
273 return set;
274 }
275
276
277 TEST(UniqueSet_IsSubsetExhaustive) {
278 const int kSetSize = 6;
279
280 CcTest::InitializeVM();
281 Isolate* isolate = Isolate::Current();
282 Factory* factory = isolate->factory();
283 HandleScope sc(isolate);
284
285 Zone zone(isolate);
286
287 Unique<String> A(factory->InternalizeUtf8String("A"));
288 Unique<String> B(factory->InternalizeUtf8String("B"));
289 Unique<String> C(factory->InternalizeUtf8String("C"));
290 Unique<String> D(factory->InternalizeUtf8String("D"));
291 Unique<String> E(factory->InternalizeUtf8String("E"));
292 Unique<String> F(factory->InternalizeUtf8String("F"));
293 Unique<String> G(factory->InternalizeUtf8String("G"));
294
295 Unique<String> elements[] = {
296 A, B, C, D, E, F, G
297 };
298
299 // Exhaustively test all sets with <= 6 elements.
300 for (int i = 0; i < (1 << kSetSize); i++) {
301 for (int j = 0; j < (1 << kSetSize); j++) {
302 UniqueSet<String>* set1 = MakeSet(&zone, i, elements);
303 UniqueSet<String>* set2 = MakeSet(&zone, j, elements);
304
305 CHECK(((i & j) == i) == set1->IsSubset(set2));
306 }
307 }
308 }
309
310
311 TEST(UniqueSet_Intersect1) {
312 CcTest::InitializeVM();
313 Isolate* isolate = Isolate::Current();
314 Factory* factory = isolate->factory();
315 HandleScope sc(isolate);
316
317 Unique<String> A(factory->InternalizeUtf8String("A"));
318 Unique<String> B(factory->InternalizeUtf8String("B"));
319 Unique<String> C(factory->InternalizeUtf8String("C"));
320
321 Zone zone(isolate);
322
323 UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
324 UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
325 UniqueSet<String>* result;
326
327 CHECK(set1->IsSubset(set2));
328 CHECK(set2->IsSubset(set1));
329
330 set1->Add(A, &zone);
331
332 result = set1->Intersect(set2, &zone);
333
334 CHECK_EQ(0, result->size());
335 CHECK(set2->Equals(result));
336
337 set2->Add(A, &zone);
338
339 result = set1->Intersect(set2, &zone);
340
341 CHECK_EQ(1, result->size());
342 CHECK(set1->Equals(result));
343 CHECK(set2->Equals(result));
344
345 set2->Add(B, &zone);
346 set2->Add(C, &zone);
347
348 result = set1->Intersect(set2, &zone);
349
350 CHECK_EQ(1, result->size());
351 CHECK(set1->Equals(result));
352 }
353
354
355 TEST(UniqueSet_IntersectExhaustive) {
356 const int kSetSize = 6;
357
358 CcTest::InitializeVM();
359 Isolate* isolate = Isolate::Current();
360 Factory* factory = isolate->factory();
361 HandleScope sc(isolate);
362
363 Zone zone(isolate);
364
365 Unique<String> A(factory->InternalizeUtf8String("A"));
366 Unique<String> B(factory->InternalizeUtf8String("B"));
367 Unique<String> C(factory->InternalizeUtf8String("C"));
368 Unique<String> D(factory->InternalizeUtf8String("D"));
369 Unique<String> E(factory->InternalizeUtf8String("E"));
370 Unique<String> F(factory->InternalizeUtf8String("F"));
371 Unique<String> G(factory->InternalizeUtf8String("G"));
372
373 Unique<String> elements[] = {
374 A, B, C, D, E, F, G
375 };
376
377 // Exhaustively test all sets with <= 6 elements.
378 for (int i = 0; i < (1 << kSetSize); i++) {
379 for (int j = 0; j < (1 << kSetSize); j++) {
380 UniqueSet<String>* set1 = MakeSet(&zone, i, elements);
381 UniqueSet<String>* set2 = MakeSet(&zone, j, elements);
382
383 UniqueSet<String>* result = set1->Intersect(set2, &zone);
384 UniqueSet<String>* expected = MakeSet(&zone, i & j, elements);
385
386 CHECK(result->Equals(expected));
387 CHECK(expected->Equals(result));
388 }
389 }
390 }
391
392
393 TEST(UniqueSet_Union1) {
394 CcTest::InitializeVM();
395 Isolate* isolate = Isolate::Current();
396 Factory* factory = isolate->factory();
397 HandleScope sc(isolate);
398
399 Unique<String> A(factory->InternalizeUtf8String("A"));
400 Unique<String> B(factory->InternalizeUtf8String("B"));
401 Unique<String> C(factory->InternalizeUtf8String("C"));
402
403 Zone zone(isolate);
404
405 UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
406 UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
407 UniqueSet<String>* result;
408
409 CHECK(set1->IsSubset(set2));
410 CHECK(set2->IsSubset(set1));
411
412 set1->Add(A, &zone);
413
414 result = set1->Union(set2, &zone);
415
416 CHECK_EQ(1, result->size());
417 CHECK(set1->Equals(result));
418
419 set2->Add(A, &zone);
420
421 result = set1->Union(set2, &zone);
422
423 CHECK_EQ(1, result->size());
424 CHECK(set1->Equals(result));
425 CHECK(set2->Equals(result));
426
427 set2->Add(B, &zone);
428 set2->Add(C, &zone);
429
430 result = set1->Union(set2, &zone);
431
432 CHECK_EQ(3, result->size());
433 CHECK(set2->Equals(result));
434 }
435
436
437 TEST(UniqueSet_UnionExhaustive) {
438 const int kSetSize = 6;
439
440 CcTest::InitializeVM();
441 Isolate* isolate = Isolate::Current();
442 Factory* factory = isolate->factory();
443 HandleScope sc(isolate);
444
445 Zone zone(isolate);
446
447 Unique<String> A(factory->InternalizeUtf8String("A"));
448 Unique<String> B(factory->InternalizeUtf8String("B"));
449 Unique<String> C(factory->InternalizeUtf8String("C"));
450 Unique<String> D(factory->InternalizeUtf8String("D"));
451 Unique<String> E(factory->InternalizeUtf8String("E"));
452 Unique<String> F(factory->InternalizeUtf8String("F"));
453 Unique<String> G(factory->InternalizeUtf8String("G"));
454
455 Unique<String> elements[] = {
456 A, B, C, D, E, F, G
457 };
458
459 // Exhaustively test all sets with <= 6 elements.
460 for (int i = 0; i < (1 << kSetSize); i++) {
461 for (int j = 0; j < (1 << kSetSize); j++) {
462 UniqueSet<String>* set1 = MakeSet(&zone, i, elements);
463 UniqueSet<String>* set2 = MakeSet(&zone, j, elements);
464
465 UniqueSet<String>* result = set1->Union(set2, &zone);
466 UniqueSet<String>* expected = MakeSet(&zone, i | j, elements);
467
468 CHECK(result->Equals(expected));
469 CHECK(expected->Equals(result));
470 }
471 }
472 }
473
OLDNEW
« no previous file with comments | « test/cctest/cctest.gyp ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698