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