6 # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
12 #include <gtest/gtest.h>
26 return std::string(buf);
31 EXPECT_FALSE(key.empty());
32 EXPECT_FALSE(value.empty());
33 const int num = atoi(value.c_str());
37 struct KeyFindPredicate
39 const std::string target;
40 KeyFindPredicate(std::string target) : target(target) { }
41 bool operator()(
const std::string& key,
const std::string&)
const {
return key == target; }
44 struct ValueFindPredicate
46 const std::string target;
47 ValueFindPredicate(std::string target) : target(target) { }
48 bool operator()(
const std::string&,
const std::string& value)
const {
return value == target; }
56 static const int POOL_BLOCKS = 3;
59 typedef Map<std::string, std::string> MapType;
60 std::unique_ptr<MapType> map(
new MapType(pool));
63 ASSERT_FALSE(map->access(
"hi"));
66 ASSERT_FALSE(map->getByIndex(0));
67 ASSERT_FALSE(map->getByIndex(1));
68 ASSERT_FALSE(map->getByIndex(10000));
71 ASSERT_EQ(
"a", *map->insert(
"1",
"a"));
72 ASSERT_EQ(
"b", *map->insert(
"2",
"b"));
74 ASSERT_EQ(2, map->getSize());
77 ASSERT_TRUE(map->getByIndex(0)->match(
"1"));
78 ASSERT_TRUE(map->getByIndex(1)->match(
"2"));
81 ASSERT_EQ(
"c", *map->insert(
"3",
"c"));
84 ASSERT_EQ(
"d", *map->insert(
"4",
"d"));
86 ASSERT_EQ(4, map->getSize());
89 ASSERT_EQ(
"a", *map->access(
"1"));
90 ASSERT_EQ(
"b", *map->access(
"2"));
91 ASSERT_EQ(
"c", *map->access(
"3"));
92 ASSERT_EQ(
"d", *map->access(
"4"));
93 ASSERT_FALSE(map->access(
"hi"));
96 *map->access(
"1") =
"A";
97 *map->access(
"2") =
"B";
98 *map->access(
"3") =
"C";
99 *map->access(
"4") =
"D";
100 ASSERT_EQ(
"A", *map->access(
"1"));
101 ASSERT_EQ(
"B", *map->access(
"2"));
102 ASSERT_EQ(
"C", *map->access(
"3"));
103 ASSERT_EQ(
"D", *map->access(
"4"));
106 ASSERT_EQ(
"1", *map->find(KeyFindPredicate(
"1")));
107 ASSERT_EQ(
"2", *map->find(KeyFindPredicate(
"2")));
108 ASSERT_EQ(
"3", *map->find(KeyFindPredicate(
"3")));
109 ASSERT_EQ(
"4", *map->find(KeyFindPredicate(
"4")));
110 ASSERT_FALSE(map->find(KeyFindPredicate(
"nonexistent_key")));
113 ASSERT_EQ(
"1", *map->find(ValueFindPredicate(
"A")));
114 ASSERT_EQ(
"2", *map->find(ValueFindPredicate(
"B")));
115 ASSERT_EQ(
"3", *map->find(ValueFindPredicate(
"C")));
116 ASSERT_EQ(
"4", *map->find(ValueFindPredicate(
"D")));
117 ASSERT_FALSE(map->find(KeyFindPredicate(
"nonexistent_value")));
123 ASSERT_EQ(3, map->getSize());
125 ASSERT_FALSE(map->access(
"1"));
126 ASSERT_EQ(
"B", *map->access(
"2"));
127 ASSERT_EQ(
"C", *map->access(
"3"));
128 ASSERT_EQ(
"D", *map->access(
"4"));
132 ASSERT_EQ(2, map->getSize());
135 ASSERT_FALSE(map->access(
"1"));
136 ASSERT_FALSE(map->access(
"2"));
137 ASSERT_EQ(
"C", *map->access(
"3"));
138 ASSERT_EQ(
"D", *map->access(
"4"));
141 unsigned max_key_integer = 0;
142 for (
int i = 0; i < 100; i++)
144 const std::string key =
toString(i);
145 const std::string value =
toString(i);
146 std::string* res = map->insert(key, value);
154 ASSERT_EQ(value, *res);
156 max_key_integer = unsigned(i);
158 std::cout <<
"Max key/value: " << max_key_integer << std::endl;
159 ASSERT_LT(4, max_key_integer);
163 ASSERT_FALSE(map->insert(
"nonexistent",
"value"));
164 ASSERT_FALSE(map->access(
"nonexistent"));
165 ASSERT_FALSE(map->access(
"value"));
171 for (
unsigned kv_int = 0; kv_int <= max_key_integer; kv_int++)
173 const std::string* val = map->access(
toString(kv_int));
176 ASSERT_FALSE(kv_int & 1);
180 ASSERT_TRUE(kv_int & 1);
195 static const int POOL_BLOCKS = 3;
198 typedef Map<short, short> MapType;
199 std::unique_ptr<MapType> map(
new MapType(pool));
202 ASSERT_FALSE(map->access(1));
205 ASSERT_EQ(0, map->getSize());
206 ASSERT_FALSE(map->getByIndex(0));
209 ASSERT_EQ(1, *map->insert(1, 1));
210 ASSERT_EQ(1, map->getSize());
211 ASSERT_EQ(2, *map->insert(2, 2));
212 ASSERT_EQ(2, map->getSize());
213 ASSERT_EQ(3, *map->insert(3, 3));
214 ASSERT_EQ(4, *map->insert(4, 4));
215 ASSERT_EQ(4, map->getSize());
218 ASSERT_TRUE(map->getByIndex(0)->match(1));
219 ASSERT_TRUE(map->getByIndex(1)->match(2));
220 ASSERT_TRUE(map->getByIndex(2)->match(3));
221 ASSERT_TRUE(map->getByIndex(3)->match(4));
222 ASSERT_FALSE(map->getByIndex(5));
223 ASSERT_FALSE(map->getByIndex(1000));