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

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