blob: 92f0d5f7af450d0283b1aa48de9cc3a703a86b5f [file] [log] [blame]
Jordan Baylesfc4b6232020-11-24 17:10:33 -08001// Copyright 2020 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 "util/flat_map.h"
6
7#include <chrono>
8
9#include "absl/strings/string_view.h"
10#include "gtest/gtest.h"
11
12namespace openscreen {
13
14namespace {
15
16const FlatMap<int, absl::string_view> kSimpleFlatMap{{-1, "bar"},
17 {123, "foo"},
18 {10000, "baz"},
19 {0, ""}};
20
21} // namespace
22
23TEST(FlatMapTest, Find) {
24 EXPECT_EQ(kSimpleFlatMap.begin(), kSimpleFlatMap.find(-1));
25 EXPECT_EQ("bar", kSimpleFlatMap.find(-1)->second);
26 EXPECT_EQ("foo", kSimpleFlatMap.find(123)->second);
27 EXPECT_EQ("baz", kSimpleFlatMap.find(10000)->second);
28 EXPECT_EQ("", kSimpleFlatMap.find(0)->second);
29 EXPECT_EQ(kSimpleFlatMap.end(), kSimpleFlatMap.find(2));
30}
31
32// Since it is backed by a vector, we don't expose an operator[] due
33// to potential confusion. If the type of Key is int or std::size_t, do
34// you want the std::pair<Key, Value> or just the Value?
35TEST(FlatMapTest, Access) {
36 EXPECT_EQ("bar", kSimpleFlatMap[0].second);
37 EXPECT_EQ("foo", kSimpleFlatMap[1].second);
38 EXPECT_EQ("baz", kSimpleFlatMap[2].second);
39 EXPECT_EQ("", kSimpleFlatMap[3].second);
40
41 // NOTE: vector doesn't do any range checking for operator[], only at().
42 EXPECT_EQ("bar", kSimpleFlatMap.at(0).second);
43 EXPECT_EQ("foo", kSimpleFlatMap.at(1).second);
44 EXPECT_EQ("baz", kSimpleFlatMap.at(2).second);
45 EXPECT_EQ("", kSimpleFlatMap.at(3).second);
Jordan Baylesf5b12d62020-11-24 22:34:54 -080046 // The error message varies widely depending on how the test is run.
47 EXPECT_DEATH(kSimpleFlatMap.at(31337), ".*");
Jordan Baylesfc4b6232020-11-24 17:10:33 -080048}
49
50TEST(FlatMapTest, ErasureAndEmplacement) {
51 auto mutable_vector = kSimpleFlatMap;
52 EXPECT_NE(mutable_vector.find(-1), mutable_vector.end());
53 mutable_vector.erase_key(-1);
54 EXPECT_EQ(mutable_vector.find(-1), mutable_vector.end());
55
56 mutable_vector.emplace_back(-1, "bar");
57 EXPECT_NE(mutable_vector.find(-1), mutable_vector.end());
58
59 // We absolutely should fail to erase something that's not there.
60 EXPECT_DEATH(mutable_vector.erase_key(12345),
61 ".*failed to erase: element not found.*");
62}
63
64TEST(FlatMapTest, Mutation) {
65 FlatMap<int, int> mutable_vector{{1, 2}};
66 EXPECT_EQ(2, mutable_vector[0].second);
67
68 mutable_vector[0].second = 3;
69 EXPECT_EQ(3, mutable_vector[0].second);
70}
71
72TEST(FlatMapTest, GenerallyBehavesLikeAVector) {
73 EXPECT_EQ(kSimpleFlatMap, kSimpleFlatMap);
74 EXPECT_TRUE(kSimpleFlatMap == kSimpleFlatMap);
75
76 for (auto& entry : kSimpleFlatMap) {
77 if (entry.first != 0) {
78 EXPECT_LT(0u, entry.second.size());
79 }
80 }
81
82 FlatMap<int, int> simple;
83 simple.emplace_back(1, 10);
84 EXPECT_EQ(simple, (FlatMap<int, int>{{1, 10}}));
85
86 auto it = simple.find(1);
87 ASSERT_NE(it, simple.end());
88 simple.erase(it);
89 EXPECT_EQ(simple.find(1), simple.end());
90}
91
92TEST(FlatMapTest, CanUseNonDefaultConstructibleThings) {
93 class NonDefaultConstructible {
94 public:
95 NonDefaultConstructible(int x, int y) : x_(x), y_(y) {}
96
97 int x() { return x_; }
98
99 int y() { return y_; }
100
101 private:
102 int x_;
103 int y_;
104 };
105
106 FlatMap<int, NonDefaultConstructible> flat_map;
107 flat_map.emplace_back(3, NonDefaultConstructible(2, 3));
108 auto it = flat_map.find(3);
109 ASSERT_TRUE(it != flat_map.end());
110 EXPECT_GT(it->second.y(), it->second.x());
111}
112} // namespace openscreen