map.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #if __GNUC__
6 # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
7 #endif
8 
9 #include <string>
10 #include <cstdio>
11 #include <memory>
12 #include <gtest/gtest.h>
13 #include <uavcan/util/map.hpp>
14 
15 
16 /*
17  * TODO: This one test has been temporarily disabled because it is not compatible with newer versions of libstdc++
18  * that ship with newer versions of GCC. The problem is that std::string has become too large to fit into a 64-byte
19  * large memory block. This should be fixed in the future.
20  */
21 #if 0
22 static std::string toString(long x)
23 {
24  char buf[80];
25  std::snprintf(buf, sizeof(buf), "%li", x);
26  return std::string(buf);
27 }
28 
29 static bool oddValuePredicate(const std::string& key, const std::string& value)
30 {
31  EXPECT_FALSE(key.empty());
32  EXPECT_FALSE(value.empty());
33  const int num = atoi(value.c_str());
34  return num & 1;
35 }
36 
37 struct KeyFindPredicate
38 {
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; }
42 };
43 
44 struct ValueFindPredicate
45 {
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; }
49 };
50 
51 
52 TEST(Map, Basic)
53 {
54  using uavcan::Map;
55 
56  static const int POOL_BLOCKS = 3;
58 
59  typedef Map<std::string, std::string> MapType;
60  std::unique_ptr<MapType> map(new MapType(pool));
61 
62  // Empty
63  ASSERT_FALSE(map->access("hi"));
64  map->remove("foo");
65  ASSERT_EQ(0, pool.getNumUsedBlocks());
66  ASSERT_FALSE(map->getByIndex(0));
67  ASSERT_FALSE(map->getByIndex(1));
68  ASSERT_FALSE(map->getByIndex(10000));
69 
70  // Insertion
71  ASSERT_EQ("a", *map->insert("1", "a"));
72  ASSERT_EQ("b", *map->insert("2", "b"));
73  ASSERT_EQ(1, pool.getNumUsedBlocks());
74  ASSERT_EQ(2, map->getSize());
75 
76  // Ordering
77  ASSERT_TRUE(map->getByIndex(0)->match("1"));
78  ASSERT_TRUE(map->getByIndex(1)->match("2"));
79 
80  // Insertion
81  ASSERT_EQ("c", *map->insert("3", "c"));
82  ASSERT_EQ(1, pool.getNumUsedBlocks());
83 
84  ASSERT_EQ("d", *map->insert("4", "d"));
85  ASSERT_EQ(2, pool.getNumUsedBlocks()); // Assuming that at least 2 items fit one block
86  ASSERT_EQ(4, map->getSize());
87 
88  // Making sure everything is here
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"));
94 
95  // Modifying existing entries
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"));
104 
105  // Finding some keys
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")));
111 
112  // Finding some values
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")));
118 
119  // Removing one
120  map->remove("1"); // One of dynamics now migrates to the static storage
121  map->remove("foo"); // There's no such thing anyway
122  ASSERT_EQ(2, pool.getNumUsedBlocks());
123  ASSERT_EQ(3, map->getSize());
124 
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"));
129 
130  // Removing another
131  map->remove("2");
132  ASSERT_EQ(2, map->getSize());
133  ASSERT_EQ(2, pool.getNumUsedBlocks());
134 
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"));
139 
140  // Adding some new
141  unsigned max_key_integer = 0;
142  for (int i = 0; i < 100; i++)
143  {
144  const std::string key = toString(i);
145  const std::string value = toString(i);
146  std::string* res = map->insert(key, value); // Will override some from the above
147  if (res == UAVCAN_NULLPTR)
148  {
149  ASSERT_LT(2, i);
150  break;
151  }
152  else
153  {
154  ASSERT_EQ(value, *res);
155  }
156  max_key_integer = unsigned(i);
157  }
158  std::cout << "Max key/value: " << max_key_integer << std::endl;
159  ASSERT_LT(4, max_key_integer);
160 
161  // Making sure there is true OOM
162  ASSERT_EQ(0, pool.getNumFreeBlocks());
163  ASSERT_FALSE(map->insert("nonexistent", "value"));
164  ASSERT_FALSE(map->access("nonexistent"));
165  ASSERT_FALSE(map->access("value"));
166 
167  // Removing odd values - nearly half of them
168  map->removeAllWhere(oddValuePredicate);
169 
170  // Making sure there's no odd values left
171  for (unsigned kv_int = 0; kv_int <= max_key_integer; kv_int++)
172  {
173  const std::string* val = map->access(toString(kv_int));
174  if (val)
175  {
176  ASSERT_FALSE(kv_int & 1);
177  }
178  else
179  {
180  ASSERT_TRUE(kv_int & 1);
181  }
182  }
183 
184  // Making sure the memory will be released
185  map.reset();
186  ASSERT_EQ(0, pool.getNumUsedBlocks());
187 }
188 #endif
189 
190 
191 TEST(Map, PrimitiveKey)
192 {
193  using uavcan::Map;
194 
195  static const int POOL_BLOCKS = 3;
197 
198  typedef Map<short, short> MapType;
199  std::unique_ptr<MapType> map(new MapType(pool));
200 
201  // Empty
202  ASSERT_FALSE(map->access(1));
203  map->remove(8);
204  ASSERT_EQ(0, pool.getNumUsedBlocks());
205  ASSERT_EQ(0, map->getSize());
206  ASSERT_FALSE(map->getByIndex(0));
207 
208  // Insertion
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());
216 
217  // Ordering
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));
224 }
UAVCAN_NULLPTR
#define UAVCAN_NULLPTR
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:51
oddValuePredicate
static bool oddValuePredicate(const std::string &value)
Definition: multiset.cpp:23
map.hpp
uavcan::PoolAllocator
Definition: dynamic_memory.hpp:51
uavcan::PoolAllocator::getNumFreeBlocks
uint16_t getNumFreeBlocks() const
Definition: dynamic_memory.hpp:91
uavcan::Map
Definition: map.hpp:33
uavcan::snprintf
int snprintf(char *out, std::size_t maxlen, const char *format,...)
Definition: std.hpp:73
toString
static std::string toString(long x)
Definition: multiset.cpp:16
TEST
TEST(Map, PrimitiveKey)
Definition: map.cpp:191
uavcan::PoolAllocator::getNumUsedBlocks
uint16_t getNumUsedBlocks() const
Definition: dynamic_memory.hpp:85
pyuavcan_v0.driver.timestamp_estimator.x
x
Definition: timestamp_estimator.py:221


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02