target_pool_unittest.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2017 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. #include "components/zucchini/target_pool.h"
  5. #include <cmath>
  6. #include <deque>
  7. #include <string>
  8. #include <utility>
  9. #include "components/zucchini/image_utils.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace zucchini {
  12. namespace {
  13. using OffsetDeque = std::deque<offset_t>;
  14. } // namespace
  15. TEST(TargetPoolTest, InsertTargetsFromReferences) {
  16. auto test_insert = [](std::vector<Reference>&& references) -> OffsetDeque {
  17. TargetPool target_pool;
  18. target_pool.InsertTargets(references);
  19. // Return copy since |target_pool| goes out of scope.
  20. return target_pool.targets();
  21. };
  22. EXPECT_EQ(OffsetDeque(), test_insert({}));
  23. EXPECT_EQ(OffsetDeque({0, 1}), test_insert({{0, 0}, {10, 1}}));
  24. EXPECT_EQ(OffsetDeque({0, 1}), test_insert({{0, 1}, {10, 0}}));
  25. EXPECT_EQ(OffsetDeque({0, 1, 2}), test_insert({{0, 1}, {10, 0}, {20, 2}}));
  26. EXPECT_EQ(OffsetDeque({0}), test_insert({{0, 0}, {10, 0}}));
  27. EXPECT_EQ(OffsetDeque({0, 1}), test_insert({{0, 0}, {10, 0}, {20, 1}}));
  28. }
  29. TEST(TargetPoolTest, KeyOffset) {
  30. auto test_key_offset = [](const std::string& nearest_offsets_key,
  31. OffsetDeque&& targets) {
  32. TargetPool target_pool(std::move(targets));
  33. for (offset_t offset : target_pool.targets()) {
  34. offset_t key = target_pool.KeyForOffset(offset);
  35. EXPECT_LT(key, target_pool.size());
  36. EXPECT_EQ(offset, target_pool.OffsetForKey(key));
  37. }
  38. for (offset_t offset = 0; offset < nearest_offsets_key.size(); ++offset) {
  39. key_t key = target_pool.KeyForNearestOffset(offset);
  40. EXPECT_EQ(key, static_cast<key_t>(nearest_offsets_key[offset] - '0'));
  41. }
  42. };
  43. test_key_offset("0000000000000000", {});
  44. test_key_offset("0000000000000000", {0});
  45. test_key_offset("0000000000000000", {1});
  46. test_key_offset("0111111111111111", {0, 1});
  47. test_key_offset("0011111111111111", {0, 2});
  48. test_key_offset("0011111111111111", {1, 2});
  49. test_key_offset("0001111111111111", {1, 3});
  50. test_key_offset("0001112223334444", {1, 3, 7, 9, 13});
  51. test_key_offset("0000011112223333", {1, 7, 9, 13});
  52. }
  53. } // namespace zucchini