Skip to content

Commit

Permalink
Updated unit tests in src/transport/retransmit/tests/ to use PW inste…
Browse files Browse the repository at this point in the history
…ad of NL. (project-chip#33157)

* Updated unit tests in src/transport/tests/ to use PW instead of NL.

* Restyle

---------

Co-authored-by: Andrei Litvin <andy314@gmail.com>
  • Loading branch information
feasel0 and andy31415 authored Apr 25, 2024
1 parent d905835 commit f27b978
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 119 deletions.
1 change: 1 addition & 0 deletions src/test_driver/openiotsdk/unit-tests/test_components.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CoreTests
MdnsTests
CredentialsTest
PlatformTests
RetransmitTests
TestShell
SetupPayloadTests
SupportTests
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ DataModelTests
InetLayerTests
MessagingLayerTests
RawTransportTests
RetransmitTests
SecureChannelTests
SetupPayloadTestsNL
SupportTestsNL
Expand Down
10 changes: 3 additions & 7 deletions src/transport/retransmit/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,16 @@

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/nlunit_test.gni")
import("//build_overrides/pigweed.gni")

import("${chip_root}/build/chip/chip_test_suite.gni")

chip_test_suite_using_nltest("tests") {
chip_test_suite("tests") {
output_name = "libRetransmitTests"

cflags = [ "-Wconversion" ]

test_sources = [ "TestCache.cpp" ]

public_deps = [
"${chip_root}/src/lib/support:testing_nlunit",
"${chip_root}/src/transport/retransmit",
"${nlunit_test_root}:nlunit-test",
]
public_deps = [ "${chip_root}/src/transport/retransmit" ]
}
195 changes: 84 additions & 111 deletions src/transport/retransmit/tests/TestCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <lib/support/UnitTestRegistration.h>
#include <transport/retransmit/Cache.h>

#include <bitset>
#include <nlunit-test.h>
#include <gtest/gtest.h>

// Helpers for simple payload management
namespace {
Expand All @@ -45,18 +44,16 @@ class TestableCache : public chip::Retransmit::Cache<KeyType, PayloadType, N>
class IntPayloadTracker
{
public:
void Init(nlTestSuite * suite) { mSuite = suite; }

void Acquire(int value)
{
NL_TEST_ASSERT(mSuite, (value > 0) && value < kMaxPayloadValue);
EXPECT_TRUE((value > 0) && value < kMaxPayloadValue);
mAcquired.set(static_cast<size_t>(value));
}

void Release(int value)
{
NL_TEST_ASSERT(mSuite, (value > 0) && value < kMaxPayloadValue);
NL_TEST_ASSERT(mSuite, mAcquired.test(static_cast<size_t>(value)));
EXPECT_TRUE((value > 0) && value < kMaxPayloadValue);
EXPECT_TRUE(mAcquired.test(static_cast<size_t>(value)));
mAcquired.reset(static_cast<size_t>(value));
}

Expand All @@ -65,7 +62,6 @@ class IntPayloadTracker
bool IsAcquired(int value) const { return mAcquired.test(static_cast<size_t>(value)); }

private:
nlTestSuite * mSuite;
std::bitset<kMaxPayloadValue> mAcquired;
};

Expand Down Expand Up @@ -104,176 +100,153 @@ void chip::Retransmit::Lifetime<int>::Release(int & value)

namespace {

void TestNoOp(nlTestSuite * inSuite, void * inContext)
TEST(TestCache, TestNoOp)
{
// unused address cache should not do any Acquire/release at any time
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(gPayloadTracker.Count(), 0u);
{
TestableCache<int, int, 20> test;
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(gPayloadTracker.Count(), 0u);
}
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(gPayloadTracker.Count(), 0u);
}

void TestDestructorFree(nlTestSuite * inSuite, void * inContext)
TEST(TestCache, TestDestructorFree)
{
{
TestableCache<int, int, 20> test;

NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(gPayloadTracker.Count(), 0u);

NL_TEST_ASSERT(inSuite, test.AddValue(1, 1) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(2, 2) == CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(1, 1), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(2, 2), CHIP_NO_ERROR);

NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 2);
EXPECT_EQ(gPayloadTracker.Count(), 2u);
}

// destructor should release the items
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(gPayloadTracker.Count(), 0u);
}

void OutOfSpace(nlTestSuite * inSuite, void * inContext)
TEST(TestCache, OutOfSpace)
{
{
TestableCache<int, int, 4> test;

NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(gPayloadTracker.Count(), 0u);

NL_TEST_ASSERT(inSuite, test.AddValue(1, 1) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(2, 2) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(3, 4) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(4, 6) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 4);
EXPECT_EQ(test.AddValue(1, 1), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(2, 2), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(3, 4), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(4, 6), CHIP_NO_ERROR);
EXPECT_EQ(gPayloadTracker.Count(), 4u);

NL_TEST_ASSERT(inSuite, test.AddValue(5, 8) == CHIP_ERROR_NO_MEMORY);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 4);
EXPECT_EQ(test.AddValue(5, 8), CHIP_ERROR_NO_MEMORY);
EXPECT_EQ(gPayloadTracker.Count(), 4u);

NL_TEST_ASSERT(inSuite, test.AddValue(6, 10) == CHIP_ERROR_NO_MEMORY);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 4);
EXPECT_EQ(test.AddValue(6, 10), CHIP_ERROR_NO_MEMORY);
EXPECT_EQ(gPayloadTracker.Count(), 4u);
}
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(gPayloadTracker.Count(), 0u);
}

void AddRemove(nlTestSuite * inSuite, void * inContext)
TEST(TestCache, AddRemove)
{
TestableCache<int, int, 3> test;

NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(gPayloadTracker.Count(), 0u);

NL_TEST_ASSERT(inSuite, test.AddValue(1, 1) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(2, 2) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(3, 4) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 3);
EXPECT_EQ(test.AddValue(1, 1), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(2, 2), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(3, 4), CHIP_NO_ERROR);
EXPECT_EQ(gPayloadTracker.Count(), 3u);

NL_TEST_ASSERT(inSuite, test.AddValue(10, 8) == CHIP_ERROR_NO_MEMORY);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 3);
EXPECT_EQ(test.AddValue(10, 8), CHIP_ERROR_NO_MEMORY);
EXPECT_EQ(gPayloadTracker.Count(), 3u);

NL_TEST_ASSERT(inSuite, test.Remove(2) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 2);
EXPECT_EQ(test.Remove(2), CHIP_NO_ERROR);
EXPECT_EQ(gPayloadTracker.Count(), 2u);

NL_TEST_ASSERT(inSuite, test.AddValue(10, 8) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 3);
EXPECT_EQ(test.AddValue(10, 8), CHIP_NO_ERROR);
EXPECT_EQ(gPayloadTracker.Count(), 3u);

NL_TEST_ASSERT(inSuite, test.Remove(14) == CHIP_ERROR_KEY_NOT_FOUND);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 3);
EXPECT_EQ(test.Remove(14), CHIP_ERROR_KEY_NOT_FOUND);
EXPECT_EQ(gPayloadTracker.Count(), 3u);

NL_TEST_ASSERT(inSuite, test.Remove(1) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 2);
EXPECT_EQ(test.Remove(1), CHIP_NO_ERROR);
EXPECT_EQ(gPayloadTracker.Count(), 2u);

NL_TEST_ASSERT(inSuite, test.Remove(3) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 1);
EXPECT_EQ(test.Remove(3), CHIP_NO_ERROR);
EXPECT_EQ(gPayloadTracker.Count(), 1u);

NL_TEST_ASSERT(inSuite, test.Remove(3) == CHIP_ERROR_KEY_NOT_FOUND);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 1);
EXPECT_EQ(test.Remove(3), CHIP_ERROR_KEY_NOT_FOUND);
EXPECT_EQ(gPayloadTracker.Count(), 1u);

NL_TEST_ASSERT(inSuite, test.Remove(10) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(test.Remove(10), CHIP_NO_ERROR);
EXPECT_EQ(gPayloadTracker.Count(), 0u);

NL_TEST_ASSERT(inSuite, test.Remove(10) == CHIP_ERROR_KEY_NOT_FOUND);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(test.Remove(10), CHIP_ERROR_KEY_NOT_FOUND);
EXPECT_EQ(gPayloadTracker.Count(), 0u);
}

void RemoveMatching(nlTestSuite * inSuite, void * inContext)
TEST(TestCache, RemoveMatching)
{
TestableCache<int, int, 4> test;

NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(gPayloadTracker.Count(), 0u);

NL_TEST_ASSERT(inSuite, test.AddValue(1, 1) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(2, 2) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(3, 4) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(4, 8) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 4);
EXPECT_EQ(test.AddValue(1, 1), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(2, 2), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(3, 4), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(4, 8), CHIP_NO_ERROR);
EXPECT_EQ(gPayloadTracker.Count(), 4u);

test.RemoveMatching(DivisibleBy(2));
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 2);
EXPECT_EQ(gPayloadTracker.Count(), 2u);

// keys 1 and 3 remain
NL_TEST_ASSERT(inSuite, gPayloadTracker.IsAcquired(1));
NL_TEST_ASSERT(inSuite, gPayloadTracker.IsAcquired(4));
EXPECT_TRUE(gPayloadTracker.IsAcquired(1));
EXPECT_TRUE(gPayloadTracker.IsAcquired(4));

NL_TEST_ASSERT(inSuite, test.Remove(3) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.IsAcquired(1));
NL_TEST_ASSERT(inSuite, !gPayloadTracker.IsAcquired(4));
EXPECT_EQ(test.Remove(3), CHIP_NO_ERROR);
EXPECT_TRUE(gPayloadTracker.IsAcquired(1));
EXPECT_FALSE(gPayloadTracker.IsAcquired(4));
}

void FindMatching(nlTestSuite * inSuite, void * inContext)
TEST(TestCache, FindMatching)
{
TestableCache<int, int, 4> test;

NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
EXPECT_EQ(gPayloadTracker.Count(), 0u);

NL_TEST_ASSERT(inSuite, test.AddValue(1, 1) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(2, 2) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(3, 4) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.AddValue(4, 8) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 4);
EXPECT_EQ(test.AddValue(1, 1), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(2, 2), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(3, 4), CHIP_NO_ERROR);
EXPECT_EQ(test.AddValue(4, 8), CHIP_NO_ERROR);
EXPECT_EQ(gPayloadTracker.Count(), 4u);

const int * key;
const int * value;

NL_TEST_ASSERT(inSuite, test.Find(DivisibleBy(20), &key, &value) == false);
NL_TEST_ASSERT(inSuite, key == nullptr);
NL_TEST_ASSERT(inSuite, value == nullptr);
EXPECT_FALSE(test.Find(DivisibleBy(20), &key, &value));
EXPECT_EQ(key, nullptr);
EXPECT_EQ(value, nullptr);

// This relies on linear add. May need changing if implementation changes
NL_TEST_ASSERT(inSuite, test.Find(DivisibleBy(2), &key, &value) == true);
NL_TEST_ASSERT(inSuite, *key == 2);
NL_TEST_ASSERT(inSuite, *value == 2);
EXPECT_TRUE(test.Find(DivisibleBy(2), &key, &value));
EXPECT_EQ(*key, 2);
EXPECT_EQ(*value, 2);

NL_TEST_ASSERT(inSuite, test.Remove(*key) == CHIP_NO_ERROR);
EXPECT_EQ(test.Remove(*key), CHIP_NO_ERROR);

NL_TEST_ASSERT(inSuite, test.Find(DivisibleBy(2), &key, &value) == true);
NL_TEST_ASSERT(inSuite, *key == 4);
NL_TEST_ASSERT(inSuite, *value == 8);
EXPECT_TRUE(test.Find(DivisibleBy(2), &key, &value));
EXPECT_EQ(*key, 4);
EXPECT_EQ(*value, 8);

NL_TEST_ASSERT(inSuite, test.Remove(*key) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.Find(DivisibleBy(2), &key, &value) == false);
NL_TEST_ASSERT(inSuite, key == nullptr);
NL_TEST_ASSERT(inSuite, value == nullptr);
EXPECT_EQ(test.Remove(*key), CHIP_NO_ERROR);
EXPECT_FALSE(test.Find(DivisibleBy(2), &key, &value));
EXPECT_EQ(key, nullptr);
EXPECT_EQ(value, nullptr);
}

} // namespace

// clang-format off
static const nlTest sTests[] =
{
NL_TEST_DEF("NoOp", TestNoOp),
NL_TEST_DEF("DestructorFree", TestDestructorFree),
NL_TEST_DEF("OutOfSpace", OutOfSpace),
NL_TEST_DEF("AddRemove", AddRemove),
NL_TEST_DEF("RemoveMatching", RemoveMatching),
NL_TEST_DEF("FindMatching", FindMatching),
NL_TEST_SENTINEL()
};
// clang-format on

int TestCache()
{
nlTestSuite theSuite = { "Retransmit-Cache", &sTests[0], nullptr, nullptr };
gPayloadTracker.Init(&theSuite);
nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
}

CHIP_REGISTER_TEST_SUITE(TestCache)

0 comments on commit f27b978

Please sign in to comment.