From 54fcfe4e06d9a8a13b31c90af01a94e9a89544a3 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sun, 3 May 2015 20:28:39 +0300 Subject: [PATCH] Log::append() tests --- .../dynamic_node_id_allocation_server.hpp | 2 +- .../dynamic_node_id_allocation_server.cpp | 82 ++++++++++++++++++- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp b/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp index 3fa03b5c50..bd9cd19ad4 100644 --- a/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp +++ b/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp @@ -130,9 +130,9 @@ class Log public: typedef uint8_t Index; -private: enum { Capacity = NodeID::Max + 1 }; +private: IDynamicNodeIDStorageBackend& storage_; protocol::dynamic_node_id::server::Entry entries_[Capacity]; Index last_index_; // Index zero always contains an empty entry diff --git a/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp b/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp index 6e2c239fe4..46d4d3be66 100644 --- a/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp +++ b/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp @@ -31,7 +31,10 @@ public: virtual void set(const String& key, const String& value) { - container_[key] = value; + if (!fail_) + { + container_[key] = value; + } } void failOnSetCalls(bool really) { fail_ = really; } @@ -50,6 +53,9 @@ public: }; +static const unsigned NumEntriesInStorageWithEmptyLog = 4; // last index + 3 items per log entry + + TEST(DynamicNodeIDAllocationServer, MarshallingStorageDecorator) { StorageBackend st; @@ -134,8 +140,6 @@ TEST(DynamicNodeIDAllocationServer, MarshallingStorageDecorator) TEST(DynamicNodeIDAllocationServer, LogInitialization) { - const unsigned NumEntriesInStorageWithEmptyLog = 4; // last index + 3 items per log entry - // No log data in the storage - initializing empty log { StorageBackend storage; @@ -227,3 +231,75 @@ TEST(DynamicNodeIDAllocationServer, LogInitialization) ASSERT_EQ(uid, log.getEntryAtIndex(1)->unique_id); } } + + +TEST(DynamicNodeIDAllocationServer, LogAppend) +{ + StorageBackend storage; + uavcan::dynamic_node_id_server_impl::Log log(storage); + + ASSERT_EQ(0, storage.getNumKeys()); + ASSERT_LE(0, log.init()); + storage.print(); + ASSERT_EQ(NumEntriesInStorageWithEmptyLog, storage.getNumKeys()); + + /* + * Entry at the index 0 always exists, and it's always zero-initialized. + */ + ASSERT_EQ("0", storage.get("log_last_index")); + ASSERT_EQ("0", storage.get("log0_term")); + ASSERT_EQ("00000000000000000000000000000000", storage.get("log0_unique_id")); + ASSERT_EQ("0", storage.get("log0_node_id")); + + /* + * Adding one entry to the log, making sure it appears in the storage + */ + uavcan::protocol::dynamic_node_id::server::Entry entry; + entry.term = 1; + entry.node_id = 1; + entry.unique_id[0] = 1; + ASSERT_LE(0, log.append(entry)); + + ASSERT_EQ("1", storage.get("log_last_index")); + ASSERT_EQ("1", storage.get("log1_term")); + ASSERT_EQ("01000000000000000000000000000000", storage.get("log1_unique_id")); + ASSERT_EQ("1", storage.get("log1_node_id")); + + ASSERT_EQ(1, log.getLastIndex()); + ASSERT_TRUE(entry == *log.getEntryAtIndex(1)); + + /* + * Adding another entry while storage is failing + */ + storage.failOnSetCalls(true); + + ASSERT_EQ(7, storage.getNumKeys()); + + entry.term = 2; + entry.node_id = 2; + entry.unique_id[0] = 2; + ASSERT_GT(0, log.append(entry)); + + ASSERT_EQ(7, storage.getNumKeys()); // No new entries, we failed + + ASSERT_EQ(1, log.getLastIndex()); + + /* + * Making sure append() fails when the log is full + */ + storage.failOnSetCalls(false); + + while (log.getLastIndex() < (log.Capacity - 1)) + { + ASSERT_LE(0, log.append(entry)); + ASSERT_TRUE(entry == *log.getEntryAtIndex(log.getLastIndex())); + + entry.term += 1; + entry.node_id = uint8_t(entry.node_id + 1U); + entry.unique_id[0] = uint8_t(entry.unique_id[0] + 1U); + } + + ASSERT_GT(0, log.append(entry)); // Failing because full + + storage.print(); +}