From 0046d04b48a64f56443918e90f225454b21f9326 Mon Sep 17 00:00:00 2001 From: Maksymilian Knust Date: Fri, 19 Apr 2024 10:50:42 +0200 Subject: [PATCH 1/3] Replaced nlunit-test with pw_unit_test in src/lib/format/ --- src/lib/format/tests/BUILD.gn | 5 +- src/lib/format/tests/TestDecoding.cpp | 125 +++++++----------- src/lib/format/tests/TestFlatTree.cpp | 53 +++----- src/lib/format/tests/TestFlatTreePosition.cpp | 99 ++++++-------- 4 files changed, 112 insertions(+), 170 deletions(-) diff --git a/src/lib/format/tests/BUILD.gn b/src/lib/format/tests/BUILD.gn index 64be2e38a3f402..1001a3bd9e77d4 100644 --- a/src/lib/format/tests/BUILD.gn +++ b/src/lib/format/tests/BUILD.gn @@ -14,12 +14,11 @@ import("//build_overrides/build.gni") import("//build_overrides/chip.gni") -import("//build_overrides/nlunit_test.gni") import("${chip_root}/build/chip/chip_test_suite.gni") import("${chip_root}/build/chip/fuzz_test.gni") -chip_test_suite_using_nltest("tests") { +chip_test_suite("tests") { output_name = "libFormatTests" test_sources = [ @@ -40,8 +39,6 @@ chip_test_suite_using_nltest("tests") { "${chip_root}/src/lib/format:flat-tree", "${chip_root}/src/lib/format:protocol-decoder", "${chip_root}/src/lib/format:protocol-tlv-metadata", - "${chip_root}/src/lib/support:testing_nlunit", - "${nlunit_test_root}:nlunit-test", ] } diff --git a/src/lib/format/tests/TestDecoding.cpp b/src/lib/format/tests/TestDecoding.cpp index 0a848e5de8f835..125961a219efa2 100644 --- a/src/lib/format/tests/TestDecoding.cpp +++ b/src/lib/format/tests/TestDecoding.cpp @@ -17,12 +17,12 @@ #include #include #include -#include + #include #include -#include +#include #include "sample_data.h" @@ -57,7 +57,7 @@ const std::array, 53 + 2> fake_protocols_meta = { { { 2, _FakeProtocolData }, } }; -void TestSampleData(nlTestSuite * inSuite, const PayloadDecoderInitParams & params, const SamplePayload & data, +void TestSampleData(const PayloadDecoderInitParams & params, const SamplePayload & data, const char * expectation) { chip::Decoders::PayloadDecoder<64, 128> decoder( @@ -130,23 +130,23 @@ void TestSampleData(nlTestSuite * inSuite, const PayloadDecoderInitParams & para printf("ACTUAL: '%s'\n", partial.Reset().Add(output_builder.c_str() + idx).AddMarkerIfOverflow().c_str()); } - NL_TEST_ASSERT(inSuite, strcmp(output_builder.c_str(), expectation) == 0); + EXPECT_EQ(strcmp(output_builder.c_str(), expectation), 0); } -void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) +TEST(TestDecoding, TestFullDataDecoding) { PayloadDecoderInitParams params; params.SetProtocolDecodeTree(chip::TLVMeta::protocols_meta).SetClusterDecodeTree(chip::TLVMeta::clusters_meta); - TestSampleData(inSuite, params, secure_channel_mrp_ack, "mrp_ack: EMPTY\n"); - TestSampleData(inSuite, params, secure_channel_pkbdf_param_request, + TestSampleData(params, secure_channel_mrp_ack, "mrp_ack: EMPTY\n"); + TestSampleData(params, secure_channel_pkbdf_param_request, "pbkdf_param_request\n" " initiator_random: hex:7C8698755B8E9866BB4FFDC27B733F3B6EF7F83D43FBE0CA6AD2B8C52C8F4236\n" " initiator_session_id: 37677\n" " passcode_id: 0\n" " has_pbkdf_parameters: false\n"); - TestSampleData(inSuite, params, secure_channel_pkbdf_param_response, + TestSampleData(params, secure_channel_pkbdf_param_response, "pbkdf_param_response\n" " initiator_random: hex:7C8698755B8E9866BB4FFDC27B733F3B6EF7F83D43FBE0CA6AD2B8C52C8F4236\n" " responder_random: hex:A44EB3E1A751A88A32BAB59EF16EB9764C20E1A9DDBEF6EFE3F588C943C58424\n" @@ -154,24 +154,24 @@ void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) " pbkdf_parameters\n" " iterations: 1000\n" " salt: hex:E8FC1E6FD0023422B3CA7ECEDD344444551C814D3D0B0EB9C096F00E8A8051B2\n"); - TestSampleData(inSuite, params, secure_channel_pase_pake1, + TestSampleData(params, secure_channel_pase_pake1, // clang-format off "pase_pake1\n" " pA: hex:0422ABC7A84352850456BD4A510905FE6BB782A0863A9382550E1228020801B22EEC4102C60F80082842B9739705FCD37F134651442A41E3723DFFE0...\n" // clang-format on ); - TestSampleData(inSuite, params, secure_channel_pase_pake2, + TestSampleData(params, secure_channel_pase_pake2, // clang-format off "pase_pake2\n" " pB: hex:04B6A44A3347C6B77900A3674CA19F40F25F056F8CB344EC1B4FA7888B9E6B570B7010431C5D0BE4021FE74A96C40721765FDA6802BE8DFDF5624332...\n" " cB: hex:40E7452275E38AEBAF0E0F6FAB33A1B0CB5AEB5E824230DD40D0071DC7E55C87\n" // clang-format on ); - TestSampleData(inSuite, params, secure_channel_pase_pake3, + TestSampleData(params, secure_channel_pase_pake3, "pase_pake3\n" " cA: hex:6008C72EDEC9D25D4A36522F0BF23058F9378EFE38CBBCCE8C6853900169BC38\n"); - TestSampleData(inSuite, params, secure_channel_status_report, "status_report: BINARY DATA\n"); - TestSampleData(inSuite, params, im_protocol_read_request, + TestSampleData(params, secure_channel_status_report, "status_report: BINARY DATA\n"); + TestSampleData(params, im_protocol_read_request, "read_request\n" " attribute_requests\n" " Anonymous<>\n" @@ -206,7 +206,7 @@ void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) " attribute_id: 3 == 'connectMaxTimeSeconds'\n" " fabric_filtered: false\n" " interaction_model_revison: 1\n"); - TestSampleData(inSuite, params, im_protocol_report_data, + TestSampleData(params, im_protocol_report_data, "report_data\n" " attribute_reports\n" " Anonymous<>\n" @@ -279,7 +279,7 @@ void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) " interaction_model_revison: 1\n"); // Different content - TestSampleData(inSuite, params, im_protocol_report_data_acl, + TestSampleData(params, im_protocol_report_data_acl, "report_data\n" " attribute_reports\n" " Anonymous<>\n" @@ -301,7 +301,7 @@ void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) " interaction_model_revison: 1\n"); TestSampleData( - inSuite, params, im_protocol_report_data_window_covering, + params, im_protocol_report_data_window_covering, "report_data\n" " attribute_reports\n" " Anonymous<>\n" @@ -315,7 +315,7 @@ void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) " suppress_response: true\n" " interaction_model_revison: 1\n"); - TestSampleData(inSuite, params, im_protocol_invoke_request, + TestSampleData(params, im_protocol_invoke_request, "invoke_request\n" " suppress_response: false\n" " timed_request: false\n" @@ -328,7 +328,7 @@ void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) " OnOff::Toggle\n" " interaction_model_revison: 1\n"); - TestSampleData(inSuite, params, im_protocol_invoke_response, + TestSampleData(params, im_protocol_invoke_response, "invoke_response\n" " suppress_response: false\n" " invoke_responses\n" @@ -342,7 +342,7 @@ void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) " status: 0 == kSuccess\n" " interaction_model_revison: 1\n"); - TestSampleData(inSuite, params, im_protocol_invoke_request_change_channel, + TestSampleData(params, im_protocol_invoke_request_change_channel, "invoke_request\n" " suppress_response: false\n" " timed_request: false\n" @@ -356,7 +356,7 @@ void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) " match: \"channel name\"\n" " interaction_model_revison: 1\n"); - TestSampleData(inSuite, params, im_protocol_event_software_fault, + TestSampleData(params, im_protocol_event_software_fault, "report_data\n" " event_reports\n" " Anonymous<>\n" @@ -375,7 +375,7 @@ void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) " suppress_response: true\n" " interaction_model_revison: 1\n"); - TestSampleData(inSuite, params, im_protocol_event_multipress, + TestSampleData(params, im_protocol_event_multipress, "report_data\n" " event_reports\n" " Anonymous<>\n" @@ -394,22 +394,22 @@ void TestFullDataDecoding(nlTestSuite * inSuite, void * inContext) " interaction_model_revison: 1\n"); } -void TestMetaDataOnlyDecoding(nlTestSuite * inSuite, void * inContext) +TEST(TestDecoding, TestMetaDataOnlyDecoding) { PayloadDecoderInitParams params; // NO CLUSTER DECODE TREE params.SetProtocolDecodeTree(chip::TLVMeta::protocols_meta); - TestSampleData(inSuite, params, secure_channel_mrp_ack, "mrp_ack: EMPTY\n"); - TestSampleData(inSuite, params, secure_channel_pkbdf_param_request, + TestSampleData(params, secure_channel_mrp_ack, "mrp_ack: EMPTY\n"); + TestSampleData(params, secure_channel_pkbdf_param_request, "pbkdf_param_request\n" " initiator_random: hex:7C8698755B8E9866BB4FFDC27B733F3B6EF7F83D43FBE0CA6AD2B8C52C8F4236\n" " initiator_session_id: 37677\n" " passcode_id: 0\n" " has_pbkdf_parameters: false\n"); - TestSampleData(inSuite, params, im_protocol_read_request, + TestSampleData(params, im_protocol_read_request, "read_request\n" " attribute_requests\n" " Anonymous<>\n" @@ -444,7 +444,7 @@ void TestMetaDataOnlyDecoding(nlTestSuite * inSuite, void * inContext) " attribute_id: 3\n" " fabric_filtered: false\n" " interaction_model_revison: 1\n"); - TestSampleData(inSuite, params, im_protocol_report_data, + TestSampleData(params, im_protocol_report_data, "report_data\n" " attribute_reports\n" " Anonymous<>\n" @@ -515,7 +515,7 @@ void TestMetaDataOnlyDecoding(nlTestSuite * inSuite, void * inContext) " interaction_model_revison: 1\n"); // Different content - TestSampleData(inSuite, params, im_protocol_report_data_acl, + TestSampleData(params, im_protocol_report_data_acl, "report_data\n" " attribute_reports\n" " Anonymous<>\n" @@ -530,14 +530,14 @@ void TestMetaDataOnlyDecoding(nlTestSuite * inSuite, void * inContext) " interaction_model_revison: 1\n"); } -void TestEmptyClusterMetaDataDecode(nlTestSuite * inSuite, void * inContext) +TEST(TestDecoding, TestEmptyClusterMetaDataDecode) { PayloadDecoderInitParams params; params.SetProtocolDecodeTree(chip::TLVMeta::protocols_meta).SetClusterDecodeTree(empty_meta); - TestSampleData(inSuite, params, secure_channel_mrp_ack, "mrp_ack: EMPTY\n"); - TestSampleData(inSuite, params, im_protocol_report_data_acl, + TestSampleData(params, secure_channel_mrp_ack, "mrp_ack: EMPTY\n"); + TestSampleData(params, im_protocol_report_data_acl, "report_data\n" " attribute_reports\n" " Anonymous<>\n" @@ -559,24 +559,24 @@ void TestEmptyClusterMetaDataDecode(nlTestSuite * inSuite, void * inContext) " interaction_model_revison: 1\n"); } -void TestMissingDecodeData(nlTestSuite * inSuite, void * inContext) +TEST(TestDecoding, TestMissingDecodeData) { PayloadDecoderInitParams params; params.SetProtocolDecodeTree(empty_meta).SetClusterDecodeTree(empty_meta); - TestSampleData(inSuite, params, secure_channel_mrp_ack, "PROTO(0x0, 0x10): UNKNOWN\n"); - TestSampleData(inSuite, params, im_protocol_report_data_acl, "PROTO(0x1, 0x5): UNKNOWN\n"); + TestSampleData(params, secure_channel_mrp_ack, "PROTO(0x0, 0x10): UNKNOWN\n"); + TestSampleData(params, im_protocol_report_data_acl, "PROTO(0x1, 0x5): UNKNOWN\n"); } -void TestWrongDecodeData(nlTestSuite * inSuite, void * inContext) +TEST(TestDecoding, TestWrongDecodeData) { PayloadDecoderInitParams params; params.SetProtocolDecodeTree(fake_protocols_meta).SetClusterDecodeTree(empty_meta); - TestSampleData(inSuite, params, secure_channel_mrp_ack, "proto16: EMPTY\n"); - TestSampleData(inSuite, params, im_protocol_report_data_acl, + TestSampleData(params, secure_channel_mrp_ack, "proto16: EMPTY\n"); + TestSampleData(params, im_protocol_report_data_acl, "proto5\n" " ContextTag(0x1)\n" " AnonymousTag()\n" @@ -598,7 +598,7 @@ void TestWrongDecodeData(nlTestSuite * inSuite, void * inContext) " ContextTag(0xFF): 1\n"); } -void TestNestingOverflow(nlTestSuite * inSuite, void * inContext) +TEST(TestDecoding, TestNestingOverflow) { PayloadDecoderInitParams params; params.SetProtocolDecodeTree(fake_protocols_meta).SetClusterDecodeTree(empty_meta); @@ -611,65 +611,62 @@ void TestNestingOverflow(nlTestSuite * inSuite, void * inContext) chip::TLV::TLVType unusedType; // Protocols start with an anonymous tagged structure, after which lists can be of any tags - NL_TEST_ASSERT(inSuite, writer.StartContainer(AnonymousTag(), kTLVType_Structure, unusedType) == CHIP_NO_ERROR); + EXPECT_EQ(writer.StartContainer(AnonymousTag(), kTLVType_Structure, unusedType), CHIP_NO_ERROR); // nesting overflow here for (uint8_t i = 0; i < 32; i++) { - NL_TEST_ASSERT(inSuite, writer.StartContainer(ContextTag(i), kTLVType_List, unusedType) == CHIP_NO_ERROR); + EXPECT_EQ(writer.StartContainer(ContextTag(i), kTLVType_List, unusedType), CHIP_NO_ERROR); } // Go back to 24 (still too much nesting) for (uint8_t i = 0; i < 8; i++) { - NL_TEST_ASSERT(inSuite, writer.EndContainer(kTLVType_List) == CHIP_NO_ERROR); + EXPECT_EQ(writer.EndContainer(kTLVType_List), CHIP_NO_ERROR); } for (uint8_t i = 0; i < 4; i++) { - NL_TEST_ASSERT( - inSuite, writer.StartContainer(ContextTag(static_cast(i + 0x10)), kTLVType_List, unusedType) == CHIP_NO_ERROR); + EXPECT_EQ(writer.StartContainer(ContextTag(static_cast(i + 0x10)), kTLVType_List, unusedType), CHIP_NO_ERROR); } for (uint8_t i = 0; i < 4; i++) { - NL_TEST_ASSERT(inSuite, writer.EndContainer(kTLVType_List) == CHIP_NO_ERROR); + EXPECT_EQ(writer.EndContainer(kTLVType_List), CHIP_NO_ERROR); } // Go back to 8 for (uint8_t i = 0; i < 16; i++) { - NL_TEST_ASSERT(inSuite, writer.EndContainer(kTLVType_List) == CHIP_NO_ERROR); + EXPECT_EQ(writer.EndContainer(kTLVType_List), CHIP_NO_ERROR); } for (uint8_t i = 0; i < 4; i++) { - NL_TEST_ASSERT( - inSuite, writer.StartContainer(ContextTag(static_cast(i + 0x20)), kTLVType_List, unusedType) == CHIP_NO_ERROR); + EXPECT_EQ(writer.StartContainer(ContextTag(static_cast(i + 0x20)), kTLVType_List, unusedType), CHIP_NO_ERROR); } for (uint8_t i = 0; i < 4; i++) { - NL_TEST_ASSERT(inSuite, writer.EndContainer(kTLVType_List) == CHIP_NO_ERROR); + EXPECT_EQ(writer.EndContainer(kTLVType_List), CHIP_NO_ERROR); } // Go back to 4 for (uint8_t i = 0; i < 4; i++) { - NL_TEST_ASSERT(inSuite, writer.EndContainer(kTLVType_List) == CHIP_NO_ERROR); + EXPECT_EQ(writer.EndContainer(kTLVType_List), CHIP_NO_ERROR); } for (uint8_t i = 0; i < 4; i++) { - NL_TEST_ASSERT( - inSuite, writer.StartContainer(ContextTag(static_cast(i + 0x30)), kTLVType_List, unusedType) == CHIP_NO_ERROR); + EXPECT_EQ(writer.StartContainer(ContextTag(static_cast(i + 0x30)), kTLVType_List, unusedType), CHIP_NO_ERROR); } for (uint8_t i = 0; i < 4; i++) { - NL_TEST_ASSERT(inSuite, writer.EndContainer(kTLVType_List) == CHIP_NO_ERROR); + EXPECT_EQ(writer.EndContainer(kTLVType_List), CHIP_NO_ERROR); } // close everything for (uint8_t i = 0; i < 4; i++) { - NL_TEST_ASSERT(inSuite, writer.EndContainer(kTLVType_List) == CHIP_NO_ERROR); + EXPECT_EQ(writer.EndContainer(kTLVType_List), CHIP_NO_ERROR); } - NL_TEST_ASSERT(inSuite, writer.EndContainer(kTLVType_Structure) == CHIP_NO_ERROR); + EXPECT_EQ(writer.EndContainer(kTLVType_Structure), CHIP_NO_ERROR); SamplePayload fake_payload{ chip::Protocols::InteractionModel::Id, 5, chip::ByteSpan(data_buffer, writer.GetLengthWritten()) }; - TestSampleData(inSuite, params, fake_payload, + TestSampleData(params, fake_payload, "proto5\n" " ContextTag(0x0)\n" " ContextTag(0x1)\n" @@ -696,24 +693,4 @@ void TestNestingOverflow(nlTestSuite * inSuite, void * inContext) " ContextTag(0x32)\n" " ContextTag(0x33)\n"); } - -const nlTest sTests[] = { - NL_TEST_DEF("TestFullDataDecoding", TestFullDataDecoding), // - NL_TEST_DEF("TestMetaDataOnlyDecoding", TestMetaDataOnlyDecoding), // - NL_TEST_DEF("TestEmptyClusterMetaDataDecode", TestEmptyClusterMetaDataDecode), // - NL_TEST_DEF("TestMissingDecodeData", TestMissingDecodeData), // - NL_TEST_DEF("TestWrongDecodeData", TestWrongDecodeData), // - NL_TEST_DEF("TestNestingOverflow", TestNestingOverflow), // - NL_TEST_SENTINEL() // -}; - } // namespace - -int TestDecode() -{ - nlTestSuite theSuite = { "TestDecode", sTests, nullptr, nullptr }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestDecode) diff --git a/src/lib/format/tests/TestFlatTree.cpp b/src/lib/format/tests/TestFlatTree.cpp index 8eebb984b6adb9..f14caa92d5c2d9 100644 --- a/src/lib/format/tests/TestFlatTree.cpp +++ b/src/lib/format/tests/TestFlatTree.cpp @@ -17,13 +17,13 @@ #include #include -#include + #include #include -#include +#include namespace { @@ -82,43 +82,28 @@ class ByName const char * mName; }; -void TestFlatTreeFind(nlTestSuite * inSuite, void * inContext) +TEST(TestFlatTree, TestFlatTreeFind) { - NL_TEST_ASSERT(inSuite, strcmp(FindEntry(tree, 0, ByTag(ContextTag(1)))->data.name, "hello") == 0); - NL_TEST_ASSERT(inSuite, strcmp(FindEntry(tree, 0, ByTag(ContextTag(2)))->data.name, "world") == 0); - NL_TEST_ASSERT(inSuite, FindEntry(tree, 0, ByTag(ContextTag(3))) == nullptr); + EXPECT_EQ(strcmp(FindEntry(tree, 0, ByTag(ContextTag(1)))->data.name, "hello"), 0); + EXPECT_EQ(strcmp(FindEntry(tree, 0, ByTag(ContextTag(2)))->data.name, "world"), 0); + EXPECT_EQ(FindEntry(tree, 0, ByTag(ContextTag(3))), nullptr); - NL_TEST_ASSERT(inSuite, FindEntry(tree, 0, ByName("hello"))->data.tag == ContextTag(1)); - NL_TEST_ASSERT(inSuite, FindEntry(tree, 0, ByName("world"))->data.tag == ContextTag(2)); - NL_TEST_ASSERT(inSuite, FindEntry(tree, 0, ByName("foo")) == nullptr); + EXPECT_EQ(FindEntry(tree, 0, ByName("hello"))->data.tag, ContextTag(1)); + EXPECT_EQ(FindEntry(tree, 0, ByName("world"))->data.tag, ContextTag(2)); + EXPECT_EQ(FindEntry(tree, 0, ByName("foo")), nullptr); - NL_TEST_ASSERT(inSuite, FindEntry(tree, 1, ByTag(ContextTag(1))) == nullptr); - NL_TEST_ASSERT(inSuite, strcmp(FindEntry(tree, 1, ByTag(ProfileTag(234, 2)))->data.name, "b") == 0); - NL_TEST_ASSERT(inSuite, strcmp(FindEntry(tree, 1, ByTag(ProfileTag(345, 3)))->data.name, "c") == 0); - NL_TEST_ASSERT(inSuite, FindEntry(tree, 1, ByTag(AnonymousTag())) == nullptr); + EXPECT_EQ(FindEntry(tree, 1, ByTag(ContextTag(1))), nullptr); + EXPECT_EQ(strcmp(FindEntry(tree, 1, ByTag(ProfileTag(234, 2)))->data.name, "b"), 0); + EXPECT_EQ(strcmp(FindEntry(tree, 1, ByTag(ProfileTag(345, 3)))->data.name, "c"), 0); + EXPECT_EQ(FindEntry(tree, 1, ByTag(AnonymousTag())), nullptr); - NL_TEST_ASSERT(inSuite, FindEntry(tree, 2, ByTag(ContextTag(1))) == nullptr); - NL_TEST_ASSERT(inSuite, strcmp(FindEntry(tree, 2, ByTag(AnonymousTag()))->data.name, "foo") == 0); + EXPECT_EQ(FindEntry(tree, 2, ByTag(ContextTag(1))), nullptr); + EXPECT_EQ(strcmp(FindEntry(tree, 2, ByTag(AnonymousTag()))->data.name, "foo"), 0); // out of array - NL_TEST_ASSERT(inSuite, FindEntry(tree, 3, ByTag(AnonymousTag())) == nullptr); - NL_TEST_ASSERT(inSuite, FindEntry(tree, 100, ByTag(AnonymousTag())) == nullptr); - NL_TEST_ASSERT(inSuite, FindEntry(tree, 1000, ByTag(AnonymousTag())) == nullptr); - NL_TEST_ASSERT(inSuite, FindEntry(tree, 9999999, ByTag(AnonymousTag())) == nullptr); + EXPECT_EQ(FindEntry(tree, 3, ByTag(AnonymousTag())), nullptr); + EXPECT_EQ(FindEntry(tree, 100, ByTag(AnonymousTag())), nullptr); + EXPECT_EQ(FindEntry(tree, 1000, ByTag(AnonymousTag())), nullptr); + EXPECT_EQ(FindEntry(tree, 9999999, ByTag(AnonymousTag())), nullptr); } - -const nlTest sTests[] = { - NL_TEST_DEF("TestFlatTreeFind", TestFlatTreeFind), // - NL_TEST_SENTINEL() // -}; - } // namespace - -int TestFlatTree() -{ - nlTestSuite theSuite = { "FlatTree", sTests, nullptr, nullptr }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestFlatTree) diff --git a/src/lib/format/tests/TestFlatTreePosition.cpp b/src/lib/format/tests/TestFlatTreePosition.cpp index ee5f6b3c94176d..907f0e16bd3da8 100644 --- a/src/lib/format/tests/TestFlatTreePosition.cpp +++ b/src/lib/format/tests/TestFlatTreePosition.cpp @@ -18,14 +18,14 @@ #include #include -#include + #include #include #include -#include +#include namespace { @@ -94,16 +94,16 @@ class ByName }; #define ASSERT_HAS_NAME(p, n) \ - NL_TEST_ASSERT(inSuite, p.Get() != nullptr); \ - NL_TEST_ASSERT(inSuite, strcmp(p.Get()->name, n) == 0) + EXPECT_NE(p.Get(), nullptr); \ + EXPECT_EQ(strcmp(p.Get()->name, n), 0) #define ASSERT_HAS_CONTEXT_TAG(p, t) \ - NL_TEST_ASSERT(inSuite, p.Get() != nullptr); \ - NL_TEST_ASSERT(inSuite, p.Get()->tag == ContextTag(t)) + EXPECT_NE(p.Get(), nullptr); \ + EXPECT_EQ(p.Get()->tag, ContextTag(t)) #define ASSERT_HAS_PROFILE_TAG(p, a, b) \ - NL_TEST_ASSERT(inSuite, p.Get() != nullptr); \ - NL_TEST_ASSERT(inSuite, p.Get()->tag == ProfileTag(a, b)) + EXPECT_NE(p.Get(), nullptr); \ + EXPECT_EQ(p.Get()->tag, ProfileTag(a, b)) template std::vector GetPath(Position & position) @@ -133,54 +133,54 @@ bool HasPath(const std::vector & v, Tag a, Tag b, Tag c) return (v.size() == 3) && (v[0] == a) && (v[1] == b) && (v[2] == c); } -void TestSimpleEnterExit(nlTestSuite * inSuite, void * inContext) +TEST(TestFlatTreePosition, TestSimpleEnterExit) { Position position(tree.data(), tree.size()); // at start, top of tree has no value - NL_TEST_ASSERT(inSuite, position.Get() == nullptr); - NL_TEST_ASSERT(inSuite, GetPath(position).empty()); + EXPECT_EQ(position.Get(), nullptr); + EXPECT_TRUE(GetPath(position).empty()); // Go to hello, try going to invalid 2x, then go back position.Enter(ByTag(ContextTag(1))); ASSERT_HAS_NAME(position, "hello"); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(1))); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(1))); position.Enter(ByTag(ContextTag(1))); - NL_TEST_ASSERT(inSuite, position.Get() == nullptr); - NL_TEST_ASSERT(inSuite, GetPath(position).empty()); + EXPECT_EQ(position.Get(), nullptr); + EXPECT_TRUE(GetPath(position).empty()); position.Enter(ByTag(ContextTag(1))); - NL_TEST_ASSERT(inSuite, position.Get() == nullptr); + EXPECT_EQ(position.Get(), nullptr); position.Exit(); - NL_TEST_ASSERT(inSuite, position.Get() == nullptr); + EXPECT_EQ(position.Get(), nullptr); position.Exit(); - NL_TEST_ASSERT(inSuite, position.Get() != nullptr); + ASSERT_NE(position.Get(), nullptr); ASSERT_HAS_NAME(position, "hello"); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(1))); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(1))); position.Exit(); - NL_TEST_ASSERT(inSuite, position.Get() == nullptr); + EXPECT_EQ(position.Get(), nullptr); } -void TestDeeperEnter(nlTestSuite * inSuite, void * inContext) +TEST(TestFlatTreePosition, TestDeeperEnter) { Position position(tree.data(), tree.size()); - NL_TEST_ASSERT(inSuite, position.Get() == nullptr); + EXPECT_EQ(position.Get(), nullptr); position.Enter(ByName("world")); ASSERT_HAS_CONTEXT_TAG(position, 2); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(2))); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(2))); position.Enter(ByTag(ProfileTag(123, 1))); ASSERT_HAS_NAME(position, "a"); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(2), ProfileTag(123, 1))); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(2), ProfileTag(123, 1))); position.Enter(ByTag(AnonymousTag())); - NL_TEST_ASSERT(inSuite, position.Get() == nullptr); - NL_TEST_ASSERT(inSuite, GetPath(position).empty()); + EXPECT_EQ(position.Get(), nullptr); + EXPECT_TRUE(GetPath(position).empty()); position.Exit(); ASSERT_HAS_NAME(position, "a"); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(2), ProfileTag(123, 1))); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(2), ProfileTag(123, 1))); position.Exit(); ASSERT_HAS_NAME(position, "world"); @@ -190,30 +190,30 @@ void TestDeeperEnter(nlTestSuite * inSuite, void * inContext) position.Enter(ByTag(AnonymousTag())); ASSERT_HAS_NAME(position, "foo"); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(2), ProfileTag(234, 2), AnonymousTag())); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(2), ProfileTag(234, 2), AnonymousTag())); // test some unknown for (int i = 0; i < 100; i++) { position.Enter(ByTag(AnonymousTag())); - NL_TEST_ASSERT(inSuite, position.Get() == nullptr); - NL_TEST_ASSERT(inSuite, GetPath(position).empty()); + EXPECT_EQ(position.Get(), nullptr); + EXPECT_TRUE(GetPath(position).empty()); } for (int i = 0; i < 100; i++) { - NL_TEST_ASSERT(inSuite, position.Get() == nullptr); - NL_TEST_ASSERT(inSuite, GetPath(position).empty()); + EXPECT_EQ(position.Get(), nullptr); + EXPECT_TRUE(GetPath(position).empty()); position.Exit(); } - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(2), ProfileTag(234, 2), AnonymousTag())); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(2), ProfileTag(234, 2), AnonymousTag())); ASSERT_HAS_NAME(position, "foo"); position.Exit(); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(2), ProfileTag(234, 2))); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(2), ProfileTag(234, 2))); ASSERT_HAS_NAME(position, "b"); position.Exit(); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(2))); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(2))); ASSERT_HAS_NAME(position, "world"); // root and stay there @@ -221,15 +221,15 @@ void TestDeeperEnter(nlTestSuite * inSuite, void * inContext) position.Exit(); position.Exit(); position.Exit(); - NL_TEST_ASSERT(inSuite, GetPath(position).empty()); + EXPECT_TRUE(GetPath(position).empty()); // can still navigate from the root position.Enter(ByName("world")); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(2))); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(2))); ASSERT_HAS_CONTEXT_TAG(position, 2); } -void TestDescendLimit(nlTestSuite * inSuite, void * inContext) +TEST(TestFlatTreePosition, TestDescendLimit) { Position position(tree.data(), tree.size()); @@ -241,34 +241,17 @@ void TestDescendLimit(nlTestSuite * inSuite, void * inContext) // only 2 positions can be remembered. Running out of space position.Enter(ByName("foo")); - NL_TEST_ASSERT(inSuite, position.Get() == nullptr); - NL_TEST_ASSERT(inSuite, GetPath(position).empty()); + EXPECT_EQ(position.Get(), nullptr); + EXPECT_TRUE(GetPath(position).empty()); position.Exit(); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(2), ProfileTag(234, 2))); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(2), ProfileTag(234, 2))); ASSERT_HAS_NAME(position, "b"); ASSERT_HAS_PROFILE_TAG(position, 234, 2); position.Exit(); - NL_TEST_ASSERT(inSuite, HasPath(GetPath(position), ContextTag(2))); + EXPECT_TRUE(HasPath(GetPath(position), ContextTag(2))); ASSERT_HAS_NAME(position, "world"); ASSERT_HAS_CONTEXT_TAG(position, 2); } - -const nlTest sTests[] = { - NL_TEST_DEF("TestSimpleEnterExit", TestSimpleEnterExit), // - NL_TEST_DEF("TestDeeperEnter", TestDeeperEnter), // - NL_TEST_DEF("TestDescendLimit", TestDescendLimit), // - NL_TEST_SENTINEL() // -}; - } // namespace - -int TestFlatTreePosition() -{ - nlTestSuite theSuite = { "FlatTree", sTests, nullptr, nullptr }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestFlatTreePosition) From 66d13bff564d4fe0dec4e87c17148c528027b49e Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 19 Apr 2024 08:58:30 +0000 Subject: [PATCH 2/3] Restyled by clang-format --- src/lib/format/tests/TestDecoding.cpp | 4 +--- src/lib/format/tests/TestFlatTree.cpp | 1 - src/lib/format/tests/TestFlatTreePosition.cpp | 7 +++---- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/lib/format/tests/TestDecoding.cpp b/src/lib/format/tests/TestDecoding.cpp index 125961a219efa2..982d59b811c32d 100644 --- a/src/lib/format/tests/TestDecoding.cpp +++ b/src/lib/format/tests/TestDecoding.cpp @@ -18,7 +18,6 @@ #include #include - #include #include @@ -57,8 +56,7 @@ const std::array, 53 + 2> fake_protocols_meta = { { { 2, _FakeProtocolData }, } }; -void TestSampleData(const PayloadDecoderInitParams & params, const SamplePayload & data, - const char * expectation) +void TestSampleData(const PayloadDecoderInitParams & params, const SamplePayload & data, const char * expectation) { chip::Decoders::PayloadDecoder<64, 128> decoder( PayloadDecoderInitParams(params).SetProtocol(data.protocolId).SetMessageType(data.messageType)); diff --git a/src/lib/format/tests/TestFlatTree.cpp b/src/lib/format/tests/TestFlatTree.cpp index f14caa92d5c2d9..e0ef327ae2a01f 100644 --- a/src/lib/format/tests/TestFlatTree.cpp +++ b/src/lib/format/tests/TestFlatTree.cpp @@ -18,7 +18,6 @@ #include - #include #include diff --git a/src/lib/format/tests/TestFlatTreePosition.cpp b/src/lib/format/tests/TestFlatTreePosition.cpp index 907f0e16bd3da8..3b8ad8a2e7807e 100644 --- a/src/lib/format/tests/TestFlatTreePosition.cpp +++ b/src/lib/format/tests/TestFlatTreePosition.cpp @@ -19,7 +19,6 @@ #include - #include #include @@ -94,15 +93,15 @@ class ByName }; #define ASSERT_HAS_NAME(p, n) \ - EXPECT_NE(p.Get(), nullptr); \ + EXPECT_NE(p.Get(), nullptr); \ EXPECT_EQ(strcmp(p.Get()->name, n), 0) #define ASSERT_HAS_CONTEXT_TAG(p, t) \ - EXPECT_NE(p.Get(), nullptr); \ + EXPECT_NE(p.Get(), nullptr); \ EXPECT_EQ(p.Get()->tag, ContextTag(t)) #define ASSERT_HAS_PROFILE_TAG(p, a, b) \ - EXPECT_NE(p.Get(), nullptr); \ + EXPECT_NE(p.Get(), nullptr); \ EXPECT_EQ(p.Get()->tag, ProfileTag(a, b)) template From 605168818a76a7655940b138f3234c34cf43c7c9 Mon Sep 17 00:00:00 2001 From: Maksymilian Knust Date: Mon, 22 Apr 2024 12:06:22 +0200 Subject: [PATCH 3/3] Use EXPECT_STREQ --- src/lib/format/tests/TestDecoding.cpp | 2 +- src/lib/format/tests/TestFlatTree.cpp | 10 +++++----- src/lib/format/tests/TestFlatTreePosition.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib/format/tests/TestDecoding.cpp b/src/lib/format/tests/TestDecoding.cpp index 982d59b811c32d..ae5c2efc553fb6 100644 --- a/src/lib/format/tests/TestDecoding.cpp +++ b/src/lib/format/tests/TestDecoding.cpp @@ -128,7 +128,7 @@ void TestSampleData(const PayloadDecoderInitParams & params, const SamplePayload printf("ACTUAL: '%s'\n", partial.Reset().Add(output_builder.c_str() + idx).AddMarkerIfOverflow().c_str()); } - EXPECT_EQ(strcmp(output_builder.c_str(), expectation), 0); + EXPECT_STREQ(output_builder.c_str(), expectation); } TEST(TestDecoding, TestFullDataDecoding) diff --git a/src/lib/format/tests/TestFlatTree.cpp b/src/lib/format/tests/TestFlatTree.cpp index e0ef327ae2a01f..fcf8e2713bf6b5 100644 --- a/src/lib/format/tests/TestFlatTree.cpp +++ b/src/lib/format/tests/TestFlatTree.cpp @@ -83,8 +83,8 @@ class ByName TEST(TestFlatTree, TestFlatTreeFind) { - EXPECT_EQ(strcmp(FindEntry(tree, 0, ByTag(ContextTag(1)))->data.name, "hello"), 0); - EXPECT_EQ(strcmp(FindEntry(tree, 0, ByTag(ContextTag(2)))->data.name, "world"), 0); + EXPECT_STREQ(FindEntry(tree, 0, ByTag(ContextTag(1)))->data.name, "hello"); + EXPECT_STREQ(FindEntry(tree, 0, ByTag(ContextTag(2)))->data.name, "world"); EXPECT_EQ(FindEntry(tree, 0, ByTag(ContextTag(3))), nullptr); EXPECT_EQ(FindEntry(tree, 0, ByName("hello"))->data.tag, ContextTag(1)); @@ -92,12 +92,12 @@ TEST(TestFlatTree, TestFlatTreeFind) EXPECT_EQ(FindEntry(tree, 0, ByName("foo")), nullptr); EXPECT_EQ(FindEntry(tree, 1, ByTag(ContextTag(1))), nullptr); - EXPECT_EQ(strcmp(FindEntry(tree, 1, ByTag(ProfileTag(234, 2)))->data.name, "b"), 0); - EXPECT_EQ(strcmp(FindEntry(tree, 1, ByTag(ProfileTag(345, 3)))->data.name, "c"), 0); + EXPECT_STREQ(FindEntry(tree, 1, ByTag(ProfileTag(234, 2)))->data.name, "b"); + EXPECT_STREQ(FindEntry(tree, 1, ByTag(ProfileTag(345, 3)))->data.name, "c"); EXPECT_EQ(FindEntry(tree, 1, ByTag(AnonymousTag())), nullptr); EXPECT_EQ(FindEntry(tree, 2, ByTag(ContextTag(1))), nullptr); - EXPECT_EQ(strcmp(FindEntry(tree, 2, ByTag(AnonymousTag()))->data.name, "foo"), 0); + EXPECT_STREQ(FindEntry(tree, 2, ByTag(AnonymousTag()))->data.name, "foo"); // out of array EXPECT_EQ(FindEntry(tree, 3, ByTag(AnonymousTag())), nullptr); diff --git a/src/lib/format/tests/TestFlatTreePosition.cpp b/src/lib/format/tests/TestFlatTreePosition.cpp index 3b8ad8a2e7807e..cf5a26e6c8662f 100644 --- a/src/lib/format/tests/TestFlatTreePosition.cpp +++ b/src/lib/format/tests/TestFlatTreePosition.cpp @@ -94,7 +94,7 @@ class ByName #define ASSERT_HAS_NAME(p, n) \ EXPECT_NE(p.Get(), nullptr); \ - EXPECT_EQ(strcmp(p.Get()->name, n), 0) + EXPECT_STREQ(p.Get()->name, n); #define ASSERT_HAS_CONTEXT_TAG(p, t) \ EXPECT_NE(p.Get(), nullptr); \