OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "courgette/label_manager.h" | 5 #include "courgette/label_manager.h" |
6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
7 #include <iterator> | 10 #include <iterator> |
8 #include <map> | 11 #include <map> |
9 #include <utility> | 12 #include <utility> |
10 #include <vector> | 13 #include <vector> |
11 | 14 |
12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
13 | 16 |
14 namespace courgette { | 17 namespace courgette { |
15 | 18 |
16 namespace { | 19 namespace { |
(...skipping 18 matching lines...) Expand all Loading... |
35 std::vector<RVA>::const_iterator rva_end_; | 38 std::vector<RVA>::const_iterator rva_end_; |
36 }; | 39 }; |
37 | 40 |
38 // Test version of LabelManager: Expose data to test implementation. | 41 // Test version of LabelManager: Expose data to test implementation. |
39 class TestLabelManager : public LabelManager { | 42 class TestLabelManager : public LabelManager { |
40 public: | 43 public: |
41 size_t LabelCount() const { return labels_.size(); }; | 44 size_t LabelCount() const { return labels_.size(); }; |
42 }; | 45 }; |
43 | 46 |
44 void CheckLabelManagerContent(TestLabelManager* label_manager, | 47 void CheckLabelManagerContent(TestLabelManager* label_manager, |
45 const std::map<RVA, int32>& expected) { | 48 const std::map<RVA, int32_t>& expected) { |
46 EXPECT_EQ(expected.size(), label_manager->LabelCount()); | 49 EXPECT_EQ(expected.size(), label_manager->LabelCount()); |
47 for (const auto& rva_and_count : expected) { | 50 for (const auto& rva_and_count : expected) { |
48 Label* label = label_manager->Find(rva_and_count.first); | 51 Label* label = label_manager->Find(rva_and_count.first); |
49 EXPECT_TRUE(label != nullptr); | 52 EXPECT_TRUE(label != nullptr); |
50 EXPECT_EQ(rva_and_count.first, label->rva_); | 53 EXPECT_EQ(rva_and_count.first, label->rva_); |
51 EXPECT_EQ(rva_and_count.second, label->count_); | 54 EXPECT_EQ(rva_and_count.second, label->count_); |
52 } | 55 } |
53 } | 56 } |
54 | 57 |
55 } // namespace | 58 } // namespace |
(...skipping 12 matching lines...) Expand all Loading... |
68 0xABCD1234 | 71 0xABCD1234 |
69 }; | 72 }; |
70 std::vector<RVA> test_targets(std::begin(kTestTargetsRaw), | 73 std::vector<RVA> test_targets(std::begin(kTestTargetsRaw), |
71 std::end(kTestTargetsRaw)); | 74 std::end(kTestTargetsRaw)); |
72 TestRvaVisitor visitor(test_targets.begin(), test_targets.end()); | 75 TestRvaVisitor visitor(test_targets.begin(), test_targets.end()); |
73 | 76 |
74 // Preallocate targets, then populate. | 77 // Preallocate targets, then populate. |
75 TestLabelManager label_manager; | 78 TestLabelManager label_manager; |
76 label_manager.Read(&visitor); | 79 label_manager.Read(&visitor); |
77 | 80 |
78 static const std::pair<RVA, int32> kExpected1Raw[] = { | 81 static const std::pair<RVA, int32_t> kExpected1Raw[] = { |
79 {0x00000110, 1}, | 82 {0x00000110, 1}, {0x04000010, 3}, {0x04000020, 1}, |
80 {0x04000010, 3}, | 83 {0x04000030, 2}, {0xABCD1234, 1}, {0xFEEDF00D, 2}}; |
81 {0x04000020, 1}, | 84 std::map<RVA, int32_t> expected1(std::begin(kExpected1Raw), |
82 {0x04000030, 2}, | 85 std::end(kExpected1Raw)); |
83 {0xABCD1234, 1}, | |
84 {0xFEEDF00D, 2} | |
85 }; | |
86 std::map<RVA, int32> expected1(std::begin(kExpected1Raw), | |
87 std::end(kExpected1Raw)); | |
88 | 86 |
89 CheckLabelManagerContent(&label_manager, expected1); | 87 CheckLabelManagerContent(&label_manager, expected1); |
90 | 88 |
91 // Expect to *not* find labels for various RVAs that were never added. | 89 // Expect to *not* find labels for various RVAs that were never added. |
92 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x00000000))); | 90 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x00000000))); |
93 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x0400000F))); | 91 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x0400000F))); |
94 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x04000011))); | 92 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x04000011))); |
95 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x5F3759DF))); | 93 EXPECT_EQ(nullptr, label_manager.Find(RVA(0x5F3759DF))); |
96 EXPECT_EQ(nullptr, label_manager.Find(RVA(0xFEEDFFF0))); | 94 EXPECT_EQ(nullptr, label_manager.Find(RVA(0xFEEDFFF0))); |
97 EXPECT_EQ(nullptr, label_manager.Find(RVA(0xFFFFFFFF))); | 95 EXPECT_EQ(nullptr, label_manager.Find(RVA(0xFFFFFFFF))); |
98 | 96 |
99 // Remove Labels with |count_| < 2. | 97 // Remove Labels with |count_| < 2. |
100 label_manager.RemoveUnderusedLabels(2); | 98 label_manager.RemoveUnderusedLabels(2); |
101 static const std::pair<RVA, int32> kExpected2Raw[] = { | 99 static const std::pair<RVA, int32_t> kExpected2Raw[] = { |
102 {0x04000010, 3}, | 100 {0x04000010, 3}, {0x04000030, 2}, {0xFEEDF00D, 2}}; |
103 {0x04000030, 2}, | 101 std::map<RVA, int32_t> expected2(std::begin(kExpected2Raw), |
104 {0xFEEDF00D, 2} | 102 std::end(kExpected2Raw)); |
105 }; | |
106 std::map<RVA, int32> expected2(std::begin(kExpected2Raw), | |
107 std::end(kExpected2Raw)); | |
108 CheckLabelManagerContent(&label_manager, expected2); | 103 CheckLabelManagerContent(&label_manager, expected2); |
109 } | 104 } |
110 | 105 |
111 TEST(LabelManagerTest, Single) { | 106 TEST(LabelManagerTest, Single) { |
112 const RVA kRva = 12U; | 107 const RVA kRva = 12U; |
113 for (int dup = 1; dup < 8; ++dup) { | 108 for (int dup = 1; dup < 8; ++dup) { |
114 // Test data: |dup| copies of kRva. | 109 // Test data: |dup| copies of kRva. |
115 std::vector<RVA> test_targets(dup, kRva); | 110 std::vector<RVA> test_targets(dup, kRva); |
116 TestRvaVisitor visitor(test_targets.begin(), test_targets.end()); | 111 TestRvaVisitor visitor(test_targets.begin(), test_targets.end()); |
117 TestLabelManager label_manager; | 112 TestLabelManager label_manager; |
(...skipping 16 matching lines...) Expand all Loading... |
134 std::vector<RVA> empty_test_targets; | 129 std::vector<RVA> empty_test_targets; |
135 TestRvaVisitor visitor(empty_test_targets.begin(), empty_test_targets.end()); | 130 TestRvaVisitor visitor(empty_test_targets.begin(), empty_test_targets.end()); |
136 TestLabelManager label_manager; | 131 TestLabelManager label_manager; |
137 label_manager.Read(&visitor); | 132 label_manager.Read(&visitor); |
138 EXPECT_EQ(0U, label_manager.LabelCount()); | 133 EXPECT_EQ(0U, label_manager.LabelCount()); |
139 for (RVA rva = 0U; rva < 16U; ++rva) | 134 for (RVA rva = 0U; rva < 16U; ++rva) |
140 EXPECT_EQ(nullptr, label_manager.Find(rva)); | 135 EXPECT_EQ(nullptr, label_manager.Find(rva)); |
141 } | 136 } |
142 | 137 |
143 } // namespace courgette | 138 } // namespace courgette |
OLD | NEW |