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