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
+8 -26
View File
@@ -71,7 +71,7 @@ TEST(Multiset, Basic)
static const int POOL_BLOCKS = 3;
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
typedef Multiset<std::string, 2> MultisetType;
typedef Multiset<std::string> MultisetType;
std::auto_ptr<MultisetType> mset(new MultisetType(pool));
typedef SummationOperator<std::string> StringConcatenationOperator;
@@ -86,9 +86,8 @@ TEST(Multiset, Basic)
// Static addion
ASSERT_EQ("1", *mset->emplace("1"));
ASSERT_EQ("2", *mset->emplace("2"));
ASSERT_EQ(0, pool.getNumUsedBlocks());
ASSERT_EQ(2, mset->getNumStaticItems());
ASSERT_EQ(0, mset->getNumDynamicItems());
ASSERT_LE(1, pool.getNumUsedBlocks()); // One or more
ASSERT_EQ(2, mset->getSize());
// Ordering
ASSERT_TRUE(*mset->getByIndex(0) == "1");
@@ -103,12 +102,11 @@ TEST(Multiset, Basic)
// Dynamic addition
ASSERT_EQ("3", *mset->emplace("3"));
ASSERT_EQ("3", *mset->getByIndex(2));
ASSERT_EQ(1, pool.getNumUsedBlocks());
ASSERT_LE(1, pool.getNumUsedBlocks()); // One or more
ASSERT_EQ("4", *mset->emplace("4"));
ASSERT_LE(1, pool.getNumUsedBlocks()); // One or more
ASSERT_EQ(2, mset->getNumStaticItems());
ASSERT_EQ(2, mset->getNumDynamicItems());
ASSERT_EQ(4, mset->getSize());
// Making sure everything is here
ASSERT_EQ("1", *mset->getByIndex(0));
@@ -117,9 +115,6 @@ TEST(Multiset, Basic)
ASSERT_FALSE(mset->getByIndex(100));
ASSERT_FALSE(mset->getByIndex(4));
const std::string data_at_pos2 = *mset->getByIndex(2);
const std::string data_at_pos3 = *mset->getByIndex(3);
// Finding some items
ASSERT_EQ("1", *mset->find(FindPredicate("1")));
ASSERT_EQ("2", *mset->find(FindPredicate("2")));
@@ -134,23 +129,10 @@ TEST(Multiset, Basic)
ASSERT_EQ(4, op.accumulator.size());
}
// Removing one static; ordering will be preserved
// Removing some
mset->removeFirst("1");
mset->removeFirst("foo"); // There's no such thing anyway
ASSERT_LE(1, pool.getNumUsedBlocks());
ASSERT_EQ(1, mset->getNumStaticItems());
ASSERT_EQ(2, mset->getNumDynamicItems()); // This container does not move items
// Ordering has not changed
ASSERT_EQ("2", *mset->getByIndex(0)); // Entry "1" was here
ASSERT_EQ(data_at_pos2, *mset->getByIndex(1));
ASSERT_EQ(data_at_pos3, *mset->getByIndex(2));
// Removing another static
mset->removeFirst("2");
ASSERT_EQ(0, mset->getNumStaticItems());
ASSERT_EQ(2, mset->getNumDynamicItems());
ASSERT_LE(1, pool.getNumUsedBlocks());
// Adding some new items
unsigned max_value_integer = 0;
@@ -219,7 +201,7 @@ TEST(Multiset, PrimitiveKey)
static const int POOL_BLOCKS = 3;
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
typedef Multiset<int, 2> MultisetType;
typedef Multiset<int> MultisetType;
std::auto_ptr<MultisetType> mset(new MultisetType(pool));
// Empty
@@ -269,7 +251,7 @@ TEST(Multiset, NoncopyableWithCounter)
static const int POOL_BLOCKS = 3;
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
typedef Multiset<NoncopyableWithCounter, 2> MultisetType;
typedef Multiset<NoncopyableWithCounter> MultisetType;
std::auto_ptr<MultisetType> mset(new MultisetType(pool));
ASSERT_EQ(0, NoncopyableWithCounter::num_objects);