From 344705b3a3bfaf24d9dffc65f6dbd22127f819f2 Mon Sep 17 00:00:00 2001 From: AYA Date: Mon, 29 Apr 2024 19:06:40 +0200 Subject: [PATCH 1/6] pw_unit_test migration: lib support batch 5 --- src/lib/support/tests/BUILD.gn | 2 +- src/lib/support/tests/TestPool.cpp | 293 +++++++++++++---------------- 2 files changed, 128 insertions(+), 167 deletions(-) diff --git a/src/lib/support/tests/BUILD.gn b/src/lib/support/tests/BUILD.gn index 6a7c7905d27c02..f3cdd2ad7a8206 100644 --- a/src/lib/support/tests/BUILD.gn +++ b/src/lib/support/tests/BUILD.gn @@ -35,6 +35,7 @@ chip_test_suite("tests") { "TestFold.cpp", "TestIniEscaping.cpp", "TestPersistedCounter.cpp", + "TestPool.cpp", "TestPrivateHeap.cpp", "TestSafeInt.cpp", "TestSafeString.cpp", @@ -81,7 +82,6 @@ chip_test_suite_using_nltest("tests_nltest") { "TestIntrusiveList.cpp", "TestJsonToTlv.cpp", "TestJsonToTlvToJson.cpp", - "TestPool.cpp", "TestStateMachine.cpp", "TestThreadOperationalDataset.cpp", "TestTlvJson.cpp", diff --git a/src/lib/support/tests/TestPool.cpp b/src/lib/support/tests/TestPool.cpp index 963a7b3c52f7ea..da76d7ebb70d7b 100644 --- a/src/lib/support/tests/TestPool.cpp +++ b/src/lib/support/tests/TestPool.cpp @@ -25,13 +25,11 @@ #include +#include + #include #include -#include #include - -#include - namespace chip { template @@ -51,60 +49,67 @@ namespace { using namespace chip; +class TestPool : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } +}; + template -void TestReleaseNull(nlTestSuite * inSuite, void * inContext) +void TestReleaseNull() { ObjectPool pool; pool.ReleaseObject(nullptr); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == 0); - NL_TEST_ASSERT(inSuite, pool.Allocated() == 0); + EXPECT_EQ(GetNumObjectsInUse(pool), 0u); + EXPECT_EQ(pool.Allocated(), 0u); } -void TestReleaseNullStatic(nlTestSuite * inSuite, void * inContext) +TEST_F(TestPool, TestReleaseNullStatic) { - TestReleaseNull(inSuite, inContext); + TestReleaseNull(); } #if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP -void TestReleaseNullDynamic(nlTestSuite * inSuite, void * inContext) +TEST_F(TestPool, TestReleaseNullDynamic) { - TestReleaseNull(inSuite, inContext); + TestReleaseNull(); } #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP template -void TestCreateReleaseObject(nlTestSuite * inSuite, void * inContext) +void TestCreateReleaseObject() { ObjectPool pool; uint32_t * obj[N]; - NL_TEST_ASSERT(inSuite, pool.Allocated() == 0); + EXPECT_EQ(pool.Allocated(), 0u); for (int t = 0; t < 2; ++t) { pool.ReleaseAll(); - NL_TEST_ASSERT(inSuite, pool.Allocated() == 0); + EXPECT_EQ(pool.Allocated(), 0u); for (size_t i = 0; i < N; ++i) { obj[i] = pool.CreateObject(); - NL_TEST_ASSERT(inSuite, obj[i] != nullptr); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == i + 1); - NL_TEST_ASSERT(inSuite, pool.Allocated() == i + 1); + ASSERT_NE(obj[i], nullptr); + EXPECT_EQ(GetNumObjectsInUse(pool), i + 1); + EXPECT_EQ(pool.Allocated(), i + 1); } } for (size_t i = 0; i < N; ++i) { pool.ReleaseObject(obj[i]); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == N - i - 1); - NL_TEST_ASSERT(inSuite, pool.Allocated() == N - i - 1); + EXPECT_EQ(GetNumObjectsInUse(pool), N - i - 1); + EXPECT_EQ(pool.Allocated(), N - i - 1); } } -void TestCreateReleaseObjectStatic(nlTestSuite * inSuite, void * inContext) +TEST_F(TestPool, TestCreateReleaseObjectStatic) { constexpr const size_t kSize = 100; - TestCreateReleaseObject(inSuite, inContext); + TestCreateReleaseObject(); ObjectPool pool; uint32_t * obj[kSize]; @@ -112,44 +117,44 @@ void TestCreateReleaseObjectStatic(nlTestSuite * inSuite, void * inContext) for (size_t i = 0; i < kSize; ++i) { obj[i] = pool.CreateObject(); - NL_TEST_ASSERT(inSuite, obj[i] != nullptr); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == i + 1); - NL_TEST_ASSERT(inSuite, pool.Allocated() == i + 1); + ASSERT_NE(obj[i], nullptr); + EXPECT_EQ(GetNumObjectsInUse(pool), i + 1); + EXPECT_EQ(pool.Allocated(), i + 1); } uint32_t * fail = pool.CreateObject(); - NL_TEST_ASSERT(inSuite, fail == nullptr); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == kSize); - NL_TEST_ASSERT(inSuite, pool.Allocated() == kSize); - NL_TEST_ASSERT(inSuite, pool.Exhausted()); + EXPECT_EQ(fail, nullptr); + EXPECT_EQ(GetNumObjectsInUse(pool), kSize); + EXPECT_EQ(pool.Allocated(), kSize); + EXPECT_TRUE(pool.Exhausted()); pool.ReleaseObject(obj[55]); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == kSize - 1); - NL_TEST_ASSERT(inSuite, pool.Allocated() == kSize - 1); - NL_TEST_ASSERT(inSuite, !pool.Exhausted()); - NL_TEST_ASSERT(inSuite, obj[55] == pool.CreateObject()); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == kSize); - NL_TEST_ASSERT(inSuite, pool.Allocated() == kSize); - NL_TEST_ASSERT(inSuite, pool.Exhausted()); + EXPECT_EQ(GetNumObjectsInUse(pool), kSize - 1); + EXPECT_EQ(pool.Allocated(), kSize - 1); + EXPECT_FALSE(pool.Exhausted()); + EXPECT_EQ(obj[55], pool.CreateObject()); + EXPECT_EQ(GetNumObjectsInUse(pool), kSize); + EXPECT_EQ(pool.Allocated(), kSize); + EXPECT_TRUE(pool.Exhausted()); fail = pool.CreateObject(); - NL_TEST_ASSERT(inSuite, fail == nullptr); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == kSize); - NL_TEST_ASSERT(inSuite, pool.Allocated() == kSize); - NL_TEST_ASSERT(inSuite, pool.Exhausted()); + ASSERT_EQ(fail, nullptr); + EXPECT_EQ(GetNumObjectsInUse(pool), kSize); + EXPECT_EQ(pool.Allocated(), kSize); + EXPECT_TRUE(pool.Exhausted()); pool.ReleaseAll(); } #if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP -void TestCreateReleaseObjectDynamic(nlTestSuite * inSuite, void * inContext) +TEST_F(TestPool, TestCreateReleaseObjectDynamic) { - TestCreateReleaseObject(inSuite, inContext); + TestCreateReleaseObject(); } #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP template -void TestCreateReleaseStruct(nlTestSuite * inSuite, void * inContext) +void TestCreateReleaseStruct() { struct S { @@ -166,17 +171,17 @@ void TestCreateReleaseStruct(nlTestSuite * inSuite, void * inContext) for (size_t i = 0; i < kSize; ++i) { objs2[i] = pool.CreateObject(objs1); - NL_TEST_ASSERT(inSuite, objs2[i] != nullptr); - NL_TEST_ASSERT(inSuite, pool.Allocated() == i + 1); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == i + 1); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == objs1.size()); + ASSERT_NE(objs2[i], nullptr); + EXPECT_EQ(pool.Allocated(), i + 1); + EXPECT_EQ(GetNumObjectsInUse(pool), i + 1); + EXPECT_EQ(GetNumObjectsInUse(pool), objs1.size()); } for (size_t i = 0; i < kSize; ++i) { pool.ReleaseObject(objs2[i]); - NL_TEST_ASSERT(inSuite, pool.Allocated() == kSize - i - 1); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == kSize - i - 1); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == objs1.size()); + EXPECT_EQ(pool.Allocated(), kSize - i - 1); + EXPECT_EQ(GetNumObjectsInUse(pool), kSize - i - 1); + EXPECT_EQ(GetNumObjectsInUse(pool), objs1.size()); } // Verify that ReleaseAll() calls the destructors. @@ -184,35 +189,35 @@ void TestCreateReleaseStruct(nlTestSuite * inSuite, void * inContext) { obj = pool.CreateObject(objs1); } - NL_TEST_ASSERT(inSuite, objs1.size() == kSize); - NL_TEST_ASSERT(inSuite, pool.Allocated() == kSize); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == kSize); + EXPECT_EQ(objs1.size(), kSize); + EXPECT_EQ(pool.Allocated(), kSize); + EXPECT_EQ(GetNumObjectsInUse(pool), kSize); printf("allocated = %u\n", static_cast(pool.Allocated())); printf("highwater = %u\n", static_cast(pool.HighWaterMark())); pool.ReleaseAll(); printf("allocated = %u\n", static_cast(pool.Allocated())); printf("highwater = %u\n", static_cast(pool.HighWaterMark())); - NL_TEST_ASSERT(inSuite, objs1.size() == 0); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(pool) == 0); - NL_TEST_ASSERT(inSuite, pool.Allocated() == 0); - NL_TEST_ASSERT(inSuite, pool.HighWaterMark() == kSize); + EXPECT_EQ(objs1.size(), 0u); + EXPECT_EQ(GetNumObjectsInUse(pool), 0u); + EXPECT_EQ(pool.Allocated(), 0u); + EXPECT_EQ(pool.HighWaterMark(), kSize); } -void TestCreateReleaseStructStatic(nlTestSuite * inSuite, void * inContext) +TEST_F(TestPool, TestCreateReleaseStructStatic) { - TestCreateReleaseStruct(inSuite, inContext); + TestCreateReleaseStruct(); } #if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP -void TestCreateReleaseStructDynamic(nlTestSuite * inSuite, void * inContext) +TEST_F(TestPool, TestCreateReleaseStructDynamic) { - TestCreateReleaseStruct(inSuite, inContext); + TestCreateReleaseStruct(); } #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP template -void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext) +void TestForEachActiveObject() { struct S { @@ -229,37 +234,37 @@ void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext) for (size_t i = 0; i < kSize; ++i) { objArray[i] = pool.CreateObject(i); - NL_TEST_ASSERT(inSuite, objArray[i] != nullptr); - NL_TEST_ASSERT(inSuite, objArray[i]->mId == i); + ASSERT_NE(objArray[i], nullptr); + EXPECT_EQ(objArray[i]->mId, i); objIds.insert(i); } // Default constructor of an iterator should be pointing to the pool end. { typename ObjectPoolIterator::Type defaultIterator; - NL_TEST_ASSERT(inSuite, defaultIterator == pool.end()); + EXPECT_EQ(defaultIterator, pool.end()); } // Verify that iteration visits all objects. size_t count = 0; { size_t sum = 0; - pool.ForEachActiveObject([&](S * object) { - NL_TEST_ASSERT(inSuite, object != nullptr); + pool.ForEachActiveObject([&](S * object) -> Loop { + EXPECT_NE(object, nullptr); if (object == nullptr) { - // NL_TEST_ASSERT doesn't stop running the test and we want to avoid nullptr dereference. + // Using EXPECT_NE instead of ASSERT_NE due to compilation errors when using ASSERT_NE return Loop::Continue; } - NL_TEST_ASSERT(inSuite, objIds.count(object->mId) == 1); + EXPECT_EQ(objIds.count(object->mId), 1u); objIds.erase(object->mId); ++count; sum += object->mId; return Loop::Continue; }); - NL_TEST_ASSERT(inSuite, count == kSize); - NL_TEST_ASSERT(inSuite, sum == kSize * (kSize - 1) / 2); - NL_TEST_ASSERT(inSuite, objIds.size() == 0); + EXPECT_EQ(count, kSize); + EXPECT_EQ(sum, kSize * (kSize - 1) / 2); + EXPECT_EQ(objIds.size(), 0u); } // Test begin/end iteration @@ -273,14 +278,14 @@ void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext) size_t sum = 0; for (auto v = pool.begin(); v != pool.end(); ++v) { - NL_TEST_ASSERT(inSuite, objIds.count((*v)->mId) == 1); + EXPECT_EQ(objIds.count((*v)->mId), 1u); objIds.erase((*v)->mId); ++count; sum += (*v)->mId; } - NL_TEST_ASSERT(inSuite, count == kSize); - NL_TEST_ASSERT(inSuite, sum == kSize * (kSize - 1) / 2); - NL_TEST_ASSERT(inSuite, objIds.size() == 0); + EXPECT_EQ(count, kSize); + EXPECT_EQ(sum, kSize * (kSize - 1) / 2); + EXPECT_EQ(objIds.size(), 0u); } // Verify that returning Loop::Break stops iterating. @@ -289,8 +294,8 @@ void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext) objIds.insert(object->mId); return ++count != kSize / 2 ? Loop::Continue : Loop::Break; }); - NL_TEST_ASSERT(inSuite, count == kSize / 2); - NL_TEST_ASSERT(inSuite, objIds.size() == kSize / 2); + EXPECT_EQ(count, kSize / 2); + EXPECT_EQ(objIds.size(), kSize / 2); // Verify that iteration can be nested. count = 0; @@ -311,8 +316,8 @@ void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext) } return Loop::Continue; }); - NL_TEST_ASSERT(inSuite, count == (kSize - 1) * kSize / 2); - NL_TEST_ASSERT(inSuite, objIds.size() == 0); + EXPECT_EQ(count, (kSize - 1) * kSize / 2); + EXPECT_EQ(objIds.size(), 0u); // Verify that iteration can be nested for iterator types { @@ -346,8 +351,8 @@ void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext) } } } - NL_TEST_ASSERT(inSuite, count == (kSize - 1) * kSize / 2); - NL_TEST_ASSERT(inSuite, objIds.size() == 0); + EXPECT_EQ(count, (kSize - 1) * kSize / 2); + EXPECT_EQ(objIds.size(), 0u); } count = 0; @@ -364,18 +369,18 @@ void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext) } return Loop::Continue; }); - NL_TEST_ASSERT(inSuite, count == kSize); - NL_TEST_ASSERT(inSuite, objIds.size() == kSize / 2); + EXPECT_EQ(count, kSize); + EXPECT_EQ(objIds.size(), kSize / 2); for (size_t i = 0; i < kSize; ++i) { if ((i % 2) == 0) { - NL_TEST_ASSERT(inSuite, objArray[i] == nullptr); + EXPECT_EQ(objArray[i], nullptr); } else { - NL_TEST_ASSERT(inSuite, objArray[i] != nullptr); - NL_TEST_ASSERT(inSuite, objArray[i]->mId == i); + ASSERT_NE(objArray[i], nullptr); + EXPECT_EQ(objArray[i]->mId, i); } } @@ -385,19 +390,19 @@ void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext) if ((object->mId % 2) == 1) { size_t id = object->mId - 1; - NL_TEST_ASSERT(inSuite, objArray[id] == nullptr); + EXPECT_EQ(objArray[id], nullptr); objArray[id] = pool.CreateObject(id); - NL_TEST_ASSERT(inSuite, objArray[id] != nullptr); + EXPECT_NE(objArray[id], nullptr); } return Loop::Continue; }); for (size_t i = 0; i < kSize; ++i) { - NL_TEST_ASSERT(inSuite, objArray[i] != nullptr); - NL_TEST_ASSERT(inSuite, objArray[i]->mId == i); + ASSERT_NE(objArray[i], nullptr); + EXPECT_EQ(objArray[i]->mId, i); } - NL_TEST_ASSERT(inSuite, count >= kSize / 2); - NL_TEST_ASSERT(inSuite, count <= kSize); + EXPECT_GE(count, kSize / 2); + EXPECT_LE(count, kSize); // Test begin/end iteration { @@ -417,25 +422,25 @@ void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext) objIds.insert(object->mId); } } - NL_TEST_ASSERT(inSuite, count == kSize); - NL_TEST_ASSERT(inSuite, objIds.size() == kSize / 2); + EXPECT_EQ(count, kSize); + EXPECT_EQ(objIds.size(), kSize / 2); // validate we iterate only over active objects for (auto object : pool) { - NL_TEST_ASSERT(inSuite, (object->mId % 2) == 1); + EXPECT_EQ((object->mId % 2), 1u); } for (size_t i = 0; i < kSize; ++i) { if ((i % 2) == 0) { - NL_TEST_ASSERT(inSuite, objArray[i] == nullptr); + EXPECT_EQ(objArray[i], nullptr); } else { - NL_TEST_ASSERT(inSuite, objArray[i] != nullptr); - NL_TEST_ASSERT(inSuite, objArray[i]->mId == i); + ASSERT_NE(objArray[i], nullptr); + EXPECT_EQ(objArray[i]->mId, i); } } @@ -448,36 +453,36 @@ void TestForEachActiveObject(nlTestSuite * inSuite, void * inContext) continue; } size_t id = object->mId - 1; - NL_TEST_ASSERT(inSuite, objArray[id] == nullptr); + EXPECT_EQ(objArray[id], nullptr); objArray[id] = pool.CreateObject(id); - NL_TEST_ASSERT(inSuite, objArray[id] != nullptr); + EXPECT_NE(objArray[id], nullptr); } for (size_t i = 0; i < kSize; ++i) { - NL_TEST_ASSERT(inSuite, objArray[i] != nullptr); - NL_TEST_ASSERT(inSuite, objArray[i]->mId == i); + ASSERT_NE(objArray[i], nullptr); + EXPECT_EQ(objArray[i]->mId, i); } - NL_TEST_ASSERT(inSuite, count >= kSize / 2); - NL_TEST_ASSERT(inSuite, count <= kSize); + EXPECT_GE(count, kSize / 2); + EXPECT_LE(count, kSize); } pool.ReleaseAll(); } -void TestForEachActiveObjectStatic(nlTestSuite * inSuite, void * inContext) +TEST_F(TestPool, TestForEachActiveObjectStatic) { - TestForEachActiveObject(inSuite, inContext); + TestForEachActiveObject(); } #if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP -void TestForEachActiveObjectDynamic(nlTestSuite * inSuite, void * inContext) +TEST_F(TestPool, TestForEachActiveObjectDynamic) { - TestForEachActiveObject(inSuite, inContext); + TestForEachActiveObject(); } #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP template -void TestPoolInterface(nlTestSuite * inSuite, void * inContext) +void TestPoolInterface() { struct TestObject { @@ -503,84 +508,40 @@ void TestPoolInterface(nlTestSuite * inSuite, void * inContext) for (size_t i = 0; i < kSize; ++i) { objs2[i] = poolHolder.mTestObjectPoolInterface.CreateObject(&bits, i); - NL_TEST_ASSERT(inSuite, objs2[i] != nullptr); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(poolHolder.mTestObjectPoolInterface) == i + 1); - NL_TEST_ASSERT(inSuite, bits == (1ul << (i + 1)) - 1); + ASSERT_NE(objs2[i], nullptr); + EXPECT_EQ(GetNumObjectsInUse(poolHolder.mTestObjectPoolInterface), i + 1); + EXPECT_EQ(bits, (1ul << (i + 1)) - 1); } for (size_t i = 0; i < kSize; ++i) { poolHolder.mTestObjectPoolInterface.ReleaseObject(objs2[i]); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(poolHolder.mTestObjectPoolInterface) == kSize - i - 1); + EXPECT_EQ(GetNumObjectsInUse(poolHolder.mTestObjectPoolInterface), kSize - i - 1); } - NL_TEST_ASSERT(inSuite, bits == 0); + EXPECT_EQ(bits, 0u); // Verify that ReleaseAll() calls the destructors. for (size_t i = 0; i < kSize; ++i) { objs2[i] = poolHolder.mTestObjectPoolInterface.CreateObject(&bits, i); } - NL_TEST_ASSERT(inSuite, bits == (1ul << kSize) - 1); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(poolHolder.mTestObjectPoolInterface) == kSize); + EXPECT_EQ(bits, (1ul << kSize) - 1); + EXPECT_EQ(GetNumObjectsInUse(poolHolder.mTestObjectPoolInterface), kSize); poolHolder.mTestObjectPoolInterface.ReleaseAll(); - NL_TEST_ASSERT(inSuite, bits == 0); - NL_TEST_ASSERT(inSuite, GetNumObjectsInUse(poolHolder.mTestObjectPoolInterface) == 0); + EXPECT_EQ(bits, 0u); + EXPECT_EQ(GetNumObjectsInUse(poolHolder.mTestObjectPoolInterface), 0u); } -void TestPoolInterfaceStatic(nlTestSuite * inSuite, void * inContext) +TEST_F(TestPool, TestPoolInterfaceStatic) { - TestPoolInterface(inSuite, inContext); + TestPoolInterface(); } #if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP -void TestPoolInterfaceDynamic(nlTestSuite * inSuite, void * inContext) +TEST_F(TestPool, TestPoolInterfaceDynamic) { - TestPoolInterface(inSuite, inContext); + TestPoolInterface(); } #endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP -int Setup(void * inContext) -{ - return ::chip::Platform::MemoryInit() == CHIP_NO_ERROR ? SUCCESS : FAILURE; -} - -int Teardown(void * inContext) -{ - ::chip::Platform::MemoryShutdown(); - return SUCCESS; -} - } // namespace - -#define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn) -/** - * Test Suite. It lists all the test functions. - */ -static const nlTest sTests[] = { - // clang-format off - NL_TEST_DEF_FN(TestReleaseNullStatic), - NL_TEST_DEF_FN(TestCreateReleaseObjectStatic), - NL_TEST_DEF_FN(TestCreateReleaseStructStatic), - NL_TEST_DEF_FN(TestForEachActiveObjectStatic), - NL_TEST_DEF_FN(TestPoolInterfaceStatic), -#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP - NL_TEST_DEF_FN(TestReleaseNullDynamic), - NL_TEST_DEF_FN(TestCreateReleaseObjectDynamic), - NL_TEST_DEF_FN(TestCreateReleaseStructDynamic), - NL_TEST_DEF_FN(TestForEachActiveObjectDynamic), - NL_TEST_DEF_FN(TestPoolInterfaceDynamic), -#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP - NL_TEST_SENTINEL() - // clang-format on -}; - -int TestPool() -{ - nlTestSuite theSuite = { "CHIP Pool tests", &sTests[0], Setup, Teardown }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestPool); From e418c80955d1d814d61c63ba342174200ebe23d2 Mon Sep 17 00:00:00 2001 From: Amine Alami <43780877+Alami-Amine@users.noreply.github.com> Date: Mon, 29 Apr 2024 17:11:20 +0200 Subject: [PATCH 2/6] pw_unit_test migration: lib support batch 4 (#33199) * pw_unit_test migration: lib support batch 4 * fix merge error BUILD.gn --- src/lib/support/tests/BUILD.gn | 16 +- src/lib/support/tests/TestCHIPCounter.cpp | 4 +- src/lib/support/tests/TestCHIPMem.cpp | 101 +- src/lib/support/tests/TestCHIPMemString.cpp | 92 +- src/lib/support/tests/TestIntrusiveList.cpp | 160 ++- src/lib/support/tests/TestJsonToTlv.cpp | 201 ++-- src/lib/support/tests/TestJsonToTlvToJson.cpp | 1035 +++++++---------- src/lib/support/tests/TestTlvJson.cpp | 53 +- src/lib/support/tests/TestTlvToJson.cpp | 62 +- src/lib/support/tests/TestZclString.cpp | 108 +- 10 files changed, 752 insertions(+), 1080 deletions(-) diff --git a/src/lib/support/tests/BUILD.gn b/src/lib/support/tests/BUILD.gn index f3cdd2ad7a8206..c4c62a21edeee8 100644 --- a/src/lib/support/tests/BUILD.gn +++ b/src/lib/support/tests/BUILD.gn @@ -29,11 +29,16 @@ chip_test_suite("tests") { "TestBytesCircularBuffer.cpp", "TestBytesToHex.cpp", "TestCHIPCounter.cpp", + "TestCHIPMem.cpp", + "TestCHIPMemString.cpp", "TestDefer.cpp", "TestErrorStr.cpp", "TestFixedBufferAllocator.cpp", "TestFold.cpp", "TestIniEscaping.cpp", + "TestIntrusiveList.cpp", + "TestJsonToTlv.cpp", + "TestJsonToTlvToJson.cpp", "TestPersistedCounter.cpp", "TestPool.cpp", "TestPrivateHeap.cpp", @@ -48,8 +53,11 @@ chip_test_suite("tests") { "TestStringSplitter.cpp", "TestTestPersistentStorageDelegate.cpp", "TestTimeUtils.cpp", + "TestTlvJson.cpp", + "TestTlvToJson.cpp", "TestUtf8.cpp", "TestVariant.cpp", + "TestZclString.cpp", ] sources = [] @@ -77,16 +85,8 @@ chip_test_suite_using_nltest("tests_nltest") { output_name = "libSupportTestsNL" test_sources = [ - "TestCHIPMem.cpp", - "TestCHIPMemString.cpp", - "TestIntrusiveList.cpp", - "TestJsonToTlv.cpp", - "TestJsonToTlvToJson.cpp", "TestStateMachine.cpp", "TestThreadOperationalDataset.cpp", - "TestTlvJson.cpp", - "TestTlvToJson.cpp", - "TestZclString.cpp", ] sources = [] diff --git a/src/lib/support/tests/TestCHIPCounter.cpp b/src/lib/support/tests/TestCHIPCounter.cpp index d1212b97c5a3b7..b2c5e21b5038c2 100644 --- a/src/lib/support/tests/TestCHIPCounter.cpp +++ b/src/lib/support/tests/TestCHIPCounter.cpp @@ -16,9 +16,11 @@ * limitations under the License. */ +#include + #include + #include -#include using namespace chip; diff --git a/src/lib/support/tests/TestCHIPMem.cpp b/src/lib/support/tests/TestCHIPMem.cpp index 2520da47a8e6a3..c19d356abfeceb 100644 --- a/src/lib/support/tests/TestCHIPMem.cpp +++ b/src/lib/support/tests/TestCHIPMem.cpp @@ -29,11 +29,10 @@ #include #include +#include + #include #include -#include -#include -#include using namespace chip; using namespace chip::Logging; @@ -43,7 +42,14 @@ using namespace chip::Platform; // Unit tests // ================================= -static void TestMemAlloc_Malloc(nlTestSuite * inSuite, void * inContext) +class TestCHIPMem : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } +}; + +TEST_F(TestCHIPMem, TestMemAlloc_Malloc) { char * p1 = nullptr; char * p2 = nullptr; @@ -51,52 +57,52 @@ static void TestMemAlloc_Malloc(nlTestSuite * inSuite, void * inContext) // Verify long-term allocation p1 = static_cast(MemoryAlloc(64)); - NL_TEST_ASSERT(inSuite, p1 != nullptr); + EXPECT_NE(p1, nullptr); p2 = static_cast(MemoryAlloc(256)); - NL_TEST_ASSERT(inSuite, p2 != nullptr); + EXPECT_NE(p2, nullptr); chip::Platform::MemoryFree(p1); chip::Platform::MemoryFree(p2); // Verify short-term allocation p1 = static_cast(MemoryAlloc(256)); - NL_TEST_ASSERT(inSuite, p1 != nullptr); + EXPECT_NE(p1, nullptr); p2 = static_cast(MemoryAlloc(256)); - NL_TEST_ASSERT(inSuite, p2 != nullptr); + EXPECT_NE(p2, nullptr); p3 = static_cast(MemoryAlloc(256)); - NL_TEST_ASSERT(inSuite, p3 != nullptr); + EXPECT_NE(p3, nullptr); chip::Platform::MemoryFree(p1); chip::Platform::MemoryFree(p2); chip::Platform::MemoryFree(p3); } -static void TestMemAlloc_Calloc(nlTestSuite * inSuite, void * inContext) +TEST_F(TestCHIPMem, TestMemAlloc_Calloc) { - char * p = static_cast(MemoryCalloc(128, true)); - NL_TEST_EXIT_ON_FAILED_ASSERT(inSuite, p != nullptr); + char * p = static_cast(MemoryCalloc(128, sizeof(char))); + ASSERT_NE(p, nullptr); for (int i = 0; i < 128; i++) - NL_TEST_ASSERT(inSuite, p[i] == 0); + EXPECT_EQ(p[i], 0); chip::Platform::MemoryFree(p); } -static void TestMemAlloc_Realloc(nlTestSuite * inSuite, void * inContext) +TEST_F(TestCHIPMem, TestMemAlloc_Realloc) { char * pa = static_cast(MemoryAlloc(128)); - NL_TEST_ASSERT(inSuite, pa != nullptr); + EXPECT_NE(pa, nullptr); char * pb = static_cast(MemoryRealloc(pa, 256)); - NL_TEST_ASSERT(inSuite, pb != nullptr); + EXPECT_NE(pb, nullptr); chip::Platform::MemoryFree(pb); } -static void TestMemAlloc_UniquePtr(nlTestSuite * inSuite, void * inContext) +TEST_F(TestCHIPMem, TestMemAlloc_UniquePtr) { // UniquePtr is a wrapper of std::unique_ptr for platform allocators, we just check if we created a correct wrapper here. int constructorCalled = 0; @@ -114,15 +120,15 @@ static void TestMemAlloc_UniquePtr(nlTestSuite * inSuite, void * inContext) { auto ptr = MakeUnique(&constructorCalled, &destructorCalled); - NL_TEST_ASSERT(inSuite, constructorCalled == 1); - NL_TEST_ASSERT(inSuite, destructorCalled == 0); + EXPECT_EQ(constructorCalled, 1); + EXPECT_EQ(destructorCalled, 0); IgnoreUnusedVariable(ptr); } - NL_TEST_ASSERT(inSuite, destructorCalled == 1); + EXPECT_TRUE(destructorCalled); } -static void TestMemAlloc_SharedPtr(nlTestSuite * inSuite, void * inContext) +TEST_F(TestCHIPMem, TestMemAlloc_SharedPtr) { // SharedPtr is a wrapper of std::shared_ptr for platform allocators. int instanceConstructorCalled = 0; @@ -145,62 +151,21 @@ static void TestMemAlloc_SharedPtr(nlTestSuite * inSuite, void * inContext) SharedPtr otherReference; { auto ptr = MakeShared(&instanceConstructorCalled, &instanceDestructorCalled); - NL_TEST_ASSERT(inSuite, instanceConstructorCalled == 1); + EXPECT_EQ(instanceConstructorCalled, 1); // Capture a shared reference so we aren't destructed when we leave this scope. otherReference = ptr; } // Verify that by sharing to otherReference, we weren't destructed. - NL_TEST_ASSERT(inSuite, instanceDestructorCalled == 0); + EXPECT_EQ(instanceDestructorCalled, 0); // Now drop our reference. otherReference = MakeShared(&otherInstanceConstructorCalled, &otherInstanceDestructorCalled); // Verify that the new instance was constructed and the first instance was // destructed, and that we retain a reference to the new instance. - NL_TEST_ASSERT(inSuite, instanceConstructorCalled == 1); - NL_TEST_ASSERT(inSuite, instanceDestructorCalled == 1); - NL_TEST_ASSERT(inSuite, otherInstanceConstructorCalled == 1); - NL_TEST_ASSERT(inSuite, otherInstanceDestructorCalled == 0); -} - -/** - * Test Suite. It lists all the test functions. - */ -static const nlTest sTests[] = { NL_TEST_DEF("Test MemAlloc::Malloc", TestMemAlloc_Malloc), - NL_TEST_DEF("Test MemAlloc::Calloc", TestMemAlloc_Calloc), - NL_TEST_DEF("Test MemAlloc::Realloc", TestMemAlloc_Realloc), - NL_TEST_DEF("Test MemAlloc::UniquePtr", TestMemAlloc_UniquePtr), - NL_TEST_DEF("Test MemAlloc::SharedPtr", TestMemAlloc_SharedPtr), - NL_TEST_SENTINEL() }; - -/** - * Set up the test suite. - */ -int TestMemAlloc_Setup(void * inContext) -{ - CHIP_ERROR error = MemoryInit(); - if (error != CHIP_NO_ERROR) - return (FAILURE); - return (SUCCESS); + EXPECT_EQ(instanceConstructorCalled, 1); + EXPECT_EQ(instanceDestructorCalled, 1); + EXPECT_EQ(otherInstanceConstructorCalled, 1); + EXPECT_EQ(otherInstanceDestructorCalled, 0); } - -/** - * Tear down the test suite. - */ -int TestMemAlloc_Teardown(void * inContext) -{ - MemoryShutdown(); - return (SUCCESS); -} - -int TestMemAlloc() -{ - nlTestSuite theSuite = { "CHIP Memory Allocation tests", &sTests[0], TestMemAlloc_Setup, TestMemAlloc_Teardown }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestMemAlloc) diff --git a/src/lib/support/tests/TestCHIPMemString.cpp b/src/lib/support/tests/TestCHIPMemString.cpp index 90507fd69cf7d6..10101bd193c0f6 100644 --- a/src/lib/support/tests/TestCHIPMemString.cpp +++ b/src/lib/support/tests/TestCHIPMemString.cpp @@ -22,12 +22,12 @@ #include #include +#include + #include #include #include #include -#include -#include using namespace chip; using namespace chip::Platform; @@ -36,6 +36,13 @@ using namespace chip::Platform; // Unit tests // ================================= +class TestCHIPMemString : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } +}; + namespace { template struct TestBuffers @@ -51,28 +58,28 @@ struct TestBuffers memset(wayTooSmallBuf, 1, sizeof(wayTooSmallBuf)); memset(tooBigBuf, 1, sizeof(tooBigBuf)); } - void CheckCorrectness(nlTestSuite * inSuite, const char * testStr) + void CheckCorrectness(const char * testStr) { // correctSizeBuf and tooBigBuf should have the complete string. - NL_TEST_ASSERT(inSuite, correctSizeBuf[kTestBufLen - 1] == '\0'); - NL_TEST_ASSERT(inSuite, tooBigBuf[kTestBufLen - 1] == '\0'); - NL_TEST_ASSERT(inSuite, strcmp(correctSizeBuf, testStr) == 0); - NL_TEST_ASSERT(inSuite, strcmp(tooBigBuf, testStr) == 0); - NL_TEST_ASSERT(inSuite, strlen(correctSizeBuf) == strlen(testStr)); - NL_TEST_ASSERT(inSuite, strlen(tooBigBuf) == strlen(testStr)); + EXPECT_EQ(correctSizeBuf[kTestBufLen - 1], '\0'); + EXPECT_EQ(tooBigBuf[kTestBufLen - 1], '\0'); + EXPECT_STREQ(correctSizeBuf, testStr); + EXPECT_STREQ(tooBigBuf, testStr); + EXPECT_EQ(strlen(correctSizeBuf), strlen(testStr)); + EXPECT_EQ(strlen(tooBigBuf), strlen(testStr)); // wayTooSmallBuf is tiny and thus will only have the null terminator - NL_TEST_ASSERT(inSuite, wayTooSmallBuf[0] == '\0'); + EXPECT_EQ(wayTooSmallBuf[0], '\0'); // tooSmallBuf should still have a null terminator on the end - NL_TEST_ASSERT(inSuite, tooSmallBuf[kTestBufLen - 2] == '\0'); - NL_TEST_ASSERT(inSuite, memcmp(tooSmallBuf, testStr, kTestBufLen - 2) == 0); + EXPECT_EQ(tooSmallBuf[kTestBufLen - 2], '\0'); + EXPECT_EQ(memcmp(tooSmallBuf, testStr, kTestBufLen - 2), 0); } }; } // namespace -static void TestCopyString(nlTestSuite * inSuite, void * inContext) +TEST_F(TestCHIPMemString, TestCopyString) { static constexpr char testWord[] = "testytest"; ByteSpan testWordSpan = ByteSpan(reinterpret_cast(testWord), sizeof(testWord) - 1); @@ -85,7 +92,7 @@ static void TestCopyString(nlTestSuite * inSuite, void * inContext) CopyString(testBuffers.tooSmallBuf, sizeof(testBuffers.tooSmallBuf), testWord); CopyString(testBuffers.wayTooSmallBuf, sizeof(testBuffers.wayTooSmallBuf), testWord); CopyString(testBuffers.tooBigBuf, sizeof(testBuffers.tooBigBuf), testWord); - testBuffers.CheckCorrectness(inSuite, testWord); + testBuffers.CheckCorrectness(testWord); // CopyString with array size testBuffers.Reset(); @@ -93,7 +100,7 @@ static void TestCopyString(nlTestSuite * inSuite, void * inContext) CopyString(testBuffers.tooSmallBuf, testWord); CopyString(testBuffers.wayTooSmallBuf, testWord); CopyString(testBuffers.tooBigBuf, testWord); - testBuffers.CheckCorrectness(inSuite, testWord); + testBuffers.CheckCorrectness(testWord); // CopyString with explicit size from ByteSpan testBuffers.Reset(); @@ -101,7 +108,7 @@ static void TestCopyString(nlTestSuite * inSuite, void * inContext) CopyString(testBuffers.tooSmallBuf, sizeof(testBuffers.tooSmallBuf), testWordSpan); CopyString(testBuffers.wayTooSmallBuf, sizeof(testBuffers.wayTooSmallBuf), testWordSpan); CopyString(testBuffers.tooBigBuf, sizeof(testBuffers.tooBigBuf), testWordSpan); - testBuffers.CheckCorrectness(inSuite, testWord); + testBuffers.CheckCorrectness(testWord); // CopyString with array size from ByteSpan testBuffers.Reset(); @@ -109,7 +116,7 @@ static void TestCopyString(nlTestSuite * inSuite, void * inContext) CopyString(testBuffers.tooSmallBuf, testWordSpan); CopyString(testBuffers.wayTooSmallBuf, testWordSpan); CopyString(testBuffers.tooBigBuf, testWordSpan); - testBuffers.CheckCorrectness(inSuite, testWord); + testBuffers.CheckCorrectness(testWord); // CopyString with explicit size from CharSpan testBuffers.Reset(); @@ -117,7 +124,7 @@ static void TestCopyString(nlTestSuite * inSuite, void * inContext) CopyString(testBuffers.tooSmallBuf, sizeof(testBuffers.tooSmallBuf), testWordSpan2); CopyString(testBuffers.wayTooSmallBuf, sizeof(testBuffers.wayTooSmallBuf), testWordSpan2); CopyString(testBuffers.tooBigBuf, sizeof(testBuffers.tooBigBuf), testWordSpan2); - testBuffers.CheckCorrectness(inSuite, testWord); + testBuffers.CheckCorrectness(testWord); // CopyString with array size from CharSpan testBuffers.Reset(); @@ -125,59 +132,24 @@ static void TestCopyString(nlTestSuite * inSuite, void * inContext) CopyString(testBuffers.tooSmallBuf, testWordSpan2); CopyString(testBuffers.wayTooSmallBuf, testWordSpan2); CopyString(testBuffers.tooBigBuf, testWordSpan2); - testBuffers.CheckCorrectness(inSuite, testWord); + testBuffers.CheckCorrectness(testWord); } -static void TestMemoryAllocString(nlTestSuite * inSuite, void * inContext) +TEST_F(TestCHIPMemString, TestMemoryAllocString) { static constexpr char testStr[] = "testytestString"; char * allocatedStr = MemoryAllocString(testStr, sizeof(testStr)); - NL_TEST_ASSERT(inSuite, allocatedStr != nullptr); - if (allocatedStr == nullptr) - { - return; - } - NL_TEST_ASSERT(inSuite, strcmp(testStr, allocatedStr) == 0); + ASSERT_NE(allocatedStr, nullptr); + + EXPECT_STREQ(testStr, allocatedStr); MemoryFree(allocatedStr); } -static void TestScopedBuffer(nlTestSuite * inSuite, void * inContext) +TEST_F(TestCHIPMemString, TestScopedBuffer) { // Scoped buffer has its own tests that check the memory properly. Here we are just testing that the string is copied in // properly. static constexpr char testStr[] = "testytestString"; ScopedMemoryString scopedString = ScopedMemoryString(testStr, sizeof(testStr)); - NL_TEST_ASSERT(inSuite, strcmp(scopedString.Get(), testStr) == 0); -} - -static const nlTest sTests[] = { NL_TEST_DEF("Test CopyString", TestCopyString), - NL_TEST_DEF("Test MemoryAllocString", TestMemoryAllocString), - NL_TEST_DEF("Test ScopedBuffer", TestScopedBuffer), NL_TEST_SENTINEL() }; - -int TestMemString_Setup(void * inContext) -{ - CHIP_ERROR error = MemoryInit(); - if (error != CHIP_NO_ERROR) - return (FAILURE); - return (SUCCESS); -} - -/** - * Tear down the test suite. - */ -int TestMemString_Teardown(void * inContext) -{ - MemoryShutdown(); - return (SUCCESS); + EXPECT_STREQ(scopedString.Get(), testStr); } - -int TestMemString() -{ - nlTestSuite theSuite = { "CHIP Memory Allocation tests", &sTests[0], TestMemString_Setup, TestMemString_Teardown }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestMemString) diff --git a/src/lib/support/tests/TestIntrusiveList.cpp b/src/lib/support/tests/TestIntrusiveList.cpp index 0d1779b00ac987..50ace6a561e699 100644 --- a/src/lib/support/tests/TestIntrusiveList.cpp +++ b/src/lib/support/tests/TestIntrusiveList.cpp @@ -18,20 +18,30 @@ #include #include -#include -#include +#include -#include +#include namespace { using namespace chip; +class TestIntrusiveList : public ::testing::Test +{ +public: + static void SetUpTestSuite() + { + unsigned seed = static_cast(std::time(nullptr)); + printf("Running " __FILE__ " using seed %d \n", seed); + std::srand(seed); + } +}; + class ListNode : public IntrusiveListNodeBase<> { }; -void TestIntrusiveListRandom(nlTestSuite * inSuite, void * inContext) +TEST_F(TestIntrusiveList, TestIntrusiveListRandom) { IntrusiveList l1; ListNode node[100]; @@ -86,9 +96,8 @@ void TestIntrusiveListRandom(nlTestSuite * inSuite, void * inContext) break; } - NL_TEST_ASSERT(inSuite, - std::equal(l1.begin(), l1.end(), l2.begin(), l2.end(), - [](const ListNode & p1, const ListNode * p2) { return &p1 == p2; })); + EXPECT_TRUE(std::equal(l1.begin(), l1.end(), l2.begin(), l2.end(), + [](const ListNode & p1, const ListNode * p2) { return &p1 == p2; })); } while (!l1.Empty()) @@ -97,85 +106,85 @@ void TestIntrusiveListRandom(nlTestSuite * inSuite, void * inContext) } } -void TestContains(nlTestSuite * inSuite, void * inContext) +TEST_F(TestIntrusiveList, TestContains) { ListNode a, b, c; IntrusiveList list; - NL_TEST_ASSERT(inSuite, !list.Contains(&a)); - NL_TEST_ASSERT(inSuite, !list.Contains(&b)); - NL_TEST_ASSERT(inSuite, !list.Contains(&c)); + EXPECT_FALSE(list.Contains(&a)); + EXPECT_FALSE(list.Contains(&b)); + EXPECT_FALSE(list.Contains(&c)); list.PushBack(&a); list.PushFront(&c); - NL_TEST_ASSERT(inSuite, list.Contains(&a)); - NL_TEST_ASSERT(inSuite, !list.Contains(&b)); - NL_TEST_ASSERT(inSuite, list.Contains(&c)); + EXPECT_TRUE(list.Contains(&a)); + EXPECT_FALSE(list.Contains(&b)); + EXPECT_TRUE(list.Contains(&c)); list.PushBack(&b); - NL_TEST_ASSERT(inSuite, list.Contains(&a)); - NL_TEST_ASSERT(inSuite, list.Contains(&b)); - NL_TEST_ASSERT(inSuite, list.Contains(&c)); + EXPECT_TRUE(list.Contains(&a)); + EXPECT_TRUE(list.Contains(&b)); + EXPECT_TRUE(list.Contains(&c)); list.Remove(&a); list.Remove(&c); - NL_TEST_ASSERT(inSuite, !list.Contains(&a)); - NL_TEST_ASSERT(inSuite, list.Contains(&b)); - NL_TEST_ASSERT(inSuite, !list.Contains(&c)); + EXPECT_FALSE(list.Contains(&a)); + EXPECT_TRUE(list.Contains(&b)); + EXPECT_FALSE(list.Contains(&c)); // all nodes have to be removed from the list on destruction. Lists do NOT do // this automatically list.Remove(&b); } -void TestClear(nlTestSuite * inSuite, void * inContext) +TEST_F(TestIntrusiveList, TestClear) { ListNode a, b, c; IntrusiveList list; - NL_TEST_ASSERT(inSuite, !list.Contains(&a)); - NL_TEST_ASSERT(inSuite, !list.Contains(&b)); - NL_TEST_ASSERT(inSuite, !list.Contains(&c)); + EXPECT_FALSE(list.Contains(&a)); + EXPECT_FALSE(list.Contains(&b)); + EXPECT_FALSE(list.Contains(&c)); list.PushBack(&a); list.PushFront(&c); - NL_TEST_ASSERT(inSuite, list.Contains(&a)); - NL_TEST_ASSERT(inSuite, !list.Contains(&b)); - NL_TEST_ASSERT(inSuite, list.Contains(&c)); + EXPECT_TRUE(list.Contains(&a)); + EXPECT_FALSE(list.Contains(&b)); + EXPECT_TRUE(list.Contains(&c)); list.PushBack(&b); - NL_TEST_ASSERT(inSuite, list.Contains(&a)); - NL_TEST_ASSERT(inSuite, list.Contains(&b)); - NL_TEST_ASSERT(inSuite, list.Contains(&c)); + EXPECT_TRUE(list.Contains(&a)); + EXPECT_TRUE(list.Contains(&b)); + EXPECT_TRUE(list.Contains(&c)); list.Clear(); - NL_TEST_ASSERT(inSuite, !list.Contains(&a)); - NL_TEST_ASSERT(inSuite, !list.Contains(&b)); - NL_TEST_ASSERT(inSuite, !list.Contains(&c)); + EXPECT_FALSE(list.Contains(&a)); + EXPECT_FALSE(list.Contains(&b)); + EXPECT_FALSE(list.Contains(&c)); } -void TestReplaceNode(nlTestSuite * inSuite, void * inContext) +TEST_F(TestIntrusiveList, TestReplaceNode) { ListNode a, b; IntrusiveList list; list.PushBack(&a); list.Replace(&a, &b); - NL_TEST_ASSERT(inSuite, !a.IsInList()); - NL_TEST_ASSERT(inSuite, b.IsInList()); - NL_TEST_ASSERT(inSuite, !list.Empty()); - NL_TEST_ASSERT(inSuite, !list.Contains(&a)); - NL_TEST_ASSERT(inSuite, list.Contains(&b)); + EXPECT_FALSE(a.IsInList()); + EXPECT_TRUE(b.IsInList()); + EXPECT_FALSE(list.Empty()); + EXPECT_FALSE(list.Contains(&a)); + EXPECT_TRUE(list.Contains(&b)); list.Remove(&b); } -void TestMoveList(nlTestSuite * inSuite, void * inContext) +TEST_F(TestIntrusiveList, TestMoveList) { ListNode a, b; @@ -183,8 +192,8 @@ void TestMoveList(nlTestSuite * inSuite, void * inContext) // Test case 1: Move construct an empty list IntrusiveList listA; IntrusiveList listB(std::move(listA)); - NL_TEST_ASSERT(inSuite, listA.Empty()); // NOLINT(bugprone-use-after-move) - NL_TEST_ASSERT(inSuite, listB.Empty()); + EXPECT_TRUE(listA.Empty()); // NOLINT(bugprone-use-after-move) + EXPECT_TRUE(listB.Empty()); } { @@ -193,8 +202,8 @@ void TestMoveList(nlTestSuite * inSuite, void * inContext) listA.PushBack(&a); IntrusiveList listB(std::move(listA)); - NL_TEST_ASSERT(inSuite, listA.Empty()); // NOLINT(bugprone-use-after-move) - NL_TEST_ASSERT(inSuite, listB.Contains(&a)); + EXPECT_TRUE(listA.Empty()); // NOLINT(bugprone-use-after-move) + EXPECT_TRUE(listB.Contains(&a)); listB.Remove(&a); } @@ -203,8 +212,8 @@ void TestMoveList(nlTestSuite * inSuite, void * inContext) IntrusiveList listA; IntrusiveList listB; listB = std::move(listA); - NL_TEST_ASSERT(inSuite, listA.Empty()); // NOLINT(bugprone-use-after-move) - NL_TEST_ASSERT(inSuite, listB.Empty()); + EXPECT_TRUE(listA.Empty()); // NOLINT(bugprone-use-after-move) + EXPECT_TRUE(listB.Empty()); } { @@ -214,8 +223,8 @@ void TestMoveList(nlTestSuite * inSuite, void * inContext) IntrusiveList listB; listB = std::move(listA); - NL_TEST_ASSERT(inSuite, listA.Empty()); // NOLINT(bugprone-use-after-move) - NL_TEST_ASSERT(inSuite, listB.Contains(&a)); + EXPECT_TRUE(listA.Empty()); // NOLINT(bugprone-use-after-move) + EXPECT_TRUE(listB.Contains(&a)); listB.Remove(&a); } } @@ -224,68 +233,29 @@ class ListNodeAutoUnlink : public IntrusiveListNodeBase list; // Test case 1: Test node->Unlink() { ListNodeAutoUnlink a; - NL_TEST_ASSERT(inSuite, !list.Contains(&a)); + EXPECT_FALSE(list.Contains(&a)); list.PushBack(&a); - NL_TEST_ASSERT(inSuite, list.Contains(&a)); + EXPECT_TRUE(list.Contains(&a)); a.Unlink(); - NL_TEST_ASSERT(inSuite, !list.Contains(&a)); - NL_TEST_ASSERT(inSuite, list.Empty()); + EXPECT_FALSE(list.Contains(&a)); + EXPECT_TRUE(list.Empty()); } // Test case 2: The node is automatically removed when goes out of scope { ListNodeAutoUnlink a; - NL_TEST_ASSERT(inSuite, !list.Contains(&a)); + EXPECT_FALSE(list.Contains(&a)); list.PushBack(&a); - NL_TEST_ASSERT(inSuite, list.Contains(&a)); + EXPECT_TRUE(list.Contains(&a)); } - NL_TEST_ASSERT(inSuite, list.Empty()); -} - -int Setup(void * inContext) -{ - return SUCCESS; -} - -int Teardown(void * inContext) -{ - return SUCCESS; + EXPECT_TRUE(list.Empty()); } } // namespace - -#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(TestIntrusiveListRandom), // - NL_TEST_DEF_FN(TestContains), // - NL_TEST_DEF_FN(TestReplaceNode), // - NL_TEST_DEF_FN(TestMoveList), // - NL_TEST_DEF_FN(TestAutoUnlink), // - NL_TEST_DEF_FN(TestClear), // - NL_TEST_SENTINEL(), // -}; - -int TestIntrusiveList() -{ - nlTestSuite theSuite = { "CHIP IntrusiveList tests", &sTests[0], Setup, Teardown }; - - unsigned seed = static_cast(std::time(nullptr)); - printf("Running " __FILE__ " using seed %d", seed); - std::srand(seed); - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestIntrusiveList); diff --git a/src/lib/support/tests/TestJsonToTlv.cpp b/src/lib/support/tests/TestJsonToTlv.cpp index a53144712c2770..9bc8696fd88621 100644 --- a/src/lib/support/tests/TestJsonToTlv.cpp +++ b/src/lib/support/tests/TestJsonToTlv.cpp @@ -15,19 +15,18 @@ * limitations under the License. */ +#include + +#include + #include #include #include #include #include -#include #include #include #include -#include - -#include - namespace { using namespace chip::Encoding; @@ -40,7 +39,13 @@ uint8_t gBuf1[1024]; uint8_t gBuf2[1024]; TLV::TLVWriter gWriter1; TLV::TLVWriter gWriter2; -nlTestSuite * gSuite; + +class TestJsonToTlv : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } +}; void SetupWriters() { @@ -87,29 +92,27 @@ void ConvertJsonToTlvAndValidate(T val, const std::string & jsonString) SetupWriters(); err = gWriter1.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = DataModel::Encode(gWriter1, TLV::ContextTag(1), val); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = gWriter1.EndContainer(container); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = gWriter1.Finalize(); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = JsonToTlv(jsonString, gWriter2); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); - NL_TEST_ASSERT(gSuite, MatchWriter1and2()); + EXPECT_TRUE(MatchWriter1and2()); } -void TestConverter(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlv, TestConverter) { std::string jsonString; - gSuite = inSuite; - jsonString = "{\n" " \"1:UINT\" : 30\n" "}\n"; @@ -239,7 +242,7 @@ void TestConverter(nlTestSuite * inSuite, void * inContext) ConvertJsonToTlvAndValidate(structList, jsonString); } -void Test32BitConvert(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlv, Test32BitConvert) { // JSON TLV format explicitly wants to support 32-bit integer preservation. // @@ -258,69 +261,69 @@ void Test32BitConvert(nlTestSuite * inSuite, void * inContext) { SetupWriters(); JsonToTlv("{\"1:INT\": 321}", gWriter1); - NL_TEST_ASSERT(inSuite, gWriter1.Finalize() == CHIP_NO_ERROR); + EXPECT_EQ(gWriter1.Finalize(), CHIP_NO_ERROR); reader.Init(gBuf1, gWriter1.GetLengthWritten()); reader.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(inSuite, reader.Next(TLV::AnonymousTag()) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_Structure); - NL_TEST_ASSERT(inSuite, reader.EnterContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next(TLV::ContextTag(1)) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_SignedInteger); - NL_TEST_ASSERT(inSuite, reader.Get(value) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 321); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); - NL_TEST_ASSERT(inSuite, reader.ExitContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); + EXPECT_EQ(reader.Next(TLV::AnonymousTag()), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_Structure); + EXPECT_EQ(reader.EnterContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(TLV::ContextTag(1)), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_SignedInteger); + EXPECT_EQ(reader.Get(value), CHIP_NO_ERROR); + EXPECT_EQ(value, 321); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); + EXPECT_EQ(reader.ExitContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); } // convert a single value that is larger than 8 bit { SetupWriters(); JsonToTlv("{\"1234:INT\": 321}", gWriter1); - NL_TEST_ASSERT(inSuite, gWriter1.Finalize() == CHIP_NO_ERROR); + EXPECT_EQ(gWriter1.Finalize(), CHIP_NO_ERROR); reader.Init(gBuf1, gWriter1.GetLengthWritten()); reader.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(inSuite, reader.Next(TLV::AnonymousTag()) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_Structure); - NL_TEST_ASSERT(inSuite, reader.EnterContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next(TLV::ProfileTag(kImplicitProfileId, 1234)) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_SignedInteger); - NL_TEST_ASSERT(inSuite, reader.Get(value) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 321); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); - NL_TEST_ASSERT(inSuite, reader.ExitContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); + EXPECT_EQ(reader.Next(TLV::AnonymousTag()), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_Structure); + EXPECT_EQ(reader.EnterContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(TLV::ProfileTag(kImplicitProfileId, 1234)), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_SignedInteger); + EXPECT_EQ(reader.Get(value), CHIP_NO_ERROR); + EXPECT_EQ(value, 321); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); + EXPECT_EQ(reader.ExitContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); } // Convert to a full 32-bit value, unsigned { SetupWriters(); JsonToTlv("{\"4275878552:INT\": 321}", gWriter1); - NL_TEST_ASSERT(inSuite, gWriter1.Finalize() == CHIP_NO_ERROR); + EXPECT_EQ(gWriter1.Finalize(), CHIP_NO_ERROR); reader.Init(gBuf1, gWriter1.GetLengthWritten()); reader.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(inSuite, reader.Next(TLV::AnonymousTag()) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_Structure); - NL_TEST_ASSERT(inSuite, reader.EnterContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next(TLV::ProfileTag((4275878552 >> 16) & 0xFFFF, 0, 4275878552 & 0xFFFF)) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_SignedInteger); - NL_TEST_ASSERT(inSuite, reader.Get(value) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 321); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); - NL_TEST_ASSERT(inSuite, reader.ExitContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); + EXPECT_EQ(reader.Next(TLV::AnonymousTag()), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_Structure); + EXPECT_EQ(reader.EnterContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(TLV::ProfileTag((4275878552 >> 16) & 0xFFFF, 0, 4275878552 & 0xFFFF)), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_SignedInteger); + EXPECT_EQ(reader.Get(value), CHIP_NO_ERROR); + EXPECT_EQ(value, 321); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); + EXPECT_EQ(reader.ExitContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); } // FIXME: implement } -void TestMEIConvert(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlv, TestMEIConvert) { TLV::TLVReader reader; TLV::TLVType tlvType; @@ -330,95 +333,63 @@ void TestMEIConvert(nlTestSuite * inSuite, void * inContext) { SetupWriters(); JsonToTlv("{\"65536:INT\": 321}", gWriter1); - NL_TEST_ASSERT(inSuite, gWriter1.Finalize() == CHIP_NO_ERROR); + EXPECT_EQ(gWriter1.Finalize(), CHIP_NO_ERROR); reader.Init(gBuf1, gWriter1.GetLengthWritten()); reader.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(inSuite, reader.Next(TLV::AnonymousTag()) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_Structure); - NL_TEST_ASSERT(inSuite, reader.EnterContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next(TLV::ProfileTag(1, 0, 0)) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_SignedInteger); - NL_TEST_ASSERT(inSuite, reader.Get(value) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 321); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); - NL_TEST_ASSERT(inSuite, reader.ExitContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); + EXPECT_EQ(reader.Next(TLV::AnonymousTag()), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_Structure); + EXPECT_EQ(reader.EnterContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(TLV::ProfileTag(1, 0, 0)), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_SignedInteger); + EXPECT_EQ(reader.Get(value), CHIP_NO_ERROR); + EXPECT_EQ(value, 321); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); + EXPECT_EQ(reader.ExitContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); } // Vendor ID = 0xFFFF, Tag ID = 0 { SetupWriters(); JsonToTlv("{\"4294901760:INT\": 123}", gWriter1); - NL_TEST_ASSERT(inSuite, gWriter1.Finalize() == CHIP_NO_ERROR); + EXPECT_EQ(gWriter1.Finalize(), CHIP_NO_ERROR); reader.Init(gBuf1, gWriter1.GetLengthWritten()); reader.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(inSuite, reader.Next(TLV::AnonymousTag()) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_Structure); - NL_TEST_ASSERT(inSuite, reader.EnterContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next(TLV::ProfileTag(0xFFFF, 0, 0)) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_SignedInteger); - NL_TEST_ASSERT(inSuite, reader.Get(value) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 123); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); - NL_TEST_ASSERT(inSuite, reader.ExitContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); + EXPECT_EQ(reader.Next(TLV::AnonymousTag()), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_Structure); + EXPECT_EQ(reader.EnterContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(TLV::ProfileTag(0xFFFF, 0, 0)), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_SignedInteger); + EXPECT_EQ(reader.Get(value), CHIP_NO_ERROR); + EXPECT_EQ(value, 123); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); + EXPECT_EQ(reader.ExitContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); } // Vendor ID = 0xFFFF, Tag ID = 0xFFFF { SetupWriters(); JsonToTlv("{\"4294967295:INT\": 123}", gWriter1); - NL_TEST_ASSERT(inSuite, gWriter1.Finalize() == CHIP_NO_ERROR); + EXPECT_EQ(gWriter1.Finalize(), CHIP_NO_ERROR); reader.Init(gBuf1, gWriter1.GetLengthWritten()); reader.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(inSuite, reader.Next(TLV::AnonymousTag()) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_Structure); - NL_TEST_ASSERT(inSuite, reader.EnterContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next(TLV::ProfileTag(0xFFFF, 0, 0xFFFF)) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.GetType() == TLV::kTLVType_SignedInteger); - NL_TEST_ASSERT(inSuite, reader.Get(value) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 123); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); - NL_TEST_ASSERT(inSuite, reader.ExitContainer(tlvType) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.Next() == CHIP_END_OF_TLV); + EXPECT_EQ(reader.Next(TLV::AnonymousTag()), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_Structure); + EXPECT_EQ(reader.EnterContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(TLV::ProfileTag(0xFFFF, 0, 0xFFFF)), CHIP_NO_ERROR); + EXPECT_EQ(reader.GetType(), TLV::kTLVType_SignedInteger); + EXPECT_EQ(reader.Get(value), CHIP_NO_ERROR); + EXPECT_EQ(value, 123); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); + EXPECT_EQ(reader.ExitContainer(tlvType), CHIP_NO_ERROR); + EXPECT_EQ(reader.Next(), CHIP_END_OF_TLV); } } - -int Initialize(void * apSuite) -{ - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - return SUCCESS; -} - -int Finalize(void * aContext) -{ - chip::Platform::MemoryShutdown(); - return SUCCESS; -} - -// clang-format off -const nlTest sTests[] = -{ - NL_TEST_DEF("TestConverter", TestConverter), - NL_TEST_DEF("Test32BitConvert", Test32BitConvert), - NL_TEST_DEF("TestMEIConvert", TestMEIConvert), - NL_TEST_SENTINEL() -}; -// clang-format on - } // namespace - -int TestJsonToTlv() -{ - nlTestSuite theSuite = { "JsonToTlv", sTests, Initialize, Finalize }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestJsonToTlv) diff --git a/src/lib/support/tests/TestJsonToTlvToJson.cpp b/src/lib/support/tests/TestJsonToTlvToJson.cpp index 2ac64670d3d596..665df4dcb30cab 100644 --- a/src/lib/support/tests/TestJsonToTlvToJson.cpp +++ b/src/lib/support/tests/TestJsonToTlvToJson.cpp @@ -16,17 +16,16 @@ */ #include +#include + +#include #include #include #include -#include #include #include #include -#include - -#include namespace { @@ -36,7 +35,12 @@ using namespace chip::app; constexpr uint32_t kImplicitProfileId = 0x1122; -nlTestSuite * gSuite; +class TestJsonToTlvToJson : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } +}; void PrintSpan(const char * prefix, const ByteSpan & span) { @@ -56,10 +60,10 @@ void CheckValidConversion(const std::string & jsonOriginal, const ByteSpan & tlv uint8_t buf[256]; MutableByteSpan tlvEncodingLocal(buf); err = JsonToTlv(jsonOriginal, tlvEncodingLocal); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); match = tlvEncodingLocal.data_equal(tlvEncoding); - NL_TEST_ASSERT(gSuite, match); + EXPECT_TRUE(match); if (!match) { printf("ERROR: TLV Encoding Doesn't Match!\n"); @@ -69,12 +73,12 @@ void CheckValidConversion(const std::string & jsonOriginal, const ByteSpan & tlv std::string generatedJsonString; err = TlvToJson(tlvEncoding, generatedJsonString); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); auto compactExpectedString = PrettyPrintJsonString(jsonExpected); auto compactGeneratedString = PrettyPrintJsonString(generatedJsonString); match = (compactGeneratedString == compactExpectedString); - NL_TEST_ASSERT(gSuite, match); + EXPECT_TRUE(match); if (!match) { printf("ERROR: Json String Doesn't Match!\n"); @@ -85,10 +89,10 @@ void CheckValidConversion(const std::string & jsonOriginal, const ByteSpan & tlv // Verify that Expected Json String Converts to the Same TLV Encoding tlvEncodingLocal = MutableByteSpan(buf); err = JsonToTlv(jsonOriginal, tlvEncodingLocal); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); match = tlvEncodingLocal.data_equal(tlvEncoding); - NL_TEST_ASSERT(gSuite, match); + EXPECT_TRUE(match); if (!match) { printf("ERROR: TLV Encoding Doesn't Match!\n"); @@ -98,19 +102,18 @@ void CheckValidConversion(const std::string & jsonOriginal, const ByteSpan & tlv } // Boolean true -void TestConverter_Boolean_True(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Boolean_True) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1:BOOL\" : true\n" @@ -121,19 +124,18 @@ void TestConverter_Boolean_True(nlTestSuite * inSuite, void * inContext) } // Signed Integer 42, 1-octet -void TestConverter_SignedInt_1BytePositive(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_SignedInt_1BytePositive) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(2), static_cast(42))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(2), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"2:INT\" : 42\n" @@ -144,19 +146,18 @@ void TestConverter_SignedInt_1BytePositive(nlTestSuite * inSuite, void * inConte } // Signed Integer -17, 1-octet -void TestConverter_SignedInt_1ByteNegative(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_SignedInt_1ByteNegative) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(3), static_cast(-17))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(3), static_cast(-17))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"3:INT\" : -17\n" @@ -167,19 +168,18 @@ void TestConverter_SignedInt_1ByteNegative(nlTestSuite * inSuite, void * inConte } // Unsigned Integer 42, 1-octet -void TestConverter_UnsignedInt_1Byte(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_UnsignedInt_1Byte) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(4), static_cast(42))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(4), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"value:4:UINT\" : 42\n" @@ -194,19 +194,18 @@ void TestConverter_UnsignedInt_1Byte(nlTestSuite * inSuite, void * inContext) } // Signed Integer 4242, 2-octet -void TestConverter_SignedInt_2Bytes(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_SignedInt_2Bytes) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(7), static_cast(4242))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(7), static_cast(4242))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"7:INT\" : 4242\n" @@ -217,19 +216,18 @@ void TestConverter_SignedInt_2Bytes(nlTestSuite * inSuite, void * inContext) } // Signed Integer -170000, 4-octet -void TestConverter_SignedInt_4Bytes(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_SignedInt_4Bytes) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(80), static_cast(-170000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(80), static_cast(-170000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"80:INT\" : -170000\n" @@ -240,19 +238,18 @@ void TestConverter_SignedInt_4Bytes(nlTestSuite * inSuite, void * inContext) } // Signed Long Integer (int64_t) 40000000000, 8-octet -void TestConverter_SignedInt_8Bytes(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_SignedInt_8Bytes) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(202), static_cast(40000000000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(202), static_cast(40000000000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"202:INT\" : \"40000000000\"\n" @@ -263,19 +260,18 @@ void TestConverter_SignedInt_8Bytes(nlTestSuite * inSuite, void * inContext) } // Unsigned Long Integer (uint64_t) 40000000000, 8-octet -void TestConverter_UnsignedInt_8Bytes(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_UnsignedInt_8Bytes) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(222), static_cast(40000000000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(222), static_cast(40000000000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"222:UINT\" : \"40000000000\"\n" @@ -286,19 +282,18 @@ void TestConverter_UnsignedInt_8Bytes(nlTestSuite * inSuite, void * inContext) } // UTF-8 String, 1-octet length, "Hello!" -void TestConverter_UTF8String_Hello(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_UTF8String_Hello) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::ContextTag(0), "Hello!")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::ContextTag(0), "Hello!")); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:STRING\" : \"Hello!\"\n" @@ -309,9 +304,8 @@ void TestConverter_UTF8String_Hello(nlTestSuite * inSuite, void * inContext) } // Octet String, 1-octet length, octets { 00 01 02 03 04 } -void TestConverter_OctetString(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_OctetString) { - gSuite = inSuite; uint8_t v[] = { 0, 1, 2, 3, 4 }; uint8_t buf[256]; @@ -319,10 +313,10 @@ void TestConverter_OctetString(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutBytes(TLV::ContextTag(1), v, sizeof(v))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutBytes(TLV::ContextTag(1), v, sizeof(v))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1:BYTES\" : \"AAECAwQ=\"\n" @@ -333,19 +327,18 @@ void TestConverter_OctetString(nlTestSuite * inSuite, void * inContext) } // Octet String, empty -void TestConverter_OctetString_Empty(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_OctetString_Empty) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutBytes(TLV::ContextTag(1), nullptr, 0)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutBytes(TLV::ContextTag(1), nullptr, 0)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1:BYTES\" : \"\"\n" @@ -356,19 +349,18 @@ void TestConverter_OctetString_Empty(nlTestSuite * inSuite, void * inContext) } // Null -void TestConverter_Null(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Null) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutNull(TLV::ContextTag(1))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutNull(TLV::ContextTag(1))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1:NULL\" : null\n" @@ -379,19 +371,18 @@ void TestConverter_Null(nlTestSuite * inSuite, void * inContext) } // Single precision floating point 0.0 -void TestConverter_Float_0(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Float_0) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), static_cast(0.0))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), static_cast(0.0))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1:FLOAT\" : 0.0\n" @@ -402,19 +393,18 @@ void TestConverter_Float_0(nlTestSuite * inSuite, void * inContext) } // Single precision floating point (1.0 / 3.0) -void TestConverter_Float_1third(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Float_1third) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(100), static_cast(1.0 / 3.0))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(100), static_cast(1.0 / 3.0))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"100:FLOAT\" : 0.33333334\n" @@ -428,19 +418,18 @@ void TestConverter_Float_1third(nlTestSuite * inSuite, void * inContext) } // Single precision floating point 17.9 -void TestConverter_Float_17_9(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Float_17_9) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(101), static_cast(17.9))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(101), static_cast(17.9))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"101:FLOAT\" : 17.9\n" @@ -454,19 +443,18 @@ void TestConverter_Float_17_9(nlTestSuite * inSuite, void * inContext) } // Single precision floating point positive infinity -void TestConverter_Float_PositiveInfinity(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Float_PositiveInfinity) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(101), std::numeric_limits::infinity())); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(101), std::numeric_limits::infinity())); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"101:FLOAT\" : \"Infinity\"\n" @@ -477,19 +465,18 @@ void TestConverter_Float_PositiveInfinity(nlTestSuite * inSuite, void * inContex } // Single precision floating point negative infinity -void TestConverter_Float_NegativeInfinity(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Float_NegativeInfinity) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(101), -std::numeric_limits::infinity())); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(101), -std::numeric_limits::infinity())); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"101:FLOAT\" : \"-Infinity\"\n" @@ -500,19 +487,18 @@ void TestConverter_Float_NegativeInfinity(nlTestSuite * inSuite, void * inContex } // Double precision floating point 0.0 -void TestConverter_Double_0(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Double_0) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), static_cast(0.0))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), static_cast(0.0))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1:DOUBLE\" : 0.0\n" @@ -523,19 +509,18 @@ void TestConverter_Double_0(nlTestSuite * inSuite, void * inContext) } // Double precision floating point (1.0 / 3.0) -void TestConverter_Double_1third(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Double_1third) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(100), static_cast(1.0 / 3.0))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(100), static_cast(1.0 / 3.0))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"100:DOUBLE\" : 0.33333333333333331\n" @@ -546,19 +531,18 @@ void TestConverter_Double_1third(nlTestSuite * inSuite, void * inContext) } // Double precision floating point 17.9 -void TestConverter_Double_17_9(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Double_17_9) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(101), static_cast(17.9))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(101), static_cast(17.9))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"101:DOUBLE\" : 17.899999999999999\n" @@ -569,19 +553,18 @@ void TestConverter_Double_17_9(nlTestSuite * inSuite, void * inContext) } // Double precision floating point positive infinity -void TestConverter_Double_PositiveInfinity(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Double_PositiveInfinity) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(101), std::numeric_limits::infinity())); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(101), std::numeric_limits::infinity())); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"101:DOUBLE\" : \"Infinity\"\n" @@ -592,19 +575,18 @@ void TestConverter_Double_PositiveInfinity(nlTestSuite * inSuite, void * inConte } // Double precision floating point negative infinity -void TestConverter_Double_NegativeInfinity(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Double_NegativeInfinity) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(101), -std::numeric_limits::infinity())); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(101), -std::numeric_limits::infinity())); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"101:DOUBLE\" : \"-Infinity\"\n" @@ -615,18 +597,17 @@ void TestConverter_Double_NegativeInfinity(nlTestSuite * inSuite, void * inConte } // Empty Top-Level Structure, {} -void TestConverter_Structure_TopLevelEmpty(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Structure_TopLevelEmpty) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{ }"; @@ -635,9 +616,8 @@ void TestConverter_Structure_TopLevelEmpty(nlTestSuite * inSuite, void * inConte } // Empty Nested Structure, { {} } -void TestConverter_Structure_NestedEmpty(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Structure_NestedEmpty) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -645,11 +625,11 @@ void TestConverter_Structure_NestedEmpty(nlTestSuite * inSuite, void * inContext TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_Structure, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_Structure, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1:STRUCT\" : { }\n" @@ -660,9 +640,8 @@ void TestConverter_Structure_NestedEmpty(nlTestSuite * inSuite, void * inContext } // Empty Array, { [] } -void TestConverter_Array_Empty(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Empty) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -670,11 +649,11 @@ void TestConverter_Array_Empty(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1:ARRAY-?\" : []\n" @@ -684,9 +663,8 @@ void TestConverter_Array_Empty(nlTestSuite * inSuite, void * inContext) CheckValidConversion(jsonString, tlvSpan, jsonString); } -void TestConverter_Array_Empty_ImplicitProfileTag2(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Empty_ImplicitProfileTag2) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -697,13 +675,12 @@ void TestConverter_Array_Empty_ImplicitProfileTag2(nlTestSuite * inSuite, void * writer.Init(buf); writer.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, - CHIP_NO_ERROR == - writer.StartContainer(TLV::ProfileTag(kImplicitProfileId, 10000), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, + writer.StartContainer(TLV::ProfileTag(kImplicitProfileId, 10000), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"emptyarray:10000:ARRAY-?\" : []\n" @@ -716,9 +693,8 @@ void TestConverter_Array_Empty_ImplicitProfileTag2(nlTestSuite * inSuite, void * CheckValidConversion(jsonString, tlvSpan, jsonExpected); } -void TestConverter_Array_Empty_ImplicitProfileTag4(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Empty_ImplicitProfileTag4) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -729,14 +705,13 @@ void TestConverter_Array_Empty_ImplicitProfileTag4(nlTestSuite * inSuite, void * writer.Init(buf); writer.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, - CHIP_NO_ERROR == - writer.StartContainer(TLV::ProfileTag((1000000 >> 16) & 0xFFFF, 0, 1000000 & 0xFFFF), TLV::kTLVType_Array, - containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ( + CHIP_NO_ERROR, + writer.StartContainer(TLV::ProfileTag((1000000 >> 16) & 0xFFFF, 0, 1000000 & 0xFFFF), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1000000:ARRAY-?\" : []\n" @@ -747,20 +722,19 @@ void TestConverter_Array_Empty_ImplicitProfileTag4(nlTestSuite * inSuite, void * } // Two Signed Integers with context specific tags: {0 = 42, 1 = -17} -void TestConverter_IntsWithContextTags(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_IntsWithContextTags) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; TLV::TLVType containerType; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(0), static_cast(42))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), static_cast(-17))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(0), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), static_cast(-17))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:INT\" : 42,\n" @@ -772,9 +746,8 @@ void TestConverter_IntsWithContextTags(nlTestSuite * inSuite, void * inContext) } // Structure with Two Signed Integers with context specific tags: { {0 = 42, 1 = -17} } -void TestConverter_Struct_IntsWithContextTags(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Struct_IntsWithContextTags) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -782,13 +755,13 @@ void TestConverter_Struct_IntsWithContextTags(nlTestSuite * inSuite, void * inCo TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Structure, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(0), static_cast(42))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), static_cast(-17))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Structure, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(0), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), static_cast(-17))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:STRUCT\" : {\n" @@ -802,9 +775,8 @@ void TestConverter_Struct_IntsWithContextTags(nlTestSuite * inSuite, void * inCo } // Array of Signed Integers: { [0, 1, 2, 3, 4] } -void TestConverter_Array_Ints(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Ints) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -812,16 +784,16 @@ void TestConverter_Array_Ints(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(0))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(1))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(2))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(3))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(4))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(0))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(1))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(2))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(3))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(4))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:ARRAY-INT\" : [\n" @@ -838,9 +810,8 @@ void TestConverter_Array_Ints(nlTestSuite * inSuite, void * inContext) } // Array of Long Signed Integers: { [42, -17, -170000, 40000000000] } -void TestConverter_Array_Ints2(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Ints2) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -848,15 +819,15 @@ void TestConverter_Array_Ints2(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(42))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(-17))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(-170000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(40000000000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(-17))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(-170000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(40000000000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:ARRAY-INT\" : [\n" @@ -872,9 +843,8 @@ void TestConverter_Array_Ints2(nlTestSuite * inSuite, void * inContext) } // Array of Signed Integers with MIN/MAX values for each type int8_t/int16_t/int32_t/int64_t -void TestConverter_Array_IntsMinMax(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_IntsMinMax) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -882,19 +852,19 @@ void TestConverter_Array_IntsMinMax(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(INT8_MIN))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(INT8_MAX))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(INT16_MIN))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(INT16_MAX))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(INT32_MIN))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(INT32_MAX))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(INT64_MIN))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(INT64_MAX))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(INT8_MIN))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(INT8_MAX))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(INT16_MIN))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(INT16_MAX))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(INT32_MIN))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(INT32_MAX))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(INT64_MIN))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(INT64_MAX))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:ARRAY-INT\" : [\n" @@ -926,9 +896,8 @@ void TestConverter_Array_IntsMinMax(nlTestSuite * inSuite, void * inContext) } // Array of Long Unsigned Integers: { [42, 170000, 40000000000] } -void TestConverter_Array_UInts(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_UInts) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -936,14 +905,14 @@ void TestConverter_Array_UInts(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(42))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(170000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(40000000000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(170000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(40000000000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:ARRAY-UINT\" : [\n" @@ -959,9 +928,8 @@ void TestConverter_Array_UInts(nlTestSuite * inSuite, void * inContext) // Array of Unsigned Integers, where each element represents MAX possible value for unsigned // integere types uint8_t, uint16_t, uint32_t, uint64_t: [0xFF, 0xFFFF, 0xFFFFFFFF, 0xFFFFFFFF_FFFFFFFF] -void TestConverter_Array_UIntsMax(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_UIntsMax) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -969,15 +937,15 @@ void TestConverter_Array_UIntsMax(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(UINT8_MAX))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(UINT16_MAX))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(UINT32_MAX))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(UINT64_MAX))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(UINT8_MAX))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(UINT16_MAX))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(UINT32_MAX))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(UINT64_MAX))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:ARRAY-UINT\" : [\n" @@ -1001,9 +969,8 @@ void TestConverter_Array_UIntsMax(nlTestSuite * inSuite, void * inContext) } // Array of Doubles: { [1.1, 134.2763, -12345.87] } -void TestConverter_Array_Doubles(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Doubles) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1011,14 +978,14 @@ void TestConverter_Array_Doubles(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(1.1))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(134.2763))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(-12345.87))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(1.1))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(134.2763))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(-12345.87))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:ARRAY-DOUBLE\" : [\n" @@ -1040,9 +1007,8 @@ void TestConverter_Array_Doubles(nlTestSuite * inSuite, void * inContext) } // Array of Floats: { [1.1, 134.2763, -12345.87] } -void TestConverter_Array_Floats(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Floats) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1052,16 +1018,14 @@ void TestConverter_Array_Floats(nlTestSuite * inSuite, void * inContext) writer.Init(buf); writer.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, - CHIP_NO_ERROR == - writer.StartContainer(TLV::ProfileTag(kImplicitProfileId, 1000), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(1.1))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(134.2763))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(-12345.87))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ProfileTag(kImplicitProfileId, 1000), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(1.1))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(134.2763))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(-12345.87))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1000:ARRAY-FLOAT\" : [\n" @@ -1083,9 +1047,8 @@ void TestConverter_Array_Floats(nlTestSuite * inSuite, void * inContext) } // Array of Strings: ["ABC", "Options", "more"] -void TestConverter_Array_Strings(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Strings) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1095,17 +1058,16 @@ void TestConverter_Array_Strings(nlTestSuite * inSuite, void * inContext) writer.Init(buf); writer.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, - CHIP_NO_ERROR == - writer.StartContainer(TLV::ProfileTag((100000 >> 16) & 0xFFFF, 0, 100000 & 0xFFFF), TLV::kTLVType_Array, - containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::AnonymousTag(), "ABC")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::AnonymousTag(), "Options")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::AnonymousTag(), "more")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ( + CHIP_NO_ERROR, + writer.StartContainer(TLV::ProfileTag((100000 >> 16) & 0xFFFF, 0, 100000 & 0xFFFF), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::AnonymousTag(), "ABC")); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::AnonymousTag(), "Options")); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::AnonymousTag(), "more")); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"100000:ARRAY-STRING\" : [\n" @@ -1120,9 +1082,8 @@ void TestConverter_Array_Strings(nlTestSuite * inSuite, void * inContext) } // Array of Booleans: [true, false, false] -void TestConverter_Array_Booleans(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Booleans) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1130,14 +1091,14 @@ void TestConverter_Array_Booleans(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(255), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), false)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), false)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(255), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), false)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), false)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"255:ARRAY-BOOL\" : [\n" @@ -1152,9 +1113,8 @@ void TestConverter_Array_Booleans(nlTestSuite * inSuite, void * inContext) } // Array of Nulls: [null, null] -void TestConverter_Array_Nulls(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Nulls) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1162,13 +1122,13 @@ void TestConverter_Array_Nulls(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutNull(TLV::AnonymousTag())); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutNull(TLV::AnonymousTag())); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutNull(TLV::AnonymousTag())); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutNull(TLV::AnonymousTag())); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1:ARRAY-NULL\" : [\n" @@ -1182,9 +1142,8 @@ void TestConverter_Array_Nulls(nlTestSuite * inSuite, void * inContext) } // Context tag 255 (max), Unsigned Integer, 1-octet value: {255 = 42U} -void TestConverter_Struct_UInt(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Struct_UInt) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1192,12 +1151,12 @@ void TestConverter_Struct_UInt(nlTestSuite * inSuite, void * inContext) TLV::TLVType containerType2; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Structure, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(255), static_cast(42))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Structure, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(255), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"value:0:STRUCT\" : {\n" @@ -1216,9 +1175,8 @@ void TestConverter_Struct_UInt(nlTestSuite * inSuite, void * inContext) // Context and Common Profile tags, Unsigned Integer structure: {255 = 42, 256 = 17000, 65535 = // 1, 65536 = 345678, 4294967295 = 500000000000} -void TestConverter_Struct_MixedTags(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Struct_MixedTags) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1228,16 +1186,16 @@ void TestConverter_Struct_MixedTags(nlTestSuite * inSuite, void * inContext) writer.Init(buf); writer.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Structure, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(255), static_cast(42))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ProfileTag(0x0001u, 0, 0), static_cast(345678))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ProfileTag(kImplicitProfileId, 256), static_cast(17000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ProfileTag(0xFFFFu, 0, 0xFFFFu), static_cast(500000000000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ProfileTag(kImplicitProfileId, 65535), static_cast(1))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Structure, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(255), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ProfileTag(0x0001u, 0, 0), static_cast(345678))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ProfileTag(kImplicitProfileId, 256), static_cast(17000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ProfileTag(0xFFFFu, 0, 0xFFFFu), static_cast(500000000000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ProfileTag(kImplicitProfileId, 65535), static_cast(1))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:STRUCT\" : {\n" @@ -1254,9 +1212,8 @@ void TestConverter_Struct_MixedTags(nlTestSuite * inSuite, void * inContext) } // Structure with mixed elements -void TestConverter_Struct_MixedElements(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Struct_MixedElements) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1265,22 +1222,20 @@ void TestConverter_Struct_MixedElements(nlTestSuite * inSuite, void * inContext) char bytes[] = "Test ByteString Value"; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Structure, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(0), static_cast(20))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(2), static_cast(0))); - NL_TEST_ASSERT( - gSuite, - CHIP_NO_ERROR == - writer.PutBytes(TLV::ContextTag(3), reinterpret_cast(bytes), static_cast(strlen(bytes)))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::ContextTag(4), "hello")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(5), static_cast(-500000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(6), static_cast(17.9))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(7), static_cast(17.9))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Structure, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(0), static_cast(20))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(2), static_cast(0))); + EXPECT_EQ(CHIP_NO_ERROR, + writer.PutBytes(TLV::ContextTag(3), reinterpret_cast(bytes), static_cast(strlen(bytes)))); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::ContextTag(4), "hello")); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(5), static_cast(-500000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(6), static_cast(17.9))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(7), static_cast(17.9))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"c:0:STRUCT\" : {\n" @@ -1312,9 +1267,8 @@ void TestConverter_Struct_MixedElements(nlTestSuite * inSuite, void * inContext) } // Array of structures with mixed elements -void TestConverter_Array_Structures(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Array_Structures) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1327,39 +1281,33 @@ void TestConverter_Array_Structures(nlTestSuite * inSuite, void * inContext) writer.Init(buf); writer.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, - CHIP_NO_ERROR == - writer.StartContainer(TLV::ProfileTag(kImplicitProfileId, 1000), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(0), static_cast(20))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(2), static_cast(0))); - NL_TEST_ASSERT( - gSuite, - CHIP_NO_ERROR == - writer.PutBytes(TLV::ContextTag(3), reinterpret_cast(bytes1), static_cast(strlen(bytes1)))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::ContextTag(4), "hello1")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(5), static_cast(-500000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(6), static_cast(17.9))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(7), static_cast(17.9))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(0), static_cast(-10))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), false)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(2), static_cast(128))); - NL_TEST_ASSERT( - gSuite, - CHIP_NO_ERROR == - writer.PutBytes(TLV::ContextTag(3), reinterpret_cast(bytes2), static_cast(strlen(bytes2)))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::ContextTag(4), "hello2")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(5), static_cast(40000000000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(6), static_cast(-1754.923))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(7), static_cast(97.945))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ProfileTag(kImplicitProfileId, 1000), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(0), static_cast(20))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(2), static_cast(0))); + EXPECT_EQ(CHIP_NO_ERROR, + writer.PutBytes(TLV::ContextTag(3), reinterpret_cast(bytes1), static_cast(strlen(bytes1)))); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::ContextTag(4), "hello1")); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(5), static_cast(-500000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(6), static_cast(17.9))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(7), static_cast(17.9))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(0), static_cast(-10))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), false)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(2), static_cast(128))); + EXPECT_EQ(CHIP_NO_ERROR, + writer.PutBytes(TLV::ContextTag(3), reinterpret_cast(bytes2), static_cast(strlen(bytes2)))); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::ContextTag(4), "hello2")); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(5), static_cast(40000000000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(6), static_cast(-1754.923))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(7), static_cast(97.945))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"1000:ARRAY-STRUCT\": [\n" @@ -1415,9 +1363,8 @@ void TestConverter_Array_Structures(nlTestSuite * inSuite, void * inContext) } // Top level with mixed elements -void TestConverter_TopLevel_MixedElements(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_TopLevel_MixedElements) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1427,39 +1374,37 @@ void TestConverter_TopLevel_MixedElements(nlTestSuite * inSuite, void * inContex char bytes[] = "Test array member 0"; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(0), static_cast(42))); - NL_TEST_ASSERT( - gSuite, - CHIP_NO_ERROR == - writer.PutBytes(TLV::ContextTag(1), reinterpret_cast(bytes), static_cast(strlen(bytes)))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(2), static_cast(156.398))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(3), static_cast(73709551615))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(4), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutNull(TLV::ContextTag(5))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(6), TLV::kTLVType_Structure, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::ContextTag(1), "John")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(2), static_cast(34))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(3), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(4), TLV::kTLVType_Array, containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(5))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(9))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(10))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(5), TLV::kTLVType_Array, containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::AnonymousTag(), "Ammy")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::AnonymousTag(), "David")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::AnonymousTag(), "Larry")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(6), TLV::kTLVType_Array, containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), false)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(7), static_cast(0.0))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(0), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, + writer.PutBytes(TLV::ContextTag(1), reinterpret_cast(bytes), static_cast(strlen(bytes)))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(2), static_cast(156.398))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(3), static_cast(73709551615))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(4), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutNull(TLV::ContextTag(5))); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(6), TLV::kTLVType_Structure, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::ContextTag(1), "John")); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(2), static_cast(34))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(3), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(4), TLV::kTLVType_Array, containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(5))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(9))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(10))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(5), TLV::kTLVType_Array, containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::AnonymousTag(), "Ammy")); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::AnonymousTag(), "David")); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::AnonymousTag(), "Larry")); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(6), TLV::kTLVType_Array, containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), false)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(7), static_cast(0.0))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"value:0:INT\": 42,\n" @@ -1525,9 +1470,8 @@ void TestConverter_TopLevel_MixedElements(nlTestSuite * inSuite, void * inContex } // Complex Structure from README -void TestConverter_Structure_FromReadme(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Structure_FromReadme) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1540,53 +1484,51 @@ void TestConverter_Structure_FromReadme(nlTestSuite * inSuite, void * inContext) char bytes4[] = "Test Bytes"; writer.Init(buf); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(0), static_cast(8))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_Structure, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(0), static_cast(12))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), false)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::ContextTag(2), "example")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(2), static_cast(40000000000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(3), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(4), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(5), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(1.1))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(134.2763))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(-12345.87))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), std::numeric_limits::infinity())); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(62534))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(-62534))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(6), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutBytes(TLV::AnonymousTag(), bytes1, static_cast(sizeof(bytes1)))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutBytes(TLV::AnonymousTag(), bytes2, static_cast(sizeof(bytes2)))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutBytes(TLV::AnonymousTag(), bytes3, static_cast(sizeof(bytes3)))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT( - gSuite, - CHIP_NO_ERROR == - writer.PutBytes(TLV::ContextTag(7), reinterpret_cast(bytes4), static_cast(strlen(bytes4)))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(8), static_cast(17.9))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(9), static_cast(17.9))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(10), -std::numeric_limits::infinity())); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(11), TLV::kTLVType_Structure, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.PutString(TLV::ContextTag(1), "John")); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(2), static_cast(34))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(3), true)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(4), TLV::kTLVType_Array, containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(5))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(9))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(10))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType3)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(0), static_cast(8))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_Structure, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(0), static_cast(12))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), false)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::ContextTag(2), "example")); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(2), static_cast(40000000000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(3), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(4), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(5), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(1.1))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(134.2763))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(-12345.87))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), std::numeric_limits::infinity())); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(62534))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(-62534))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(6), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutBytes(TLV::AnonymousTag(), bytes1, static_cast(sizeof(bytes1)))); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutBytes(TLV::AnonymousTag(), bytes2, static_cast(sizeof(bytes2)))); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutBytes(TLV::AnonymousTag(), bytes3, static_cast(sizeof(bytes3)))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, + writer.PutBytes(TLV::ContextTag(7), reinterpret_cast(bytes4), static_cast(strlen(bytes4)))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(8), static_cast(17.9))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(9), static_cast(17.9))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(10), -std::numeric_limits::infinity())); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(11), TLV::kTLVType_Structure, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.PutString(TLV::ContextTag(1), "John")); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(2), static_cast(34))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(3), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(4), TLV::kTLVType_Array, containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(5))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(9))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(10))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType3)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); std::string jsonString = "{\n" " \"0:ARRAY-STRUCT\" : [\n" @@ -1679,7 +1621,7 @@ void TestConverter_Structure_FromReadme(nlTestSuite * inSuite, void * inContext) CheckValidConversion(jsonString, tlvSpan, jsonExpected); } -void TestConverter_TlvToJson_ErrorCases(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_TlvToJson_ErrorCases) { CHIP_ERROR err; TLV::TLVWriter writer; @@ -1695,48 +1637,48 @@ void TestConverter_TlvToJson_ErrorCases(nlTestSuite * inSuite, void * inContext) uint8_t buf1[32]; writer.Init(buf1); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::CommonTag(1), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), true)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::CommonTag(1), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); ByteSpan topLevelStructWithTag(buf1, writer.GetLengthWritten()); uint8_t buf2[32]; writer.Init(buf2); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Array, containerType)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), true)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Array, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); ByteSpan topLevelIsArray(buf2, writer.GetLengthWritten()); uint8_t buf3[32]; writer.Init(buf3); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_List, containerType2)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Put(TLV::ContextTag(1), true)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_List, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ContextTag(1), true)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); ByteSpan usingList(buf3, writer.GetLengthWritten()); uint8_t buf8[32]; writer.Init(buf8); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_Array, containerType2)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(42))); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(-170000))); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Put(TLV::AnonymousTag(), static_cast(42456))); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(1), TLV::kTLVType_Array, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(-170000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::AnonymousTag(), static_cast(42456))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); ByteSpan arrayWithMixedElements(buf8, writer.GetLengthWritten()); uint8_t buf9[32]; writer.Init(buf9); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Put(TLV::ProfileTag(0xFEED, 234), static_cast(42))); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ProfileTag(0xFEED, 234), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); ByteSpan useFullyQualifiedTag(buf9, writer.GetLengthWritten()); // clang-format off @@ -1755,11 +1697,11 @@ void TestConverter_TlvToJson_ErrorCases(nlTestSuite * inSuite, void * inContext) { std::string jsonString; err = TlvToJson(testCase.nEncodedTlv, jsonString); - NL_TEST_ASSERT(inSuite, err == testCase.mExpectedResult); + EXPECT_EQ(err, testCase.mExpectedResult); } } -void TestConverter_JsonToTlv_ErrorCases(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_JsonToTlv_ErrorCases) { CHIP_ERROR err; @@ -1873,7 +1815,7 @@ void TestConverter_JsonToTlv_ErrorCases(nlTestSuite * inSuite, void * inContext) uint8_t buf[256]; MutableByteSpan tlvSpan(buf); err = JsonToTlv(testCase.mJsonString, tlvSpan); - NL_TEST_ASSERT(inSuite, err == testCase.mExpectedResult); + EXPECT_EQ(err, testCase.mExpectedResult); #if CHIP_CONFIG_ERROR_FORMAT_AS_STRING if (err != testCase.mExpectedResult) { @@ -1888,9 +1830,8 @@ void TestConverter_JsonToTlv_ErrorCases(nlTestSuite * inSuite, void * inContext) } // Full Qualified Profile tags, Unsigned Integer structure: {65536 = 42, 4294901760 = 17000, 4294967295 = 500000000000} -void TestConverter_Struct_MEITags(nlTestSuite * inSuite, void * inContext) +TEST_F(TestJsonToTlvToJson, TestConverter_Struct_MEITags) { - gSuite = inSuite; uint8_t buf[256]; TLV::TLVWriter writer; @@ -1900,14 +1841,14 @@ void TestConverter_Struct_MEITags(nlTestSuite * inSuite, void * inContext) writer.Init(buf); writer.ImplicitProfileId = kImplicitProfileId; - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Structure, containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ProfileTag(0xFFFFu, 0, 0), static_cast(17000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ProfileTag(0x0001u, 0, 0), static_cast(42))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Put(TLV::ProfileTag(0xFFFFu, 0, 0xFFFFu), static_cast(500000000000))); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType2)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.EndContainer(containerType)); - NL_TEST_ASSERT(gSuite, CHIP_NO_ERROR == writer.Finalize()); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.StartContainer(TLV::ContextTag(0), TLV::kTLVType_Structure, containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ProfileTag(0xFFFFu, 0, 0), static_cast(17000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ProfileTag(0x0001u, 0, 0), static_cast(42))); + EXPECT_EQ(CHIP_NO_ERROR, writer.Put(TLV::ProfileTag(0xFFFFu, 0, 0xFFFFu), static_cast(500000000000))); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType2)); + EXPECT_EQ(CHIP_NO_ERROR, writer.EndContainer(containerType)); + EXPECT_EQ(CHIP_NO_ERROR, writer.Finalize()); std::string jsonString = "{\n" " \"0:STRUCT\" : {\n" @@ -1920,84 +1861,4 @@ void TestConverter_Struct_MEITags(nlTestSuite * inSuite, void * inContext) ByteSpan tlvSpan(buf, writer.GetLengthWritten()); CheckValidConversion(jsonString, tlvSpan, jsonString); } - -int Initialize(void * apSuite) -{ - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - return SUCCESS; -} - -int Finalize(void * aContext) -{ - chip::Platform::MemoryShutdown(); - return SUCCESS; -} - -const nlTest sTests[] = { - NL_TEST_DEF("Test Json Tlv Converter - Boolean True", TestConverter_Boolean_True), - NL_TEST_DEF("Test Json Tlv Converter - Signed Integer 1-Byte Positive", TestConverter_SignedInt_1BytePositive), - NL_TEST_DEF("Test Json Tlv Converter - Signed Integer 1-Byte Negative", TestConverter_SignedInt_1ByteNegative), - NL_TEST_DEF("Test Json Tlv Converter - Unsigned Integer 1-Byte", TestConverter_UnsignedInt_1Byte), - NL_TEST_DEF("Test Json Tlv Converter - Signed Integer 2-Bytes", TestConverter_SignedInt_2Bytes), - NL_TEST_DEF("Test Json Tlv Converter - Signed Integer 4-Bytes", TestConverter_SignedInt_4Bytes), - NL_TEST_DEF("Test Json Tlv Converter - Signed Integer 8-Bytes", TestConverter_SignedInt_8Bytes), - NL_TEST_DEF("Test Json Tlv Converter - Unsigned Integer 8-Bytes", TestConverter_UnsignedInt_8Bytes), - NL_TEST_DEF("Test Json Tlv Converter - UTF-8 String Hello!", TestConverter_UTF8String_Hello), - NL_TEST_DEF("Test Json Tlv Converter - Octet String", TestConverter_OctetString), - NL_TEST_DEF("Test Json Tlv Converter - Empty Octet String", TestConverter_OctetString_Empty), - NL_TEST_DEF("Test Json Tlv Converter - Null", TestConverter_Null), - NL_TEST_DEF("Test Json Tlv Converter - Floating Point Single Precision 0.0", TestConverter_Float_0), - NL_TEST_DEF("Test Json Tlv Converter - Floating Point Single Precision 1/3", TestConverter_Float_1third), - NL_TEST_DEF("Test Json Tlv Converter - Floating Point Single Precision 17.9", TestConverter_Float_17_9), - NL_TEST_DEF("Test Json Tlv Converter - Floating Point Single Precision Positive Infinity", - TestConverter_Float_PositiveInfinity), - NL_TEST_DEF("Test Json Tlv Converter - Floating Point Single Precision Negative Infinity", - TestConverter_Float_NegativeInfinity), - NL_TEST_DEF("Test Json Tlv Converter - Floating Point Double Precision 0.0", TestConverter_Double_0), - NL_TEST_DEF("Test Json Tlv Converter - Floating Point Double Precision 1/3", TestConverter_Double_1third), - NL_TEST_DEF("Test Json Tlv Converter - Floating Point Double Precision 17.9", TestConverter_Double_17_9), - NL_TEST_DEF("Test Json Tlv Converter - Floating Point Double Precision Positive Infinity", - TestConverter_Double_PositiveInfinity), - NL_TEST_DEF("Test Json Tlv Converter - Floating Point Double Precision Negative Infinity", - TestConverter_Double_NegativeInfinity), - NL_TEST_DEF("Test Json Tlv Converter - Structure Top-Level Empty", TestConverter_Structure_TopLevelEmpty), - NL_TEST_DEF("Test Json Tlv Converter - Structure Nested Empty", TestConverter_Structure_NestedEmpty), - NL_TEST_DEF("Test Json Tlv Converter - Array Empty", TestConverter_Array_Empty), - NL_TEST_DEF("Test Json Tlv Converter - Array Empty with Implicit Profile Tag (length 2)", - TestConverter_Array_Empty_ImplicitProfileTag2), - NL_TEST_DEF("Test Json Tlv Converter - Array Empty with Implicit Profile Tag (length 4)", - TestConverter_Array_Empty_ImplicitProfileTag4), - NL_TEST_DEF("Test Json Tlv Converter - Two Signed Integers", TestConverter_IntsWithContextTags), - NL_TEST_DEF("Test Json Tlv Converter - Structure With Two Signed Integers", TestConverter_Struct_IntsWithContextTags), - NL_TEST_DEF("Test Json Tlv Converter - Array Of Signed Integers", TestConverter_Array_Ints), - NL_TEST_DEF("Test Json Tlv Converter - Array Of Long Signed Integers", TestConverter_Array_Ints2), - NL_TEST_DEF("Test Json Tlv Converter - Array Of Min/Max Signed Integers", TestConverter_Array_IntsMinMax), - NL_TEST_DEF("Test Json Tlv Converter - Array Of Long Unsigned Integers", TestConverter_Array_UInts), - NL_TEST_DEF("Test Json Tlv Converter - Array of Unsigned Integers with Max values", TestConverter_Array_UIntsMax), - NL_TEST_DEF("Test Json Tlv Converter - Array Of Doubles", TestConverter_Array_Doubles), - NL_TEST_DEF("Test Json Tlv Converter - Array Of Floats", TestConverter_Array_Floats), - NL_TEST_DEF("Test Json Tlv Converter - Array Of Strings", TestConverter_Array_Strings), - NL_TEST_DEF("Test Json Tlv Converter - Array Of Booleans", TestConverter_Array_Booleans), - NL_TEST_DEF("Test Json Tlv Converter - Array Of Nulls", TestConverter_Array_Nulls), - NL_TEST_DEF("Test Json Tlv Converter - Structure with Unsigned Integer", TestConverter_Struct_UInt), - NL_TEST_DEF("Test Json Tlv Converter - Structure Elements with Mixed Tags", TestConverter_Struct_MixedTags), - NL_TEST_DEF("Test Json Tlv Converter - Structure with Mixed Elements", TestConverter_Struct_MixedElements), - NL_TEST_DEF("Test Json Tlv Converter - Array of Structures with Mixed Elements", TestConverter_Array_Structures), - NL_TEST_DEF("Test Json Tlv Converter - Top-Level Structure with Mixed Elements", TestConverter_TopLevel_MixedElements), - NL_TEST_DEF("Test Json Tlv Converter - Complex Structure from the README File", TestConverter_Structure_FromReadme), - NL_TEST_DEF("Test Json Tlv Converter - Tlv to Json Error Cases", TestConverter_TlvToJson_ErrorCases), - NL_TEST_DEF("Test Json Tlv Converter - Json To Tlv Error Cases", TestConverter_JsonToTlv_ErrorCases), - NL_TEST_DEF("Test Json Tlv Converter - Structure with MEI Elements", TestConverter_Struct_MEITags), - NL_TEST_SENTINEL() -}; - } // namespace - -int TestJsonToTlvToJson() -{ - nlTestSuite theSuite = { "JsonToTlvToJson", sTests, Initialize, Finalize }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestJsonToTlvToJson) diff --git a/src/lib/support/tests/TestTlvJson.cpp b/src/lib/support/tests/TestTlvJson.cpp index 9d9a91e243a3c1..83b538dad4a755 100644 --- a/src/lib/support/tests/TestTlvJson.cpp +++ b/src/lib/support/tests/TestTlvJson.cpp @@ -15,12 +15,12 @@ * limitations under the License. */ +#include + #include #include #include -#include #include -#include #include #include @@ -33,7 +33,17 @@ using namespace chip::app; System::TLVPacketBufferBackingStore gStore; TLV::TLVWriter gWriter; TLV::TLVReader gReader; -nlTestSuite * gSuite; + +class TestTlvJson : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() + { + (void) gStore.Release(); + chip::Platform::MemoryShutdown(); + } +}; void SetupBuf() { @@ -88,25 +98,24 @@ void EncodeAndValidate(T val, const char * expectedJsonString) SetupBuf(); err = DataModel::Encode(gWriter, TLV::AnonymousTag(), val); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = gWriter.Finalize(); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = SetupReader(); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); Json::Value d; err = TlvToJson(gReader, d); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); bool matches = Matches(expectedJsonString, d); - NL_TEST_ASSERT(gSuite, matches); + EXPECT_TRUE(matches); } -void TestConverter(nlTestSuite * inSuite, void * inContext) +TEST_F(TestTlvJson, TestConverter) { - gSuite = inSuite; EncodeAndValidate(static_cast(30), "{\n" @@ -231,28 +240,4 @@ void TestConverter(nlTestSuite * inSuite, void * inContext) "}\n"); } -int Initialize(void * apSuite) -{ - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - return SUCCESS; -} - -int Finalize(void * aContext) -{ - (void) gStore.Release(); - chip::Platform::MemoryShutdown(); - return SUCCESS; -} - -const nlTest sTests[] = { NL_TEST_DEF("TestConverter", TestConverter), NL_TEST_SENTINEL() }; - } // namespace - -int TestTlvJson() -{ - nlTestSuite theSuite = { "TlvJson", sTests, Initialize, Finalize }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestTlvJson) diff --git a/src/lib/support/tests/TestTlvToJson.cpp b/src/lib/support/tests/TestTlvToJson.cpp index 1467744ba0478e..b313b843616671 100644 --- a/src/lib/support/tests/TestTlvToJson.cpp +++ b/src/lib/support/tests/TestTlvToJson.cpp @@ -15,18 +15,18 @@ * limitations under the License. */ +#include + +#include + #include #include #include -#include #include #include -#include #include #include -#include - namespace { using namespace chip::Encoding; @@ -36,7 +36,17 @@ using namespace chip::app; System::TLVPacketBufferBackingStore gStore; TLV::TLVWriter gWriter; TLV::TLVReader gReader; -nlTestSuite * gSuite; + +class TestTlvToJson : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() + { + (void) gStore.Release(); + chip::Platform::MemoryShutdown(); + } +}; void SetupBuf() { @@ -84,34 +94,32 @@ void EncodeAndValidate(T val, const std::string & expectedJsonString) SetupBuf(); err = gWriter.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = DataModel::Encode(gWriter, TLV::ContextTag(1), val); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = gWriter.EndContainer(container); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = gWriter.Finalize(); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = SetupReader(); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); std::string jsonString; err = TlvToJson(gReader, jsonString); - NL_TEST_ASSERT(gSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); bool matches = Matches(expectedJsonString, jsonString); - NL_TEST_ASSERT(gSuite, matches); + EXPECT_TRUE(matches); } -void TestConverter(nlTestSuite * inSuite, void * inContext) +TEST_F(TestTlvToJson, TestConverter) { std::string jsonString; - gSuite = inSuite; - jsonString = "{\n" " \"1:UINT\" : 30\n" "}\n"; @@ -236,28 +244,4 @@ void TestConverter(nlTestSuite * inSuite, void * inContext) EncodeAndValidate(structList, jsonString); } -int Initialize(void * apSuite) -{ - VerifyOrReturnError(chip::Platform::MemoryInit() == CHIP_NO_ERROR, FAILURE); - return SUCCESS; -} - -int Finalize(void * aContext) -{ - (void) gStore.Release(); - chip::Platform::MemoryShutdown(); - return SUCCESS; -} - -const nlTest sTests[] = { NL_TEST_DEF("TestConverter", TestConverter), NL_TEST_SENTINEL() }; - } // namespace - -int TestTlvToJson() -{ - nlTestSuite theSuite = { "TlvToJson", sTests, Initialize, Finalize }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestTlvToJson) diff --git a/src/lib/support/tests/TestZclString.cpp b/src/lib/support/tests/TestZclString.cpp index 3110c66ce4894e..13174248af62d7 100644 --- a/src/lib/support/tests/TestZclString.cpp +++ b/src/lib/support/tests/TestZclString.cpp @@ -26,18 +26,24 @@ #include #include +#include + #include #include #include -#include -#include - #include using namespace chip; using namespace chip::Logging; using namespace chip::Platform; +class TestZclString : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } +}; + bool allCharactersSame(uint8_t zclString[]) { int n = zclString[0]; @@ -48,132 +54,88 @@ bool allCharactersSame(uint8_t zclString[]) return true; } -static void TestZclStringWhenBufferIsZero(nlTestSuite * inSuite, void * inContext) +TEST_F(TestZclString, TestZclStringWhenBufferIsZero) { uint8_t bufferMemory[1]; MutableByteSpan zclString(bufferMemory); chip::Platform::ScopedMemoryBuffer cString1; - NL_TEST_ASSERT(inSuite, cString1.Calloc(1024)); + EXPECT_TRUE(cString1.Calloc(1024)); memset(cString1.Get(), 'A', 1); CHIP_ERROR err = MakeZclCharString(zclString, cString1.Get()); - NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG); - NL_TEST_ASSERT(inSuite, zclString.data()[0] == 0); + EXPECT_EQ(err, CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG); + EXPECT_EQ(zclString.data()[0], 0); } -static void TestZclStringLessThanMaximumSize_Length_64(nlTestSuite * inSuite, void * inContext) +TEST_F(TestZclString, TestZclStringLessThanMaximumSize_Length_64) { uint8_t bufferMemory[256]; MutableByteSpan zclString(bufferMemory); chip::Platform::ScopedMemoryBuffer cString64; - NL_TEST_ASSERT(inSuite, cString64.Calloc(1024)); + EXPECT_TRUE(cString64.Calloc(1024)); memset(cString64.Get(), 'A', 64); CHIP_ERROR err = MakeZclCharString(zclString, cString64.Get()); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, zclString.data()[0] == 64); - NL_TEST_ASSERT(inSuite, allCharactersSame(zclString.data()) == true); + EXPECT_EQ(err, CHIP_NO_ERROR); + EXPECT_EQ(zclString.data()[0], 64); + EXPECT_TRUE(allCharactersSame(zclString.data())); } -static void TestZclStringEqualsMaximumSize(nlTestSuite * inSuite, void * inContext) +TEST_F(TestZclString, TestZclStringEqualsMaximumSize) { uint8_t bufferMemory[256]; MutableByteSpan zclString(bufferMemory); chip::Platform::ScopedMemoryBuffer cString254; - NL_TEST_ASSERT(inSuite, cString254.Calloc(1024)); + EXPECT_TRUE(cString254.Calloc(1024)); memset(cString254.Get(), 'A', 254); CHIP_ERROR err = MakeZclCharString(zclString, cString254.Get()); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, zclString.data()[0] == 254); - NL_TEST_ASSERT(inSuite, allCharactersSame(zclString.data()) == true); + EXPECT_EQ(err, CHIP_NO_ERROR); + EXPECT_EQ(zclString.data()[0], 254); + EXPECT_TRUE(allCharactersSame(zclString.data())); } -static void TestSizeZclStringBiggerThanMaximumSize_Length_255(nlTestSuite * inSuite, void * inContext) +TEST_F(TestZclString, TestSizeZclStringBiggerThanMaximumSize_Length_255) { uint8_t bufferMemory[256]; MutableByteSpan zclString(bufferMemory); chip::Platform::ScopedMemoryBuffer cString255; - NL_TEST_ASSERT(inSuite, cString255.Calloc(1024)); + EXPECT_TRUE(cString255.Calloc(1024)); memset(cString255.Get(), 'A', 255); CHIP_ERROR err = MakeZclCharString(zclString, cString255.Get()); - NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG); - NL_TEST_ASSERT(inSuite, zclString.data()[0] == 0); + EXPECT_EQ(err, CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG); + EXPECT_EQ(zclString.data()[0], 0); } -static void TestSizeZclStringBiggerThanMaximumSize_Length_256(nlTestSuite * inSuite, void * inContext) +TEST_F(TestZclString, TestSizeZclStringBiggerThanMaximumSize_Length_256) { uint8_t bufferMemory[256]; MutableByteSpan zclString(bufferMemory); chip::Platform::ScopedMemoryBuffer cString256; - NL_TEST_ASSERT(inSuite, cString256.Calloc(1024)); + EXPECT_TRUE(cString256.Calloc(1024)); memset(cString256.Get(), 'A', 256); CHIP_ERROR err = MakeZclCharString(zclString, cString256.Get()); - NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG); - NL_TEST_ASSERT(inSuite, zclString.data()[0] == 0); + EXPECT_EQ(err, CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG); + EXPECT_EQ(zclString.data()[0], 0); } -static void TestZclStringBiggerThanMaximumSize_Length_257(nlTestSuite * inSuite, void * inContext) +TEST_F(TestZclString, TestZclStringBiggerThanMaximumSize_Length_257) { uint8_t bufferMemory[257]; MutableByteSpan zclString(bufferMemory); chip::Platform::ScopedMemoryBuffer cString257; - NL_TEST_ASSERT(inSuite, cString257.Calloc(1024)); + EXPECT_TRUE(cString257.Calloc(1024)); memset(cString257.Get(), 'A', 257); CHIP_ERROR err = MakeZclCharString(zclString, cString257.Get()); - NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG); - NL_TEST_ASSERT(inSuite, zclString.data()[0] == 0); -} - -#define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn) - -/** - * Set up the test suite. - */ -int TestZclString_Setup(void * inContext) -{ - CHIP_ERROR error = MemoryInit(); - if (error != CHIP_NO_ERROR) - return (FAILURE); - return (SUCCESS); -} - -/** - * Tear down the test suite. - */ -int TestZclString_Teardown(void * inContext) -{ - MemoryShutdown(); - return (SUCCESS); + EXPECT_EQ(err, CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG); + EXPECT_EQ(zclString.data()[0], 0); } - -/** - * Test Suite. It lists all the test functions. - */ -static const nlTest sTests[] = { NL_TEST_DEF_FN(TestZclStringWhenBufferIsZero), - NL_TEST_DEF_FN(TestZclStringLessThanMaximumSize_Length_64), - NL_TEST_DEF_FN(TestZclStringEqualsMaximumSize), - NL_TEST_DEF_FN(TestSizeZclStringBiggerThanMaximumSize_Length_255), - NL_TEST_DEF_FN(TestSizeZclStringBiggerThanMaximumSize_Length_256), - NL_TEST_DEF_FN(TestZclStringBiggerThanMaximumSize_Length_257), - NL_TEST_SENTINEL() }; - -int TestZclString() -{ - nlTestSuite theSuite = { "CHIP Memory Allocation tests", &sTests[0], TestZclString_Setup, TestZclString_Teardown }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestZclString) From 87ccf900b83408625197c6459cf2672dd8efd74b Mon Sep 17 00:00:00 2001 From: AYA Date: Tue, 30 Apr 2024 14:57:44 +0200 Subject: [PATCH 3/6] pw_unit_test migration: lib support batch 5 --- src/BUILD.gn | 1 - src/lib/support/tests/BUILD.gn | 37 +--- src/lib/support/tests/TestStateMachine.cpp | 123 ++++------- .../tests/TestThreadOperationalDataset.cpp | 203 ++++++++---------- .../unit-tests/test_components_nl.txt | 1 - 5 files changed, 135 insertions(+), 230 deletions(-) diff --git a/src/BUILD.gn b/src/BUILD.gn index 6bf4a2ed6d7071..14d19f226ba96b 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -97,7 +97,6 @@ if (chip_build_tests) { "${chip_root}/src/credentials/tests", "${chip_root}/src/lib/format/tests", "${chip_root}/src/lib/support/tests", - "${chip_root}/src/lib/support/tests:tests_nltest", "${chip_root}/src/protocols/secure_channel/tests", "${chip_root}/src/protocols/secure_channel/tests:tests_nltest", "${chip_root}/src/system/tests", diff --git a/src/lib/support/tests/BUILD.gn b/src/lib/support/tests/BUILD.gn index c4c62a21edeee8..3f36390c9ad710 100644 --- a/src/lib/support/tests/BUILD.gn +++ b/src/lib/support/tests/BUILD.gn @@ -48,10 +48,12 @@ chip_test_suite("tests") { "TestScopedBuffer.cpp", "TestSorting.cpp", "TestSpan.cpp", + "TestStateMachine.cpp", "TestStaticSupportSmartPtr.cpp", "TestStringBuilder.cpp", "TestStringSplitter.cpp", "TestTestPersistentStorageDelegate.cpp", + "TestThreadOperationalDataset.cpp", "TestTimeUtils.cpp", "TestTlvJson.cpp", "TestTlvToJson.cpp", @@ -80,38 +82,3 @@ chip_test_suite("tests") { "${chip_root}/src/platform", ] } - -chip_test_suite_using_nltest("tests_nltest") { - output_name = "libSupportTestsNL" - - test_sources = [ - "TestStateMachine.cpp", - "TestThreadOperationalDataset.cpp", - ] - sources = [] - - if (current_os != "mbed") { - test_sources += [ "TestCHIPArgParser.cpp" ] - } - - cflags = [ - "-Wconversion", - - # TODO(#21255): work-around for SimpleStateMachine constructor issue. - "-Wno-uninitialized", - - # TestStringSplitter intentionally validates string overflows. - "-Wno-stringop-truncation", - ] - - public_deps = [ - "${chip_root}/src/credentials", - "${chip_root}/src/lib/core", - "${chip_root}/src/lib/support:static-support", - "${chip_root}/src/lib/support:testing", - "${chip_root}/src/lib/support:testing_nlunit", - "${chip_root}/src/lib/support/jsontlv", - "${chip_root}/src/platform", - "${nlunit_test_root}:nlunit-test", - ] -} diff --git a/src/lib/support/tests/TestStateMachine.cpp b/src/lib/support/tests/TestStateMachine.cpp index cef020e019ff74..4b6af3a73d85cf 100644 --- a/src/lib/support/tests/TestStateMachine.cpp +++ b/src/lib/support/tests/TestStateMachine.cpp @@ -16,10 +16,10 @@ * limitations under the License. */ +#include + #include -#include #include -#include namespace { @@ -166,67 +166,67 @@ class SimpleStateMachine ~SimpleStateMachine() {} }; -void TestInit(nlTestSuite * inSuite, void * inContext) +TEST(TestStateMachine, TestInit) { // state machine initializes to State1 SimpleStateMachine fsm; - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); } -void TestIgnoredEvents(nlTestSuite * inSuite, void * inContext) +TEST(TestStateMachine, TestIgnoredEvents) { // in State1 - ignore Event1 and Event3 SimpleStateMachine fsm; fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); // transition to State2 fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); // in State2 - ignore Event2 and Event3 fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); } -void TestTransitions(nlTestSuite * inSuite, void * inContext) +TEST(TestStateMachine, TestTransitions) { // in State1 SimpleStateMachine fsm; // dispatch Event2 to transition to State2 fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); // dispatch Event1 to transition back to State1 fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); // dispatch Event2 to transition to State2 fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); // dispatch Event4 to transitions to State1. fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); } -void TestTransitionsDispatch(nlTestSuite * inSuite, void * inContext) +TEST(TestStateMachine, TestTransitionsDispatch) { // in State1 SimpleStateMachine fsm; // Dispatch Event4, which in turn dispatches Event2 from the transitions // table and ultimately places us in State2. fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); } -void TestNestedDispatch(nlTestSuite * inSuite, void * inContext) +TEST(TestStateMachine, TestNestedDispatch) { // in State1 SimpleStateMachine fsm; // Dispatch Event5, which places us into State3, which will dispatch // Event5 again from its Enter method to place us into State2. fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); // Make sure that Enter methods execute the correct number of times. // This helps verify that pattern matching is working correctly. // Specifically, we need to verify this case: State3 creates State2 @@ -238,74 +238,45 @@ void TestNestedDispatch(nlTestSuite * inSuite, void * inContext) // been constructed when the State3 Enter method executes a second // time. The state machine pattern matching has code to explicitly // prevent this double-execution. This is testing that. - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms1.mEntered == 0); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms1.mExited == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms1.mLogged == 0); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms2.mEntered == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms2.mExited == 0); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms2.mLogged == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms3.mEntered == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms3.mExited == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms3.mLogged == 1); + EXPECT_EQ(fsm.mTransitions.mFactory.ms1.mEntered, 0u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms1.mExited, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms1.mLogged, 0u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms2.mEntered, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms2.mExited, 0u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms2.mLogged, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms3.mEntered, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms3.mExited, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms3.mLogged, 1u); } -void TestMethodExec(nlTestSuite * inSuite, void * inContext) +TEST(TestStateMachine, TestMethodExec) { // in State1 SimpleStateMachine fsm; // transition to State2 fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); // verify expected method calls - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms1.mEntered == 0); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms1.mExited == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms1.mLogged == 0); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms1.mPrevious == nullptr); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms2.mEntered == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms2.mExited == 0); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms2.mLogged == 1); - NL_TEST_ASSERT(inSuite, strcmp(fsm.mTransitions.mFactory.ms2.mPrevious, "State1") == 0); + EXPECT_EQ(fsm.mTransitions.mFactory.ms1.mEntered, 0u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms1.mExited, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms1.mLogged, 0u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms1.mPrevious, nullptr); + EXPECT_EQ(fsm.mTransitions.mFactory.ms2.mEntered, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms2.mExited, 0u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms2.mLogged, 1u); + EXPECT_STREQ(fsm.mTransitions.mFactory.ms2.mPrevious, "State1"); // transition back to State1 fsm.mStateMachine.Dispatch(Event::Create()); - NL_TEST_ASSERT(inSuite, fsm.mStateMachine.GetState().Is()); + EXPECT_TRUE(fsm.mStateMachine.GetState().Is()); // verify expected method calls - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms1.mEntered == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms1.mExited == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms1.mLogged == 1); - NL_TEST_ASSERT(inSuite, strcmp(fsm.mTransitions.mFactory.ms1.mPrevious, "State2") == 0); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms2.mEntered == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms2.mExited == 1); - NL_TEST_ASSERT(inSuite, fsm.mTransitions.mFactory.ms2.mLogged == 1); - NL_TEST_ASSERT(inSuite, strcmp(fsm.mTransitions.mFactory.ms2.mPrevious, "State1") == 0); -} - -int Setup(void * inContext) -{ - return SUCCESS; -} - -int Teardown(void * inContext) -{ - return SUCCESS; + EXPECT_EQ(fsm.mTransitions.mFactory.ms1.mEntered, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms1.mExited, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms1.mLogged, 1u); + EXPECT_STREQ(fsm.mTransitions.mFactory.ms1.mPrevious, "State2"); + EXPECT_EQ(fsm.mTransitions.mFactory.ms2.mEntered, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms2.mExited, 1u); + EXPECT_EQ(fsm.mTransitions.mFactory.ms2.mLogged, 1u); + EXPECT_STREQ(fsm.mTransitions.mFactory.ms2.mPrevious, "State1"); } } // namespace - -static const nlTest sTests[] = { - NL_TEST_DEF("TestInit", TestInit), - NL_TEST_DEF("TestIgnoredEvents", TestIgnoredEvents), - NL_TEST_DEF("TestTransitions", TestTransitions), - NL_TEST_DEF("TestTransitionsDispatch", TestTransitionsDispatch), - NL_TEST_DEF("TestNestedDispatch", TestNestedDispatch), - NL_TEST_DEF("TestMethodExec", TestMethodExec), - NL_TEST_SENTINEL(), -}; - -int StateMachineTestSuite() -{ - nlTestSuite suite = { "CHIP State Machine tests", &sTests[0], Setup, Teardown }; - nlTestRunner(&suite, nullptr); - return nlTestRunnerStats(&suite); -} - -CHIP_REGISTER_TEST_SUITE(StateMachineTestSuite); diff --git a/src/lib/support/tests/TestThreadOperationalDataset.cpp b/src/lib/support/tests/TestThreadOperationalDataset.cpp index 27e18eac87e23e..e02a8541c2c2df 100644 --- a/src/lib/support/tests/TestThreadOperationalDataset.cpp +++ b/src/lib/support/tests/TestThreadOperationalDataset.cpp @@ -14,286 +14,255 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include -#include -#include -#include +// #include +// #include + +#include +// #include namespace { using namespace chip; -void TestInit(nlTestSuite * inSuite, void * inContext) +class TestThreadOperationalDataset : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } + + static Thread::OperationalDataset dataset; +}; + +Thread::OperationalDataset TestThreadOperationalDataset::dataset; + +TEST_F(TestThreadOperationalDataset, TestInit) { - Thread::OperationalDataset & dataset = *static_cast(inContext); uint8_t longerThanOperationalDatasetSize[255]{}; - NL_TEST_ASSERT(inSuite, dataset.Init(ByteSpan(longerThanOperationalDatasetSize)) == CHIP_ERROR_INVALID_ARGUMENT); - NL_TEST_ASSERT(inSuite, dataset.Init(ByteSpan()) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.Init(ByteSpan(longerThanOperationalDatasetSize)), CHIP_ERROR_INVALID_ARGUMENT); + EXPECT_EQ(dataset.Init(ByteSpan()), CHIP_NO_ERROR); { uint8_t data[] = { 0x01, 0x02, 0x03 }; - NL_TEST_ASSERT(inSuite, dataset.Init(ByteSpan(data)) == CHIP_ERROR_INVALID_ARGUMENT); + EXPECT_EQ(dataset.Init(ByteSpan(data)), CHIP_ERROR_INVALID_ARGUMENT); } { uint8_t data[] = { 0x01 }; - NL_TEST_ASSERT(inSuite, dataset.Init(ByteSpan(data)) == CHIP_ERROR_INVALID_ARGUMENT); + EXPECT_EQ(dataset.Init(ByteSpan(data)), CHIP_ERROR_INVALID_ARGUMENT); } } -void TestActiveTimestamp(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestActiveTimestamp) { static constexpr uint64_t kActiveTimestampValue = 1; - Thread::OperationalDataset & dataset = *static_cast(inContext); - uint64_t activeTimestamp = 0; - NL_TEST_ASSERT(inSuite, dataset.SetActiveTimestamp(kActiveTimestampValue) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, dataset.GetActiveTimestamp(activeTimestamp) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, activeTimestamp == kActiveTimestampValue); + EXPECT_EQ(dataset.SetActiveTimestamp(kActiveTimestampValue), CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetActiveTimestamp(activeTimestamp), CHIP_NO_ERROR); + EXPECT_EQ(activeTimestamp, kActiveTimestampValue); } -void TestChannel(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestChannel) { static constexpr uint16_t kChannelValue = 15; - Thread::OperationalDataset & dataset = *static_cast(inContext); - uint16_t channel = 0; - NL_TEST_ASSERT(inSuite, dataset.SetChannel(kChannelValue) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, dataset.GetChannel(channel) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, channel == kChannelValue); + EXPECT_EQ(dataset.SetChannel(kChannelValue), CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetChannel(channel), CHIP_NO_ERROR); + EXPECT_EQ(channel, kChannelValue); } -void TestExtendedPanId(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestExtendedPanId) { static constexpr uint8_t kExtendedPanId[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 }; - Thread::OperationalDataset & dataset = *static_cast(inContext); - uint8_t extendedPanId[Thread::kSizeExtendedPanId] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.SetExtendedPanId(kExtendedPanId) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, dataset.GetExtendedPanId(extendedPanId) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, memcmp(extendedPanId, kExtendedPanId, sizeof(kExtendedPanId)) == 0); + EXPECT_EQ(dataset.SetExtendedPanId(kExtendedPanId), CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetExtendedPanId(extendedPanId), CHIP_NO_ERROR); + EXPECT_EQ(memcmp(extendedPanId, kExtendedPanId, sizeof(kExtendedPanId)), 0); ByteSpan span; - NL_TEST_ASSERT(inSuite, dataset.GetExtendedPanIdAsByteSpan(span) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, span.size() == sizeof(kExtendedPanId)); - NL_TEST_ASSERT(inSuite, memcmp(extendedPanId, span.data(), sizeof(kExtendedPanId)) == 0); + EXPECT_EQ(dataset.GetExtendedPanIdAsByteSpan(span), CHIP_NO_ERROR); + EXPECT_EQ(span.size(), sizeof(kExtendedPanId)); + EXPECT_EQ(memcmp(extendedPanId, span.data(), sizeof(kExtendedPanId)), 0); } -void TestMasterKey(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestMasterKey) { static constexpr uint8_t kMasterKey[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; - Thread::OperationalDataset & dataset = *static_cast(inContext); - uint8_t masterKey[Thread::kSizeMasterKey] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.SetMasterKey(kMasterKey) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, dataset.GetMasterKey(masterKey) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, memcmp(masterKey, kMasterKey, sizeof(kMasterKey)) == 0); + EXPECT_EQ(dataset.SetMasterKey(kMasterKey), CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetMasterKey(masterKey), CHIP_NO_ERROR); + EXPECT_EQ(memcmp(masterKey, kMasterKey, sizeof(kMasterKey)), 0); } -void TestMeshLocalPrefix(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestMeshLocalPrefix) { static constexpr uint8_t kMeshLocalPrefix[] = { 0xfd, 0xde, 0xad, 0x00, 0xbe, 0xef, 0x00, 0x00 }; - Thread::OperationalDataset & dataset = *static_cast(inContext); - uint8_t meshLocalPrefix[Thread::kSizeMeshLocalPrefix] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.SetMeshLocalPrefix(kMeshLocalPrefix) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, dataset.GetMeshLocalPrefix(meshLocalPrefix) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, memcmp(meshLocalPrefix, kMeshLocalPrefix, sizeof(kMeshLocalPrefix)) == 0); + EXPECT_EQ(dataset.SetMeshLocalPrefix(kMeshLocalPrefix), CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetMeshLocalPrefix(meshLocalPrefix), CHIP_NO_ERROR); + EXPECT_EQ(memcmp(meshLocalPrefix, kMeshLocalPrefix, sizeof(kMeshLocalPrefix)), 0); } -void TestNetworkName(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestNetworkName) { static constexpr char kNetworkName[] = "ThreadNetwork"; - Thread::OperationalDataset & dataset = *static_cast(inContext); - char networkName[Thread::kSizeNetworkName + 1] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.SetNetworkName(kNetworkName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, dataset.GetNetworkName(networkName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, strcmp(networkName, kNetworkName) == 0); + EXPECT_EQ(dataset.SetNetworkName(kNetworkName), CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetNetworkName(networkName), CHIP_NO_ERROR); + EXPECT_STREQ(networkName, kNetworkName); - NL_TEST_ASSERT(inSuite, dataset.SetNetworkName("0123456789abcdef") == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, dataset.SetNetworkName("0123456789abcdefg") == CHIP_ERROR_INVALID_STRING_LENGTH); - NL_TEST_ASSERT(inSuite, dataset.SetNetworkName("") == CHIP_ERROR_INVALID_STRING_LENGTH); + EXPECT_EQ(dataset.SetNetworkName("0123456789abcdef"), CHIP_NO_ERROR); + EXPECT_EQ(dataset.SetNetworkName("0123456789abcdefg"), CHIP_ERROR_INVALID_STRING_LENGTH); + EXPECT_EQ(dataset.SetNetworkName(""), CHIP_ERROR_INVALID_STRING_LENGTH); } -void TestPanId(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestPanId) { static constexpr uint16_t kPanIdValue = 0x1234; - Thread::OperationalDataset & dataset = *static_cast(inContext); - uint16_t panid = 0; - NL_TEST_ASSERT(inSuite, dataset.SetPanId(kPanIdValue) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, dataset.GetPanId(panid) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, panid == kPanIdValue); + EXPECT_EQ(dataset.SetPanId(kPanIdValue), CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetPanId(panid), CHIP_NO_ERROR); + EXPECT_EQ(panid, kPanIdValue); } -void TestPSKc(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestPSKc) { static constexpr uint8_t kPSKc[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; - Thread::OperationalDataset & dataset = *static_cast(inContext); - uint8_t pskc[Thread::kSizePSKc] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.SetPSKc(kPSKc) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, dataset.GetPSKc(pskc) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, memcmp(pskc, kPSKc, sizeof(kPSKc)) == 0); + EXPECT_EQ(dataset.SetPSKc(kPSKc), CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetPSKc(pskc), CHIP_NO_ERROR); + EXPECT_FALSE(memcmp(pskc, kPSKc, sizeof(kPSKc))); } -void TestUnsetMasterKey(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestUnsetMasterKey) { - Thread::OperationalDataset & dataset = *static_cast(inContext); uint8_t masterKey[Thread::kSizeMasterKey] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetMasterKey(masterKey) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetMasterKey(masterKey), CHIP_NO_ERROR); dataset.UnsetMasterKey(); - NL_TEST_ASSERT(inSuite, dataset.GetMasterKey(masterKey) == CHIP_ERROR_TLV_TAG_NOT_FOUND); - NL_TEST_ASSERT(inSuite, dataset.SetMasterKey(masterKey) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetMasterKey(masterKey), CHIP_ERROR_TLV_TAG_NOT_FOUND); + EXPECT_EQ(dataset.SetMasterKey(masterKey), CHIP_NO_ERROR); } -void TestUnsetPSKc(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestUnsetPSKc) { - Thread::OperationalDataset & dataset = *static_cast(inContext); uint8_t pskc[Thread::kSizePSKc] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetPSKc(pskc) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetPSKc(pskc), CHIP_NO_ERROR); dataset.UnsetPSKc(); - NL_TEST_ASSERT(inSuite, dataset.GetPSKc(pskc) == CHIP_ERROR_TLV_TAG_NOT_FOUND); - NL_TEST_ASSERT(inSuite, dataset.SetPSKc(pskc) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetPSKc(pskc), CHIP_ERROR_TLV_TAG_NOT_FOUND); + EXPECT_EQ(dataset.SetPSKc(pskc), CHIP_NO_ERROR); } -void TestClear(nlTestSuite * inSuite, void * inContext) +TEST_F(TestThreadOperationalDataset, TestClear) { - Thread::OperationalDataset & dataset = *static_cast(inContext); { uint64_t activeTimestamp; - NL_TEST_ASSERT(inSuite, dataset.GetActiveTimestamp(activeTimestamp) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetActiveTimestamp(activeTimestamp), CHIP_NO_ERROR); } { uint16_t channel; - NL_TEST_ASSERT(inSuite, dataset.GetChannel(channel) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetChannel(channel), CHIP_NO_ERROR); } { uint8_t extendedPanId[Thread::kSizeExtendedPanId] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetExtendedPanId(extendedPanId) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetExtendedPanId(extendedPanId), CHIP_NO_ERROR); } { uint8_t masterKey[Thread::kSizeMasterKey] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetMasterKey(masterKey) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetMasterKey(masterKey), CHIP_NO_ERROR); } { uint8_t meshLocalPrefix[Thread::kSizeMeshLocalPrefix] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetMeshLocalPrefix(meshLocalPrefix) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetMeshLocalPrefix(meshLocalPrefix), CHIP_NO_ERROR); } { char networkName[Thread::kSizeNetworkName + 1] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetNetworkName(networkName) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetNetworkName(networkName), CHIP_NO_ERROR); } { uint16_t panid; - NL_TEST_ASSERT(inSuite, dataset.GetPanId(panid) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetPanId(panid), CHIP_NO_ERROR); } { uint8_t pskc[Thread::kSizePSKc] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetPSKc(pskc) == CHIP_NO_ERROR); + EXPECT_EQ(dataset.GetPSKc(pskc), CHIP_NO_ERROR); } dataset.Clear(); { uint64_t activeTimestamp; - NL_TEST_ASSERT(inSuite, dataset.GetActiveTimestamp(activeTimestamp) == CHIP_ERROR_TLV_TAG_NOT_FOUND); + EXPECT_EQ(dataset.GetActiveTimestamp(activeTimestamp), CHIP_ERROR_TLV_TAG_NOT_FOUND); } { uint16_t channel; - NL_TEST_ASSERT(inSuite, dataset.GetChannel(channel) == CHIP_ERROR_TLV_TAG_NOT_FOUND); + EXPECT_EQ(dataset.GetChannel(channel), CHIP_ERROR_TLV_TAG_NOT_FOUND); } { uint8_t extendedPanId[Thread::kSizeExtendedPanId] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetExtendedPanId(extendedPanId) == CHIP_ERROR_TLV_TAG_NOT_FOUND); + EXPECT_EQ(dataset.GetExtendedPanId(extendedPanId), CHIP_ERROR_TLV_TAG_NOT_FOUND); } { uint8_t masterKey[Thread::kSizeMasterKey] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetMasterKey(masterKey) == CHIP_ERROR_TLV_TAG_NOT_FOUND); + EXPECT_EQ(dataset.GetMasterKey(masterKey), CHIP_ERROR_TLV_TAG_NOT_FOUND); } { uint8_t meshLocalPrefix[Thread::kSizeMeshLocalPrefix] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetMeshLocalPrefix(meshLocalPrefix) == CHIP_ERROR_TLV_TAG_NOT_FOUND); + EXPECT_EQ(dataset.GetMeshLocalPrefix(meshLocalPrefix), CHIP_ERROR_TLV_TAG_NOT_FOUND); } { char networkName[Thread::kSizeNetworkName + 1] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetNetworkName(networkName) == CHIP_ERROR_TLV_TAG_NOT_FOUND); + EXPECT_EQ(dataset.GetNetworkName(networkName), CHIP_ERROR_TLV_TAG_NOT_FOUND); } { uint16_t panid; - NL_TEST_ASSERT(inSuite, dataset.GetPanId(panid) == CHIP_ERROR_TLV_TAG_NOT_FOUND); + EXPECT_EQ(dataset.GetPanId(panid), CHIP_ERROR_TLV_TAG_NOT_FOUND); } { uint8_t pskc[Thread::kSizePSKc] = { 0 }; - NL_TEST_ASSERT(inSuite, dataset.GetPSKc(pskc) == CHIP_ERROR_TLV_TAG_NOT_FOUND); + EXPECT_EQ(dataset.GetPSKc(pskc), CHIP_ERROR_TLV_TAG_NOT_FOUND); } } -const nlTest sTests[] = { - NL_TEST_DEF("TestInit", TestInit), // - NL_TEST_DEF("TestActiveTimestamp", TestActiveTimestamp), // - NL_TEST_DEF("TestChannel", TestChannel), // - NL_TEST_DEF("TestExtendedPanId", TestExtendedPanId), // - NL_TEST_DEF("TestMasterKey", TestMasterKey), // - NL_TEST_DEF("TestMeshLocalPrefix", TestMeshLocalPrefix), // - NL_TEST_DEF("TestNetworkName", TestNetworkName), // - NL_TEST_DEF("TestPanId", TestPanId), // - NL_TEST_DEF("TestPSKc", TestPSKc), // - NL_TEST_DEF("TestUnsetMasterKey", TestUnsetMasterKey), // - NL_TEST_DEF("TestUnsetPSKc", TestUnsetPSKc), // - NL_TEST_DEF("TestClear", TestClear), // - NL_TEST_SENTINEL() // -}; - -} // namespace - -int TestThreadOperationalDatasetBuilder() -{ - nlTestSuite theSuite = { "ThreadOperationalDataset", sTests, nullptr, nullptr }; - - return ExecuteTestsWithContext(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestThreadOperationalDatasetBuilder) +} // namespace \ No newline at end of file diff --git a/src/test_driver/openiotsdk/unit-tests/test_components_nl.txt b/src/test_driver/openiotsdk/unit-tests/test_components_nl.txt index d30358d4353c85..79c017fccc23cf 100644 --- a/src/test_driver/openiotsdk/unit-tests/test_components_nl.txt +++ b/src/test_driver/openiotsdk/unit-tests/test_components_nl.txt @@ -4,5 +4,4 @@ InetLayerTests MessagingLayerTests RawTransportTests SecureChannelTestsNL -SupportTestsNL TransportLayerTests From a5225c788b7fd9398fe42b4594a74debd311911b Mon Sep 17 00:00:00 2001 From: AYA Date: Tue, 30 Apr 2024 15:03:19 +0200 Subject: [PATCH 4/6] newline --- src/lib/support/tests/TestThreadOperationalDataset.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/support/tests/TestThreadOperationalDataset.cpp b/src/lib/support/tests/TestThreadOperationalDataset.cpp index e02a8541c2c2df..bfbe044c477d44 100644 --- a/src/lib/support/tests/TestThreadOperationalDataset.cpp +++ b/src/lib/support/tests/TestThreadOperationalDataset.cpp @@ -265,4 +265,4 @@ TEST_F(TestThreadOperationalDataset, TestClear) } } -} // namespace \ No newline at end of file +} // namespace From 47873d39ee7f343e95187c72258ade3ed0293528 Mon Sep 17 00:00:00 2001 From: AYA Date: Tue, 30 Apr 2024 15:25:26 +0200 Subject: [PATCH 5/6] removing commented-out code --- src/lib/support/tests/TestThreadOperationalDataset.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/lib/support/tests/TestThreadOperationalDataset.cpp b/src/lib/support/tests/TestThreadOperationalDataset.cpp index bfbe044c477d44..d9e13c8c25705f 100644 --- a/src/lib/support/tests/TestThreadOperationalDataset.cpp +++ b/src/lib/support/tests/TestThreadOperationalDataset.cpp @@ -14,14 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include -#include - -// #include -// #include #include -// #include + +#include +#include namespace { From 6e57610739e42d792a9d2a94207f1fac391cbc30 Mon Sep 17 00:00:00 2001 From: AYA Date: Wed, 1 May 2024 11:27:37 +0200 Subject: [PATCH 6/6] convert TestCHIPArgParser test --- src/lib/support/tests/BUILD.gn | 4 + src/lib/support/tests/TestCHIPArgParser.cpp | 214 ++++++++------------ 2 files changed, 84 insertions(+), 134 deletions(-) diff --git a/src/lib/support/tests/BUILD.gn b/src/lib/support/tests/BUILD.gn index 3f36390c9ad710..565eb85097d133 100644 --- a/src/lib/support/tests/BUILD.gn +++ b/src/lib/support/tests/BUILD.gn @@ -61,6 +61,10 @@ chip_test_suite("tests") { "TestVariant.cpp", "TestZclString.cpp", ] + if (current_os != "mbed") { + test_sources += [ "TestCHIPArgParser.cpp" ] + } + sources = [] cflags = [ diff --git a/src/lib/support/tests/TestCHIPArgParser.cpp b/src/lib/support/tests/TestCHIPArgParser.cpp index c54e0223ca7e07..426758aff1e5c8 100644 --- a/src/lib/support/tests/TestCHIPArgParser.cpp +++ b/src/lib/support/tests/TestCHIPArgParser.cpp @@ -22,17 +22,16 @@ #include #include +#include + #include #include #include #include #include #include -#include #include -#if CHIP_CONFIG_ENABLE_ARG_PARSER - using namespace chip::ArgParser; static bool HandleOption(const char * progName, OptionSet * optSet, int id, const char * name, const char * arg); @@ -40,71 +39,68 @@ static bool HandleNonOptionArgs(const char * progName, int argc, char * const ar static void HandleArgError(const char * msg, ...); static void ClearCallbackRecords(); -#define DEBUG_TESTS 0 - -#define QuitWithError(MSG) \ - do \ - { \ - fprintf(stderr, "%s FAILED (line %d): ", __FUNCTION__, __LINE__); \ - fputs(MSG, stderr); \ - fputs("\n", stderr); \ - exit(EXIT_FAILURE); \ - } while (0) +class TestCHIPArgParser : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() + { + ClearCallbackRecords(); + chip::Platform::MemoryShutdown(); + } +}; -#define VerifyOrQuit(TST, MSG) \ - do \ - { \ - if (!(TST)) \ - { \ - QuitWithError(MSG); \ - } \ - } while (0) +#define DEBUG_TESTS 0 #define VerifyHandleOptionCallback(INDEX, EXPECT_PROG_NAME, EXPECT_OPTSET, EXPECT_ID, EXPECT_NAME, EXPECT_ARG) \ do \ { \ CallbackRecord & rec = sCallbackRecords[INDEX]; \ const char * arg = EXPECT_ARG; \ - VerifyOrQuit(rec.Type == CallbackRecord::kHandleOption, "Invalid callback type (expected HandleOption)"); \ - VerifyOrQuit(strcmp(rec.ProgName, EXPECT_PROG_NAME) == 0, "Invalid value for HandleOption argument: progName"); \ - VerifyOrQuit(rec.OptSet == EXPECT_OPTSET, "Invalid value for HandleOption argument: optSet"); \ - VerifyOrQuit(rec.Id == EXPECT_ID, "Invalid value for HandleOption argument: id"); \ - VerifyOrQuit(strcmp(rec.Name, EXPECT_NAME) == 0, "Invalid value for HandleOption argument: name"); \ + ASSERT_EQ(rec.Type, CallbackRecord::kHandleOption) << "Invalid callback type (expected HandleOption)"; \ + ASSERT_STREQ(rec.ProgName, EXPECT_PROG_NAME) << "Invalid value for HandleOption argument: progName"; \ + ASSERT_EQ(rec.OptSet, EXPECT_OPTSET) << "Invalid value for HandleOption argument: optSet"; \ + ASSERT_EQ(rec.Id, EXPECT_ID) << "Invalid value for HandleOption argument: id"; \ + ASSERT_STREQ(rec.Name, EXPECT_NAME) << "Invalid value for HandleOption argument: name"; \ if (arg != NULL) \ - VerifyOrQuit(strcmp(rec.Arg, arg) == 0, "Invalid value for HandleOption argument: arg"); \ + { \ + ASSERT_STREQ(rec.Arg, arg) << "Invalid value for HandleOption argument: arg"; \ + } \ else \ - VerifyOrQuit(rec.Arg == NULL, "Invalid value for HandleOption argument: arg"); \ + { \ + ASSERT_EQ(rec.Arg, nullptr) << "Invalid value for HandleOption argument: arg"; \ + } \ } while (0) #define VerifyHandleNonOptionArgsCallback(INDEX, EXPECT_PROG_NAME, EXPECT_ARGC) \ do \ { \ CallbackRecord & rec = sCallbackRecords[INDEX]; \ - VerifyOrQuit(rec.Type == CallbackRecord::kHandleNonOptionArgs, "Invalid callback type (expected HandleNonOptionArgs)"); \ - VerifyOrQuit(strcmp(rec.ProgName, EXPECT_PROG_NAME) == 0, "Invalid value for HandleNonOptionArgs argument: progName"); \ - VerifyOrQuit(rec.Argc == EXPECT_ARGC, "Invalid value for HandleNonOptionArgs argument: argc"); \ + ASSERT_EQ(rec.Type, CallbackRecord::kHandleNonOptionArgs) << "Invalid callback type (expected HandleNonOptionArgs)"; \ + ASSERT_STREQ(rec.ProgName, EXPECT_PROG_NAME) << "Invalid value for HandleNonOptionArgs argument: progName"; \ + ASSERT_EQ(rec.Argc, EXPECT_ARGC) << "Invalid value for HandleNonOptionArgs argument: argc"; \ } while (0) #define VerifyNonOptionArg(INDEX, EXPECT_ARG) \ do \ { \ CallbackRecord & rec = sCallbackRecords[INDEX]; \ - VerifyOrQuit(rec.Type == CallbackRecord::kNonOptionArg, "Invalid callback type (expected NonOptionArg)"); \ - VerifyOrQuit(strcmp(rec.Arg, EXPECT_ARG) == 0, "Invalid value for NonOptionArg"); \ + ASSERT_EQ(rec.Type, CallbackRecord::kNonOptionArg) << "Invalid callback type (expected NonOptionArg)"; \ + ASSERT_STREQ(rec.Arg, EXPECT_ARG) << "Invalid value for NonOptionArg"; \ } while (0) #define VerifyPrintArgErrorCallback(INDEX) \ do \ { \ CallbackRecord & rec = sCallbackRecords[INDEX]; \ - VerifyOrQuit(rec.Type == CallbackRecord::kArgError, "Invalid callback type (expected ArgError)"); \ + ASSERT_EQ(rec.Type, CallbackRecord::kArgError) << "Invalid callback type (expected ArgError)"; \ } while (0) #define VerifyArgErrorContains(INDEX, EXPECT_TEXT) \ do \ { \ CallbackRecord & rec = sCallbackRecords[INDEX]; \ - VerifyOrQuit(strstr(rec.Error, EXPECT_TEXT) != NULL, "Expected text not found in error output"); \ + ASSERT_NE(strstr(rec.Error, EXPECT_TEXT), nullptr) << "Expected text not found in error output"; \ } while (0) struct CallbackRecord @@ -212,7 +208,7 @@ TestArgv DupeArgs(const char * argv[], int argc_as_int) } // namespace -static void SimpleParseTest_SingleLongOption() +TEST_F(TestCHIPArgParser, SimpleParseTest_SingleLongOption) { bool res; @@ -232,13 +228,13 @@ static void SimpleParseTest_SingleLongOption() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == true, "ParseArgs() returned false"); - VerifyOrQuit(sCallbackRecordCount == 2, "Invalid value returned for sCallbackRecordCount"); + ASSERT_TRUE(res) << "ParseArgs() returned false"; + ASSERT_EQ(sCallbackRecordCount, 2u) << "Invalid value returned for sCallbackRecordCount"; VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetA, '1', "--foo", nullptr); VerifyHandleNonOptionArgsCallback(1, __FUNCTION__, 0); } -static void SimpleParseTest_SingleShortOption() +TEST_F(TestCHIPArgParser, SimpleParseTest_SingleShortOption) { bool res; @@ -258,13 +254,13 @@ static void SimpleParseTest_SingleShortOption() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == true, "ParseArgs() returned false"); - VerifyOrQuit(sCallbackRecordCount == 2, "Invalid value returned for sCallbackRecordCount"); + ASSERT_TRUE(res) << "ParseArgs() returned false"; + ASSERT_EQ(sCallbackRecordCount, 2u) << "Invalid value returned for sCallbackRecordCount"; VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetB, 's', "-s", nullptr); VerifyHandleNonOptionArgsCallback(1, __FUNCTION__, 0); } -static void SimpleParseTest_SingleLongOptionWithValue() +TEST_F(TestCHIPArgParser, SimpleParseTest_SingleLongOptionWithValue) { bool res; @@ -284,13 +280,13 @@ static void SimpleParseTest_SingleLongOptionWithValue() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == true, "ParseArgs() returned false"); - VerifyOrQuit(sCallbackRecordCount == 2, "Invalid value returned for sCallbackRecordCount"); + ASSERT_TRUE(res) << "ParseArgs() returned false"; + ASSERT_EQ(sCallbackRecordCount, 2u) << "Invalid value returned for sCallbackRecordCount"; VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetB, 1000, "--run", "run-value"); VerifyHandleNonOptionArgsCallback(1, __FUNCTION__, 0); } -static void SimpleParseTest_SingleShortOptionWithValue() +TEST_F(TestCHIPArgParser, SimpleParseTest_SingleShortOptionWithValue) { bool res; @@ -310,13 +306,13 @@ static void SimpleParseTest_SingleShortOptionWithValue() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == true, "ParseArgs() returned false"); - VerifyOrQuit(sCallbackRecordCount == 2, "Invalid value returned for sCallbackRecordCount"); + ASSERT_TRUE(res) << "ParseArgs() returned false"; + ASSERT_EQ(sCallbackRecordCount, 2u) << "Invalid value returned for sCallbackRecordCount"; VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetA, 'Z', "-Z", "baz-value"); VerifyHandleNonOptionArgsCallback(1, __FUNCTION__, 0); } -static void SimpleParseTest_VariousShortAndLongWithArgs() +TEST_F(TestCHIPArgParser, SimpleParseTest_VariousShortAndLongWithArgs) { bool res; @@ -345,8 +341,8 @@ static void SimpleParseTest_VariousShortAndLongWithArgs() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == true, "ParseArgs() returned false"); - VerifyOrQuit(sCallbackRecordCount == 12, "Invalid value returned for sCallbackRecordCount"); + ASSERT_TRUE(res) << "ParseArgs() returned false"; + ASSERT_EQ(sCallbackRecordCount, 12u) << "Invalid value returned for sCallbackRecordCount"; VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetA, '1', "--foo", nullptr); VerifyHandleOptionCallback(1, __FUNCTION__, &sOptionSetB, 1000, "--run", "run-value"); VerifyHandleOptionCallback(2, __FUNCTION__, &sOptionSetB, 's', "-s", nullptr); @@ -361,7 +357,7 @@ static void SimpleParseTest_VariousShortAndLongWithArgs() VerifyNonOptionArg(11, "non-opt-arg-4"); } -static void UnknownOptionTest_UnknownShortOption() +TEST_F(TestCHIPArgParser, UnknownOptionTest_UnknownShortOption) { bool res; @@ -385,8 +381,8 @@ static void UnknownOptionTest_UnknownShortOption() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == false, "ParseArgs() returned true"); - VerifyOrQuit(sCallbackRecordCount == 3, "Invalid value returned for sCallbackRecordCount"); + ASSERT_FALSE(res) << "ParseArgs() returned true"; + ASSERT_EQ(sCallbackRecordCount, 3u) << "Invalid value returned for sCallbackRecordCount"; VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetA, '1', "--foo", nullptr); VerifyHandleOptionCallback(1, __FUNCTION__, &sOptionSetB, 1000, "--run", "run-value"); VerifyPrintArgErrorCallback(2); @@ -394,7 +390,7 @@ static void UnknownOptionTest_UnknownShortOption() VerifyArgErrorContains(2, "-q"); } -static void UnknownOptionTest_UnknownLongOption() +TEST_F(TestCHIPArgParser, UnknownOptionTest_UnknownLongOption) { bool res; @@ -418,8 +414,8 @@ static void UnknownOptionTest_UnknownLongOption() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == false, "ParseArgs() returned true"); - VerifyOrQuit(sCallbackRecordCount == 3, "Invalid value returned for sCallbackRecordCount"); + ASSERT_FALSE(res) << "ParseArgs() returned true"; + ASSERT_EQ(sCallbackRecordCount, 3u) << "Invalid value returned for sCallbackRecordCount"; VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetA, '1', "--foo", nullptr); VerifyHandleOptionCallback(1, __FUNCTION__, &sOptionSetB, 1000, "--run", "run-value"); VerifyPrintArgErrorCallback(2); @@ -427,7 +423,7 @@ static void UnknownOptionTest_UnknownLongOption() VerifyArgErrorContains(2, "--bad"); } -static void UnknownOptionTest_UnknownShortOptionAfterKnown() +TEST_F(TestCHIPArgParser, UnknownOptionTest_UnknownShortOptionAfterKnown) { bool res; @@ -451,8 +447,8 @@ static void UnknownOptionTest_UnknownShortOptionAfterKnown() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == false, "ParseArgs() returned true"); - VerifyOrQuit(sCallbackRecordCount == 4, "Invalid value returned for sCallbackRecordCount"); + ASSERT_FALSE(res) << "ParseArgs() returned true"; + ASSERT_EQ(sCallbackRecordCount, 4u) << "Invalid value returned for sCallbackRecordCount"; VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetA, '1', "--foo", nullptr); VerifyHandleOptionCallback(1, __FUNCTION__, &sOptionSetB, 1000, "--run", "run-value"); VerifyHandleOptionCallback(2, __FUNCTION__, &sOptionSetA, '1', "-1", nullptr); @@ -461,7 +457,7 @@ static void UnknownOptionTest_UnknownShortOptionAfterKnown() VerifyArgErrorContains(3, "-Q"); } -static void UnknownOptionTest_UnknownShortOptionBeforeKnown() +TEST_F(TestCHIPArgParser, UnknownOptionTest_UnknownShortOptionBeforeKnown) { bool res; @@ -481,14 +477,14 @@ static void UnknownOptionTest_UnknownShortOptionBeforeKnown() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == false, "ParseArgs() returned true"); - VerifyOrQuit(sCallbackRecordCount == 1, "Invalid value returned for sCallbackRecordCount"); + ASSERT_FALSE(res) << "ParseArgs() returned true"; + ASSERT_EQ(sCallbackRecordCount, 1u) << "Invalid value returned for sCallbackRecordCount"; VerifyPrintArgErrorCallback(0); VerifyArgErrorContains(0, "Unknown"); VerifyArgErrorContains(0, "-Q"); } -static void UnknownOptionTest_UnknownShortOptionAfterArgs() +TEST_F(TestCHIPArgParser, UnknownOptionTest_UnknownShortOptionAfterArgs) { bool res; @@ -510,14 +506,14 @@ static void UnknownOptionTest_UnknownShortOptionAfterArgs() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == false, "ParseArgs() returned true"); - VerifyOrQuit(sCallbackRecordCount == 1, "Invalid value returned for sCallbackRecordCount"); + ASSERT_FALSE(res) << "ParseArgs() returned true"; + ASSERT_EQ(sCallbackRecordCount, 1u) << "Invalid value returned for sCallbackRecordCount"; VerifyPrintArgErrorCallback(0); VerifyArgErrorContains(0, "Unknown"); VerifyArgErrorContains(0, "-Q"); } -static void UnknownOptionTest_UnknownLongOptionAfterArgs() +TEST_F(TestCHIPArgParser, UnknownOptionTest_UnknownLongOptionAfterArgs) { bool res; @@ -539,15 +535,14 @@ static void UnknownOptionTest_UnknownLongOptionAfterArgs() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs); - VerifyOrQuit(res == false, "ParseArgs() returned true"); - VerifyOrQuit(sCallbackRecordCount == 1, "Invalid value returned for sCallbackRecordCount"); + ASSERT_FALSE(res) << "ParseArgs() returned true"; + ASSERT_EQ(sCallbackRecordCount, 1u) << "Invalid value returned for sCallbackRecordCount"; VerifyPrintArgErrorCallback(0); VerifyArgErrorContains(0, "Unknown"); VerifyArgErrorContains(0, "--barf"); } -#ifndef CHIP_CONFIG_NON_POSIX_LONG_OPT -static void UnknownOptionTest_IgnoreUnknownLongOption() +TEST_F(TestCHIPArgParser, UnknownOptionTest_IgnoreUnknownLongOption) { bool res; @@ -570,18 +565,17 @@ static void UnknownOptionTest_IgnoreUnknownLongOption() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs, true); - VerifyOrQuit(res == true, "ParseArgs() returned false"); + ASSERT_TRUE(res) << "ParseArgs() returned false"; - VerifyOrQuit(sCallbackRecordCount == 4, "Invalid value returned for sCallbackRecordCount"); + ASSERT_EQ(sCallbackRecordCount, 4u) << "Invalid value returned for sCallbackRecordCount"; VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetA, 'Z', "-Z", "baz-value"); VerifyHandleNonOptionArgsCallback(1, __FUNCTION__, 2); VerifyNonOptionArg(2, "non-opt-arg-1"); VerifyNonOptionArg(3, "non-opt-arg-2"); } -#endif // !CHIP_CONFIG_NON_POSIX_LONG_OPT -static void UnknownOptionTest_IgnoreUnknownShortOption() +TEST_F(TestCHIPArgParser, UnknownOptionTest_IgnoreUnknownShortOption) { bool res; @@ -604,9 +598,9 @@ static void UnknownOptionTest_IgnoreUnknownShortOption() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs, true); - VerifyOrQuit(res == true, "ParseArgs() returned false"); + ASSERT_TRUE(res) << "ParseArgs() returned false"; - VerifyOrQuit(sCallbackRecordCount == 5, "Invalid value returned for sCallbackRecordCount"); + ASSERT_EQ(sCallbackRecordCount, 5u) << "Invalid value returned for sCallbackRecordCount"; VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetA, '1', "-1", nullptr); VerifyHandleOptionCallback(1, __FUNCTION__, &sOptionSetA, 'Z', "-Z", "baz-value"); @@ -615,7 +609,7 @@ static void UnknownOptionTest_IgnoreUnknownShortOption() VerifyNonOptionArg(4, "non-opt-arg-2"); } -static void MissingValueTest_MissingShortOptionValue() +TEST_F(TestCHIPArgParser, MissingValueTest_MissingShortOptionValue) { bool res; @@ -635,14 +629,14 @@ static void MissingValueTest_MissingShortOptionValue() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs, true); - VerifyOrQuit(res == false, "ParseArgs() returned true"); - VerifyOrQuit(sCallbackRecordCount == 1, "Invalid value returned for sCallbackRecordCount"); + ASSERT_FALSE(res) << "ParseArgs() returned true"; + ASSERT_EQ(sCallbackRecordCount, 1u) << "Invalid value returned for sCallbackRecordCount"; VerifyPrintArgErrorCallback(0); VerifyArgErrorContains(0, "Missing"); VerifyArgErrorContains(0, "-Z"); } -static void MissingValueTest_MissingLongOptionValue() +TEST_F(TestCHIPArgParser, MissingValueTest_MissingLongOptionValue) { bool res; @@ -662,8 +656,8 @@ static void MissingValueTest_MissingLongOptionValue() TestArgv argsDup = DupeArgs(argv, argc); res = ParseArgs(__FUNCTION__, argc, argsDup.argv.Get(), optionSets, HandleNonOptionArgs, true); - VerifyOrQuit(res == false, "ParseArgs() returned true"); - VerifyOrQuit(sCallbackRecordCount == 1, "Invalid value returned for sCallbackRecordCount"); + ASSERT_FALSE(res) << "ParseArgs() returned true"; + ASSERT_EQ(sCallbackRecordCount, 1u) << "Invalid value returned for sCallbackRecordCount"; VerifyPrintArgErrorCallback(0); VerifyArgErrorContains(0, "Missing"); VerifyArgErrorContains(0, "--run"); @@ -692,7 +686,7 @@ static bool HandleOption(const char * progName, OptionSet * optSet, int id, cons printf("HandleOption called: progName:%s optSet:%08lX id:%d name:%s arg:%s\n", progName, (intptr_t) optSet, id, name, arg); #endif - VerifyOrQuit(sCallbackRecordCount < kMaxCallbackRecords, "Out of callback records"); + EXPECT_LT(sCallbackRecordCount, kMaxCallbackRecords) << "Out of callback records"; sCallbackRecords[sCallbackRecordCount].Type = CallbackRecord::kHandleOption; sCallbackRecords[sCallbackRecordCount].ProgName = chip::Platform::MemoryAllocString(progName, strlen(progName)); sCallbackRecords[sCallbackRecordCount].OptSet = optSet; @@ -717,7 +711,7 @@ static bool HandleNonOptionArgs(const char * progName, int argc, char * const ar // clang-format on #endif - VerifyOrQuit(sCallbackRecordCount < kMaxCallbackRecords, "Out of callback records"); + EXPECT_LT(sCallbackRecordCount, kMaxCallbackRecords) << "Out of callback records"; sCallbackRecords[sCallbackRecordCount].Type = CallbackRecord::kHandleNonOptionArgs; sCallbackRecords[sCallbackRecordCount].ProgName = chip::Platform::MemoryAllocString(progName, strlen(progName)); sCallbackRecords[sCallbackRecordCount].Argc = argc; @@ -725,7 +719,7 @@ static bool HandleNonOptionArgs(const char * progName, int argc, char * const ar for (int i = 0; i < argc; i++) { - VerifyOrQuit(sCallbackRecordCount < kMaxCallbackRecords, "Out of callback records"); + EXPECT_LT(sCallbackRecordCount, kMaxCallbackRecords) << "Out of callback records"; sCallbackRecords[sCallbackRecordCount].Type = CallbackRecord::kNonOptionArg; sCallbackRecords[sCallbackRecordCount].Arg = chip::Platform::MemoryAllocString(argv[i], strlen(argv[i])); sCallbackRecordCount++; @@ -740,7 +734,7 @@ static void ENFORCE_FORMAT(1, 2) HandleArgError(const char * msg, ...) int status; va_list ap; - VerifyOrQuit(sCallbackRecordCount < kMaxCallbackRecords, "Out of callback records"); + ASSERT_LT(sCallbackRecordCount, kMaxCallbackRecords) << "Out of callback records"; sCallbackRecords[sCallbackRecordCount].Type = CallbackRecord::kArgError; @@ -760,51 +754,3 @@ static void ENFORCE_FORMAT(1, 2) HandleArgError(const char * msg, ...) sCallbackRecordCount++; } - -int TestCHIPArgParser() -{ - if (chip::Platform::MemoryInit() != CHIP_NO_ERROR) - { - return EXIT_FAILURE; - } - - SimpleParseTest_SingleLongOption(); - SimpleParseTest_SingleShortOption(); - SimpleParseTest_SingleLongOptionWithValue(); - SimpleParseTest_SingleShortOptionWithValue(); - SimpleParseTest_VariousShortAndLongWithArgs(); - - UnknownOptionTest_UnknownShortOption(); - UnknownOptionTest_UnknownLongOption(); - UnknownOptionTest_UnknownShortOptionAfterArgs(); - UnknownOptionTest_UnknownShortOptionAfterKnown(); - UnknownOptionTest_UnknownShortOptionBeforeKnown(); - UnknownOptionTest_UnknownLongOptionAfterArgs(); - UnknownOptionTest_IgnoreUnknownShortOption(); - - /* Skip this test because the parser successfully captures all the options - but the error reporting is incorrect in this case due to long_opt limitations */ -#ifndef CHIP_CONFIG_NON_POSIX_LONG_OPT - UnknownOptionTest_IgnoreUnknownLongOption(); -#endif // !CHIP_CONFIG_NON_POSIX_LONG_OPT - - MissingValueTest_MissingShortOptionValue(); - MissingValueTest_MissingLongOptionValue(); - - ClearCallbackRecords(); - - printf("All tests succeeded\n"); - - chip::Platform::MemoryShutdown(); - - return (EXIT_SUCCESS); -} -#else // CHIP_CONFIG_ENABLE_ARG_PARSER -int TestCHIPArgParser(void) -{ - printf("No tests were run\n"); - return (EXIT_SUCCESS); -} -#endif // CHIP_CONFIG_ENABLE_ARG_PARSER - -CHIP_REGISTER_TEST_SUITE(TestCHIPArgParser);