OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <algorithm> |
| 6 |
| 7 #include "src/base/utils/random-number-generator.h" |
| 8 #include "src/compiler/node.h" |
| 9 #include "src/compiler/node-cache.h" |
| 10 #include "src/flags.h" |
| 11 #include "test/compiler-unittests/compiler-unittests.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest-type-names.h" |
| 14 |
| 15 using testing::AllOf; |
| 16 using testing::IsNull; |
| 17 using testing::NotNull; |
| 18 using testing::Pointee; |
| 19 |
| 20 namespace v8 { |
| 21 namespace internal { |
| 22 namespace compiler { |
| 23 |
| 24 template <typename T> |
| 25 class NodeCacheTest : public CompilerTest { |
| 26 public: |
| 27 NodeCacheTest() : rng_(FLAG_random_seed) {} |
| 28 virtual ~NodeCacheTest() {} |
| 29 |
| 30 protected: |
| 31 NodeCache<T>* cache() { return &cache_; } |
| 32 base::RandomNumberGenerator* rng() { return &rng_; } |
| 33 |
| 34 void GenerateRandom(T* first, T* last) { |
| 35 for (T* i = first; i != last; ++i) { |
| 36 do { |
| 37 *i = GenerateRandom(); |
| 38 } while (std::find(first, i, *i) != i); |
| 39 } |
| 40 } |
| 41 |
| 42 private: |
| 43 T GenerateRandom(); |
| 44 |
| 45 NodeCache<T> cache_; |
| 46 base::RandomNumberGenerator rng_; |
| 47 }; |
| 48 |
| 49 |
| 50 template <> |
| 51 int32_t NodeCacheTest<int32_t>::GenerateRandom() { |
| 52 return rng()->NextInt(); |
| 53 } |
| 54 |
| 55 |
| 56 template <> |
| 57 int64_t NodeCacheTest<int64_t>::GenerateRandom() { |
| 58 int64_t v; |
| 59 rng()->NextBytes(&v, sizeof(v)); |
| 60 return v; |
| 61 } |
| 62 |
| 63 |
| 64 typedef ::testing::Types<int32_t, int64_t> NodeCacheTypes; |
| 65 TYPED_TEST_CASE(NodeCacheTest, NodeCacheTypes); |
| 66 |
| 67 |
| 68 TYPED_TEST(NodeCacheTest, BackToBack) { |
| 69 static const size_t kSize = 100; |
| 70 TypeParam values[kSize]; |
| 71 this->GenerateRandom(&values[0], &values[kSize]); |
| 72 for (const TypeParam* i = &values[0]; i != &values[kSize]; ++i) { |
| 73 TypeParam value = *i; |
| 74 SCOPED_TRACE(::testing::Message() << "value " << value); |
| 75 Node** location = this->cache()->Find(this->zone(), value); |
| 76 ASSERT_THAT(location, AllOf(NotNull(), Pointee(IsNull()))); |
| 77 for (int attempt = 1; attempt < 4; ++attempt) { |
| 78 SCOPED_TRACE(::testing::Message() << "attempt " << attempt); |
| 79 EXPECT_EQ(location, this->cache()->Find(this->zone(), value)); |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 |
| 85 TYPED_TEST(NodeCacheTest, MinimumSize) { |
| 86 static const size_t kSize = 5; |
| 87 TypeParam values[kSize]; |
| 88 this->GenerateRandom(&values[0], &values[kSize]); |
| 89 Node** locations[kSize]; |
| 90 Node* nodes = this->zone()->template NewArray<Node>(kSize); |
| 91 for (size_t i = 0; i < kSize; ++i) { |
| 92 locations[i] = this->cache()->Find(this->zone(), values[i]); |
| 93 ASSERT_THAT(locations[i], NotNull()); |
| 94 EXPECT_EQ(&locations[i], |
| 95 std::find(&locations[0], &locations[i], locations[i])); |
| 96 *locations[i] = &nodes[i]; |
| 97 } |
| 98 for (size_t i = 0; i < kSize; ++i) { |
| 99 EXPECT_EQ(locations[i], this->cache()->Find(this->zone(), values[i])); |
| 100 } |
| 101 } |
| 102 |
| 103 |
| 104 TYPED_TEST(NodeCacheTest, MinimumHits) { |
| 105 static const size_t kSize = 250; |
| 106 static const size_t kMinHits = 10; |
| 107 TypeParam* values = this->zone()->template NewArray<TypeParam>(kSize); |
| 108 this->GenerateRandom(&values[0], &values[kSize]); |
| 109 Node* nodes = this->zone()->template NewArray<Node>(kSize); |
| 110 for (size_t i = 0; i < kSize; ++i) { |
| 111 Node** location = this->cache()->Find(this->zone(), values[i]); |
| 112 ASSERT_THAT(location, AllOf(NotNull(), Pointee(IsNull()))); |
| 113 *location = &nodes[i]; |
| 114 } |
| 115 size_t hits = 0; |
| 116 for (size_t i = 0; i < kSize; ++i) { |
| 117 Node** location = this->cache()->Find(this->zone(), values[i]); |
| 118 ASSERT_THAT(location, NotNull()); |
| 119 if (*location != NULL) { |
| 120 EXPECT_EQ(&nodes[i], *location); |
| 121 ++hits; |
| 122 } |
| 123 } |
| 124 EXPECT_GE(hits, kMinHits); |
| 125 } |
| 126 |
| 127 } // namespace compiler |
| 128 } // namespace internal |
| 129 } // namespace v8 |
OLD | NEW |