mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-21 03:43:33 +08:00
First stab at global refactoring of memory management - the library builds, but unit tests are failing horribly
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
struct TestNode : public uavcan::INode
|
||||
{
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * 100, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::OutgoingTransferRegistry<8> otr;
|
||||
uavcan::OutgoingTransferRegistry otr;
|
||||
uavcan::Scheduler scheduler;
|
||||
uint64_t internal_failure_count;
|
||||
|
||||
|
||||
@@ -190,6 +190,5 @@ TEST(dynamic_node_id_server, ObjectSizes)
|
||||
std::cout << "ServiceServer<AppendEntries>: " << sizeof(ServiceServer<AppendEntries>) << std::endl;
|
||||
std::cout << "ServiceClient<AppendEntries>: " << sizeof(ServiceClient<AppendEntries>) << std::endl;
|
||||
std::cout << "ServiceServer<RequestVote>: " << sizeof(ServiceServer<RequestVote>) << std::endl;
|
||||
std::cout << "ServiceClient<RequestVote,~,5>:"
|
||||
<< sizeof(ServiceClient<RequestVote, void (*)(const ServiceCallResult<RequestVote>&), 5>) << std::endl;
|
||||
std::cout << "ServiceClient<RequestVote>: " << sizeof(ServiceClient<RequestVote>) << std::endl;
|
||||
}
|
||||
|
||||
@@ -281,7 +281,5 @@ TEST(dynamic_node_id_server_NodeDiscoverer, Sizes)
|
||||
std::cout << "BitSet<NodeID::Max + 1>: " << sizeof(BitSet<NodeID::Max + 1>) << std::endl;
|
||||
std::cout << "ServiceClient<protocol::GetNodeInfo>: " << sizeof(ServiceClient<protocol::GetNodeInfo>) << std::endl;
|
||||
std::cout << "protocol::GetNodeInfo::Response: " << sizeof(protocol::GetNodeInfo::Response) << std::endl;
|
||||
std::cout << "Subscriber<protocol::NodeStatus, ~, 64, 0>: "
|
||||
<< sizeof(Subscriber<protocol::NodeStatus,
|
||||
void (*)(const ReceivedDataStructure<protocol::NodeStatus>&), 64, 0>) << std::endl;
|
||||
std::cout << "Subscriber<protocol::NodeStatus>: " << sizeof(Subscriber<protocol::NodeStatus>) << std::endl;
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ TEST(Dispatcher, Reception)
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(pool);
|
||||
uavcan::OutgoingTransferRegistry out_trans_reg(pool);
|
||||
|
||||
uavcan::Dispatcher dispatcher(driver, pool, clockmock, out_trans_reg);
|
||||
ASSERT_TRUE(dispatcher.setNodeID(SELF_NODE_ID)); // Can be set only once
|
||||
@@ -86,7 +86,7 @@ TEST(Dispatcher, Reception)
|
||||
makeDataType(uavcan::DataTypeKindService, 1)
|
||||
};
|
||||
|
||||
typedef TestListener<512, 2, 2> Subscriber;
|
||||
typedef TestListener<512> Subscriber;
|
||||
typedef std::auto_ptr<Subscriber> SubscriberPtr;
|
||||
static const int NUM_SUBSCRIBERS = 6;
|
||||
SubscriberPtr subscribers[NUM_SUBSCRIBERS] =
|
||||
@@ -255,7 +255,7 @@ TEST(Dispatcher, Transmission)
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(pool);
|
||||
uavcan::OutgoingTransferRegistry out_trans_reg(pool);
|
||||
|
||||
uavcan::Dispatcher dispatcher(driver, pool, clockmock, out_trans_reg);
|
||||
ASSERT_TRUE(dispatcher.setNodeID(SELF_NODE_ID)); // Can be set only once
|
||||
@@ -319,7 +319,7 @@ TEST(Dispatcher, Spin)
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(poolmgr);
|
||||
uavcan::OutgoingTransferRegistry out_trans_reg(poolmgr);
|
||||
|
||||
uavcan::Dispatcher dispatcher(driver, poolmgr, clockmock, out_trans_reg);
|
||||
ASSERT_TRUE(dispatcher.setNodeID(SELF_NODE_ID)); // Can be set only once
|
||||
@@ -365,7 +365,7 @@ TEST(Dispatcher, Loopback)
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(poolmgr);
|
||||
uavcan::OutgoingTransferRegistry out_trans_reg(poolmgr);
|
||||
|
||||
uavcan::Dispatcher dispatcher(driver, poolmgr, clockmock, out_trans_reg);
|
||||
ASSERT_TRUE(dispatcher.setNodeID(SELF_NODE_ID));
|
||||
|
||||
@@ -73,7 +73,7 @@ TEST(MultiFrameIncomingTransfer, Basic)
|
||||
using uavcan::MultiFrameIncomingTransfer;
|
||||
|
||||
NullAllocator poolmgr; // We don't need dynamic memory
|
||||
uavcan::TransferBufferManager<256, 1> bufmgr(poolmgr);
|
||||
uavcan::TransferBufferManager<256> bufmgr(poolmgr);
|
||||
|
||||
const RxFrame frame = makeFrame();
|
||||
uavcan::TransferBufferManagerKey bufmgr_key(frame.getSrcNodeID(), frame.getTransferType());
|
||||
|
||||
@@ -13,7 +13,7 @@ TEST(OutgoingTransferRegistry, Basic)
|
||||
{
|
||||
using uavcan::OutgoingTransferRegistryKey;
|
||||
NullAllocator poolmgr; // Empty
|
||||
uavcan::OutgoingTransferRegistry<4> otr(poolmgr);
|
||||
uavcan::OutgoingTransferRegistry otr(poolmgr);
|
||||
|
||||
otr.cleanup(tsMono(1000));
|
||||
|
||||
|
||||
@@ -229,10 +229,10 @@ TEST(TransferBufferManager, Basic)
|
||||
using uavcan::TransferBufferManagerKey;
|
||||
using uavcan::ITransferBuffer;
|
||||
|
||||
static const int POOL_BLOCKS = 6;
|
||||
static const int POOL_BLOCKS = 8;
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
|
||||
typedef TransferBufferManager<MGR_MAX_BUFFER_SIZE, 2> TransferBufferManagerType;
|
||||
typedef TransferBufferManager<MGR_MAX_BUFFER_SIZE> TransferBufferManagerType;
|
||||
std::auto_ptr<TransferBufferManagerType> mgr(new TransferBufferManagerType(pool));
|
||||
|
||||
// Empty
|
||||
@@ -250,40 +250,32 @@ TEST(TransferBufferManager, Basic)
|
||||
TransferBufferManagerKey(64, uavcan::TransferTypeMessageBroadcast)
|
||||
};
|
||||
|
||||
// Static 0
|
||||
ASSERT_TRUE((tbb = mgr->create(keys[0])));
|
||||
ASSERT_EQ(MGR_MAX_BUFFER_SIZE, fillTestData(MGR_TEST_DATA[0], tbb));
|
||||
ASSERT_EQ(1, mgr->getNumStaticBuffers());
|
||||
ASSERT_EQ(1, mgr->getNumBuffers());
|
||||
|
||||
// Static 1
|
||||
ASSERT_TRUE((tbb = mgr->create(keys[1])));
|
||||
ASSERT_EQ(MGR_MAX_BUFFER_SIZE, fillTestData(MGR_TEST_DATA[1], tbb));
|
||||
ASSERT_EQ(2, mgr->getNumStaticBuffers());
|
||||
ASSERT_EQ(0, mgr->getNumDynamicBuffers());
|
||||
ASSERT_EQ(0, mgr->getNumBuffers());
|
||||
ASSERT_EQ(0, pool.getNumUsedBlocks());
|
||||
|
||||
// Dynamic 0
|
||||
ASSERT_TRUE((tbb = mgr->create(keys[2])));
|
||||
ASSERT_EQ(1, pool.getNumUsedBlocks()); // Empty dynamic buffer occupies one block
|
||||
ASSERT_EQ(MGR_MAX_BUFFER_SIZE, fillTestData(MGR_TEST_DATA[2], tbb));
|
||||
ASSERT_EQ(2, mgr->getNumStaticBuffers());
|
||||
ASSERT_EQ(1, mgr->getNumDynamicBuffers());
|
||||
ASSERT_EQ(1, mgr->getNumBuffers());
|
||||
ASSERT_LT(1, pool.getNumUsedBlocks());
|
||||
|
||||
std::cout << "TransferBufferManager - Basic: Pool usage: " << pool.getNumUsedBlocks() << std::endl;
|
||||
|
||||
// Dynamic 2
|
||||
ASSERT_TRUE((tbb = mgr->create(keys[3])));
|
||||
ASSERT_LT(0, pool.getNumUsedBlocks());
|
||||
|
||||
ASSERT_LT(0, fillTestData(MGR_TEST_DATA[3], tbb));
|
||||
ASSERT_EQ(2, mgr->getNumStaticBuffers());
|
||||
ASSERT_EQ(2, mgr->getNumDynamicBuffers());
|
||||
ASSERT_EQ(2, mgr->getNumBuffers());
|
||||
|
||||
// Dynamic 3 - will fail due to OOM
|
||||
ASSERT_FALSE((tbb = mgr->create(keys[4])));
|
||||
ASSERT_EQ(2, mgr->getNumStaticBuffers());
|
||||
ASSERT_EQ(2, mgr->getNumDynamicBuffers());
|
||||
ASSERT_EQ(2, mgr->getNumBuffers());
|
||||
|
||||
// Making sure all buffers contain proper data
|
||||
ASSERT_TRUE((tbb = mgr->access(keys[0])));
|
||||
@@ -298,18 +290,14 @@ TEST(TransferBufferManager, Basic)
|
||||
ASSERT_TRUE((tbb = mgr->access(keys[3])));
|
||||
ASSERT_TRUE(matchAgainst(MGR_TEST_DATA[3], *tbb));
|
||||
|
||||
// Freeing one static buffer; one dynamic must migrate
|
||||
mgr->remove(keys[1]);
|
||||
ASSERT_FALSE(mgr->access(keys[1]));
|
||||
ASSERT_EQ(2, mgr->getNumStaticBuffers());
|
||||
ASSERT_EQ(1, mgr->getNumDynamicBuffers()); // One migrated to the static
|
||||
ASSERT_EQ(1, mgr->getNumBuffers());
|
||||
ASSERT_LT(0, pool.getNumFreeBlocks());
|
||||
|
||||
// Removing NodeID 0; one dynamic must migrate
|
||||
mgr->remove(keys[0]);
|
||||
ASSERT_FALSE(mgr->access(keys[0]));
|
||||
ASSERT_EQ(2, mgr->getNumStaticBuffers());
|
||||
ASSERT_EQ(0, mgr->getNumDynamicBuffers());
|
||||
ASSERT_EQ(0, mgr->getNumBuffers());
|
||||
|
||||
// At this time we have the following NodeID: 2, 127
|
||||
ASSERT_TRUE((tbb = mgr->access(keys[2])));
|
||||
@@ -336,7 +324,7 @@ TEST(TransferBufferManager, Basic)
|
||||
|
||||
TEST(TransferBufferManager, EmptySpecialization)
|
||||
{
|
||||
uavcan::TransferBufferManager<0, 0> mgr;
|
||||
uavcan::TransferBufferManager<0> mgr;
|
||||
(void)mgr;
|
||||
ASSERT_GE(sizeof(void*), sizeof(mgr));
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ TEST(TransferListener, BasicMFT)
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * NUM_POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<256, 1, 1> subscriber(perf, type, pool);
|
||||
TestListener<256> subscriber(perf, type, pool);
|
||||
|
||||
/*
|
||||
* Test data
|
||||
@@ -95,7 +95,7 @@ TEST(TransferListener, CrcFailure)
|
||||
|
||||
NullAllocator poolmgr; // No dynamic memory
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<256, 2, 2> subscriber(perf, type, poolmgr); // Static buffer only, 2 entries
|
||||
TestListener<256> subscriber(perf, type, poolmgr); // Static buffer only, 2 entries
|
||||
|
||||
/*
|
||||
* Generating transfers with damaged payload (CRC is not valid)
|
||||
@@ -138,7 +138,7 @@ TEST(TransferListener, BasicSFT)
|
||||
|
||||
NullAllocator poolmgr; // No dynamic memory. At all.
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<0, 0, 5> subscriber(perf, type, poolmgr); // Max buf size is 0, i.e. SFT-only
|
||||
TestListener<0> subscriber(perf, type, poolmgr); // Max buf size is 0, i.e. SFT-only
|
||||
|
||||
TransferListenerEmulator emulator(subscriber, type);
|
||||
const Transfer transfers[] =
|
||||
@@ -174,7 +174,7 @@ TEST(TransferListener, Cleanup)
|
||||
|
||||
NullAllocator poolmgr; // No dynamic memory
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<256, 1, 2> subscriber(perf, type, poolmgr); // Static buffer only, 1 entry
|
||||
TestListener<256> subscriber(perf, type, poolmgr); // Static buffer only, 1 entry
|
||||
|
||||
/*
|
||||
* Generating transfers
|
||||
@@ -229,7 +229,7 @@ TEST(TransferListener, AnonymousTransfers)
|
||||
|
||||
NullAllocator poolmgr;
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<0, 0, 0> subscriber(perf, type, poolmgr);
|
||||
TestListener<0> subscriber(perf, type, poolmgr);
|
||||
|
||||
TransferListenerEmulator emulator(subscriber, type);
|
||||
const Transfer transfers[] =
|
||||
@@ -261,5 +261,5 @@ TEST(TransferListener, Sizes)
|
||||
{
|
||||
using namespace uavcan;
|
||||
|
||||
std::cout << "sizeof(TransferListener<64, 1, 2>): " << sizeof(TransferListener<64, 1, 2>) << std::endl;
|
||||
std::cout << "sizeof(TransferListener<64>): " << sizeof(TransferListener<64>) << std::endl;
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ struct Context
|
||||
{
|
||||
NullAllocator pool; // We don't need dynamic memory for this test
|
||||
uavcan::TransferReceiver receiver; // Must be default constructible and copyable
|
||||
uavcan::TransferBufferManager<BufSize, 1> bufmgr;
|
||||
uavcan::TransferBufferManager<BufSize> bufmgr;
|
||||
|
||||
Context() :
|
||||
bufmgr(pool)
|
||||
|
||||
@@ -34,7 +34,7 @@ TEST(TransferSender, Basic)
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(poolmgr);
|
||||
uavcan::OutgoingTransferRegistry out_trans_reg(poolmgr);
|
||||
|
||||
static const uavcan::NodeID TX_NODE_ID(64);
|
||||
static const uavcan::NodeID RX_NODE_ID(65);
|
||||
@@ -123,9 +123,9 @@ TEST(TransferSender, Basic)
|
||||
}
|
||||
}
|
||||
|
||||
TestListener<512, 2, 2> sub_msg(dispatcher_rx.getTransferPerfCounter(), TYPES[0], poolmgr);
|
||||
TestListener<512, 2, 2> sub_srv_req(dispatcher_rx.getTransferPerfCounter(), TYPES[1], poolmgr);
|
||||
TestListener<512, 2, 2> sub_srv_resp(dispatcher_rx.getTransferPerfCounter(), TYPES[1], poolmgr);
|
||||
TestListener<512> sub_msg(dispatcher_rx.getTransferPerfCounter(), TYPES[0], poolmgr);
|
||||
TestListener<512> sub_srv_req(dispatcher_rx.getTransferPerfCounter(), TYPES[1], poolmgr);
|
||||
TestListener<512> sub_srv_resp(dispatcher_rx.getTransferPerfCounter(), TYPES[1], poolmgr);
|
||||
|
||||
dispatcher_rx.registerMessageListener(&sub_msg);
|
||||
dispatcher_rx.registerServiceRequestListener(&sub_srv_req);
|
||||
@@ -195,7 +195,7 @@ TEST(TransferSender, Loopback)
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(poolmgr);
|
||||
uavcan::OutgoingTransferRegistry out_trans_reg(poolmgr);
|
||||
|
||||
static const uavcan::NodeID TX_NODE_ID(64);
|
||||
uavcan::Dispatcher dispatcher(driver, poolmgr, clockmock, out_trans_reg);
|
||||
@@ -236,7 +236,7 @@ TEST(TransferSender, PassiveMode)
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(poolmgr);
|
||||
uavcan::OutgoingTransferRegistry out_trans_reg(poolmgr);
|
||||
|
||||
uavcan::Dispatcher dispatcher(driver, poolmgr, clockmock, out_trans_reg);
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ TEST(TransferTestHelpers, Transfer)
|
||||
{
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * 8, uavcan::MemPoolBlockSize> pool;
|
||||
|
||||
uavcan::TransferBufferManager<128, 1> mgr(pool);
|
||||
uavcan::TransferBufferManager<128> mgr(pool);
|
||||
uavcan::TransferBufferAccessor tba(mgr, uavcan::TransferBufferManagerKey(0, uavcan::TransferTypeMessageBroadcast));
|
||||
|
||||
uavcan::RxFrame frame(uavcan::Frame(123, uavcan::TransferTypeMessageBroadcast, 1, 0, 0),
|
||||
|
||||
@@ -117,10 +117,10 @@ struct Transfer
|
||||
* In reality, uavcan::TransferListener should accept only specific transfer types
|
||||
* which are dispatched/filtered by uavcan::Dispatcher.
|
||||
*/
|
||||
template <unsigned MAX_BUF_SIZE, unsigned NUM_STATIC_BUFS, unsigned NUM_STATIC_RECEIVERS>
|
||||
class TestListener : public uavcan::TransferListener<MAX_BUF_SIZE, NUM_STATIC_BUFS, NUM_STATIC_RECEIVERS>
|
||||
template <unsigned MAX_BUF_SIZE>
|
||||
class TestListener : public uavcan::TransferListener<MAX_BUF_SIZE>
|
||||
{
|
||||
typedef uavcan::TransferListener<MAX_BUF_SIZE, NUM_STATIC_BUFS, NUM_STATIC_RECEIVERS> Base;
|
||||
typedef uavcan::TransferListener<MAX_BUF_SIZE> Base;
|
||||
|
||||
std::queue<Transfer> transfers_;
|
||||
|
||||
|
||||
+19
-25
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user