GCC Code Coverage Report
Directory: . Exec Total Coverage
File: test/unit/base/map_util_black.cpp Lines: 103 103 100.0 %
Date: 2021-09-16 Branches: 332 1110 29.9 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Aina Niemetz, Tim King
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * Black box testing of map utility functions.
14
 */
15
16
#include <map>
17
#include <set>
18
#include <string>
19
#include <unordered_map>
20
#include <unordered_set>
21
22
#include "base/map_util.h"
23
#include "context/cdhashmap.h"
24
#include "context/cdhashset.h"
25
#include "context/cdinsert_hashmap.h"
26
#include "context/context.h"
27
#include "test.h"
28
29
namespace cvc5 {
30
31
using context::CDHashMap;
32
using context::CDInsertHashMap;
33
using context::Context;
34
35
namespace test {
36
37
40
class TestBaseBlackMap : public TestInternal
38
{
39
 protected:
40
  /** Returns a map containing {"key"->"value", "other"->"entry"}. */
41
20
  static const std::map<std::string, std::string>& default_map()
42
  {
43
    static const std::map<std::string, std::string> static_stored_map{
44
20
        {"key", "value"}, {"other", "entry"}};
45
20
    return static_stored_map;
46
  }
47
48
  /**
49
   * For each <key, value> pair in source, inserts mapping from key to value
50
   * using insert into dest.
51
   */
52
  template <class M>
53
8
  void insert_all(const std::map<std::string, std::string>& source, M* dest)
54
  {
55
24
    for (const auto& key_value : source)
56
    {
57
16
      dest->insert(key_value.first, key_value.second);
58
    }
59
8
  }
60
};
61
62
19
TEST_F(TestBaseBlackMap, map)
63
{
64
4
  std::map<std::string, std::string> map = default_map();
65
2
  ASSERT_TRUE(cvc5::ContainsKey(map, "key"));
66
2
  ASSERT_FALSE(cvc5::ContainsKey(map, "non key"));
67
68
2
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
69
2
  if (std::string* found_value = FindOrNull(map, "other"))
70
  {
71
2
    ASSERT_EQ(*found_value, "entry");
72
2
    *found_value = "new value";
73
  }
74
2
  ASSERT_EQ(FindOrDie(map, "other"), "new value");
75
}
76
77
19
TEST_F(TestBaseBlackMap, constant_map)
78
{
79
4
  const std::map<std::string, std::string> map = default_map();
80
2
  ASSERT_TRUE(cvc5::ContainsKey(map, "key"));
81
2
  ASSERT_FALSE(cvc5::ContainsKey(map, "non key"));
82
83
2
  if (const std::string* found_value = FindOrNull(map, "other"))
84
  {
85
2
    ASSERT_EQ(*found_value, "entry");
86
  }
87
2
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
88
2
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
89
2
  ASSERT_DEATH(FindOrDie(map, "asdf"), "The map does not contain the key.");
90
}
91
92
19
TEST_F(TestBaseBlackMap, unordered_map)
93
{
94
2
  std::unordered_map<std::string, std::string> map(default_map().begin(),
95
6
                                                   default_map().end());
96
2
  ASSERT_TRUE(cvc5::ContainsKey(map, "key"));
97
2
  ASSERT_FALSE(cvc5::ContainsKey(map, "non key"));
98
99
2
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
100
2
  if (std::string* found_value = FindOrNull(map, "other"))
101
  {
102
2
    ASSERT_EQ(*found_value, "entry");
103
2
    *found_value = "new value";
104
  }
105
2
  ASSERT_EQ(FindOrDie(map, "other"), "new value");
106
}
107
108
19
TEST_F(TestBaseBlackMap, const_unordered_map)
109
{
110
2
  const std::unordered_map<std::string, std::string> map(default_map().begin(),
111
6
                                                         default_map().end());
112
2
  ASSERT_TRUE(cvc5::ContainsKey(map, "key"));
113
2
  ASSERT_FALSE(cvc5::ContainsKey(map, "non key"));
114
115
2
  if (const std::string* found_value = FindOrNull(map, "other"))
116
  {
117
2
    ASSERT_EQ(*found_value, "entry");
118
  }
119
2
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
120
2
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
121
2
  ASSERT_DEATH(FindOrDie(map, "asdf"), "The map does not contain the key.");
122
}
123
124
19
TEST_F(TestBaseBlackMap, set)
125
{
126
4
  std::set<std::string> set{"entry", "other"};
127
2
  ASSERT_TRUE(cvc5::ContainsKey(set, "entry"));
128
2
  ASSERT_FALSE(cvc5::ContainsKey(set, "non member"));
129
130
4
  const std::set<std::string> const_set{"entry", "other"};
131
2
  ASSERT_TRUE(cvc5::ContainsKey(const_set, "entry"));
132
2
  ASSERT_FALSE(cvc5::ContainsKey(const_set, "non member"));
133
}
134
135
19
TEST_F(TestBaseBlackMap, unordered_set)
136
{
137
4
  std::unordered_set<std::string> set{"entry", "other"};
138
2
  ASSERT_TRUE(cvc5::ContainsKey(set, "entry"));
139
2
  ASSERT_FALSE(cvc5::ContainsKey(set, "non member"));
140
141
4
  const std::unordered_set<std::string> const_set{"entry", "other"};
142
2
  ASSERT_TRUE(cvc5::ContainsKey(const_set, "entry"));
143
2
  ASSERT_FALSE(cvc5::ContainsKey(const_set, "non member"));
144
}
145
146
19
TEST_F(TestBaseBlackMap, CDHashMap)
147
{
148
4
  Context context;
149
4
  CDHashMap<std::string, std::string> map(&context);
150
2
  insert_all(default_map(), &map);
151
152
2
  ASSERT_TRUE(cvc5::ContainsKey(map, "key"));
153
2
  ASSERT_FALSE(cvc5::ContainsKey(map, "non key"));
154
155
2
  if (const std::string* found_value = FindOrNull(map, "other"))
156
  {
157
2
    ASSERT_EQ(*found_value, "entry");
158
  }
159
2
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
160
2
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
161
}
162
163
19
TEST_F(TestBaseBlackMap, const_CDHashMap)
164
{
165
4
  Context context;
166
4
  CDHashMap<std::string, std::string> store(&context);
167
2
  insert_all(default_map(), &store);
168
2
  const auto& map = store;
169
170
2
  ASSERT_TRUE(cvc5::ContainsKey(map, "key"));
171
2
  ASSERT_FALSE(cvc5::ContainsKey(map, "non key"));
172
173
2
  if (const std::string* found_value = FindOrNull(map, "other"))
174
  {
175
2
    ASSERT_EQ(*found_value, "entry");
176
  }
177
2
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
178
2
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
179
}
180
181
19
TEST_F(TestBaseBlackMap, CDInsertHashMap)
182
{
183
4
  Context context;
184
4
  CDInsertHashMap<std::string, std::string> map(&context);
185
2
  insert_all(default_map(), &map);
186
187
2
  ASSERT_TRUE(cvc5::ContainsKey(map, "key"));
188
2
  ASSERT_FALSE(cvc5::ContainsKey(map, "non key"));
189
190
2
  if (const std::string* found_value = FindOrNull(map, "other"))
191
  {
192
2
    ASSERT_EQ(*found_value, "entry");
193
  }
194
2
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
195
2
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
196
}
197
198
19
TEST_F(TestBaseBlackMap, const_CDInsertHashMap)
199
{
200
4
  Context context;
201
4
  CDInsertHashMap<std::string, std::string> store(&context);
202
2
  insert_all(default_map(), &store);
203
2
  const auto& map = store;
204
205
2
  ASSERT_TRUE(cvc5::ContainsKey(map, "key"));
206
2
  ASSERT_FALSE(cvc5::ContainsKey(map, "non key"));
207
2
  if (const std::string* found_value = FindOrNull(map, "other"))
208
  {
209
2
    ASSERT_EQ(*found_value, "entry");
210
  }
211
2
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
212
2
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
213
}
214
}  // namespace test
215
33
}  // namespace cvc5