From bdc0a327b702a31e01663511a3df9e16d14a686d Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Wed, 27 May 2015 16:10:14 +0300 Subject: [PATCH] centralized::Storage - new test --- .../centralized/storage.hpp | 14 ++++- .../centralized/storage.cpp | 61 ++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/libuavcan/include/uavcan/protocol/dynamic_node_id_server/centralized/storage.hpp b/libuavcan/include/uavcan/protocol/dynamic_node_id_server/centralized/storage.hpp index 280e077f38..99e04eb183 100644 --- a/libuavcan/include/uavcan/protocol/dynamic_node_id_server/centralized/storage.hpp +++ b/libuavcan/include/uavcan/protocol/dynamic_node_id_server/centralized/storage.hpp @@ -25,13 +25,20 @@ class Storage public: typedef uint8_t Size; - enum { Capacity = NodeID::Max }; + enum { Capacity = NodeID::Max - 1 }; struct Entry { UniqueID unique_id; NodeID node_id; + Entry() { } + + Entry(const NodeID nid, const UniqueID& uid) + : unique_id(uid) + , node_id(nid) + { } + bool operator==(const Entry& rhs) const { return unique_id == rhs.unique_id && @@ -171,6 +178,11 @@ public: return -ErrLogic; } + if (!node_id.isUnicast()) + { + return -ErrInvalidParam; + } + Entry entry; entry.node_id = node_id; entry.unique_id = unique_id; diff --git a/libuavcan/test/protocol/dynamic_node_id_server/centralized/storage.cpp b/libuavcan/test/protocol/dynamic_node_id_server/centralized/storage.cpp index b2d4e83819..615dc73f11 100644 --- a/libuavcan/test/protocol/dynamic_node_id_server/centralized/storage.cpp +++ b/libuavcan/test/protocol/dynamic_node_id_server/centralized/storage.cpp @@ -8,7 +8,7 @@ #include "../memory_storage_backend.hpp" -TEST(dynamic_node_id_server_centralized_Storage, Basic) +TEST(dynamic_node_id_server_centralized_Storage, Initialization) { using namespace uavcan::dynamic_node_id_server::centralized; @@ -113,3 +113,62 @@ TEST(dynamic_node_id_server_centralized_Storage, Basic) } +TEST(dynamic_node_id_server_centralized_Storage, Basic) +{ + using namespace uavcan::dynamic_node_id_server::centralized; + + MemoryStorageBackend storage; + Storage stor(storage); + + ASSERT_EQ(0, storage.getNumKeys()); + ASSERT_LE(0, stor.init()); + storage.print(); + ASSERT_EQ(1, storage.getNumKeys()); + + /* + * Adding one entry to the log, making sure it appears in the storage + */ + Storage::Entry entry; + entry.node_id = 1; + entry.unique_id[0] = 1; + ASSERT_LE(0, stor.add(entry.node_id, entry.unique_id)); + + ASSERT_EQ("1", storage.get("size")); + ASSERT_EQ("01000000000000000000000000000000", storage.get("0_unique_id")); + ASSERT_EQ("1", storage.get("0_node_id")); + + ASSERT_EQ(3, storage.getNumKeys()); + ASSERT_EQ(1, stor.getSize()); + + /* + * Adding another entry while storage is failing + */ + storage.failOnSetCalls(true); + + ASSERT_EQ(3, storage.getNumKeys()); + + entry.node_id = 2; + entry.unique_id[0] = 2; + ASSERT_GT(0, stor.add(entry.node_id, entry.unique_id)); + + ASSERT_EQ(3, storage.getNumKeys()); // No new entries, we failed + + ASSERT_EQ(1, stor.getSize()); + + /* + * Making sure add() fails when the log is full + */ + storage.failOnSetCalls(false); + + while (stor.getSize() < stor.Capacity) + { + ASSERT_LE(0, stor.add(entry.node_id, entry.unique_id)); + + entry.node_id = uint8_t(entry.node_id.get() + 1U); + entry.unique_id[0] = uint8_t(entry.unique_id[0] + 1U); + } + + ASSERT_GT(0, stor.add(entry.node_id, entry.unique_id)); // Failing because full + + storage.print(); +}