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