summaryrefslogtreecommitdiff
path: root/test/unit/base/map_util_black.cpp
blob: 76238ad0453844ec5f10b2e4441a23732ad72cfd (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*********************                                                        */
/*! \file map_util_black.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Aina Niemetz, Tim King
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 ** in the top-level source directory and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Black box testing of map utility functions.
 **
 ** Black box testing of map utility functions.
 **/

#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>

#include "base/map_util.h"
#include "context/cdhashmap.h"
#include "context/cdhashset.h"
#include "context/cdinsert_hashmap.h"
#include "context/context.h"
#include "test.h"

namespace CVC5 {

using context::CDHashMap;
using context::CDInsertHashMap;
using context::Context;

namespace test {

class TestBaseBlackMap : public TestInternal
{
 protected:
  /** Returns a map containing {"key"->"value", "other"->"entry"}. */
  static const std::map<std::string, std::string>& default_map()
  {
    static const std::map<std::string, std::string> static_stored_map{
        {"key", "value"}, {"other", "entry"}};
    return static_stored_map;
  }

  /**
   * For each <key, value> pair in source, inserts mapping from key to value
   * using insert into dest.
   */
  template <class M>
  void insert_all(const std::map<std::string, std::string>& source, M* dest)
  {
    for (const auto& key_value : source)
    {
      dest->insert(key_value.first, key_value.second);
    }
  }
};

TEST_F(TestBaseBlackMap, map)
{
  std::map<std::string, std::string> map = default_map();
  ASSERT_TRUE(CVC5::ContainsKey(map, "key"));
  ASSERT_FALSE(CVC5::ContainsKey(map, "non key"));

  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
  if (std::string* found_value = FindOrNull(map, "other"))
  {
    ASSERT_EQ(*found_value, "entry");
    *found_value = "new value";
  }
  ASSERT_EQ(FindOrDie(map, "other"), "new value");
}

TEST_F(TestBaseBlackMap, constant_map)
{
  const std::map<std::string, std::string> map = default_map();
  ASSERT_TRUE(CVC5::ContainsKey(map, "key"));
  ASSERT_FALSE(CVC5::ContainsKey(map, "non key"));

  if (const std::string* found_value = FindOrNull(map, "other"))
  {
    ASSERT_EQ(*found_value, "entry");
  }
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
  ASSERT_DEATH(FindOrDie(map, "asdf"), "The map does not contain the key.");
}

TEST_F(TestBaseBlackMap, unordered_map)
{
  std::unordered_map<std::string, std::string> map(default_map().begin(),
                                                   default_map().end());
  ASSERT_TRUE(CVC5::ContainsKey(map, "key"));
  ASSERT_FALSE(CVC5::ContainsKey(map, "non key"));

  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
  if (std::string* found_value = FindOrNull(map, "other"))
  {
    ASSERT_EQ(*found_value, "entry");
    *found_value = "new value";
  }
  ASSERT_EQ(FindOrDie(map, "other"), "new value");
}

TEST_F(TestBaseBlackMap, const_unordered_map)
{
  const std::unordered_map<std::string, std::string> map(default_map().begin(),
                                                         default_map().end());
  ASSERT_TRUE(CVC5::ContainsKey(map, "key"));
  ASSERT_FALSE(CVC5::ContainsKey(map, "non key"));

  if (const std::string* found_value = FindOrNull(map, "other"))
  {
    ASSERT_EQ(*found_value, "entry");
  }
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
  ASSERT_DEATH(FindOrDie(map, "asdf"), "The map does not contain the key.");
}

TEST_F(TestBaseBlackMap, set)
{
  std::set<std::string> set{"entry", "other"};
  ASSERT_TRUE(CVC5::ContainsKey(set, "entry"));
  ASSERT_FALSE(CVC5::ContainsKey(set, "non member"));

  const std::set<std::string> const_set{"entry", "other"};
  ASSERT_TRUE(CVC5::ContainsKey(const_set, "entry"));
  ASSERT_FALSE(CVC5::ContainsKey(const_set, "non member"));
}

TEST_F(TestBaseBlackMap, unordered_set)
{
  std::unordered_set<std::string> set{"entry", "other"};
  ASSERT_TRUE(CVC5::ContainsKey(set, "entry"));
  ASSERT_FALSE(CVC5::ContainsKey(set, "non member"));

  const std::unordered_set<std::string> const_set{"entry", "other"};
  ASSERT_TRUE(CVC5::ContainsKey(const_set, "entry"));
  ASSERT_FALSE(CVC5::ContainsKey(const_set, "non member"));
}

TEST_F(TestBaseBlackMap, CDHashMap)
{
  Context context;
  CDHashMap<std::string, std::string> map(&context);
  insert_all(default_map(), &map);

  ASSERT_TRUE(CVC5::ContainsKey(map, "key"));
  ASSERT_FALSE(CVC5::ContainsKey(map, "non key"));

  if (const std::string* found_value = FindOrNull(map, "other"))
  {
    ASSERT_EQ(*found_value, "entry");
  }
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
}

TEST_F(TestBaseBlackMap, const_CDHashMap)
{
  Context context;
  CDHashMap<std::string, std::string> store(&context);
  insert_all(default_map(), &store);
  const auto& map = store;

  ASSERT_TRUE(CVC5::ContainsKey(map, "key"));
  ASSERT_FALSE(CVC5::ContainsKey(map, "non key"));

  if (const std::string* found_value = FindOrNull(map, "other"))
  {
    ASSERT_EQ(*found_value, "entry");
  }
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
}

TEST_F(TestBaseBlackMap, CDInsertHashMap)
{
  Context context;
  CDInsertHashMap<std::string, std::string> map(&context);
  insert_all(default_map(), &map);

  ASSERT_TRUE(CVC5::ContainsKey(map, "key"));
  ASSERT_FALSE(CVC5::ContainsKey(map, "non key"));

  if (const std::string* found_value = FindOrNull(map, "other"))
  {
    ASSERT_EQ(*found_value, "entry");
  }
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
}

TEST_F(TestBaseBlackMap, const_CDInsertHashMap)
{
  Context context;
  CDInsertHashMap<std::string, std::string> store(&context);
  insert_all(default_map(), &store);
  const auto& map = store;

  ASSERT_TRUE(CVC5::ContainsKey(map, "key"));
  ASSERT_FALSE(CVC5::ContainsKey(map, "non key"));
  if (const std::string* found_value = FindOrNull(map, "other"))
  {
    ASSERT_EQ(*found_value, "entry");
  }
  ASSERT_EQ(FindOrNull(map, "non key"), nullptr);
  ASSERT_EQ(FindOrDie(map, "other"), "entry");
}
}  // namespace test
}  // namespace CVC5
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback