Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(446)

Side by Side Diff: courgette/label_manager_unittest.cc

Issue 1491703003: [Courgette] Initial Implementation of LabelManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix signed/unsigned issue; make Label::count_ int32. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« courgette/label_manager.h ('K') | « courgette/label_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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, size_t>& expected) {
26 EXPECT_EQ(expected.size(), label_manager->LabelCount());
27 for (auto rva_and_count : expected) {
grt (UTC plus 2) 2015/12/04 19:33:41 const auto& rva_and_count
huangs 2015/12/04 20:01:13 Done.
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_; }
grt (UTC plus 2) 2015/12/04 19:33:41 nit: space after "override"
huangs 2015/12/04 20:01:13 Done.
50
51 void Next() override { ++it_; }
52
53 private:
54 const std::vector<RVA>& rva_list_;
grt (UTC plus 2) 2015/12/04 19:33:41 holding a ref to an external object is a bit dange
huangs 2015/12/04 20:01:13 Done.
55 std::vector<RVA>::const_iterator it_;
56 };
57
58 TEST(LabelManagerTest, Basic) {
59 const RVA test_targets_raw[] = {
grt (UTC plus 2) 2015/12/04 19:33:41 static const RVA kTestTargetsRaw[]
huangs 2015/12/04 20:01:13 Done, also applied below.
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, size_t> expected1_raw[] = {
80 {0x00000110, 1U},
81 {0x04000010, 3U},
82 {0x04000020, 1U},
83 {0x04000030, 2U},
84 {0xABCD1234, 1U},
85 {0xFEEDF00D, 2U}
86 };
87 std::map<RVA, size_t> 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(2U);
102 const std::pair<RVA, size_t> expected2_raw[] = {
103 {0x04000010, 3U},
104 {0x04000030, 2U},
105 {0xFEEDF00D, 2U}
106 };
107 std::map<RVA, size_t> 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
OLDNEW
« courgette/label_manager.h ('K') | « courgette/label_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698