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

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