From ebf12010af37b581f296dcbe979a99c4c4e383c1 Mon Sep 17 00:00:00 2001 From: Evgeny Margolis Date: Mon, 14 Jun 2021 06:49:45 -0700 Subject: [PATCH] Added data_equal() and empty() Methods to Span/FixedSpan Classes (#7444) --- src/lib/support/Span.h | 12 +++++ src/lib/support/tests/TestSpan.cpp | 73 ++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/lib/support/Span.h b/src/lib/support/Span.h index 45282e3f9ceab5..5e7b46d79ad533 100644 --- a/src/lib/support/Span.h +++ b/src/lib/support/Span.h @@ -19,6 +19,7 @@ #include #include +#include namespace chip { @@ -38,6 +39,11 @@ class Span const T * data() const { return mDataBuf; } size_t size() const { return mDataLen; } + bool empty() const { return size() == 0; } + bool data_equal(const Span & other) const + { + return (size() == other.size()) && (empty() || (memcmp(data(), other.data(), size() * sizeof(T)) == 0)); + } private: const T * mDataBuf; @@ -53,6 +59,12 @@ class FixedSpan const T * data() const { return mDataBuf; } size_t size() const { return N; } + bool empty() const { return data() == nullptr; } + bool data_equal(const FixedSpan & other) const + { + return (empty() && other.empty()) || + (!empty() && !other.empty() && (memcmp(data(), other.data(), size() * sizeof(T)) == 0)); + } private: const T * mDataBuf; diff --git a/src/lib/support/tests/TestSpan.cpp b/src/lib/support/tests/TestSpan.cpp index ff87e357b28ed2..5546df79b45719 100644 --- a/src/lib/support/tests/TestSpan.cpp +++ b/src/lib/support/tests/TestSpan.cpp @@ -29,24 +29,89 @@ using namespace chip; -static void SpanConstructors(nlTestSuite * inSuite, void * inContext) +static void TestByteSpan(nlTestSuite * inSuite, void * inContext) { uint8_t arr[] = { 1, 2, 3 }; - Span s1(arr, 2); + ByteSpan s0 = ByteSpan(); + NL_TEST_ASSERT(inSuite, s0.data() == nullptr); + NL_TEST_ASSERT(inSuite, s0.size() == 0); + NL_TEST_ASSERT(inSuite, s0.empty()); + NL_TEST_ASSERT(inSuite, s0.data_equal(s0)); + + ByteSpan s1(arr, 2); NL_TEST_ASSERT(inSuite, s1.data() == arr); NL_TEST_ASSERT(inSuite, s1.size() == 2); + NL_TEST_ASSERT(inSuite, !s1.empty()); + NL_TEST_ASSERT(inSuite, s1.data_equal(s1)); + NL_TEST_ASSERT(inSuite, !s1.data_equal(s0)); - Span s2(arr); + ByteSpan s2(arr); NL_TEST_ASSERT(inSuite, s2.data() == arr); NL_TEST_ASSERT(inSuite, s2.size() == 3); + NL_TEST_ASSERT(inSuite, s2.data()[2] == 3); + NL_TEST_ASSERT(inSuite, !s2.empty()); + NL_TEST_ASSERT(inSuite, s2.data_equal(s2)); + NL_TEST_ASSERT(inSuite, !s2.data_equal(s1)); + + ByteSpan s3 = s2; + NL_TEST_ASSERT(inSuite, s3.data() == arr); + NL_TEST_ASSERT(inSuite, s3.size() == 3); + NL_TEST_ASSERT(inSuite, s3.data()[2] == 3); + NL_TEST_ASSERT(inSuite, !s3.empty()); + NL_TEST_ASSERT(inSuite, s3.data_equal(s2)); + + uint8_t arr2[] = { 3, 2, 1 }; + ByteSpan s4(arr2); + NL_TEST_ASSERT(inSuite, !s4.data_equal(s2)); + + ByteSpan s5(arr2, 0); + NL_TEST_ASSERT(inSuite, s5.data() != nullptr); + NL_TEST_ASSERT(inSuite, !s5.data_equal(s4)); + NL_TEST_ASSERT(inSuite, s5.data_equal(s0)); + NL_TEST_ASSERT(inSuite, s0.data_equal(s5)); +} + +static void TestFixedByteSpan(nlTestSuite * inSuite, void * inContext) +{ + uint8_t arr[] = { 1, 2, 3 }; + + FixedByteSpan<3> s0 = FixedByteSpan<3>(); + NL_TEST_ASSERT(inSuite, s0.data() == nullptr); + NL_TEST_ASSERT(inSuite, s0.size() == 3); + NL_TEST_ASSERT(inSuite, s0.empty()); + NL_TEST_ASSERT(inSuite, s0.data_equal(s0)); + + FixedByteSpan<2> s1(arr); + NL_TEST_ASSERT(inSuite, s1.data() == arr); + NL_TEST_ASSERT(inSuite, s1.size() == 2); + NL_TEST_ASSERT(inSuite, !s1.empty()); + NL_TEST_ASSERT(inSuite, s1.data_equal(s1)); + + FixedByteSpan<3> s2(arr); + NL_TEST_ASSERT(inSuite, s2.data() == arr); + NL_TEST_ASSERT(inSuite, s2.size() == 3); + NL_TEST_ASSERT(inSuite, s2.data()[2] == 3); + NL_TEST_ASSERT(inSuite, !s2.empty()); + NL_TEST_ASSERT(inSuite, s2.data_equal(s2)); + + FixedByteSpan<3> s3 = s2; + NL_TEST_ASSERT(inSuite, s3.data() == arr); + NL_TEST_ASSERT(inSuite, s3.size() == 3); + NL_TEST_ASSERT(inSuite, s3.data()[2] == 3); + NL_TEST_ASSERT(inSuite, !s3.empty()); + NL_TEST_ASSERT(inSuite, s3.data_equal(s2)); + + uint8_t arr2[] = { 3, 2, 1 }; + FixedByteSpan<3> s4(arr2); + NL_TEST_ASSERT(inSuite, !s4.data_equal(s2)); } #define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn) /** * Test Suite. It lists all the test functions. */ -static const nlTest sTests[] = { NL_TEST_DEF_FN(SpanConstructors), NL_TEST_SENTINEL() }; +static const nlTest sTests[] = { NL_TEST_DEF_FN(TestByteSpan), NL_TEST_DEF_FN(TestFixedByteSpan), NL_TEST_SENTINEL() }; int TestSpan(void) {