File: flat_map_unittest.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (118 lines) | stat: -rw-r--r-- 3,647 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2020 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 "util/flat_map.h"

#include <chrono>

#include "absl/strings/string_view.h"
#include "gtest/gtest.h"

namespace openscreen {

namespace {

const FlatMap<int, absl::string_view> kSimpleFlatMap{{-1, "bar"},
                                                     {123, "foo"},
                                                     {10000, "baz"},
                                                     {0, ""}};

}  // namespace

TEST(FlatMapTest, Find) {
  EXPECT_EQ(kSimpleFlatMap.begin(), kSimpleFlatMap.find(-1));
  EXPECT_EQ("bar", kSimpleFlatMap.find(-1)->second);
  EXPECT_EQ("foo", kSimpleFlatMap.find(123)->second);
  EXPECT_EQ("baz", kSimpleFlatMap.find(10000)->second);
  EXPECT_EQ("", kSimpleFlatMap.find(0)->second);
  EXPECT_EQ(kSimpleFlatMap.end(), kSimpleFlatMap.find(2));
}

// Since it is backed by a vector, we don't expose an operator[] due
// to potential confusion. If the type of Key is int or std::size_t, do
// you want the std::pair<Key, Value> or just the Value?
TEST(FlatMapTest, Access) {
  EXPECT_EQ("bar", kSimpleFlatMap[0].second);
  EXPECT_EQ("foo", kSimpleFlatMap[1].second);
  EXPECT_EQ("baz", kSimpleFlatMap[2].second);
  EXPECT_EQ("", kSimpleFlatMap[3].second);

  // NOTE: vector doesn't do any range checking for operator[], only at().
  EXPECT_EQ("bar", kSimpleFlatMap.at(0).second);
  EXPECT_EQ("foo", kSimpleFlatMap.at(1).second);
  EXPECT_EQ("baz", kSimpleFlatMap.at(2).second);
  EXPECT_EQ("", kSimpleFlatMap.at(3).second);
  // The error message varies widely depending on how the test is run.
  // NOTE: Google Test doesn't support death tests on some platforms, such
  // as iOS.
#if defined(GTEST_HAS_DEATH_TEST)
  EXPECT_DEATH(kSimpleFlatMap.at(31337), ".*");
#endif
}

TEST(FlatMapTest, ErasureAndEmplacement) {
  auto mutable_vector = kSimpleFlatMap;
  EXPECT_NE(mutable_vector.find(-1), mutable_vector.end());
  mutable_vector.erase_key(-1);
  EXPECT_EQ(mutable_vector.find(-1), mutable_vector.end());

  mutable_vector.emplace_back(-1, "bar");
  EXPECT_NE(mutable_vector.find(-1), mutable_vector.end());

  // We absolutely should fail to erase something that's not there.
#if defined(GTEST_HAS_DEATH_TEST)
  EXPECT_DEATH(mutable_vector.erase_key(12345),
               ".*failed to erase: element not found.*");
#endif
}

TEST(FlatMapTest, Mutation) {
  FlatMap<int, int> mutable_vector{{1, 2}};
  EXPECT_EQ(2, mutable_vector[0].second);

  mutable_vector[0].second = 3;
  EXPECT_EQ(3, mutable_vector[0].second);
}

TEST(FlatMapTest, GenerallyBehavesLikeAVector) {
  EXPECT_EQ(kSimpleFlatMap, kSimpleFlatMap);
  EXPECT_TRUE(kSimpleFlatMap == kSimpleFlatMap);

  for (auto& entry : kSimpleFlatMap) {
    if (entry.first != 0) {
      EXPECT_LT(0u, entry.second.size());
    }
  }

  FlatMap<int, int> simple;
  simple.emplace_back(1, 10);
  EXPECT_EQ(simple, (FlatMap<int, int>{{1, 10}}));

  auto it = simple.find(1);
  ASSERT_NE(it, simple.end());
  simple.erase(it);
  EXPECT_EQ(simple.find(1), simple.end());
}

TEST(FlatMapTest, CanUseNonDefaultConstructibleThings) {
  class NonDefaultConstructible {
   public:
    NonDefaultConstructible(int x, int y) : x_(x), y_(y) {}

    int x() { return x_; }

    int y() { return y_; }

   private:
    int x_;
    int y_;
  };

  FlatMap<int, NonDefaultConstructible> flat_map;
  flat_map.emplace_back(3, NonDefaultConstructible(2, 3));
  auto it = flat_map.find(3);
  ASSERT_TRUE(it != flat_map.end());
  EXPECT_GT(it->second.y(), it->second.x());
}
}  // namespace openscreen