From 0f1b124086d4b334dd693a52a0986c24019b1d78 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Fri, 14 Mar 2014 02:19:57 +0400 Subject: [PATCH] Array<>::operator+=(Array<>) implementation and test --- .../include/uavcan/internal/marshal/array.hpp | 12 +++++++++ libuavcan/test/internal/marshal/array.cpp | 25 +++++++++++++++++++ libuavcan/test/service_client.cpp | 2 +- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/libuavcan/include/uavcan/internal/marshal/array.hpp b/libuavcan/include/uavcan/internal/marshal/array.hpp index c968cf6323..45f1cf03d8 100644 --- a/libuavcan/include/uavcan/internal/marshal/array.hpp +++ b/libuavcan/include/uavcan/internal/marshal/array.hpp @@ -394,6 +394,18 @@ public: return *this; } + template + SelfType& operator+=(const Array& rhs) + { + typedef Array Rhs; + typedef typename Select<(sizeof(SizeType) > sizeof(typename Rhs::SizeType)), + SizeType, typename Rhs::SizeType>::Result CommonSizeType; + StaticAssert::check(); + for (CommonSizeType i = 0; i < rhs.size(); i++) + push_back(rhs[i]); + return *this; + } + typedef ValueType value_type; typedef SizeType size_type; }; diff --git a/libuavcan/test/internal/marshal/array.cpp b/libuavcan/test/internal/marshal/array.cpp index 0bcb7dbeb4..fe5e0ebdf9 100644 --- a/libuavcan/test/internal/marshal/array.cpp +++ b/libuavcan/test/internal/marshal/array.cpp @@ -652,6 +652,31 @@ TEST(Array, Copyability) } +TEST(Array, Appending) +{ + typedef Array, ArrayModeDynamic, 2> A; + typedef Array, ArrayModeDynamic, 257> B; + A a; + B b; + + a.push_back(1); + a.push_back(2); + a += b; + ASSERT_EQ(2, a.size()); + ASSERT_EQ(1, a[0]); + ASSERT_EQ(2, a[1]); + + b += a; + ASSERT_TRUE(a == b); + b += a; + ASSERT_EQ(4, b.size()); + ASSERT_EQ(1, b[0]); + ASSERT_EQ(2, b[1]); + ASSERT_EQ(1, b[2]); + ASSERT_EQ(2, b[3]); +} + + TEST(Array, Strings) { typedef Array, ArrayModeDynamic, 32> A8; diff --git a/libuavcan/test/service_client.cpp b/libuavcan/test/service_client.cpp index 8a851be423..573c0057b2 100644 --- a/libuavcan/test/service_client.cpp +++ b/libuavcan/test/service_client.cpp @@ -47,7 +47,7 @@ static void stringServiceServerCallback(const uavcan::ReceivedDataStructure