First stab at global refactoring of memory management - the library builds, but unit tests are failing horribly

This commit is contained in:
Pavel Kirienko
2015-10-14 08:29:50 +03:00
parent 0643879922
commit be84897ed6
33 changed files with 203 additions and 756 deletions
+19 -25
View File
@@ -2,6 +2,11 @@
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#if __GNUC__
// We need auto_ptr for compatibility reasons
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <string>
#include <cstdio>
#include <memory>
@@ -46,7 +51,7 @@ TEST(Map, Basic)
static const int POOL_BLOCKS = 3;
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
typedef Map<std::string, std::string, 2> MapType;
typedef Map<std::string, std::string> MapType;
std::auto_ptr<MapType> map(new MapType(pool));
// Empty
@@ -57,25 +62,23 @@ TEST(Map, Basic)
ASSERT_FALSE(map->getByIndex(1));
ASSERT_FALSE(map->getByIndex(10000));
// Static insertion
// Insertion
ASSERT_EQ("a", *map->insert("1", "a"));
ASSERT_EQ("b", *map->insert("2", "b"));
ASSERT_EQ(0, pool.getNumUsedBlocks());
ASSERT_EQ(2, map->getNumStaticPairs());
ASSERT_EQ(0, map->getNumDynamicPairs());
ASSERT_EQ(1, pool.getNumUsedBlocks());
ASSERT_EQ(2, map->getSize());
// Ordering
ASSERT_TRUE(map->getByIndex(0)->match("1"));
ASSERT_TRUE(map->getByIndex(1)->match("2"));
// Dynamic insertion
// Insertion
ASSERT_EQ("c", *map->insert("3", "c"));
ASSERT_EQ(1, pool.getNumUsedBlocks());
ASSERT_EQ("d", *map->insert("4", "d"));
ASSERT_EQ(1, pool.getNumUsedBlocks()); // Assuming that at least 2 items fit one block
ASSERT_EQ(2, map->getNumStaticPairs());
ASSERT_EQ(2, map->getNumDynamicPairs());
ASSERT_EQ(2, pool.getNumUsedBlocks()); // Assuming that at least 2 items fit one block
ASSERT_EQ(4, map->getSize());
// Making sure everything is here
ASSERT_EQ("a", *map->access("1"));
@@ -116,12 +119,11 @@ TEST(Map, Basic)
ASSERT_EQ("4", *map->find(ValueFindPredicate("D")));
ASSERT_FALSE(map->find(KeyFindPredicate("nonexistent_value")));
// Removing one static
// Removing one
map->remove("1"); // One of dynamics now migrates to the static storage
map->remove("foo"); // There's no such thing anyway
ASSERT_EQ(1, pool.getNumUsedBlocks());
ASSERT_EQ(2, map->getNumStaticPairs());
ASSERT_EQ(1, map->getNumDynamicPairs());
ASSERT_EQ(2, pool.getNumUsedBlocks());
ASSERT_EQ(3, map->getSize());
ASSERT_FALSE(map->access("1"));
ASSERT_EQ("B", *map->access("2"));
@@ -135,9 +137,8 @@ TEST(Map, Basic)
// Removing another static
map->remove("2");
ASSERT_EQ(2, map->getNumStaticPairs());
ASSERT_EQ(0, map->getNumDynamicPairs());
ASSERT_EQ(0, pool.getNumUsedBlocks()); // No dynamic entries left
ASSERT_EQ(1, map->getSize());
ASSERT_EQ(1, pool.getNumUsedBlocks()); // No dynamic entries left
ASSERT_FALSE(map->access("1"));
ASSERT_FALSE(map->access("2"));
@@ -172,13 +173,7 @@ TEST(Map, Basic)
ASSERT_FALSE(map->access("value"));
// Removing odd values - nearly half of them
ASSERT_EQ(2, map->getNumStaticPairs());
const unsigned num_dynamics_old = map->getNumDynamicPairs();
map->removeAllWhere(oddValuePredicate);
ASSERT_EQ(2, map->getNumStaticPairs());
const unsigned num_dynamics_new = map->getNumDynamicPairs();
std::cout << "Num of dynamic pairs reduced from " << num_dynamics_old << " to " << num_dynamics_new << std::endl;
ASSERT_LT(num_dynamics_new, num_dynamics_old);
// Making sure there's no odd values left
for (unsigned kv_int = 0; kv_int <= max_key_integer; kv_int++)
@@ -220,8 +215,7 @@ TEST(Map, NoStatic)
ASSERT_EQ("a", *map->insert("1", "a"));
ASSERT_EQ("b", *map->insert("2", "b"));
ASSERT_EQ(1, pool.getNumUsedBlocks());
ASSERT_EQ(0, map->getNumStaticPairs());
ASSERT_EQ(2, map->getNumDynamicPairs());
ASSERT_EQ(2, map->getSize());
// Ordering
ASSERT_TRUE(map->getByIndex(0)->match("1"));
@@ -238,7 +232,7 @@ TEST(Map, PrimitiveKey)
static const int POOL_BLOCKS = 3;
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
typedef Map<short, short, 2> MapType;
typedef Map<short, short> MapType;
std::auto_ptr<MapType> map(new MapType(pool));
// Empty