From 3380876f73968a29c27c326938c82b95b5b2b9da Mon Sep 17 00:00:00 2001 From: Amine Alami <43780877+Alami-Amine@users.noreply.github.com> Date: Wed, 24 Apr 2024 13:48:21 +0200 Subject: [PATCH] pw_unit_test migration: lib support batch #1 (#33091) * pw_unit_test migration: lib support batch #1 * apply restyled patch * integrate comments --- src/BUILD.gn | 1 + src/lib/support/tests/BUILD.gn | 40 +- src/lib/support/tests/TestBitMask.cpp | 60 +- src/lib/support/tests/TestBufferReader.cpp | 179 +++--- src/lib/support/tests/TestDefer.cpp | 30 +- .../tests/TestFixedBufferAllocator.cpp | 43 +- src/lib/support/tests/TestFold.cpp | 39 +- src/lib/support/tests/TestIniEscaping.cpp | 95 ++- src/lib/support/tests/TestSafeInt.cpp | 605 +++++++++--------- .../openiotsdk/unit-tests/test_components.txt | 3 +- .../unit-tests/test_components_nl.txt | 2 +- 11 files changed, 498 insertions(+), 599 deletions(-) diff --git a/src/BUILD.gn b/src/BUILD.gn index d283c7ec4defad..60ba11c0d28f61 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -98,6 +98,7 @@ 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/system/tests", "${chip_root}/src/transport/tests", diff --git a/src/lib/support/tests/BUILD.gn b/src/lib/support/tests/BUILD.gn index ab91a4f943deae..2a9c1ac81fe6a8 100644 --- a/src/lib/support/tests/BUILD.gn +++ b/src/lib/support/tests/BUILD.gn @@ -15,33 +15,61 @@ import("//build_overrides/build.gni") import("//build_overrides/chip.gni") import("//build_overrides/nlunit_test.gni") +import("//build_overrides/pigweed.gni") import("${chip_root}/build/chip/chip_test_suite.gni") -chip_test_suite_using_nltest("tests") { +chip_test_suite("tests") { output_name = "libSupportTests" test_sources = [ "TestBitMask.cpp", "TestBufferReader.cpp", + "TestDefer.cpp", + "TestFixedBufferAllocator.cpp", + "TestFold.cpp", + "TestIniEscaping.cpp", + "TestSafeInt.cpp", + ] + sources = [] + + 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/jsontlv", + "${chip_root}/src/platform", + ] +} + +chip_test_suite_using_nltest("tests_nltest") { + output_name = "libSupportTestsNL" + + test_sources = [ "TestBufferWriter.cpp", "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", - "TestSafeInt.cpp", "TestSafeString.cpp", "TestScoped.cpp", "TestScopedBuffer.cpp", diff --git a/src/lib/support/tests/TestBitMask.cpp b/src/lib/support/tests/TestBitMask.cpp index 36f603bb4577db..f19c90fb2e20af 100644 --- a/src/lib/support/tests/TestBitMask.cpp +++ b/src/lib/support/tests/TestBitMask.cpp @@ -14,13 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include -#include #include #include #include -#include using namespace chip; @@ -37,68 +36,68 @@ enum class TestEnum : uint16_t kBits_High8 = 0xFF00, }; -void TestBitMaskOperations(nlTestSuite * inSuite, void * inContext) +TEST(TestBitMask, TestBitMaskOperations) { BitMask mask; - NL_TEST_ASSERT(inSuite, mask.Raw() == 0); + EXPECT_EQ(mask.Raw(), 0); mask.SetField(TestEnum::kBits_1_2, 2); - NL_TEST_ASSERT(inSuite, mask.Raw() == 0x0004); + EXPECT_EQ(mask.Raw(), 0x0004); mask.SetRaw(0); mask.SetField(TestEnum::kBits_4_7, 0x0B); - NL_TEST_ASSERT(inSuite, mask.Raw() == 0x00B0); + EXPECT_EQ(mask.Raw(), 0x00B0); mask.SetRaw(0); for (uint16_t i = 0; i < 0x10; i++) { mask.SetField(TestEnum::kBits_High4, i); - NL_TEST_ASSERT(inSuite, mask.Raw() == (i << 12)); + EXPECT_EQ(mask.Raw(), (i << 12)); } mask.SetField(TestEnum::kBits_High8, 0x23); - NL_TEST_ASSERT(inSuite, mask.Raw() == 0x2300); + EXPECT_EQ(mask.Raw(), 0x2300); mask.SetField(TestEnum::kBits_High4, 0xA); - NL_TEST_ASSERT(inSuite, mask.Raw() == 0xA300); + EXPECT_EQ(mask.Raw(), 0xA300); } -void TestBitFieldLogic(nlTestSuite * inSuite, void * inContext) +TEST(TestBitMask, TestBitFieldLogic) { BitMask mask; // some general logic that still applies for bit fields just in case - NL_TEST_ASSERT(inSuite, !mask.HasAny(TestEnum::kBits_High4)); - NL_TEST_ASSERT(inSuite, !mask.HasAny(TestEnum::kBits_High8)); + EXPECT_FALSE(mask.HasAny(TestEnum::kBits_High4)); + EXPECT_FALSE(mask.HasAny(TestEnum::kBits_High8)); // setting something non-zero in the upper 4 bits sets "something" in both // upper and 4 and 8 bits mask.SetField(TestEnum::kBits_High4, 0x01); - NL_TEST_ASSERT(inSuite, mask.HasAny(TestEnum::kBits_High4)); - NL_TEST_ASSERT(inSuite, mask.HasAny(TestEnum::kBits_High8)); + EXPECT_TRUE(mask.HasAny(TestEnum::kBits_High4)); + EXPECT_TRUE(mask.HasAny(TestEnum::kBits_High8)); // sets something visible in high 8 bits, but not high 4 bits mask.SetField(TestEnum::kBits_High8, 0x01); - NL_TEST_ASSERT(inSuite, !mask.HasAny(TestEnum::kBits_High4)); - NL_TEST_ASSERT(inSuite, mask.HasAny(TestEnum::kBits_High8)); + EXPECT_FALSE(mask.HasAny(TestEnum::kBits_High4)); + EXPECT_TRUE(mask.HasAny(TestEnum::kBits_High8)); } -void TestBitMaskInvalid(nlTestSuite * inSuite, void * inContext) +TEST(TestBitMask, TestBitMaskInvalid) { BitMask mask; // This generally tests for no infinite loops. Nothing to set here mask.SetField(TestEnum::kZero, 0x01); - NL_TEST_ASSERT(inSuite, mask.Raw() == 0); + EXPECT_EQ(mask.Raw(), 0); mask.SetRaw(0x1234); mask.SetField(TestEnum::kZero, 0x01); - NL_TEST_ASSERT(inSuite, mask.Raw() == 0x1234); + EXPECT_EQ(mask.Raw(), 0x1234); } -void TestClear(nlTestSuite * inSuite, void * inContext) +TEST(TestBitMask, TestClear) { BitMask mask1; BitMask mask2; @@ -110,26 +109,7 @@ void TestClear(nlTestSuite * inSuite, void * inContext) mask2.Set(TestEnum::kBits_1_2); mask1.Clear(mask2); - NL_TEST_ASSERT(inSuite, mask1.Raw() == 0xFF01); + EXPECT_EQ(mask1.Raw(), 0xFF01); } -const nlTest sTests[] = { - NL_TEST_DEF("BitMask operations", TestBitMaskOperations), // - NL_TEST_DEF("BitFields logic", TestBitFieldLogic), // - NL_TEST_DEF("Invalid operations", TestBitMaskInvalid), // - NL_TEST_DEF("Clear operations", TestClear), // - NL_TEST_SENTINEL() // -}; - } // namespace - -int TestBitMask() -{ - nlTestSuite theSuite = { "BitMask tests", &sTests[0], nullptr, nullptr }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestBitMask) diff --git a/src/lib/support/tests/TestBufferReader.cpp b/src/lib/support/tests/TestBufferReader.cpp index f21d04ba24efb4..6d14c3973f58ed 100644 --- a/src/lib/support/tests/TestBufferReader.cpp +++ b/src/lib/support/tests/TestBufferReader.cpp @@ -22,11 +22,10 @@ * */ +#include #include -#include -#include -#include +#include using namespace chip; using namespace chip::Encoding::LittleEndian; @@ -43,7 +42,7 @@ struct TestSpanReader : public Reader TestSpanReader() : Reader(ByteSpan{ test_buffer, std::extent::value }) {} }; -static void TestBufferReader_BasicImpl(nlTestSuite * inSuite, void * inContext, Reader & reader) +static void TestBufferReader_BasicImpl(Reader & reader) { uint8_t first; uint16_t second; @@ -55,62 +54,62 @@ static void TestBufferReader_BasicImpl(nlTestSuite * inSuite, void * inContext, CHIP_ERROR err = reader.Read8(&first).Read16(&second).Read32(&third).Read64(&fourth).ReadBytes(&read_buf[0], sizeof(read_buf)).StatusCode(); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, first == 0x01); - NL_TEST_ASSERT(inSuite, second == 0x0302); - NL_TEST_ASSERT(inSuite, third == 0x07060504); - NL_TEST_ASSERT(inSuite, fourth == 0x0f0e0d0c0b0a0908); - NL_TEST_ASSERT(inSuite, memcmp(&read_buf[0], &read_buf_expected[0], sizeof(read_buf)) == 0); - NL_TEST_ASSERT(inSuite, reader.OctetsRead() == 18); - NL_TEST_ASSERT(inSuite, reader.Remaining() == 3); - NL_TEST_ASSERT(inSuite, reader.HasAtLeast(2)); - NL_TEST_ASSERT(inSuite, reader.HasAtLeast(3)); - NL_TEST_ASSERT(inSuite, !reader.HasAtLeast(4)); + EXPECT_EQ(err, CHIP_NO_ERROR); + EXPECT_EQ(first, 0x01); + EXPECT_EQ(second, 0x0302); + EXPECT_EQ(third, 0x07060504u); + EXPECT_EQ(fourth, 0x0f0e0d0c0b0a0908u); + EXPECT_EQ(memcmp(&read_buf[0], &read_buf_expected[0], sizeof(read_buf)), 0); + EXPECT_EQ(reader.OctetsRead(), 18u); + EXPECT_EQ(reader.Remaining(), 3u); + EXPECT_TRUE(reader.HasAtLeast(2)); + EXPECT_TRUE(reader.HasAtLeast(3)); + EXPECT_FALSE(reader.HasAtLeast(4)); uint32_t fourMore; err = reader.Read32(&fourMore).StatusCode(); - NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR); + EXPECT_NE(err, CHIP_NO_ERROR); } -static void TestBufferReader_Basic(nlTestSuite * inSuite, void * inContext) +TEST(TestBufferReader, TestBufferReader_Basic) { TestReader reader; - TestBufferReader_BasicImpl(inSuite, inContext, reader); + TestBufferReader_BasicImpl(reader); } -static void TestBufferReader_BasicSpan(nlTestSuite * inSuite, void * inContext) +TEST(TestBufferReader, TestBufferReader_BasicSpan) { TestSpanReader reader; - TestBufferReader_BasicImpl(inSuite, inContext, reader); + TestBufferReader_BasicImpl(reader); } -static void TestBufferReader_Saturation(nlTestSuite * inSuite, void * inContext) +TEST(TestBufferReader, TestBufferReader_Saturation) { TestReader reader; uint64_t temp; // Read some bytes out so we can get to the end of the buffer. CHIP_ERROR err = reader.Read64(&temp).StatusCode(); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); err = reader.Read64(&temp).StatusCode(); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + EXPECT_EQ(err, CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.HasAtLeast(5)); - NL_TEST_ASSERT(inSuite, !reader.HasAtLeast(6)); + EXPECT_TRUE(reader.HasAtLeast(5)); + EXPECT_FALSE(reader.HasAtLeast(6)); uint64_t tooBig; err = reader.Read64(&tooBig).StatusCode(); - NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, !reader.HasAtLeast(1)); + EXPECT_NE(err, CHIP_NO_ERROR); + EXPECT_FALSE(reader.HasAtLeast(1)); // Check that even though we only really read out 16 bytes, we can't read // out one more bytes, because our previous read failed. uint8_t small; err = reader.Read8(&small).StatusCode(); - NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR); + EXPECT_NE(err, CHIP_NO_ERROR); } -static void TestBufferReader_Skip(nlTestSuite * inSuite, void * inContext) +TEST(TestBufferReader, TestBufferReader_Skip) { TestReader reader; uint8_t temp = 0; @@ -118,22 +117,22 @@ static void TestBufferReader_Skip(nlTestSuite * inSuite, void * inContext) // Verify Skip() advances the start pointer the correct amount. CHIP_ERROR err = reader.Skip(firstSkipLen).Read8(&temp).StatusCode(); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, temp == test_buffer[firstSkipLen]); - NL_TEST_ASSERT(inSuite, reader.OctetsRead() == (firstSkipLen + 1u)); + EXPECT_EQ(err, CHIP_NO_ERROR); + EXPECT_EQ(temp, test_buffer[firstSkipLen]); + EXPECT_EQ(reader.OctetsRead(), (firstSkipLen + 1u)); // Verify Skip() called with a length larger than available buffer space jumps to the end. err = reader.Skip(sizeof(test_buffer)).StatusCode(); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, reader.OctetsRead() == sizeof(test_buffer)); - NL_TEST_ASSERT(inSuite, reader.Remaining() == 0); + EXPECT_EQ(err, CHIP_NO_ERROR); + EXPECT_EQ(reader.OctetsRead(), sizeof(test_buffer)); + EXPECT_EQ(reader.Remaining(), 0u); // Verify no read allowed after jumping to the end. err = reader.Read8(&temp).StatusCode(); - NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR); + EXPECT_NE(err, CHIP_NO_ERROR); } -static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * inContext) +TEST(TestBufferReader, TestBufferReader_LittleEndianScalars) { const uint8_t test_buf1[10] = { 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01 }; @@ -142,9 +141,9 @@ static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * i chip::Encoding::LittleEndian::Reader reader{ ByteSpan{ test_buf1 } }; uint8_t val1 = 0; uint8_t val2 = 0; - NL_TEST_ASSERT(inSuite, reader.Read8(&val1).Read8(&val2).IsSuccess()); - NL_TEST_ASSERT(inSuite, val1 == 0xfe); - NL_TEST_ASSERT(inSuite, val2 == 0xff); + EXPECT_TRUE(reader.Read8(&val1).Read8(&val2).IsSuccess()); + EXPECT_EQ(val1, 0xfe); + EXPECT_EQ(val2, 0xff); } // Unsigned 16 bits reads @@ -152,9 +151,9 @@ static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * i chip::Encoding::LittleEndian::Reader reader{ ByteSpan{ test_buf1 } }; uint16_t val1 = 0; uint16_t val2 = 0; - NL_TEST_ASSERT(inSuite, reader.Read16(&val1).Read16(&val2).IsSuccess()); - NL_TEST_ASSERT(inSuite, val1 == 0xfffe); - NL_TEST_ASSERT(inSuite, val2 == 0xffff); + EXPECT_TRUE(reader.Read16(&val1).Read16(&val2).IsSuccess()); + EXPECT_EQ(val1, 0xfffe); + EXPECT_EQ(val2, 0xffff); } // Unsigned 32 bits reads @@ -162,9 +161,9 @@ static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * i chip::Encoding::LittleEndian::Reader reader{ ByteSpan{ test_buf1 } }; uint32_t val1 = 0; uint32_t val2 = 0; - NL_TEST_ASSERT(inSuite, reader.Read32(&val1).Read32(&val2).IsSuccess()); - NL_TEST_ASSERT(inSuite, val1 == static_cast(0xfffffffeUL)); - NL_TEST_ASSERT(inSuite, val2 == static_cast(0xffffffffUL)); + EXPECT_TRUE(reader.Read32(&val1).Read32(&val2).IsSuccess()); + EXPECT_EQ(val1, static_cast(0xfffffffeUL)); + EXPECT_EQ(val2, static_cast(0xffffffffUL)); } // Unsigned 32 bits reads, unaligned @@ -174,19 +173,19 @@ static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * i uint32_t val1 = 0; uint32_t val2 = 0; - NL_TEST_ASSERT(inSuite, reader.Skip(1).Read32(&val1).Read32(&val2).IsSuccess()); - NL_TEST_ASSERT(inSuite, reader.Remaining() == 1); - NL_TEST_ASSERT(inSuite, val1 == static_cast(0xfffffffeUL)); - NL_TEST_ASSERT(inSuite, val2 == static_cast(0xffffffffUL)); + EXPECT_TRUE(reader.Skip(1).Read32(&val1).Read32(&val2).IsSuccess()); + EXPECT_EQ(reader.Remaining(), 1u); + EXPECT_EQ(val1, static_cast(0xfffffffeUL)); + EXPECT_EQ(val2, static_cast(0xffffffffUL)); } // Unsigned 64 bits read { chip::Encoding::LittleEndian::Reader reader{ ByteSpan{ test_buf1 } }; uint64_t val = 0; - NL_TEST_ASSERT(inSuite, reader.Read64(&val).IsSuccess()); - NL_TEST_ASSERT(inSuite, reader.Remaining() == 2); - NL_TEST_ASSERT(inSuite, val == static_cast(0xfffffffffffffffeULL)); + EXPECT_TRUE(reader.Read64(&val).IsSuccess()); + EXPECT_EQ(reader.Remaining(), 2u); + EXPECT_EQ(val, static_cast(0xfffffffffffffffeULL)); } // Signed 8 bits reads @@ -194,9 +193,9 @@ static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * i chip::Encoding::LittleEndian::Reader reader{ ByteSpan{ test_buf1 } }; int8_t val1 = 0; int8_t val2 = 0; - NL_TEST_ASSERT(inSuite, reader.ReadSigned8(&val1).ReadSigned8(&val2).IsSuccess()); - NL_TEST_ASSERT(inSuite, val1 == -2); - NL_TEST_ASSERT(inSuite, val2 == -1); + EXPECT_TRUE(reader.ReadSigned8(&val1).ReadSigned8(&val2).IsSuccess()); + EXPECT_EQ(val1, -2); + EXPECT_EQ(val2, -1); } // Signed 16 bits reads @@ -204,9 +203,9 @@ static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * i chip::Encoding::LittleEndian::Reader reader{ ByteSpan{ test_buf1 } }; int16_t val1 = 0; int16_t val2 = 0; - NL_TEST_ASSERT(inSuite, reader.ReadSigned16(&val1).ReadSigned16(&val2).IsSuccess()); - NL_TEST_ASSERT(inSuite, val1 == -2); - NL_TEST_ASSERT(inSuite, val2 == -1); + EXPECT_TRUE(reader.ReadSigned16(&val1).ReadSigned16(&val2).IsSuccess()); + EXPECT_EQ(val1, -2); + EXPECT_EQ(val2, -1); } // Signed 32 bits reads @@ -214,9 +213,9 @@ static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * i chip::Encoding::LittleEndian::Reader reader{ ByteSpan{ test_buf1 } }; int32_t val1 = 0; int32_t val2 = 0; - NL_TEST_ASSERT(inSuite, reader.ReadSigned32(&val1).ReadSigned32(&val2).IsSuccess()); - NL_TEST_ASSERT(inSuite, val1 == -2); - NL_TEST_ASSERT(inSuite, val2 == -1); + EXPECT_TRUE(reader.ReadSigned32(&val1).ReadSigned32(&val2).IsSuccess()); + EXPECT_EQ(val1, -2); + EXPECT_EQ(val2, -1); } // Signed 32 bits reads, unaligned @@ -226,19 +225,19 @@ static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * i int32_t val1 = 0; int32_t val2 = 0; - NL_TEST_ASSERT(inSuite, reader.Skip(1).ReadSigned32(&val1).ReadSigned32(&val2).IsSuccess()); - NL_TEST_ASSERT(inSuite, reader.Remaining() == 1); - NL_TEST_ASSERT(inSuite, val1 == static_cast(-2L)); - NL_TEST_ASSERT(inSuite, val2 == static_cast(-1L)); + EXPECT_TRUE(reader.Skip(1).ReadSigned32(&val1).ReadSigned32(&val2).IsSuccess()); + EXPECT_EQ(reader.Remaining(), 1u); + EXPECT_EQ(val1, static_cast(-2L)); + EXPECT_EQ(val2, static_cast(-1L)); } // Signed 64 bits read { chip::Encoding::LittleEndian::Reader reader{ ByteSpan{ test_buf1 } }; int64_t val = 0; - NL_TEST_ASSERT(inSuite, reader.ReadSigned64(&val).IsSuccess()); - NL_TEST_ASSERT(inSuite, reader.Remaining() == 2); - NL_TEST_ASSERT(inSuite, val == static_cast(-2LL)); + EXPECT_TRUE(reader.ReadSigned64(&val).IsSuccess()); + EXPECT_EQ(reader.Remaining(), 2u); + EXPECT_EQ(val, static_cast(-2LL)); } // Bools @@ -249,11 +248,11 @@ static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * i bool val2 = false; bool val3 = false; - NL_TEST_ASSERT(inSuite, reader.ReadBool(&val1).ReadBool(&val2).ReadBool(&val3).IsSuccess()); - NL_TEST_ASSERT(inSuite, reader.Remaining() == 2); - NL_TEST_ASSERT(inSuite, val1 == false); - NL_TEST_ASSERT(inSuite, val2 == true); - NL_TEST_ASSERT(inSuite, val3 == true); + EXPECT_TRUE(reader.ReadBool(&val1).ReadBool(&val2).ReadBool(&val3).IsSuccess()); + EXPECT_EQ(reader.Remaining(), 2u); + EXPECT_EQ(val1, false); + EXPECT_EQ(val2, true); + EXPECT_EQ(val3, true); } // Chars @@ -264,32 +263,10 @@ static void TestBufferReader_LittleEndianScalars(nlTestSuite * inSuite, void * i char val2 = 'z'; char val3 = 'z'; - NL_TEST_ASSERT(inSuite, reader.ReadChar(&val1).ReadChar(&val2).ReadChar(&val3).IsSuccess()); - NL_TEST_ASSERT(inSuite, reader.Remaining() == 2); - NL_TEST_ASSERT(inSuite, val1 == 'a'); - NL_TEST_ASSERT(inSuite, val2 == '\0'); - NL_TEST_ASSERT(inSuite, val3 == '\xff'); + EXPECT_TRUE(reader.ReadChar(&val1).ReadChar(&val2).ReadChar(&val3).IsSuccess()); + EXPECT_EQ(reader.Remaining(), 2u); + EXPECT_EQ(val1, 'a'); + EXPECT_EQ(val2, '\0'); + EXPECT_EQ(val3, '\xff'); } } - -#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(TestBufferReader_Basic), - NL_TEST_DEF_FN(TestBufferReader_BasicSpan), - NL_TEST_DEF_FN(TestBufferReader_Saturation), - NL_TEST_DEF_FN(TestBufferReader_Skip), - NL_TEST_DEF("Test Little-endian buffer Reader scalar reads", TestBufferReader_LittleEndianScalars), - NL_TEST_SENTINEL() }; - -int TestBufferReader() -{ - nlTestSuite theSuite = { "CHIP BufferReader tests", &sTests[0], nullptr, nullptr }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestBufferReader) diff --git a/src/lib/support/tests/TestDefer.cpp b/src/lib/support/tests/TestDefer.cpp index c16583af38ab53..9b5159bf557830 100644 --- a/src/lib/support/tests/TestDefer.cpp +++ b/src/lib/support/tests/TestDefer.cpp @@ -17,48 +17,28 @@ */ #include -#include #include -#include - -using namespace chip; +#include namespace { -static void TestDeferUsage(nlTestSuite * inSuite, void * inContext) +TEST(TestDefer, TestDeferUsage) { bool deferred = false; { auto deferredFunction = MakeDefer([&]() { deferred = true; }); - NL_TEST_ASSERT(inSuite, !deferred); + EXPECT_FALSE(deferred); } - NL_TEST_ASSERT(inSuite, deferred); + EXPECT_TRUE(deferred); deferred = false; { std::unique_ptr movable; auto deferredFunction = MakeDefer([movable = std::move(movable), &deferred]() { deferred = true; }); } - NL_TEST_ASSERT(inSuite, deferred); + EXPECT_TRUE(deferred); } } // 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(TestDeferUsage), NL_TEST_SENTINEL() }; - -int TestDefer() -{ - nlTestSuite theSuite = { "CHIP Defer tests", &sTests[0], nullptr, nullptr }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestDefer) diff --git a/src/lib/support/tests/TestFixedBufferAllocator.cpp b/src/lib/support/tests/TestFixedBufferAllocator.cpp index ceb52f0cad327f..c5c588041ef65c 100644 --- a/src/lib/support/tests/TestFixedBufferAllocator.cpp +++ b/src/lib/support/tests/TestFixedBufferAllocator.cpp @@ -17,17 +17,15 @@ */ #include -#include -#include #include -#include +#include using namespace chip; namespace { -void TestClone(nlTestSuite * inSuite, void * inContext) +TEST(TestFixedBufferAllocator, TestClone) { uint8_t buffer[128]; FixedBufferAllocator alloc(buffer); @@ -35,23 +33,23 @@ void TestClone(nlTestSuite * inSuite, void * inContext) static const char kTestString[] = "Test string"; const char * allocatedString = alloc.Clone(kTestString); - NL_TEST_EXIT_ON_FAILED_ASSERT(inSuite, allocatedString != nullptr); - NL_TEST_ASSERT(inSuite, allocatedString != kTestString); + ASSERT_NE(allocatedString, nullptr); + EXPECT_NE(allocatedString, kTestString); // NOLINTNEXTLINE(clang-analyzer-unix.cstring.NullArg): null check for allocated string already done - NL_TEST_ASSERT(inSuite, strcmp(allocatedString, kTestString) == 0); + EXPECT_STREQ(allocatedString, kTestString); const uint8_t kTestData[] = { 0xDE, 0xAD, 0xBE, 0xEF }; const uint8_t * allocatedData = alloc.Clone(kTestData, sizeof(kTestData)); - NL_TEST_EXIT_ON_FAILED_ASSERT(inSuite, allocatedData != nullptr); - NL_TEST_ASSERT(inSuite, allocatedData != kTestData); + ASSERT_NE(allocatedData, nullptr); + EXPECT_NE(allocatedData, kTestData); // NOLINTNEXTLINE(clang-analyzer-unix.cstring.NullArg): null check for allocated data already done - NL_TEST_ASSERT(inSuite, memcmp(allocatedData, kTestData, sizeof(kTestData)) == 0); + EXPECT_EQ(memcmp(allocatedData, kTestData, sizeof(kTestData)), 0); } -void TestOutOfMemory(nlTestSuite * inSuite, void * inContext) +TEST(TestFixedBufferAllocator, TestOutOfMemory) { uint8_t buffer[16]; FixedBufferAllocator alloc(buffer); @@ -59,26 +57,11 @@ void TestOutOfMemory(nlTestSuite * inSuite, void * inContext) static const char kTestData[] = "0123456789abcdef"; // Allocating 16 bytes still works... - NL_TEST_ASSERT(inSuite, alloc.Clone(kTestData, 16) != nullptr); - NL_TEST_ASSERT(inSuite, !alloc.AnyAllocFailed()); + EXPECT_NE(alloc.Clone(kTestData, 16), nullptr); + EXPECT_FALSE(alloc.AnyAllocFailed()); // ...but cannot allocate even one more byte... - NL_TEST_ASSERT(inSuite, alloc.Clone(kTestData, 1) == nullptr); - NL_TEST_ASSERT(inSuite, alloc.AnyAllocFailed()); + EXPECT_EQ(alloc.Clone(kTestData, 1), nullptr); + EXPECT_TRUE(alloc.AnyAllocFailed()); } - -const nlTest sTests[] = { NL_TEST_DEF("Test successful clone", TestClone), NL_TEST_DEF("Test out of memory", TestOutOfMemory), - NL_TEST_SENTINEL() }; - } // namespace - -int TestFixedBufferAllocator() -{ - nlTestSuite theSuite = { "CHIP FixedBufferAllocator tests", &sTests[0], nullptr, nullptr }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestFixedBufferAllocator) diff --git a/src/lib/support/tests/TestFold.cpp b/src/lib/support/tests/TestFold.cpp index f35160fa2fb4e4..48dbb454dfac3b 100644 --- a/src/lib/support/tests/TestFold.cpp +++ b/src/lib/support/tests/TestFold.cpp @@ -16,67 +16,52 @@ * limitations under the License. */ +#include #include -#include #include #include #include -#include - using namespace chip; namespace { -void TestFoldMax(nlTestSuite * inSuite, void * inContext) +TEST(TestFold, TestFoldMax) { using List = std::initializer_list; using Limits = std::numeric_limits; const auto max = [](int left, int right) { return std::max(left, right); }; // Test empty list - NL_TEST_ASSERT(inSuite, Fold(List{}, -1000, max) == -1000); + EXPECT_EQ(Fold(List{}, -1000, max), -1000); // Test one-element (less than the initial value) - NL_TEST_ASSERT(inSuite, Fold(List{ -1001 }, -1000, max) == -1000); + EXPECT_EQ(Fold(List{ -1001 }, -1000, max), -1000); // Test one-element (greater than the initial value) - NL_TEST_ASSERT(inSuite, Fold(List{ -999 }, -1000, max) == -999); + EXPECT_EQ(Fold(List{ -999 }, -1000, max), -999); // Test limits - NL_TEST_ASSERT(inSuite, Fold(List{ 1000, Limits::max(), 0 }, 0, max) == Limits::max()); - NL_TEST_ASSERT(inSuite, Fold(List{ Limits::max(), 1000, Limits::min() }, Limits::min(), max) == Limits::max()); + EXPECT_EQ(Fold(List{ 1000, Limits::max(), 0 }, 0, max), Limits::max()); + EXPECT_EQ(Fold(List{ Limits::max(), 1000, Limits::min() }, Limits::min(), max), Limits::max()); } -void TestSum(nlTestSuite * inSuite, void * inContext) +TEST(TestFold, TestSum) { using List = std::initializer_list; using Limits = std::numeric_limits; // Test empty list - NL_TEST_ASSERT(inSuite, Sum(List{}) == 0); + EXPECT_FALSE(Sum(List{})); // Test one-element (min) - NL_TEST_ASSERT(inSuite, Sum(List{ Limits::min() }) == Limits::min()); + EXPECT_EQ(Sum(List{ Limits::min() }), Limits::min()); // Test one-element (max) - NL_TEST_ASSERT(inSuite, Sum(List{ Limits::max() }) == Limits::max()); + EXPECT_EQ(Sum(List{ Limits::max() }), Limits::max()); // Test multiple elements - NL_TEST_ASSERT(inSuite, Sum(List{ 0, 5, 1, 4, 2, 3 }) == 15); + EXPECT_EQ(Sum(List{ 0, 5, 1, 4, 2, 3 }), 15); } -const nlTest sTests[] = { NL_TEST_DEF("Test fold (max)", TestFoldMax), NL_TEST_DEF("Test sum", TestSum), NL_TEST_SENTINEL() }; - } // namespace - -int TestFold() -{ - nlTestSuite theSuite = { "Fold tests", &sTests[0], nullptr, nullptr }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestFold) diff --git a/src/lib/support/tests/TestIniEscaping.cpp b/src/lib/support/tests/TestIniEscaping.cpp index 734ddf85b31e7b..90570add67b18d 100644 --- a/src/lib/support/tests/TestIniEscaping.cpp +++ b/src/lib/support/tests/TestIniEscaping.cpp @@ -16,10 +16,8 @@ * limitations under the License. */ +#include #include -#include -#include - #include using namespace chip; @@ -33,80 +31,67 @@ struct TestCase const char * expectedOutput; }; -void TestEscaping(nlTestSuite * inSuite, void * inContext) +TEST(TestIniEscaping, TestEscaping) { - NL_TEST_ASSERT(inSuite, EscapeKey("") == ""); - NL_TEST_ASSERT(inSuite, EscapeKey("abcd1234,!") == "abcd1234,!"); - NL_TEST_ASSERT(inSuite, EscapeKey("ab\ncd =12\\34\x7f") == "ab\\x0acd\\x20\\x3d12\\x5c34\\x7f"); - NL_TEST_ASSERT(inSuite, EscapeKey(" ") == "\\x20"); - NL_TEST_ASSERT(inSuite, EscapeKey("===") == "\\x3d\\x3d\\x3d"); + EXPECT_EQ(EscapeKey(""), ""); + EXPECT_EQ(EscapeKey("abcd1234,!"), "abcd1234,!"); + EXPECT_EQ(EscapeKey("ab\ncd =12\\34\x7f"), "ab\\x0acd\\x20\\x3d12\\x5c34\\x7f"); + EXPECT_EQ(EscapeKey(" "), "\\x20"); + EXPECT_EQ(EscapeKey("==="), "\\x3d\\x3d\\x3d"); } -void TestUnescaping(nlTestSuite * inSuite, void * inContext) +TEST(TestIniEscaping, TestUnescaping) { // Test valid cases - NL_TEST_ASSERT(inSuite, UnescapeKey("") == ""); - NL_TEST_ASSERT(inSuite, UnescapeKey("abcd1234,!") == "abcd1234,!"); - NL_TEST_ASSERT(inSuite, UnescapeKey("ab\\x0acd\\x20\\x3d12\\x5c34\\x7f") == "ab\ncd =12\\34\x7f"); - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x20") == " "); - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x3d\\x3d\\x3d") == "==="); - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x0d") == "\r"); - - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x01\\x02\\x03\\x04\\x05\\x06\\x07") == "\x01\x02\x03\x04\x05\x06\x07"); - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x0e") == "\x08\x09\x0a\x0b\x0c\x0d\x0e"); - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x0f\\x10\\x11\\x12\\x13\\x14\\x15") == "\x0f\x10\x11\x12\x13\x14\x15"); - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c") == "\x16\x17\x18\x19\x1a\x1b\x1c"); - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x1d\\x1e\\x1f\\x20\\x7f\\x3d\\x5c") == "\x1d\x1e\x1f \x7f=\\"); - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x81\\x82\\xff") == "\x81\x82\xff"); + EXPECT_EQ(UnescapeKey(""), ""); + EXPECT_EQ(UnescapeKey("abcd1234,!"), "abcd1234,!"); + EXPECT_EQ(UnescapeKey("ab\\x0acd\\x20\\x3d12\\x5c34\\x7f"), "ab\ncd =12\\34\x7f"); + EXPECT_EQ(UnescapeKey("\\x20"), " "); + EXPECT_EQ(UnescapeKey("\\x3d\\x3d\\x3d"), "==="); + EXPECT_EQ(UnescapeKey("\\x0d"), "\r"); + + EXPECT_EQ(UnescapeKey("\\x01\\x02\\x03\\x04\\x05\\x06\\x07"), "\x01\x02\x03\x04\x05\x06\x07"); + EXPECT_EQ(UnescapeKey("\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x0e"), "\x08\x09\x0a\x0b\x0c\x0d\x0e"); + EXPECT_EQ(UnescapeKey("\\x0f\\x10\\x11\\x12\\x13\\x14\\x15"), "\x0f\x10\x11\x12\x13\x14\x15"); + EXPECT_EQ(UnescapeKey("\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c"), "\x16\x17\x18\x19\x1a\x1b\x1c"); + EXPECT_EQ(UnescapeKey("\\x1d\\x1e\\x1f\\x20\\x7f\\x3d\\x5c"), "\x1d\x1e\x1f \x7f=\\"); + EXPECT_EQ(UnescapeKey("\\x81\\x82\\xff"), "\x81\x82\xff"); // Test invalid cases // letters should never be escaped - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x5a\55") != "ZU"); - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x5a\55") == ""); + EXPECT_NE(UnescapeKey("\\x5a\55"), "ZU"); + EXPECT_EQ(UnescapeKey("\\x5a\55"), ""); // Capitalized hex forbidden - NL_TEST_ASSERT(inSuite, UnescapeKey("\\x0D") == ""); + EXPECT_EQ(UnescapeKey("\\x0D"), ""); // Partial escapes forbidden - NL_TEST_ASSERT(inSuite, UnescapeKey("1\\x0") == ""); + EXPECT_EQ(UnescapeKey("1\\x0"), ""); } -void TestRoundTrip(nlTestSuite * inSuite, void * inContext) +TEST(TestIniEscaping, TestRoundTrip) { - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("")) == ""); - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("abcd1234,!")) == "abcd1234,!"); - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("ab\ncd =12\\34\x7f")) == "ab\ncd =12\\34\x7f"); - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey(" ")) == " "); - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("===")) == "==="); - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("\r")) == "\r"); - - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("\x01\x02\x03\x04\x05\x06\x07")) == "\x01\x02\x03\x04\x05\x06\x07"); - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("\x08\x09\x0a\x0b\x0c\x0d\x0e")) == "\x08\x09\x0a\x0b\x0c\x0d\x0e"); - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("\x0f\x10\x11\x12\x13\x14\x15")) == "\x0f\x10\x11\x12\x13\x14\x15"); - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("\x16\x17\x18\x19\x1a\x1b\x1c")) == "\x16\x17\x18\x19\x1a\x1b\x1c"); - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("\x1d\x1e\x1f \x7f=\\")) == "\x1d\x1e\x1f \x7f=\\"); - NL_TEST_ASSERT(inSuite, UnescapeKey(EscapeKey("\x81\x82\xff")) == "\x81\x82\xff"); + EXPECT_EQ(UnescapeKey(EscapeKey("")), ""); + EXPECT_EQ(UnescapeKey(EscapeKey("abcd1234,!")), "abcd1234,!"); + EXPECT_EQ(UnescapeKey(EscapeKey("ab\ncd =12\\34\x7f")), "ab\ncd =12\\34\x7f"); + EXPECT_EQ(UnescapeKey(EscapeKey(" ")), " "); + EXPECT_EQ(UnescapeKey(EscapeKey("===")), "==="); + EXPECT_EQ(UnescapeKey(EscapeKey("\r")), "\r"); + + EXPECT_EQ(UnescapeKey(EscapeKey("\x01\x02\x03\x04\x05\x06\x07")), "\x01\x02\x03\x04\x05\x06\x07"); + EXPECT_EQ(UnescapeKey(EscapeKey("\x08\x09\x0a\x0b\x0c\x0d\x0e")), "\x08\x09\x0a\x0b\x0c\x0d\x0e"); + EXPECT_EQ(UnescapeKey(EscapeKey("\x0f\x10\x11\x12\x13\x14\x15")), "\x0f\x10\x11\x12\x13\x14\x15"); + EXPECT_EQ(UnescapeKey(EscapeKey("\x16\x17\x18\x19\x1a\x1b\x1c")), "\x16\x17\x18\x19\x1a\x1b\x1c"); + EXPECT_EQ(UnescapeKey(EscapeKey("\x1d\x1e\x1f \x7f=\\")), "\x1d\x1e\x1f \x7f=\\"); + EXPECT_EQ(UnescapeKey(EscapeKey("\x81\x82\xff")), "\x81\x82\xff"); // Make sure entire range is escapable for (int c = 0; c <= 255; c++) { std::string s(5, static_cast(c)); - NL_TEST_ASSERT_LOOP(inSuite, c, UnescapeKey(EscapeKey(s)) == s); + EXPECT_EQ(UnescapeKey(EscapeKey(s)), s) << "c: " << c; } } -const nlTest sTests[] = { NL_TEST_DEF("Test escaping API", TestEscaping), NL_TEST_DEF("Test unescaping API", TestUnescaping), - NL_TEST_DEF("Test escaping API round-tripping with itself", TestRoundTrip), NL_TEST_SENTINEL() }; - } // namespace - -int TestIniEscaping() -{ - nlTestSuite theSuite = { "IniEscaping tests", &sTests[0], nullptr, nullptr }; - - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestIniEscaping); diff --git a/src/lib/support/tests/TestSafeInt.cpp b/src/lib/support/tests/TestSafeInt.cpp index 3108e74778ce7e..1464c0e9735b8c 100644 --- a/src/lib/support/tests/TestSafeInt.cpp +++ b/src/lib/support/tests/TestSafeInt.cpp @@ -22,346 +22,325 @@ * */ +#include #include -#include - -#include using namespace chip; -static void TestCanCastTo_Int8(nlTestSuite * inSuite, void * inContext) +TEST(TestSafeInt, TestCanCastTo_Int8) { - NL_TEST_ASSERT(inSuite, CanCastTo(0)); - NL_TEST_ASSERT(inSuite, CanCastTo(127)); - NL_TEST_ASSERT(inSuite, CanCastTo(128)); - NL_TEST_ASSERT(inSuite, CanCastTo(129)); - NL_TEST_ASSERT(inSuite, CanCastTo(255)); - NL_TEST_ASSERT(inSuite, !CanCastTo(256)); - NL_TEST_ASSERT(inSuite, !CanCastTo(32767)); - NL_TEST_ASSERT(inSuite, !CanCastTo(32768)); - NL_TEST_ASSERT(inSuite, !CanCastTo(32769)); - NL_TEST_ASSERT(inSuite, !CanCastTo(65535)); - NL_TEST_ASSERT(inSuite, !CanCastTo(65536)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775807ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775808ull)); + EXPECT_TRUE(CanCastTo(0)); + EXPECT_TRUE(CanCastTo(127)); + EXPECT_TRUE(CanCastTo(128)); + EXPECT_TRUE(CanCastTo(129)); + EXPECT_TRUE(CanCastTo(255)); + EXPECT_FALSE(CanCastTo(256)); + EXPECT_FALSE(CanCastTo(32767)); + EXPECT_FALSE(CanCastTo(32768)); + EXPECT_FALSE(CanCastTo(32769)); + EXPECT_FALSE(CanCastTo(65535)); + EXPECT_FALSE(CanCastTo(65536)); + EXPECT_FALSE(CanCastTo(2147483647ll)); + EXPECT_FALSE(CanCastTo(2147483648ll)); + EXPECT_FALSE(CanCastTo(2147483649ll)); + EXPECT_FALSE(CanCastTo(4294967295ll)); + EXPECT_FALSE(CanCastTo(4294967296ll)); + EXPECT_FALSE(CanCastTo(9223372036854775807ull)); + EXPECT_FALSE(CanCastTo(9223372036854775808ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-1)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-127)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-128)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-129)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-255)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-256)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32767)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32768)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32769)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65535)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65536)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-9223372036854775807ll)); + EXPECT_FALSE(CanCastTo(-1)); + EXPECT_FALSE(CanCastTo(-127)); + EXPECT_FALSE(CanCastTo(-128)); + EXPECT_FALSE(CanCastTo(-129)); + EXPECT_FALSE(CanCastTo(-255)); + EXPECT_FALSE(CanCastTo(-256)); + EXPECT_FALSE(CanCastTo(-32767)); + EXPECT_FALSE(CanCastTo(-32768)); + EXPECT_FALSE(CanCastTo(-32769)); + EXPECT_FALSE(CanCastTo(-65535)); + EXPECT_FALSE(CanCastTo(-65536)); + EXPECT_FALSE(CanCastTo(-2147483647ll)); + EXPECT_FALSE(CanCastTo(-2147483648ll)); + EXPECT_FALSE(CanCastTo(-2147483649ll)); + EXPECT_FALSE(CanCastTo(-4294967295ll)); + EXPECT_FALSE(CanCastTo(-4294967296ll)); + EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(0)); - NL_TEST_ASSERT(inSuite, CanCastTo(127)); - NL_TEST_ASSERT(inSuite, !CanCastTo(128)); - NL_TEST_ASSERT(inSuite, !CanCastTo(129)); - NL_TEST_ASSERT(inSuite, !CanCastTo(255)); - NL_TEST_ASSERT(inSuite, !CanCastTo(256)); - NL_TEST_ASSERT(inSuite, !CanCastTo(32767)); - NL_TEST_ASSERT(inSuite, !CanCastTo(32768)); - NL_TEST_ASSERT(inSuite, !CanCastTo(32769)); - NL_TEST_ASSERT(inSuite, !CanCastTo(65535)); - NL_TEST_ASSERT(inSuite, !CanCastTo(65536)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775807ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775808ull)); + EXPECT_TRUE(CanCastTo(0)); + EXPECT_TRUE(CanCastTo(127)); + EXPECT_FALSE(CanCastTo(128)); + EXPECT_FALSE(CanCastTo(129)); + EXPECT_FALSE(CanCastTo(255)); + EXPECT_FALSE(CanCastTo(256)); + EXPECT_FALSE(CanCastTo(32767)); + EXPECT_FALSE(CanCastTo(32768)); + EXPECT_FALSE(CanCastTo(32769)); + EXPECT_FALSE(CanCastTo(65535)); + EXPECT_FALSE(CanCastTo(65536)); + EXPECT_FALSE(CanCastTo(2147483647ll)); + EXPECT_FALSE(CanCastTo(2147483648ll)); + EXPECT_FALSE(CanCastTo(2147483649ll)); + EXPECT_FALSE(CanCastTo(4294967295ll)); + EXPECT_FALSE(CanCastTo(4294967296ll)); + EXPECT_FALSE(CanCastTo(9223372036854775807ull)); + EXPECT_FALSE(CanCastTo(9223372036854775808ull)); - NL_TEST_ASSERT(inSuite, CanCastTo(-1)); - NL_TEST_ASSERT(inSuite, CanCastTo(-127)); - NL_TEST_ASSERT(inSuite, CanCastTo(-128)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-129)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-255)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-256)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32767)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32768)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32769)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65535)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65536)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-9223372036854775807ll)); + EXPECT_TRUE(CanCastTo(-1)); + EXPECT_TRUE(CanCastTo(-127)); + EXPECT_TRUE(CanCastTo(-128)); + EXPECT_FALSE(CanCastTo(-129)); + EXPECT_FALSE(CanCastTo(-255)); + EXPECT_FALSE(CanCastTo(-256)); + EXPECT_FALSE(CanCastTo(-32767)); + EXPECT_FALSE(CanCastTo(-32768)); + EXPECT_FALSE(CanCastTo(-32769)); + EXPECT_FALSE(CanCastTo(-65535)); + EXPECT_FALSE(CanCastTo(-65536)); + EXPECT_FALSE(CanCastTo(-2147483647ll)); + EXPECT_FALSE(CanCastTo(-2147483648ll)); + EXPECT_FALSE(CanCastTo(-2147483649ll)); + EXPECT_FALSE(CanCastTo(-4294967295ll)); + EXPECT_FALSE(CanCastTo(-4294967296ll)); + EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); } -static void TestCanCastTo_Int16(nlTestSuite * inSuite, void * inContext) +TEST(TestSafeInt, TestCanCastTo_Int16) { - NL_TEST_ASSERT(inSuite, CanCastTo(0)); - NL_TEST_ASSERT(inSuite, CanCastTo(127)); - NL_TEST_ASSERT(inSuite, CanCastTo(128)); - NL_TEST_ASSERT(inSuite, CanCastTo(129)); - NL_TEST_ASSERT(inSuite, CanCastTo(255)); - NL_TEST_ASSERT(inSuite, CanCastTo(256)); - NL_TEST_ASSERT(inSuite, CanCastTo(32767)); - NL_TEST_ASSERT(inSuite, CanCastTo(32768)); - NL_TEST_ASSERT(inSuite, CanCastTo(32769)); - NL_TEST_ASSERT(inSuite, CanCastTo(65535)); - NL_TEST_ASSERT(inSuite, !CanCastTo(65536)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775807ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775808ull)); + EXPECT_TRUE(CanCastTo(0)); + EXPECT_TRUE(CanCastTo(127)); + EXPECT_TRUE(CanCastTo(128)); + EXPECT_TRUE(CanCastTo(129)); + EXPECT_TRUE(CanCastTo(255)); + EXPECT_TRUE(CanCastTo(256)); + EXPECT_TRUE(CanCastTo(32767)); + EXPECT_TRUE(CanCastTo(32768)); + EXPECT_TRUE(CanCastTo(32769)); + EXPECT_TRUE(CanCastTo(65535)); + EXPECT_FALSE(CanCastTo(65536)); + EXPECT_FALSE(CanCastTo(2147483647ll)); + EXPECT_FALSE(CanCastTo(2147483648ll)); + EXPECT_FALSE(CanCastTo(2147483649ll)); + EXPECT_FALSE(CanCastTo(4294967295ll)); + EXPECT_FALSE(CanCastTo(4294967296ll)); + EXPECT_FALSE(CanCastTo(9223372036854775807ull)); + EXPECT_FALSE(CanCastTo(9223372036854775808ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-1)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-127)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-128)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-129)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-255)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-256)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32767)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32768)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32769)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65535)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65536)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-9223372036854775807ll)); + EXPECT_FALSE(CanCastTo(-1)); + EXPECT_FALSE(CanCastTo(-127)); + EXPECT_FALSE(CanCastTo(-128)); + EXPECT_FALSE(CanCastTo(-129)); + EXPECT_FALSE(CanCastTo(-255)); + EXPECT_FALSE(CanCastTo(-256)); + EXPECT_FALSE(CanCastTo(-32767)); + EXPECT_FALSE(CanCastTo(-32768)); + EXPECT_FALSE(CanCastTo(-32769)); + EXPECT_FALSE(CanCastTo(-65535)); + EXPECT_FALSE(CanCastTo(-65536)); + EXPECT_FALSE(CanCastTo(-2147483647ll)); + EXPECT_FALSE(CanCastTo(-2147483648ll)); + EXPECT_FALSE(CanCastTo(-2147483649ll)); + EXPECT_FALSE(CanCastTo(-4294967295ll)); + EXPECT_FALSE(CanCastTo(-4294967296ll)); + EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(0)); - NL_TEST_ASSERT(inSuite, CanCastTo(127)); - NL_TEST_ASSERT(inSuite, CanCastTo(128)); - NL_TEST_ASSERT(inSuite, CanCastTo(129)); - NL_TEST_ASSERT(inSuite, CanCastTo(255)); - NL_TEST_ASSERT(inSuite, CanCastTo(256)); - NL_TEST_ASSERT(inSuite, CanCastTo(32767)); - NL_TEST_ASSERT(inSuite, !CanCastTo(32768)); - NL_TEST_ASSERT(inSuite, !CanCastTo(32769)); - NL_TEST_ASSERT(inSuite, !CanCastTo(65535)); - NL_TEST_ASSERT(inSuite, !CanCastTo(65536)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775807ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775808ull)); + EXPECT_TRUE(CanCastTo(0)); + EXPECT_TRUE(CanCastTo(127)); + EXPECT_TRUE(CanCastTo(128)); + EXPECT_TRUE(CanCastTo(129)); + EXPECT_TRUE(CanCastTo(255)); + EXPECT_TRUE(CanCastTo(256)); + EXPECT_TRUE(CanCastTo(32767)); + EXPECT_FALSE(CanCastTo(32768)); + EXPECT_FALSE(CanCastTo(32769)); + EXPECT_FALSE(CanCastTo(65535)); + EXPECT_FALSE(CanCastTo(65536)); + EXPECT_FALSE(CanCastTo(2147483647ll)); + EXPECT_FALSE(CanCastTo(2147483648ll)); + EXPECT_FALSE(CanCastTo(2147483649ll)); + EXPECT_FALSE(CanCastTo(4294967295ll)); + EXPECT_FALSE(CanCastTo(4294967296ll)); + EXPECT_FALSE(CanCastTo(9223372036854775807ull)); + EXPECT_FALSE(CanCastTo(9223372036854775808ull)); - NL_TEST_ASSERT(inSuite, CanCastTo(-1)); - NL_TEST_ASSERT(inSuite, CanCastTo(-127)); - NL_TEST_ASSERT(inSuite, CanCastTo(-128)); - NL_TEST_ASSERT(inSuite, CanCastTo(-129)); - NL_TEST_ASSERT(inSuite, CanCastTo(-255)); - NL_TEST_ASSERT(inSuite, CanCastTo(-256)); - NL_TEST_ASSERT(inSuite, CanCastTo(-32767)); - NL_TEST_ASSERT(inSuite, CanCastTo(-32768)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32769)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65535)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65536)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-9223372036854775807ll)); + EXPECT_TRUE(CanCastTo(-1)); + EXPECT_TRUE(CanCastTo(-127)); + EXPECT_TRUE(CanCastTo(-128)); + EXPECT_TRUE(CanCastTo(-129)); + EXPECT_TRUE(CanCastTo(-255)); + EXPECT_TRUE(CanCastTo(-256)); + EXPECT_TRUE(CanCastTo(-32767)); + EXPECT_TRUE(CanCastTo(-32768)); + EXPECT_FALSE(CanCastTo(-32769)); + EXPECT_FALSE(CanCastTo(-65535)); + EXPECT_FALSE(CanCastTo(-65536)); + EXPECT_FALSE(CanCastTo(-2147483647ll)); + EXPECT_FALSE(CanCastTo(-2147483648ll)); + EXPECT_FALSE(CanCastTo(-2147483649ll)); + EXPECT_FALSE(CanCastTo(-4294967295ll)); + EXPECT_FALSE(CanCastTo(-4294967296ll)); + EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); } -static void TestCanCastTo_Int32(nlTestSuite * inSuite, void * inContext) +TEST(TestSafeInt, TestCanCastTo_Int32) { - NL_TEST_ASSERT(inSuite, CanCastTo(0)); - NL_TEST_ASSERT(inSuite, CanCastTo(127)); - NL_TEST_ASSERT(inSuite, CanCastTo(128)); - NL_TEST_ASSERT(inSuite, CanCastTo(129)); - NL_TEST_ASSERT(inSuite, CanCastTo(255)); - NL_TEST_ASSERT(inSuite, CanCastTo(256)); - NL_TEST_ASSERT(inSuite, CanCastTo(32767)); - NL_TEST_ASSERT(inSuite, CanCastTo(32768)); - NL_TEST_ASSERT(inSuite, CanCastTo(32769)); - NL_TEST_ASSERT(inSuite, CanCastTo(65535)); - NL_TEST_ASSERT(inSuite, CanCastTo(65536)); - NL_TEST_ASSERT(inSuite, CanCastTo(2147483647ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(2147483648ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(2147483649ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775807ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775808ull)); + EXPECT_TRUE(CanCastTo(0)); + EXPECT_TRUE(CanCastTo(127)); + EXPECT_TRUE(CanCastTo(128)); + EXPECT_TRUE(CanCastTo(129)); + EXPECT_TRUE(CanCastTo(255)); + EXPECT_TRUE(CanCastTo(256)); + EXPECT_TRUE(CanCastTo(32767)); + EXPECT_TRUE(CanCastTo(32768)); + EXPECT_TRUE(CanCastTo(32769)); + EXPECT_TRUE(CanCastTo(65535)); + EXPECT_TRUE(CanCastTo(65536)); + EXPECT_TRUE(CanCastTo(2147483647ll)); + EXPECT_TRUE(CanCastTo(2147483648ll)); + EXPECT_TRUE(CanCastTo(2147483649ll)); + EXPECT_TRUE(CanCastTo(4294967295ll)); + EXPECT_FALSE(CanCastTo(4294967296ll)); + EXPECT_FALSE(CanCastTo(9223372036854775807ull)); + EXPECT_FALSE(CanCastTo(9223372036854775808ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-1)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-127)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-128)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-129)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-255)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-256)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32767)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32768)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32769)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65535)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65536)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-9223372036854775807ll)); + EXPECT_FALSE(CanCastTo(-1)); + EXPECT_FALSE(CanCastTo(-127)); + EXPECT_FALSE(CanCastTo(-128)); + EXPECT_FALSE(CanCastTo(-129)); + EXPECT_FALSE(CanCastTo(-255)); + EXPECT_FALSE(CanCastTo(-256)); + EXPECT_FALSE(CanCastTo(-32767)); + EXPECT_FALSE(CanCastTo(-32768)); + EXPECT_FALSE(CanCastTo(-32769)); + EXPECT_FALSE(CanCastTo(-65535)); + EXPECT_FALSE(CanCastTo(-65536)); + EXPECT_FALSE(CanCastTo(-2147483647ll)); + EXPECT_FALSE(CanCastTo(-2147483648ll)); + EXPECT_FALSE(CanCastTo(-2147483649ll)); + EXPECT_FALSE(CanCastTo(-4294967295ll)); + EXPECT_FALSE(CanCastTo(-4294967296ll)); + EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(0)); - NL_TEST_ASSERT(inSuite, CanCastTo(127)); - NL_TEST_ASSERT(inSuite, CanCastTo(128)); - NL_TEST_ASSERT(inSuite, CanCastTo(129)); - NL_TEST_ASSERT(inSuite, CanCastTo(255)); - NL_TEST_ASSERT(inSuite, CanCastTo(256)); - NL_TEST_ASSERT(inSuite, CanCastTo(32767)); - NL_TEST_ASSERT(inSuite, CanCastTo(32768)); - NL_TEST_ASSERT(inSuite, CanCastTo(32769)); - NL_TEST_ASSERT(inSuite, CanCastTo(65535)); - NL_TEST_ASSERT(inSuite, CanCastTo(65536)); - NL_TEST_ASSERT(inSuite, CanCastTo(2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775807ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775808ull)); + EXPECT_TRUE(CanCastTo(0)); + EXPECT_TRUE(CanCastTo(127)); + EXPECT_TRUE(CanCastTo(128)); + EXPECT_TRUE(CanCastTo(129)); + EXPECT_TRUE(CanCastTo(255)); + EXPECT_TRUE(CanCastTo(256)); + EXPECT_TRUE(CanCastTo(32767)); + EXPECT_TRUE(CanCastTo(32768)); + EXPECT_TRUE(CanCastTo(32769)); + EXPECT_TRUE(CanCastTo(65535)); + EXPECT_TRUE(CanCastTo(65536)); + EXPECT_TRUE(CanCastTo(2147483647ll)); + EXPECT_FALSE(CanCastTo(2147483648ll)); + EXPECT_FALSE(CanCastTo(2147483649ll)); + EXPECT_FALSE(CanCastTo(4294967295ll)); + EXPECT_FALSE(CanCastTo(4294967296ll)); + EXPECT_FALSE(CanCastTo(9223372036854775807ull)); + EXPECT_FALSE(CanCastTo(9223372036854775808ull)); - NL_TEST_ASSERT(inSuite, CanCastTo(-1)); - NL_TEST_ASSERT(inSuite, CanCastTo(-127)); - NL_TEST_ASSERT(inSuite, CanCastTo(-128)); - NL_TEST_ASSERT(inSuite, CanCastTo(-129)); - NL_TEST_ASSERT(inSuite, CanCastTo(-255)); - NL_TEST_ASSERT(inSuite, CanCastTo(-256)); - NL_TEST_ASSERT(inSuite, CanCastTo(-32767)); - NL_TEST_ASSERT(inSuite, CanCastTo(-32768)); - NL_TEST_ASSERT(inSuite, CanCastTo(-32769)); - NL_TEST_ASSERT(inSuite, CanCastTo(-65535)); - NL_TEST_ASSERT(inSuite, CanCastTo(-65536)); - NL_TEST_ASSERT(inSuite, CanCastTo(-2147483647ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(-2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-9223372036854775807ll)); + EXPECT_TRUE(CanCastTo(-1)); + EXPECT_TRUE(CanCastTo(-127)); + EXPECT_TRUE(CanCastTo(-128)); + EXPECT_TRUE(CanCastTo(-129)); + EXPECT_TRUE(CanCastTo(-255)); + EXPECT_TRUE(CanCastTo(-256)); + EXPECT_TRUE(CanCastTo(-32767)); + EXPECT_TRUE(CanCastTo(-32768)); + EXPECT_TRUE(CanCastTo(-32769)); + EXPECT_TRUE(CanCastTo(-65535)); + EXPECT_TRUE(CanCastTo(-65536)); + EXPECT_TRUE(CanCastTo(-2147483647ll)); + EXPECT_TRUE(CanCastTo(-2147483648ll)); + EXPECT_FALSE(CanCastTo(-2147483649ll)); + EXPECT_FALSE(CanCastTo(-4294967295ll)); + EXPECT_FALSE(CanCastTo(-4294967296ll)); + EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); } -static void TestCanCastTo_Int64(nlTestSuite * inSuite, void * inContext) +TEST(TestSafeInt, TestCanCastTo_Int64) { - NL_TEST_ASSERT(inSuite, CanCastTo(0)); - NL_TEST_ASSERT(inSuite, CanCastTo(127)); - NL_TEST_ASSERT(inSuite, CanCastTo(128)); - NL_TEST_ASSERT(inSuite, CanCastTo(129)); - NL_TEST_ASSERT(inSuite, CanCastTo(255)); - NL_TEST_ASSERT(inSuite, CanCastTo(256)); - NL_TEST_ASSERT(inSuite, CanCastTo(32767)); - NL_TEST_ASSERT(inSuite, CanCastTo(32768)); - NL_TEST_ASSERT(inSuite, CanCastTo(32769)); - NL_TEST_ASSERT(inSuite, CanCastTo(65535)); - NL_TEST_ASSERT(inSuite, CanCastTo(65536)); - NL_TEST_ASSERT(inSuite, CanCastTo(2147483647ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(2147483648ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(2147483649ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(4294967295ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(4294967296ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(9223372036854775807ull)); - NL_TEST_ASSERT(inSuite, CanCastTo(9223372036854775808ull)); + EXPECT_TRUE(CanCastTo(0)); + EXPECT_TRUE(CanCastTo(127)); + EXPECT_TRUE(CanCastTo(128)); + EXPECT_TRUE(CanCastTo(129)); + EXPECT_TRUE(CanCastTo(255)); + EXPECT_TRUE(CanCastTo(256)); + EXPECT_TRUE(CanCastTo(32767)); + EXPECT_TRUE(CanCastTo(32768)); + EXPECT_TRUE(CanCastTo(32769)); + EXPECT_TRUE(CanCastTo(65535)); + EXPECT_TRUE(CanCastTo(65536)); + EXPECT_TRUE(CanCastTo(2147483647ll)); + EXPECT_TRUE(CanCastTo(2147483648ll)); + EXPECT_TRUE(CanCastTo(2147483649ll)); + EXPECT_TRUE(CanCastTo(4294967295ll)); + EXPECT_TRUE(CanCastTo(4294967296ll)); + EXPECT_TRUE(CanCastTo(9223372036854775807ull)); + EXPECT_TRUE(CanCastTo(9223372036854775808ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-1)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-127)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-128)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-129)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-255)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-256)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32767)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32768)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-32769)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65535)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-65536)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483647ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483648ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-2147483649ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967295ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-4294967296ll)); - NL_TEST_ASSERT(inSuite, !CanCastTo(-9223372036854775807ll)); + EXPECT_FALSE(CanCastTo(-1)); + EXPECT_FALSE(CanCastTo(-127)); + EXPECT_FALSE(CanCastTo(-128)); + EXPECT_FALSE(CanCastTo(-129)); + EXPECT_FALSE(CanCastTo(-255)); + EXPECT_FALSE(CanCastTo(-256)); + EXPECT_FALSE(CanCastTo(-32767)); + EXPECT_FALSE(CanCastTo(-32768)); + EXPECT_FALSE(CanCastTo(-32769)); + EXPECT_FALSE(CanCastTo(-65535)); + EXPECT_FALSE(CanCastTo(-65536)); + EXPECT_FALSE(CanCastTo(-2147483647ll)); + EXPECT_FALSE(CanCastTo(-2147483648ll)); + EXPECT_FALSE(CanCastTo(-2147483649ll)); + EXPECT_FALSE(CanCastTo(-4294967295ll)); + EXPECT_FALSE(CanCastTo(-4294967296ll)); + EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(0)); - NL_TEST_ASSERT(inSuite, CanCastTo(127)); - NL_TEST_ASSERT(inSuite, CanCastTo(128)); - NL_TEST_ASSERT(inSuite, CanCastTo(129)); - NL_TEST_ASSERT(inSuite, CanCastTo(255)); - NL_TEST_ASSERT(inSuite, CanCastTo(256)); - NL_TEST_ASSERT(inSuite, CanCastTo(32767)); - NL_TEST_ASSERT(inSuite, CanCastTo(32768)); - NL_TEST_ASSERT(inSuite, CanCastTo(32769)); - NL_TEST_ASSERT(inSuite, CanCastTo(65535)); - NL_TEST_ASSERT(inSuite, CanCastTo(65536)); - NL_TEST_ASSERT(inSuite, CanCastTo(2147483647ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(2147483648ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(2147483649ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(4294967295ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(4294967296ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(9223372036854775807ull)); - NL_TEST_ASSERT(inSuite, !CanCastTo(9223372036854775808ull)); + EXPECT_TRUE(CanCastTo(0)); + EXPECT_TRUE(CanCastTo(127)); + EXPECT_TRUE(CanCastTo(128)); + EXPECT_TRUE(CanCastTo(129)); + EXPECT_TRUE(CanCastTo(255)); + EXPECT_TRUE(CanCastTo(256)); + EXPECT_TRUE(CanCastTo(32767)); + EXPECT_TRUE(CanCastTo(32768)); + EXPECT_TRUE(CanCastTo(32769)); + EXPECT_TRUE(CanCastTo(65535)); + EXPECT_TRUE(CanCastTo(65536)); + EXPECT_TRUE(CanCastTo(2147483647ll)); + EXPECT_TRUE(CanCastTo(2147483648ll)); + EXPECT_TRUE(CanCastTo(2147483649ll)); + EXPECT_TRUE(CanCastTo(4294967295ll)); + EXPECT_TRUE(CanCastTo(4294967296ll)); + EXPECT_TRUE(CanCastTo(9223372036854775807ull)); + EXPECT_FALSE(CanCastTo(9223372036854775808ull)); - NL_TEST_ASSERT(inSuite, CanCastTo(-1)); - NL_TEST_ASSERT(inSuite, CanCastTo(-127)); - NL_TEST_ASSERT(inSuite, CanCastTo(-128)); - NL_TEST_ASSERT(inSuite, CanCastTo(-129)); - NL_TEST_ASSERT(inSuite, CanCastTo(-255)); - NL_TEST_ASSERT(inSuite, CanCastTo(-256)); - NL_TEST_ASSERT(inSuite, CanCastTo(-32767)); - NL_TEST_ASSERT(inSuite, CanCastTo(-32768)); - NL_TEST_ASSERT(inSuite, CanCastTo(-32769)); - NL_TEST_ASSERT(inSuite, CanCastTo(-65535)); - NL_TEST_ASSERT(inSuite, CanCastTo(-65536)); - NL_TEST_ASSERT(inSuite, CanCastTo(-2147483647ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(-2147483648ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(-2147483649ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(-4294967295ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(-4294967296ll)); - NL_TEST_ASSERT(inSuite, CanCastTo(-9223372036854775807ll)); + EXPECT_TRUE(CanCastTo(-1)); + EXPECT_TRUE(CanCastTo(-127)); + EXPECT_TRUE(CanCastTo(-128)); + EXPECT_TRUE(CanCastTo(-129)); + EXPECT_TRUE(CanCastTo(-255)); + EXPECT_TRUE(CanCastTo(-256)); + EXPECT_TRUE(CanCastTo(-32767)); + EXPECT_TRUE(CanCastTo(-32768)); + EXPECT_TRUE(CanCastTo(-32769)); + EXPECT_TRUE(CanCastTo(-65535)); + EXPECT_TRUE(CanCastTo(-65536)); + EXPECT_TRUE(CanCastTo(-2147483647ll)); + EXPECT_TRUE(CanCastTo(-2147483648ll)); + EXPECT_TRUE(CanCastTo(-2147483649ll)); + EXPECT_TRUE(CanCastTo(-4294967295ll)); + EXPECT_TRUE(CanCastTo(-4294967296ll)); + EXPECT_TRUE(CanCastTo(-9223372036854775807ll)); } -static void TestCastToSigned(nlTestSuite * inSuite, void * inContext) +TEST(TestSafeInt, TestCastToSigned) { - NL_TEST_ASSERT(inSuite, CastToSigned(static_cast(5)) == 5); - NL_TEST_ASSERT(inSuite, CastToSigned(static_cast(-5)) == -5); - NL_TEST_ASSERT(inSuite, CastToSigned(static_cast(254)) == -2); - NL_TEST_ASSERT(inSuite, CastToSigned(static_cast(65533)) == -3); - NL_TEST_ASSERT(inSuite, CastToSigned(static_cast(254)) == 254); - NL_TEST_ASSERT(inSuite, CastToSigned(static_cast(65533)) == -3); + EXPECT_EQ(CastToSigned(static_cast(5)), 5); + EXPECT_EQ(CastToSigned(static_cast(-5)), -5); + EXPECT_EQ(CastToSigned(static_cast(254)), -2); + EXPECT_EQ(CastToSigned(static_cast(65533)), -3); + EXPECT_EQ(CastToSigned(static_cast(254)), 254); + EXPECT_EQ(CastToSigned(static_cast(65533)), -3); } - -#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(TestCanCastTo_Int8), NL_TEST_DEF_FN(TestCanCastTo_Int16), - NL_TEST_DEF_FN(TestCanCastTo_Int32), NL_TEST_DEF_FN(TestCanCastTo_Int64), - NL_TEST_DEF_FN(TestCastToSigned), NL_TEST_SENTINEL() }; - -int TestSafeInt() -{ - nlTestSuite theSuite = { "CHIP SafeInt tests", &sTests[0], nullptr, nullptr }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestSafeInt) diff --git a/src/test_driver/openiotsdk/unit-tests/test_components.txt b/src/test_driver/openiotsdk/unit-tests/test_components.txt index 5c3dcbae75a2a3..43a4bf883a9567 100644 --- a/src/test_driver/openiotsdk/unit-tests/test_components.txt +++ b/src/test_driver/openiotsdk/unit-tests/test_components.txt @@ -8,4 +8,5 @@ MdnsTests PlatformTests SystemLayerTests TestShell -SetupPayloadTests \ No newline at end of file +SetupPayloadTests +SupportTests \ 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 7806ae3318cdf2..809a36b167c817 100644 --- a/src/test_driver/openiotsdk/unit-tests/test_components_nl.txt +++ b/src/test_driver/openiotsdk/unit-tests/test_components_nl.txt @@ -9,6 +9,6 @@ RawTransportTests RetransmitTests SecureChannelTests SetupPayloadTestsNL -SupportTests +SupportTestsNL TransportLayerTests UserDirectedCommissioningTests