First implementation of HUnique<T> and HUniqueSet<T>, which is supposed to replace UniqueValueId.
BUG= R=rossberg@chromium.org, verwaest@chromium.org Review URL: https://codereview.chromium.org/23609020 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@16683 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
bd5fa9c93b
commit
dc901fae7f
14
src/checks.h
14
src/checks.h
@ -196,6 +196,20 @@ inline void CheckEqualsHelper(const char* file,
|
||||
}
|
||||
|
||||
|
||||
inline void CheckNonEqualsHelper(const char* file,
|
||||
int line,
|
||||
const char* expected_source,
|
||||
int64_t expected,
|
||||
const char* value_source,
|
||||
int64_t value) {
|
||||
if (expected == value) {
|
||||
V8_Fatal(file, line,
|
||||
"CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f",
|
||||
expected_source, value_source, expected, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline void CheckNonEqualsHelper(const char* file,
|
||||
int line,
|
||||
const char* expected_source,
|
||||
|
266
src/unique.h
Normal file
266
src/unique.h
Normal file
@ -0,0 +1,266 @@
|
||||
// Copyright 2013 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#ifndef V8_HYDROGEN_UNIQUE_H_
|
||||
#define V8_HYDROGEN_UNIQUE_H_
|
||||
|
||||
#include "handles.h"
|
||||
#include "utils.h"
|
||||
#include "zone.h"
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
|
||||
|
||||
template <typename T>
|
||||
class UniqueSet;
|
||||
|
||||
|
||||
// Represents a handle to an object on the heap, but with the additional
|
||||
// ability of checking for equality and hashing without accessing the heap.
|
||||
//
|
||||
// Creating a Unique<T> requires first dereferencing the handle to obtain
|
||||
// the address of the object, which is used as the hashcode and the basis for
|
||||
// comparison. The object can be moved later by the GC, but comparison
|
||||
// and hashing use the old address of the object, without dereferencing it.
|
||||
//
|
||||
// Careful! Comparison of two Uniques is only correct if both were created
|
||||
// in the same "era" of GC or if at least one is a non-movable object.
|
||||
template <typename T>
|
||||
class Unique V8_FINAL {
|
||||
public:
|
||||
// TODO(titzer): make private and introduce some builder/owner class.
|
||||
explicit Unique(Handle<T> handle) {
|
||||
if (handle.is_null()) {
|
||||
raw_address_ = NULL;
|
||||
} else {
|
||||
raw_address_ = reinterpret_cast<Address>(*handle);
|
||||
ASSERT_NE(raw_address_, NULL);
|
||||
}
|
||||
handle_ = handle;
|
||||
}
|
||||
|
||||
// Constructor for handling automatic up casting.
|
||||
// Ex. Unique<JSFunction> can be passed when Unique<Object> is expected.
|
||||
template <class S> Unique(Unique<S> uniq) {
|
||||
#ifdef DEBUG
|
||||
T* a = NULL;
|
||||
S* b = NULL;
|
||||
a = b; // Fake assignment to enforce type checks.
|
||||
USE(a);
|
||||
#endif
|
||||
raw_address_ = uniq.raw_address_;
|
||||
handle_ = uniq.handle_; // Creates a new handle sharing the same location.
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
bool operator==(const Unique<U>& other) const {
|
||||
return raw_address_ == other.raw_address_;
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
bool operator!=(const Unique<U>& other) const {
|
||||
return raw_address_ != other.raw_address_;
|
||||
}
|
||||
|
||||
intptr_t Hashcode() const {
|
||||
return reinterpret_cast<intptr_t>(raw_address_);
|
||||
}
|
||||
|
||||
bool IsNull() {
|
||||
return raw_address_ == NULL;
|
||||
}
|
||||
|
||||
// Don't do this unless you have access to the heap!
|
||||
// No, seriously! You can compare and hash and set-ify uniques that were
|
||||
// all created at the same time; please don't dereference.
|
||||
Handle<T> handle() {
|
||||
return handle_;
|
||||
}
|
||||
|
||||
friend class UniqueSet<T>; // Uses internal details for speed.
|
||||
template <class U>
|
||||
friend class Unique; // For comparing raw_address values.
|
||||
|
||||
private:
|
||||
Address raw_address_;
|
||||
Handle<T> handle_;
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
class UniqueSet V8_FINAL : public ZoneObject {
|
||||
public:
|
||||
// Constructor. A new set will be empty.
|
||||
UniqueSet() : size_(0), capacity_(0), array_(NULL) { }
|
||||
|
||||
// Add a new element to this unique set. Mutates this set. O(|this|).
|
||||
void Add(Unique<T> uniq, Zone* zone) {
|
||||
// Keep the set sorted by the {raw_address} of the unique elements.
|
||||
for (int i = 0; i < size_; i++) {
|
||||
if (array_[i] == uniq) return;
|
||||
if (array_[i].raw_address_ > uniq.raw_address_) {
|
||||
// Insert in the middle.
|
||||
Grow(size_ + 1, zone);
|
||||
for (int j = size_ - 1; j >= i; j--) array_[j + 1] = array_[j];
|
||||
array_[i] = uniq;
|
||||
size_++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Append the element to the the end.
|
||||
Grow(size_ + 1, zone);
|
||||
array_[size_++] = uniq;
|
||||
}
|
||||
|
||||
// Compare this set against another set. O(|this|).
|
||||
bool Equals(UniqueSet<T>* that) {
|
||||
if (that->size_ != this->size_) return false;
|
||||
for (int i = 0; i < this->size_; i++) {
|
||||
if (this->array_[i] != that->array_[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if this set is a subset of the given set. O(|this| + |that|).
|
||||
bool IsSubset(UniqueSet<T>* that) {
|
||||
if (that->size_ < this->size_) return false;
|
||||
int j = 0;
|
||||
for (int i = 0; i < this->size_; i++) {
|
||||
Unique<T> sought = this->array_[i];
|
||||
while (true) {
|
||||
if (sought == that->array_[j++]) break;
|
||||
// Fail whenever there are more elements in {this} than {that}.
|
||||
if ((this->size_ - i) > (that->size_ - j)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Returns a new set representing the intersection of this set and the other.
|
||||
// O(|this| + |that|).
|
||||
UniqueSet<T>* Intersect(UniqueSet<T>* that, Zone* zone) {
|
||||
if (that->size_ == 0 || this->size_ == 0) return new(zone) UniqueSet<T>();
|
||||
|
||||
UniqueSet<T>* out = new(zone) UniqueSet<T>();
|
||||
out->Grow(Min(this->size_, that->size_), zone);
|
||||
|
||||
int i = 0, j = 0, k = 0;
|
||||
while (i < this->size_ && j < that->size_) {
|
||||
Unique<T> a = this->array_[i];
|
||||
Unique<T> b = that->array_[j];
|
||||
if (a == b) {
|
||||
out->array_[k++] = a;
|
||||
i++;
|
||||
j++;
|
||||
} else if (a.raw_address_ < b.raw_address_) {
|
||||
i++;
|
||||
} else {
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
out->size_ = k;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Returns a new set representing the union of this set and the other.
|
||||
// O(|this| + |that|).
|
||||
UniqueSet<T>* Union(UniqueSet<T>* that, Zone* zone) {
|
||||
if (that->size_ == 0) return this->Copy(zone);
|
||||
if (this->size_ == 0) return that->Copy(zone);
|
||||
|
||||
UniqueSet<T>* out = new(zone) UniqueSet<T>();
|
||||
out->Grow(this->size_ + that->size_, zone);
|
||||
|
||||
int i = 0, j = 0, k = 0;
|
||||
while (i < this->size_ && j < that->size_) {
|
||||
Unique<T> a = this->array_[i];
|
||||
Unique<T> b = that->array_[j];
|
||||
if (a == b) {
|
||||
out->array_[k++] = a;
|
||||
i++;
|
||||
j++;
|
||||
} else if (a.raw_address_ < b.raw_address_) {
|
||||
out->array_[k++] = a;
|
||||
i++;
|
||||
} else {
|
||||
out->array_[k++] = b;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
while (i < this->size_) out->array_[k++] = this->array_[i++];
|
||||
while (j < that->size_) out->array_[k++] = that->array_[j++];
|
||||
|
||||
out->size_ = k;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Makes an exact copy of this set. O(|this| + |that|).
|
||||
UniqueSet<T>* Copy(Zone* zone) {
|
||||
UniqueSet<T>* copy = new(zone) UniqueSet<T>();
|
||||
copy->size_ = this->size_;
|
||||
copy->capacity_ = this->size_;
|
||||
copy->array_ = zone->NewArray<Unique<T> >(this->size_);
|
||||
memcpy(copy->array_, this->array_, this->size_ * sizeof(Unique<T>));
|
||||
return copy;
|
||||
}
|
||||
|
||||
inline int size() {
|
||||
return size_;
|
||||
}
|
||||
|
||||
private:
|
||||
// These sets should be small, since operations are implemented with simple
|
||||
// linear algorithms. Enforce a maximum size.
|
||||
static const int kMaxCapacity = 65535;
|
||||
|
||||
uint16_t size_;
|
||||
uint16_t capacity_;
|
||||
Unique<T>* array_;
|
||||
|
||||
// Grow the size of internal storage to be at least {size} elements.
|
||||
void Grow(int size, Zone* zone) {
|
||||
CHECK(size < kMaxCapacity); // Enforce maximum size.
|
||||
if (capacity_ < size) {
|
||||
int new_capacity = 2 * capacity_ + size;
|
||||
if (new_capacity > kMaxCapacity) new_capacity = kMaxCapacity;
|
||||
Unique<T>* new_array = zone->NewArray<Unique<T> >(new_capacity);
|
||||
if (size_ > 0) {
|
||||
memcpy(new_array, array_, size_ * sizeof(Unique<T>));
|
||||
}
|
||||
capacity_ = new_capacity;
|
||||
array_ = new_array;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} } // namespace v8::internal
|
||||
|
||||
#endif // V8_HYDROGEN_UNIQUE_H_
|
@ -106,6 +106,7 @@
|
||||
'test-time.cc',
|
||||
'test-types.cc',
|
||||
'test-unbound-queue.cc',
|
||||
'test-unique.cc',
|
||||
'test-utils.cc',
|
||||
'test-version.cc',
|
||||
'test-weakmaps.cc',
|
||||
|
473
test/cctest/test-unique.cc
Normal file
473
test/cctest/test-unique.cc
Normal file
@ -0,0 +1,473 @@
|
||||
// Copyright 2013 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "v8.h"
|
||||
|
||||
#include "factory.h"
|
||||
#include "global-handles.h"
|
||||
#include "unique.h"
|
||||
#include "cctest.h"
|
||||
|
||||
using namespace v8::internal;
|
||||
|
||||
TEST(UniqueCreate) {
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Handle<String> A = factory->InternalizeUtf8String("A");
|
||||
Unique<String> HA(A);
|
||||
|
||||
CHECK_NE(static_cast<intptr_t>(0), HA.Hashcode());
|
||||
CHECK(*HA.handle() == *A);
|
||||
CHECK_EQ(*A, *HA.handle());
|
||||
|
||||
Unique<String> HA2(A);
|
||||
|
||||
CHECK_EQ(HA.Hashcode(), HA2.Hashcode());
|
||||
CHECK(HA == HA2);
|
||||
CHECK_EQ(*HA.handle(), *HA2.handle());
|
||||
|
||||
CHECK_EQ(HA2.Hashcode(), HA.Hashcode());
|
||||
CHECK(HA2 == HA);
|
||||
CHECK_EQ(*HA2.handle(), *HA.handle());
|
||||
|
||||
Handle<String> B = factory->InternalizeUtf8String("B");
|
||||
Unique<String> HB(B);
|
||||
|
||||
CHECK_NE(HA.Hashcode(), HB.Hashcode());
|
||||
CHECK(HA != HB);
|
||||
CHECK_NE(*HA.handle(), *HB.handle());
|
||||
|
||||
CHECK_NE(HB.Hashcode(), HA.Hashcode());
|
||||
CHECK(HB != HA);
|
||||
CHECK_NE(*HB.handle(), *HA.handle());
|
||||
|
||||
// TODO(titzer): check that Unique properly survives a GC.
|
||||
}
|
||||
|
||||
|
||||
TEST(UniqueSubsume) {
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Handle<String> A = factory->InternalizeUtf8String("A");
|
||||
Unique<String> HA(A);
|
||||
|
||||
CHECK_NE(static_cast<intptr_t>(0), HA.Hashcode());
|
||||
CHECK(*HA.handle() == *A);
|
||||
CHECK_EQ(*A, *HA.handle());
|
||||
|
||||
Unique<Object> HO = HA; // Here comes the subsumption, boys.
|
||||
|
||||
CHECK_EQ(HA.Hashcode(), HO.Hashcode());
|
||||
CHECK(HA == HO);
|
||||
CHECK_EQ(*HA.handle(), *HO.handle());
|
||||
|
||||
CHECK_EQ(HO.Hashcode(), HA.Hashcode());
|
||||
CHECK(HO == HA);
|
||||
CHECK_EQ(*HO.handle(), *HA.handle());
|
||||
}
|
||||
|
||||
|
||||
TEST(UniqueSet_Add) {
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Unique<String> A(factory->InternalizeUtf8String("A"));
|
||||
Unique<String> B(factory->InternalizeUtf8String("B"));
|
||||
Unique<String> C(factory->InternalizeUtf8String("C"));
|
||||
|
||||
Zone zone(isolate);
|
||||
|
||||
UniqueSet<String>* set = new(&zone) UniqueSet<String>();
|
||||
|
||||
CHECK_EQ(0, set->size());
|
||||
set->Add(A, &zone);
|
||||
CHECK_EQ(1, set->size());
|
||||
set->Add(A, &zone);
|
||||
CHECK_EQ(1, set->size());
|
||||
set->Add(B, &zone);
|
||||
CHECK_EQ(2, set->size());
|
||||
set->Add(C, &zone);
|
||||
CHECK_EQ(3, set->size());
|
||||
set->Add(C, &zone);
|
||||
CHECK_EQ(3, set->size());
|
||||
set->Add(B, &zone);
|
||||
CHECK_EQ(3, set->size());
|
||||
set->Add(A, &zone);
|
||||
CHECK_EQ(3, set->size());
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
static void CHECK_SETS(
|
||||
UniqueSet<T>* set1, UniqueSet<T>* set2, bool expected) {
|
||||
CHECK(set1->Equals(set1));
|
||||
CHECK(set2->Equals(set2));
|
||||
CHECK(expected == set1->Equals(set2));
|
||||
CHECK(expected == set2->Equals(set1));
|
||||
}
|
||||
|
||||
|
||||
TEST(UniqueSet_Equals) {
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Unique<String> A(factory->InternalizeUtf8String("A"));
|
||||
Unique<String> B(factory->InternalizeUtf8String("B"));
|
||||
Unique<String> C(factory->InternalizeUtf8String("C"));
|
||||
|
||||
Zone zone(isolate);
|
||||
|
||||
UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
|
||||
UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
|
||||
|
||||
CHECK_SETS(set1, set2, true);
|
||||
|
||||
set1->Add(A, &zone);
|
||||
|
||||
CHECK_SETS(set1, set2, false);
|
||||
|
||||
set2->Add(A, &zone);
|
||||
|
||||
CHECK_SETS(set1, set2, true);
|
||||
|
||||
set1->Add(B, &zone);
|
||||
|
||||
CHECK_SETS(set1, set2, false);
|
||||
|
||||
set2->Add(C, &zone);
|
||||
|
||||
CHECK_SETS(set1, set2, false);
|
||||
|
||||
set1->Add(C, &zone);
|
||||
|
||||
CHECK_SETS(set1, set2, false);
|
||||
|
||||
set2->Add(B, &zone);
|
||||
|
||||
CHECK_SETS(set1, set2, true);
|
||||
}
|
||||
|
||||
|
||||
TEST(UniqueSet_IsSubset1) {
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Unique<String> A(factory->InternalizeUtf8String("A"));
|
||||
Unique<String> B(factory->InternalizeUtf8String("B"));
|
||||
Unique<String> C(factory->InternalizeUtf8String("C"));
|
||||
|
||||
Zone zone(isolate);
|
||||
|
||||
UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
|
||||
UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
|
||||
|
||||
CHECK(set1->IsSubset(set2));
|
||||
CHECK(set2->IsSubset(set1));
|
||||
|
||||
set1->Add(A, &zone);
|
||||
|
||||
CHECK(!set1->IsSubset(set2));
|
||||
CHECK(set2->IsSubset(set1));
|
||||
|
||||
set2->Add(B, &zone);
|
||||
|
||||
CHECK(!set1->IsSubset(set2));
|
||||
CHECK(!set2->IsSubset(set1));
|
||||
|
||||
set2->Add(A, &zone);
|
||||
|
||||
CHECK(set1->IsSubset(set2));
|
||||
CHECK(!set2->IsSubset(set1));
|
||||
|
||||
set1->Add(B, &zone);
|
||||
|
||||
CHECK(set1->IsSubset(set2));
|
||||
CHECK(set2->IsSubset(set1));
|
||||
}
|
||||
|
||||
|
||||
TEST(UniqueSet_IsSubset2) {
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Unique<String> A(factory->InternalizeUtf8String("A"));
|
||||
Unique<String> B(factory->InternalizeUtf8String("B"));
|
||||
Unique<String> C(factory->InternalizeUtf8String("C"));
|
||||
Unique<String> D(factory->InternalizeUtf8String("D"));
|
||||
Unique<String> E(factory->InternalizeUtf8String("E"));
|
||||
Unique<String> F(factory->InternalizeUtf8String("F"));
|
||||
Unique<String> G(factory->InternalizeUtf8String("G"));
|
||||
|
||||
Zone zone(isolate);
|
||||
|
||||
UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
|
||||
UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
|
||||
|
||||
set1->Add(A, &zone);
|
||||
set1->Add(C, &zone);
|
||||
set1->Add(E, &zone);
|
||||
|
||||
set2->Add(A, &zone);
|
||||
set2->Add(B, &zone);
|
||||
set2->Add(C, &zone);
|
||||
set2->Add(D, &zone);
|
||||
set2->Add(E, &zone);
|
||||
set2->Add(F, &zone);
|
||||
|
||||
CHECK(set1->IsSubset(set2));
|
||||
CHECK(!set2->IsSubset(set1));
|
||||
|
||||
set1->Add(G, &zone);
|
||||
|
||||
CHECK(!set1->IsSubset(set2));
|
||||
CHECK(!set2->IsSubset(set1));
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
static UniqueSet<T>* MakeSet(Zone* zone, int which, Unique<T>* elements) {
|
||||
UniqueSet<T>* set = new(zone) UniqueSet<T>();
|
||||
for (int i = 0; i < 32; i++) {
|
||||
if ((which & (1 << i)) != 0) set->Add(elements[i], zone);
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
|
||||
TEST(UniqueSet_IsSubsetExhaustive) {
|
||||
const int kSetSize = 6;
|
||||
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Zone zone(isolate);
|
||||
|
||||
Unique<String> A(factory->InternalizeUtf8String("A"));
|
||||
Unique<String> B(factory->InternalizeUtf8String("B"));
|
||||
Unique<String> C(factory->InternalizeUtf8String("C"));
|
||||
Unique<String> D(factory->InternalizeUtf8String("D"));
|
||||
Unique<String> E(factory->InternalizeUtf8String("E"));
|
||||
Unique<String> F(factory->InternalizeUtf8String("F"));
|
||||
Unique<String> G(factory->InternalizeUtf8String("G"));
|
||||
|
||||
Unique<String> elements[] = {
|
||||
A, B, C, D, E, F, G
|
||||
};
|
||||
|
||||
// Exhaustively test all sets with <= 6 elements.
|
||||
for (int i = 0; i < (1 << kSetSize); i++) {
|
||||
for (int j = 0; j < (1 << kSetSize); j++) {
|
||||
UniqueSet<String>* set1 = MakeSet(&zone, i, elements);
|
||||
UniqueSet<String>* set2 = MakeSet(&zone, j, elements);
|
||||
|
||||
CHECK(((i & j) == i) == set1->IsSubset(set2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST(UniqueSet_Intersect1) {
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Unique<String> A(factory->InternalizeUtf8String("A"));
|
||||
Unique<String> B(factory->InternalizeUtf8String("B"));
|
||||
Unique<String> C(factory->InternalizeUtf8String("C"));
|
||||
|
||||
Zone zone(isolate);
|
||||
|
||||
UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
|
||||
UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
|
||||
UniqueSet<String>* result;
|
||||
|
||||
CHECK(set1->IsSubset(set2));
|
||||
CHECK(set2->IsSubset(set1));
|
||||
|
||||
set1->Add(A, &zone);
|
||||
|
||||
result = set1->Intersect(set2, &zone);
|
||||
|
||||
CHECK_EQ(0, result->size());
|
||||
CHECK(set2->Equals(result));
|
||||
|
||||
set2->Add(A, &zone);
|
||||
|
||||
result = set1->Intersect(set2, &zone);
|
||||
|
||||
CHECK_EQ(1, result->size());
|
||||
CHECK(set1->Equals(result));
|
||||
CHECK(set2->Equals(result));
|
||||
|
||||
set2->Add(B, &zone);
|
||||
set2->Add(C, &zone);
|
||||
|
||||
result = set1->Intersect(set2, &zone);
|
||||
|
||||
CHECK_EQ(1, result->size());
|
||||
CHECK(set1->Equals(result));
|
||||
}
|
||||
|
||||
|
||||
TEST(UniqueSet_IntersectExhaustive) {
|
||||
const int kSetSize = 6;
|
||||
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Zone zone(isolate);
|
||||
|
||||
Unique<String> A(factory->InternalizeUtf8String("A"));
|
||||
Unique<String> B(factory->InternalizeUtf8String("B"));
|
||||
Unique<String> C(factory->InternalizeUtf8String("C"));
|
||||
Unique<String> D(factory->InternalizeUtf8String("D"));
|
||||
Unique<String> E(factory->InternalizeUtf8String("E"));
|
||||
Unique<String> F(factory->InternalizeUtf8String("F"));
|
||||
Unique<String> G(factory->InternalizeUtf8String("G"));
|
||||
|
||||
Unique<String> elements[] = {
|
||||
A, B, C, D, E, F, G
|
||||
};
|
||||
|
||||
// Exhaustively test all sets with <= 6 elements.
|
||||
for (int i = 0; i < (1 << kSetSize); i++) {
|
||||
for (int j = 0; j < (1 << kSetSize); j++) {
|
||||
UniqueSet<String>* set1 = MakeSet(&zone, i, elements);
|
||||
UniqueSet<String>* set2 = MakeSet(&zone, j, elements);
|
||||
|
||||
UniqueSet<String>* result = set1->Intersect(set2, &zone);
|
||||
UniqueSet<String>* expected = MakeSet(&zone, i & j, elements);
|
||||
|
||||
CHECK(result->Equals(expected));
|
||||
CHECK(expected->Equals(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST(UniqueSet_Union1) {
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Unique<String> A(factory->InternalizeUtf8String("A"));
|
||||
Unique<String> B(factory->InternalizeUtf8String("B"));
|
||||
Unique<String> C(factory->InternalizeUtf8String("C"));
|
||||
|
||||
Zone zone(isolate);
|
||||
|
||||
UniqueSet<String>* set1 = new(&zone) UniqueSet<String>();
|
||||
UniqueSet<String>* set2 = new(&zone) UniqueSet<String>();
|
||||
UniqueSet<String>* result;
|
||||
|
||||
CHECK(set1->IsSubset(set2));
|
||||
CHECK(set2->IsSubset(set1));
|
||||
|
||||
set1->Add(A, &zone);
|
||||
|
||||
result = set1->Union(set2, &zone);
|
||||
|
||||
CHECK_EQ(1, result->size());
|
||||
CHECK(set1->Equals(result));
|
||||
|
||||
set2->Add(A, &zone);
|
||||
|
||||
result = set1->Union(set2, &zone);
|
||||
|
||||
CHECK_EQ(1, result->size());
|
||||
CHECK(set1->Equals(result));
|
||||
CHECK(set2->Equals(result));
|
||||
|
||||
set2->Add(B, &zone);
|
||||
set2->Add(C, &zone);
|
||||
|
||||
result = set1->Union(set2, &zone);
|
||||
|
||||
CHECK_EQ(3, result->size());
|
||||
CHECK(set2->Equals(result));
|
||||
}
|
||||
|
||||
|
||||
TEST(UniqueSet_UnionExhaustive) {
|
||||
const int kSetSize = 6;
|
||||
|
||||
CcTest::InitializeVM();
|
||||
Isolate* isolate = Isolate::Current();
|
||||
Factory* factory = isolate->factory();
|
||||
HandleScope sc(isolate);
|
||||
|
||||
Zone zone(isolate);
|
||||
|
||||
Unique<String> A(factory->InternalizeUtf8String("A"));
|
||||
Unique<String> B(factory->InternalizeUtf8String("B"));
|
||||
Unique<String> C(factory->InternalizeUtf8String("C"));
|
||||
Unique<String> D(factory->InternalizeUtf8String("D"));
|
||||
Unique<String> E(factory->InternalizeUtf8String("E"));
|
||||
Unique<String> F(factory->InternalizeUtf8String("F"));
|
||||
Unique<String> G(factory->InternalizeUtf8String("G"));
|
||||
|
||||
Unique<String> elements[] = {
|
||||
A, B, C, D, E, F, G
|
||||
};
|
||||
|
||||
// Exhaustively test all sets with <= 6 elements.
|
||||
for (int i = 0; i < (1 << kSetSize); i++) {
|
||||
for (int j = 0; j < (1 << kSetSize); j++) {
|
||||
UniqueSet<String>* set1 = MakeSet(&zone, i, elements);
|
||||
UniqueSet<String>* set2 = MakeSet(&zone, j, elements);
|
||||
|
||||
UniqueSet<String>* result = set1->Union(set2, &zone);
|
||||
UniqueSet<String>* expected = MakeSet(&zone, i | j, elements);
|
||||
|
||||
CHECK(result->Equals(expected));
|
||||
CHECK(expected->Equals(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -367,6 +367,8 @@
|
||||
'../../src/hydrogen-mark-deoptimize.h',
|
||||
'../../src/hydrogen-minus-zero.cc',
|
||||
'../../src/hydrogen-minus-zero.h',
|
||||
'../../src/hydrogen-osr.cc',
|
||||
'../../src/hydrogen-osr.h',
|
||||
'../../src/hydrogen-range-analysis.cc',
|
||||
'../../src/hydrogen-range-analysis.h',
|
||||
'../../src/hydrogen-redundant-phi.cc',
|
||||
@ -379,8 +381,6 @@
|
||||
'../../src/hydrogen-sce.h',
|
||||
'../../src/hydrogen-uint32-analysis.cc',
|
||||
'../../src/hydrogen-uint32-analysis.h',
|
||||
'../../src/hydrogen-osr.cc',
|
||||
'../../src/hydrogen-osr.h',
|
||||
'../../src/i18n.cc',
|
||||
'../../src/i18n.h',
|
||||
'../../src/icu_util.cc',
|
||||
@ -528,6 +528,7 @@
|
||||
'../../src/unicode-inl.h',
|
||||
'../../src/unicode.cc',
|
||||
'../../src/unicode.h',
|
||||
'../../src/unique.h',
|
||||
'../../src/uri.h',
|
||||
'../../src/utils-inl.h',
|
||||
'../../src/utils.cc',
|
||||
|
Loading…
Reference in New Issue
Block a user