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

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: Optimization and cleanups. 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
« no previous file with comments | « 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 {
Will Harris 2015/12/03 23:47:34 can you move this out of anon namespace too?
huangs 2015/12/04 00:20:29 Done; moving just TestRvaVisitor.
grt (UTC plus 2) 2015/12/04 19:33:41 will and i are giving you conflicting advice! :-)
Will Harris 2015/12/04 19:35:36 woops sorry - listen to grt!
huangs 2015/12/04 20:01:13 K, moving back.
17
18 // Test version of RvaVisitor: Just wrap std::vector<RVA>.
19 class TestRvaVisitor : public LabelManager::RvaVisitor {
20 public:
21 explicit TestRvaVisitor(const std::vector<RVA>& rva_list)
22 : rva_list_(rva_list), it_(rva_list.begin()) {}
23
24 ~TestRvaVisitor() override {}
25
26 size_t Remaining() const override {
27 return std::distance(it_, rva_list_.end());
28 }
29
30 RVA Get() const override{ return *it_; }
31
32 void Next() override { ++it_; }
33
34 private:
35 const std::vector<RVA>& rva_list_;
36 std::vector<RVA>::const_iterator it_;
37 };
38
39 // Test version of LabelManager: Expose data to test implementation.
40 class TestLabelManager : public LabelManager {
41 public:
42 const size_t LabelCount() const { return labels_.size(); };
43 };
44
45 void CheckLabelManagerContent(TestLabelManager* label_manager,
46 const std::map<RVA, size_t>& expected) {
47 EXPECT_EQ(expected.size(), label_manager->LabelCount());
48 for (auto rva_and_count : expected) {
49 Label* label = label_manager->Find(rva_and_count.first);
50 EXPECT_TRUE(label != nullptr);
51 EXPECT_EQ(rva_and_count.first, label->rva_);
52 EXPECT_EQ(rva_and_count.second, label->count_);
53 }
54 }
55
56 } // namespace
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, size_t> expected1_raw[] = {
80 {0x00000110, 1},
Will Harris 2015/12/03 23:47:34 will clang complain about these not being 1U?
huangs 2015/12/04 00:20:29 Don't know, but fixing these for consistency. Als
81 {0x04000010, 3},
82 {0x04000020, 1},
83 {0x04000030, 2},
84 {0xABCD1234, 1},
85 {0xFEEDF00D, 2}
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(2);
102 const std::pair<RVA, size_t> expected2_raw[] = {
103 {0x04000010, 3},
104 {0x04000030, 2},
105 {0xFEEDF00D, 2}
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 = 12;
114 for (size_t 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 = 0; rva < 16; ++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 = 0; rva < 16; ++rva)
141 EXPECT_EQ(nullptr, label_manager.Find(rva));
142 }
143
144 } // namespace courgette
OLDNEW
« no previous file with comments | « courgette/label_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698