123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- #if __GNUC__
- # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
- # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
- #endif
- #include <string>
- #include <cstdio>
- #include <memory>
- #include <gtest/gtest.h>
- #include <uavcan/util/map.hpp>
- #if 0
- static std::string toString(long x)
- {
- char buf[80];
- std::snprintf(buf, sizeof(buf), "%li", x);
- return std::string(buf);
- }
- static bool oddValuePredicate(const std::string& key, const std::string& value)
- {
- EXPECT_FALSE(key.empty());
- EXPECT_FALSE(value.empty());
- const int num = atoi(value.c_str());
- return num & 1;
- }
- struct KeyFindPredicate
- {
- const std::string target;
- KeyFindPredicate(std::string target) : target(target) { }
- bool operator()(const std::string& key, const std::string&) const { return key == target; }
- };
- struct ValueFindPredicate
- {
- const std::string target;
- ValueFindPredicate(std::string target) : target(target) { }
- bool operator()(const std::string&, const std::string& value) const { return value == target; }
- };
- TEST(Map, Basic)
- {
- using uavcan::Map;
- static const int POOL_BLOCKS = 3;
- uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
- typedef Map<std::string, std::string> MapType;
- std::auto_ptr<MapType> map(new MapType(pool));
-
- ASSERT_FALSE(map->access("hi"));
- map->remove("foo");
- ASSERT_EQ(0, pool.getNumUsedBlocks());
- ASSERT_FALSE(map->getByIndex(0));
- ASSERT_FALSE(map->getByIndex(1));
- ASSERT_FALSE(map->getByIndex(10000));
-
- ASSERT_EQ("a", *map->insert("1", "a"));
- ASSERT_EQ("b", *map->insert("2", "b"));
- ASSERT_EQ(1, pool.getNumUsedBlocks());
- ASSERT_EQ(2, map->getSize());
-
- ASSERT_TRUE(map->getByIndex(0)->match("1"));
- ASSERT_TRUE(map->getByIndex(1)->match("2"));
-
- ASSERT_EQ("c", *map->insert("3", "c"));
- ASSERT_EQ(1, pool.getNumUsedBlocks());
- ASSERT_EQ("d", *map->insert("4", "d"));
- ASSERT_EQ(2, pool.getNumUsedBlocks());
- ASSERT_EQ(4, map->getSize());
-
- ASSERT_EQ("a", *map->access("1"));
- ASSERT_EQ("b", *map->access("2"));
- ASSERT_EQ("c", *map->access("3"));
- ASSERT_EQ("d", *map->access("4"));
- ASSERT_FALSE(map->access("hi"));
-
- *map->access("1") = "A";
- *map->access("2") = "B";
- *map->access("3") = "C";
- *map->access("4") = "D";
- ASSERT_EQ("A", *map->access("1"));
- ASSERT_EQ("B", *map->access("2"));
- ASSERT_EQ("C", *map->access("3"));
- ASSERT_EQ("D", *map->access("4"));
-
- ASSERT_EQ("1", *map->find(KeyFindPredicate("1")));
- ASSERT_EQ("2", *map->find(KeyFindPredicate("2")));
- ASSERT_EQ("3", *map->find(KeyFindPredicate("3")));
- ASSERT_EQ("4", *map->find(KeyFindPredicate("4")));
- ASSERT_FALSE(map->find(KeyFindPredicate("nonexistent_key")));
-
- ASSERT_EQ("1", *map->find(ValueFindPredicate("A")));
- ASSERT_EQ("2", *map->find(ValueFindPredicate("B")));
- ASSERT_EQ("3", *map->find(ValueFindPredicate("C")));
- ASSERT_EQ("4", *map->find(ValueFindPredicate("D")));
- ASSERT_FALSE(map->find(KeyFindPredicate("nonexistent_value")));
-
- map->remove("1");
- map->remove("foo");
- ASSERT_EQ(2, pool.getNumUsedBlocks());
- ASSERT_EQ(3, map->getSize());
- ASSERT_FALSE(map->access("1"));
- ASSERT_EQ("B", *map->access("2"));
- ASSERT_EQ("C", *map->access("3"));
- ASSERT_EQ("D", *map->access("4"));
-
- map->remove("2");
- ASSERT_EQ(2, map->getSize());
- ASSERT_EQ(2, pool.getNumUsedBlocks());
- ASSERT_FALSE(map->access("1"));
- ASSERT_FALSE(map->access("2"));
- ASSERT_EQ("C", *map->access("3"));
- ASSERT_EQ("D", *map->access("4"));
-
- unsigned max_key_integer = 0;
- for (int i = 0; i < 100; i++)
- {
- const std::string key = toString(i);
- const std::string value = toString(i);
- std::string* res = map->insert(key, value);
- if (res == UAVCAN_NULLPTR)
- {
- ASSERT_LT(2, i);
- break;
- }
- else
- {
- ASSERT_EQ(value, *res);
- }
- max_key_integer = unsigned(i);
- }
- std::cout << "Max key/value: " << max_key_integer << std::endl;
- ASSERT_LT(4, max_key_integer);
-
- ASSERT_EQ(0, pool.getNumFreeBlocks());
- ASSERT_FALSE(map->insert("nonexistent", "value"));
- ASSERT_FALSE(map->access("nonexistent"));
- ASSERT_FALSE(map->access("value"));
-
- map->removeAllWhere(oddValuePredicate);
-
- for (unsigned kv_int = 0; kv_int <= max_key_integer; kv_int++)
- {
- const std::string* val = map->access(toString(kv_int));
- if (val)
- {
- ASSERT_FALSE(kv_int & 1);
- }
- else
- {
- ASSERT_TRUE(kv_int & 1);
- }
- }
-
- map.reset();
- ASSERT_EQ(0, pool.getNumUsedBlocks());
- }
- #endif
- TEST(Map, PrimitiveKey)
- {
- using uavcan::Map;
- static const int POOL_BLOCKS = 3;
- uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
- typedef Map<short, short> MapType;
- std::auto_ptr<MapType> map(new MapType(pool));
-
- ASSERT_FALSE(map->access(1));
- map->remove(8);
- ASSERT_EQ(0, pool.getNumUsedBlocks());
- ASSERT_EQ(0, map->getSize());
- ASSERT_FALSE(map->getByIndex(0));
-
- ASSERT_EQ(1, *map->insert(1, 1));
- ASSERT_EQ(1, map->getSize());
- ASSERT_EQ(2, *map->insert(2, 2));
- ASSERT_EQ(2, map->getSize());
- ASSERT_EQ(3, *map->insert(3, 3));
- ASSERT_EQ(4, *map->insert(4, 4));
- ASSERT_EQ(4, map->getSize());
-
- ASSERT_TRUE(map->getByIndex(0)->match(1));
- ASSERT_TRUE(map->getByIndex(1)->match(2));
- ASSERT_TRUE(map->getByIndex(2)->match(3));
- ASSERT_TRUE(map->getByIndex(3)->match(4));
- ASSERT_FALSE(map->getByIndex(5));
- ASSERT_FALSE(map->getByIndex(1000));
- }
|