OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium 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 <map> |
| 6 #include <vector> |
| 7 |
| 8 #include "courgette/label_manager.h" |
| 9 |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace courgette { |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Test version of RvaVisitor: Just wrap std::vector<RVA>. |
| 17 class TestRvaVisitor : public LabelManager::RvaVisitor { |
| 18 public: |
| 19 explicit TestRvaVisitor(const std::vector<RVA>& rva_list) |
| 20 : rva_list_(rva_list), it_(rva_list.begin()) {} |
| 21 |
| 22 ~TestRvaVisitor() override {} |
| 23 |
| 24 size_t Remaining() const override { |
| 25 return std::distance(it_, rva_list_.end()); |
| 26 } |
| 27 |
| 28 const RVA& Get() const override{ return *it_; } |
| 29 |
| 30 void Next() override { ++it_; } |
| 31 |
| 32 private: |
| 33 const std::vector<RVA>& rva_list_; |
| 34 std::vector<RVA>::const_iterator it_; |
| 35 }; |
| 36 |
| 37 // Test version of LabelManager: Expose data to test implementation. |
| 38 class TestLabelManager : public LabelManager { |
| 39 public: |
| 40 const size_t LabelCount() const { return labels_.size(); }; |
| 41 }; |
| 42 |
| 43 void CheckLabelManagerContent(TestLabelManager* label_manager, |
| 44 std::map<RVA, size_t>* expected) { |
| 45 EXPECT_EQ(expected->size(), label_manager->LabelCount()); |
| 46 for (auto it : *expected) { |
| 47 Label* label = label_manager->Find(it.first); |
| 48 EXPECT_TRUE(label != nullptr); |
| 49 EXPECT_EQ(it.first, label->rva_); |
| 50 EXPECT_EQ(it.second, label->count_); |
| 51 } |
| 52 } |
| 53 |
| 54 TEST(LabelManagerTest, Basic) { |
| 55 std::vector<RVA> test_targets = { |
| 56 0x04000010, |
| 57 0x04000030, |
| 58 0x04000020, |
| 59 0x04000010, // Redundant |
| 60 0xFEEDF00D, |
| 61 0x04000030, // Redundant |
| 62 0xFEEDF00D, // Redundant |
| 63 0x00000110, |
| 64 0x04000010, // Redundant |
| 65 0xABCD1234 |
| 66 }; |
| 67 ASSERT_EQ(10U, test_targets.size()); |
| 68 TestRvaVisitor visitor(test_targets); |
| 69 |
| 70 // Preallocate targets, then populate. |
| 71 TestLabelManager label_manager; |
| 72 label_manager.Read(&visitor); |
| 73 |
| 74 std::map<RVA, size_t> expected1 = { |
| 75 {0x00000110, 1}, |
| 76 {0x04000010, 3}, |
| 77 {0x04000020, 1}, |
| 78 {0x04000030, 2}, |
| 79 {0xABCD1234, 1}, |
| 80 {0xFEEDF00D, 2} |
| 81 }; |
| 82 CheckLabelManagerContent(&label_manager, &expected1); |
| 83 |
| 84 // Expect to *not* find labels for various RVAs that were never added. |
| 85 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x00000000))); |
| 86 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x0400000F))); |
| 87 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x04000011))); |
| 88 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x5F3759DF))); |
| 89 EXPECT_EQ(nullptr, label_manager.Find(RVA(0xFEEDFFF0))); |
| 90 EXPECT_EQ(nullptr, label_manager.Find(RVA(0xFFFFFFFF))); |
| 91 |
| 92 // Remove Labels with |count_| < 2. |
| 93 label_manager.RemoveUnderusedLabels(2); |
| 94 std::map<RVA, size_t> expected2 = { |
| 95 {0x04000010, 3}, |
| 96 {0x04000030, 2}, |
| 97 {0xFEEDF00D, 2} |
| 98 }; |
| 99 CheckLabelManagerContent(&label_manager, &expected2); |
| 100 } |
| 101 |
| 102 TEST(LabelManagerTest, Single) { |
| 103 uint32 kRva = 12; |
| 104 for (size_t dup = 1; dup < 5; ++dup) { |
| 105 // Test data: |dup| copies of kRva. |
| 106 std::vector<RVA> test_targets(dup, RVA(kRva)); |
| 107 TestRvaVisitor visitor(test_targets); |
| 108 TestLabelManager label_manager; |
| 109 label_manager.Read(&visitor); |
| 110 EXPECT_EQ(1U, label_manager.LabelCount()); // Deduped to 1 Label. |
| 111 |
| 112 Label* label = label_manager.Find(RVA(kRva)); |
| 113 EXPECT_TRUE(label != nullptr); |
| 114 EXPECT_EQ(RVA(kRva), label->rva_); |
| 115 EXPECT_EQ(dup, label->count_); |
| 116 |
| 117 for (size_t i = 0; i < 16; ++i) { |
| 118 if (i != kRva) |
| 119 EXPECT_EQ(nullptr, label_manager.Find(RVA(i))); |
| 120 } |
| 121 } |
| 122 } |
| 123 |
| 124 TEST(LabelManagerTest, Empty) { |
| 125 std::vector<RVA> empty_test_targets; |
| 126 TestRvaVisitor visitor(empty_test_targets); |
| 127 TestLabelManager label_manager; |
| 128 label_manager.Read(&visitor); |
| 129 EXPECT_EQ(0U, label_manager.LabelCount()); |
| 130 for (size_t i = 0; i < 16; ++i) |
| 131 EXPECT_EQ(nullptr, label_manager.Find(RVA(i))); |
| 132 } |
| 133 |
| 134 } // namespace |
| 135 |
| 136 } // namespace courgette |
OLD | NEW |