Chromium Code Reviews| Index: courgette/label_manager_unittest.cc |
| diff --git a/courgette/label_manager_unittest.cc b/courgette/label_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3a29c659ee74a6364b4c2341ec3b13f4c6f8f6e4 |
| --- /dev/null |
| +++ b/courgette/label_manager_unittest.cc |
| @@ -0,0 +1,136 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <map> |
| +#include <vector> |
| + |
| +#include "courgette/label_manager.h" |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace courgette { |
| + |
| +namespace { |
| + |
| +// Test version of RvaVisitor: Just wrap std::vector<RVA>. |
| +class TestRvaVisitor : public LabelManager::RvaVisitor { |
| + public: |
| + explicit TestRvaVisitor(const std::vector<RVA>& rva_list) |
| + : rva_list_(rva_list), it_(rva_list.begin()) {} |
| + |
| + ~TestRvaVisitor() override {} |
| + |
| + size_t Remaining() const override { |
| + return std::distance(it_, rva_list_.end()); |
| + } |
| + |
| + const RVA& Get() const override{ return *it_; } |
| + |
| + void Next() override { ++it_; } |
| + |
| + private: |
| + const std::vector<RVA>& rva_list_; |
| + std::vector<RVA>::const_iterator it_; |
| +}; |
| + |
| +// Test version of LabelManager: Expose data to test implementation. |
| +class TestLabelManager : public LabelManager { |
| + public: |
| + const size_t LabelCount() const { return labels_.size(); }; |
| +}; |
| + |
| +void CheckLabelManagerContent(TestLabelManager* label_manager, |
| + std::map<RVA, size_t>* expected) { |
|
grt (UTC plus 2)
2015/12/03 15:19:09
pass |expected| by const-ref
huangs
2015/12/03 19:29:06
Done.
|
| + EXPECT_EQ(expected->size(), label_manager->LabelCount()); |
| + for (auto it : *expected) { |
|
grt (UTC plus 2)
2015/12/03 15:19:09
|it| is a bad name since it doesn't name an iterat
huangs
2015/12/03 19:29:07
Done.
|
| + Label* label = label_manager->Find(it.first); |
| + EXPECT_TRUE(label != nullptr); |
| + EXPECT_EQ(it.first, label->rva_); |
| + EXPECT_EQ(it.second, label->count_); |
| + } |
| +} |
| + |
|
grt (UTC plus 2)
2015/12/03 15:19:09
} // namespace
huangs
2015/12/03 19:29:06
Done.
|
| +TEST(LabelManagerTest, Basic) { |
| + std::vector<RVA> test_targets = { |
|
grt (UTC plus 2)
2015/12/03 15:19:09
C++-11 uniform initialization is still banned. you
huangs
2015/12/03 19:29:06
Done.
|
| + 0x04000010, |
| + 0x04000030, |
| + 0x04000020, |
| + 0x04000010, // Redundant |
| + 0xFEEDF00D, |
| + 0x04000030, // Redundant |
| + 0xFEEDF00D, // Redundant |
| + 0x00000110, |
| + 0x04000010, // Redundant |
| + 0xABCD1234 |
| + }; |
| + ASSERT_EQ(10U, test_targets.size()); |
| + TestRvaVisitor visitor(test_targets); |
| + |
| + // Preallocate targets, then populate. |
| + TestLabelManager label_manager; |
| + label_manager.Read(&visitor); |
| + |
| + std::map<RVA, size_t> expected1 = { |
|
grt (UTC plus 2)
2015/12/03 15:19:09
same comment about this initialization syntax
huangs
2015/12/03 19:29:06
Done.
|
| + {0x00000110, 1}, |
| + {0x04000010, 3}, |
| + {0x04000020, 1}, |
| + {0x04000030, 2}, |
| + {0xABCD1234, 1}, |
| + {0xFEEDF00D, 2} |
| + }; |
| + CheckLabelManagerContent(&label_manager, &expected1); |
| + |
| + // Expect to *not* find labels for various RVAs that were never added. |
| + EXPECT_EQ(nullptr, label_manager.Find(RVA(0x00000000))); |
| + EXPECT_EQ(nullptr, label_manager.Find(RVA(0x0400000F))); |
| + EXPECT_EQ(nullptr, label_manager.Find(RVA(0x04000011))); |
| + EXPECT_EQ(nullptr, label_manager.Find(RVA(0x5F3759DF))); |
| + EXPECT_EQ(nullptr, label_manager.Find(RVA(0xFEEDFFF0))); |
| + EXPECT_EQ(nullptr, label_manager.Find(RVA(0xFFFFFFFF))); |
| + |
| + // Remove Labels with |count_| < 2. |
| + label_manager.RemoveUnderusedLabels(2); |
| + std::map<RVA, size_t> expected2 = { |
| + {0x04000010, 3}, |
| + {0x04000030, 2}, |
| + {0xFEEDF00D, 2} |
| + }; |
| + CheckLabelManagerContent(&label_manager, &expected2); |
| +} |
| + |
| +TEST(LabelManagerTest, Single) { |
| + uint32 kRva = 12; |
|
grt (UTC plus 2)
2015/12/03 15:19:09
uint32 -> const RVA
huangs
2015/12/03 19:29:07
Done.
|
| + for (size_t dup = 1; dup < 5; ++dup) { |
| + // Test data: |dup| copies of kRva. |
| + std::vector<RVA> test_targets(dup, RVA(kRva)); |
|
grt (UTC plus 2)
2015/12/03 15:19:10
RVA(kRva) -> kRva
here and below
huangs
2015/12/03 19:29:06
Done.
|
| + TestRvaVisitor visitor(test_targets); |
| + TestLabelManager label_manager; |
| + label_manager.Read(&visitor); |
| + EXPECT_EQ(1U, label_manager.LabelCount()); // Deduped to 1 Label. |
| + |
| + Label* label = label_manager.Find(RVA(kRva)); |
| + EXPECT_TRUE(label != nullptr); |
|
grt (UTC plus 2)
2015/12/03 15:19:09
EXPECT_NE?
huangs
2015/12/03 19:29:06
Done.
|
| + EXPECT_EQ(RVA(kRva), label->rva_); |
| + EXPECT_EQ(dup, label->count_); |
| + |
| + for (size_t i = 0; i < 16; ++i) { |
|
grt (UTC plus 2)
2015/12/03 15:19:10
RVA is a typedef to uint32, so can you just use RV
huangs
2015/12/03 19:29:06
Done.
|
| + if (i != kRva) |
| + EXPECT_EQ(nullptr, label_manager.Find(RVA(i))); |
| + } |
| + } |
| +} |
| + |
| +TEST(LabelManagerTest, Empty) { |
| + std::vector<RVA> empty_test_targets; |
| + TestRvaVisitor visitor(empty_test_targets); |
| + TestLabelManager label_manager; |
| + label_manager.Read(&visitor); |
| + EXPECT_EQ(0U, label_manager.LabelCount()); |
| + for (size_t i = 0; i < 16; ++i) |
| + EXPECT_EQ(nullptr, label_manager.Find(RVA(i))); |
| +} |
| + |
| +} // namespace |
| + |
| +} // namespace courgette |