xref: /aosp_15_r20/external/cronet/base/containers/to_value_list_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 The Chromium Authors
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 "base/containers/to_value_list.h"
6 
7 #include <set>
8 
9 #include "base/containers/flat_set.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace base {
14 namespace {
15 
16 // `Value` isn't copyable, so it's not possible to match against Value(x)
17 // directly in `testing::ElementsAre`. This is why Value(x) is replaced with
18 // IsInt(x).
IsInt(int value)19 auto IsInt(int value) {
20   return testing::Property(&Value::GetInt, testing::Eq(value));
21 }
22 
23 template <class C>
IdentityTest()24 void IdentityTest() {
25   C c = {1, 2, 3, 4, 5};
26   // See comment in `IsInt()` above.
27   EXPECT_THAT(ToValueList(c), testing::ElementsAre(IsInt(1), IsInt(2), IsInt(3),
28                                                    IsInt(4), IsInt(5)));
29 }
30 
31 template <class C>
ProjectionTest()32 void ProjectionTest() {
33   C c = {1, 2, 3, 4, 5};
34   // See comment in `IsInt()` above.
35   EXPECT_THAT(
36       ToValueList(c, [](int x) { return x + 1; }),
37       testing::ElementsAre(IsInt(2), IsInt(3), IsInt(4), IsInt(5), IsInt(6)));
38 }
39 
TEST(ToListTest,Identity)40 TEST(ToListTest, Identity) {
41   IdentityTest<std::vector<int>>();
42   IdentityTest<std::set<int>>();
43   IdentityTest<int[]>();
44   IdentityTest<flat_set<int>>();
45 }
46 
TEST(ToListTest,Projection)47 TEST(ToListTest, Projection) {
48   ProjectionTest<std::vector<int>>();
49   ProjectionTest<std::set<int>>();
50   ProjectionTest<int[]>();
51   ProjectionTest<flat_set<int>>();
52 }
53 
54 // Validates that consuming projections work as intended (every single `Value`
55 // inside `Value::List` is a move-only type).
TEST(ToListTest,MoveOnly)56 TEST(ToListTest, MoveOnly) {
57   Value::List list;
58   list.resize(10);
59   Value::List mapped_list = ToValueList(
60       std::move(list), [](Value& value) { return std::move(value); });
61   EXPECT_EQ(mapped_list.size(), 10U);
62 }
63 
64 }  // namespace
65 }  // namespace base
66