Use std::string_view in several APIs
Change-Id: I2c13b229162b247738cbf46d6bf71fc69d45816f
diff --git a/tests/unit/encoding/block-helpers.t.cpp b/tests/unit/encoding/block-helpers.t.cpp
index 9325851..ed8641e 100644
--- a/tests/unit/encoding/block-helpers.t.cpp
+++ b/tests/unit/encoding/block-helpers.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2022 Regents of the University of California.
+ * Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -31,6 +31,13 @@
BOOST_AUTO_TEST_SUITE(Encoding)
BOOST_AUTO_TEST_SUITE(TestBlockHelpers)
+BOOST_AUTO_TEST_CASE(Empty)
+{
+ Block b = makeEmptyBlock(200);
+ BOOST_CHECK_EQUAL(b.type(), 200);
+ BOOST_CHECK_EQUAL(b.value_size(), 0);
+}
+
enum E8 : uint8_t
{
E8_NONE
@@ -69,21 +76,6 @@
BOOST_CHECK_EQUAL(static_cast<uint16_t>(readNonNegativeIntegerAs<EC16>(b)), 1000);
}
-BOOST_AUTO_TEST_CASE(Empty)
-{
- Block b = makeEmptyBlock(200);
- BOOST_CHECK_EQUAL(b.type(), 200);
- BOOST_CHECK_EQUAL(b.value_size(), 0);
-}
-
-BOOST_AUTO_TEST_CASE(String)
-{
- Block b = makeStringBlock(100, "Hello, world!");
- BOOST_CHECK_EQUAL(b.type(), 100);
- BOOST_CHECK_GT(b.value_size(), 0);
- BOOST_CHECK_EQUAL(readString(b), "Hello, world!");
-}
-
BOOST_AUTO_TEST_CASE(Double)
{
const double f = 0.25;
@@ -103,29 +95,41 @@
BOOST_AUTO_TEST_CASE(Binary)
{
- std::string buf1{1, 1, 1, 1};
+ const std::string buf1{1, 1, 1, 1};
const uint8_t buf2[]{1, 1, 1, 1};
- std::list<uint8_t> buf3{1, 1, 1, 1};
+ const std::list<uint8_t> buf3{1, 1, 1, 1};
- Block b1 = makeBinaryBlock(100, buf1.data(), buf1.size()); // char* overload
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+ Block b1 = makeBinaryBlock(100, buf1.data(), buf1.size()); // char* overload (deprecated)
+#pragma GCC diagnostic pop
Block b2 = makeBinaryBlock(100, buf2); // span overload
Block b3 = makeBinaryBlock(100, buf1.begin(), buf1.end()); // fast encoding (random access iterator)
Block b4 = makeBinaryBlock(100, buf3.begin(), buf3.end()); // slow encoding (general iterator)
- BOOST_CHECK_EQUAL(b1, b2);
- BOOST_CHECK_EQUAL(b1, b3);
- BOOST_CHECK_EQUAL(b1, b4);
- BOOST_CHECK_EQUAL(b1.type(), 100);
- BOOST_CHECK_EQUAL(b1.value_size(), buf1.size());
- BOOST_CHECK_EQUAL_COLLECTIONS(b1.value_begin(), b1.value_end(), buf2, buf2 + sizeof(buf2));
+ BOOST_TEST(b1 == b2);
+ BOOST_TEST(b1 == b3);
+ BOOST_TEST(b1 == b4);
+ BOOST_TEST(b1.type() == 100);
+ BOOST_TEST(b1.value_size() == sizeof(buf2));
+ BOOST_TEST(b1.value_bytes() == buf2, boost::test_tools::per_element());
EncodingEstimator estimator;
size_t length = prependBinaryBlock(estimator, 100, buf2);
- BOOST_CHECK_EQUAL(length, 6);
+ BOOST_TEST(length == 6);
EncodingBuffer encoder(length, 0);
- BOOST_CHECK_EQUAL(prependBinaryBlock(encoder, 100, buf2), 6);
- BOOST_CHECK_EQUAL(encoder.block(), b1);
+ BOOST_TEST(prependBinaryBlock(encoder, 100, buf2) == 6);
+ BOOST_TEST(encoder.block() == b1);
+}
+
+BOOST_AUTO_TEST_CASE(String)
+{
+ constexpr std::string_view sv{"Hello, world!"sv};
+ Block b = makeStringBlock(100, sv);
+ BOOST_TEST(b.type() == 100);
+ BOOST_TEST(b.value_size() == sv.size());
+ BOOST_TEST(readString(b) == sv);
}
BOOST_AUTO_TEST_CASE(PrependBlock)
diff --git a/tests/unit/face.t.cpp b/tests/unit/face.t.cpp
index 2372e24..94a2649 100644
--- a/tests/unit/face.t.cpp
+++ b/tests/unit/face.t.cpp
@@ -854,11 +854,6 @@
using ndn::Transport;
-struct PibDirWithDefaultTpm
-{
- const std::string PATH = "build/keys-with-default-tpm";
-};
-
BOOST_FIXTURE_TEST_CASE(FaceTransport, IoKeyChainFixture)
{
BOOST_CHECK(Face().getTransport() != nullptr);
diff --git a/tests/unit/name.t.cpp b/tests/unit/name.t.cpp
index bfddb4f..cfe13e0 100644
--- a/tests/unit/name.t.cpp
+++ b/tests/unit/name.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2022 Regents of the University of California.
+ * Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -68,13 +68,19 @@
// URI with correct scheme
BOOST_CHECK_EQUAL(Name("ndn:/hello/world").toUri(), "/hello/world");
+ BOOST_CHECK_EQUAL(Name("ndn:/").toUri(), "/");
+ BOOST_CHECK_EQUAL(Name("ndn:").toUri(), "/");
// URI with incorrect scheme: auto-corrected
BOOST_CHECK_EQUAL(Name("ncc:/hello/world").toUri(), "/hello/world");
+ BOOST_CHECK_EQUAL(Name(":/").toUri(), "/");
+ BOOST_CHECK_EQUAL(Name(":").toUri(), "/");
// URI with authority: authority ignored
BOOST_CHECK_EQUAL(Name("//authority/hello/world").toUri(), "/hello/world");
BOOST_CHECK_EQUAL(Name("ndn://authority/hello/world").toUri(), "/hello/world");
+ BOOST_CHECK_EQUAL(Name("//authority").toUri(), "/");
+ BOOST_CHECK_EQUAL(Name("ndn://").toUri(), "/");
// URI containing unescaped characters: auto-corrected
BOOST_CHECK_EQUAL(Name("/ hello\t/\tworld \r\n").toUri(), "/%20hello%09/%09world%20%0D%0A");
@@ -84,8 +90,12 @@
// URI not starting with '/': accepted as PartialName
BOOST_CHECK_EQUAL(Name("").toUri(), "/");
BOOST_CHECK_EQUAL(Name(" ").toUri(), "/%20");
+ BOOST_CHECK_EQUAL(Name("ndn: ").toUri(), "/%20");
+ BOOST_CHECK_EQUAL(Name("ndn: /").toUri(), "/%20");
BOOST_CHECK_EQUAL(Name(" /hello/world").toUri(), "/%20%20/hello/world");
BOOST_CHECK_EQUAL(Name("hello/world").toUri(), "/hello/world");
+ BOOST_CHECK_EQUAL(Name("hello").toUri(), "/hello");
+ BOOST_CHECK_EQUAL(Name("ndn:hello").toUri(), "/hello");
// URI ending with '/': auto-corrected
BOOST_CHECK_EQUAL(Name("/hello/world/").toUri(), "/hello/world");
diff --git a/tests/unit/security/key-chain.t.cpp b/tests/unit/security/key-chain.t.cpp
index 9bd97af..be544a4 100644
--- a/tests/unit/security/key-chain.t.cpp
+++ b/tests/unit/security/key-chain.t.cpp
@@ -59,7 +59,7 @@
struct PibPathConfigFileHome
{
- const std::string PATH = "build/config-file-home/";
+ static constexpr std::string_view PATH = "build/config-file-home/";
};
BOOST_FIXTURE_TEST_CASE(ConstructorNormalConfig, TestHomeAndPibFixture<PibPathConfigFileHome>)
@@ -74,7 +74,7 @@
struct PibPathConfigFileEmptyHome
{
- const std::string PATH = "build/config-file-empty-home/";
+ static constexpr std::string_view PATH = "build/config-file-empty-home/";
};
BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig, TestHomeAndPibFixture<PibPathConfigFileEmptyHome>)
@@ -122,7 +122,7 @@
struct PibPathConfigFileEmpty2Home
{
- const std::string PATH = "build/config-file-empty2-home/";
+ static constexpr std::string_view PATH = "build/config-file-empty2-home/";
};
BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig2, TestHomeAndPibFixture<PibPathConfigFileEmpty2Home>)
@@ -137,7 +137,7 @@
struct PibPathConfigFileMalformedHome
{
- const std::string PATH = "build/config-file-malformed-home/";
+ static constexpr std::string_view PATH = "build/config-file-malformed-home/";
};
BOOST_FIXTURE_TEST_CASE(ConstructorBadConfig, TestHomeAndPibFixture<PibPathConfigFileMalformedHome>)
@@ -148,7 +148,7 @@
struct PibPathConfigFileMalformed2Home
{
- const std::string PATH = "build/config-file-malformed2-home/";
+ static constexpr std::string_view PATH = "build/config-file-malformed2-home/";
};
BOOST_FIXTURE_TEST_CASE(ConstructorBadConfig2, TestHomeAndPibFixture<PibPathConfigFileMalformed2Home>)
@@ -159,7 +159,7 @@
struct PibPathConfigFileNonCanonicalTpm
{
- const std::string PATH = "build/config-file-non-canonical-tpm/";
+ static constexpr std::string_view PATH = "build/config-file-non-canonical-tpm/";
};
BOOST_FIXTURE_TEST_CASE(ConstructorNonCanonicalTpm, TestHomeAndPibFixture<PibPathConfigFileNonCanonicalTpm>) // Bug 4297
diff --git a/tests/unit/util/logging.t.cpp b/tests/unit/util/logging.t.cpp
index 821e11e..02da03a 100644
--- a/tests/unit/util/logging.t.cpp
+++ b/tests/unit/util/logging.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2022 Regents of the University of California.
+ * Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -162,7 +162,7 @@
NDN_LOG_MEMBER_INIT_SPECIALIZED((ClassTemplateWithLogger<int, double>), ndn.util.tests.Specialized1);
NDN_LOG_MEMBER_INIT_SPECIALIZED((ClassTemplateWithLogger<int, std::string>), ndn.util.tests.Specialized2);
-const time::microseconds LOG_SYSTIME(1468108800311239LL);
+constexpr time::microseconds LOG_SYSTIME{1468108800311239LL};
const std::string LOG_SYSTIME_STR("1468108800.311239");
class LoggingFixture : public ndn::tests::ClockFixture
@@ -177,7 +177,7 @@
Logging::setDestination(os, true);
}
- ~LoggingFixture()
+ ~LoggingFixture() override
{
Logging::get().setLevelImpl(m_oldEnabledLevel);
Logging::setDestination(m_oldDestination);
diff --git a/tests/unit/util/string-helper.t.cpp b/tests/unit/util/string-helper.t.cpp
index e3229e5..3816fab 100644
--- a/tests/unit/util/string-helper.t.cpp
+++ b/tests/unit/util/string-helper.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2022 Regents of the University of California.
+ * Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -182,8 +182,7 @@
BOOST_CHECK_EQUAL(escape(":/?#[]@"), "%3A%2F%3F%23%5B%5D%40");
output_test_stream os;
- const char str[] = "\x01\x2a\x3b\xc4\xde\xfa\xb5\xcd\xef";
- escape(os, str, std::strlen(str));
+ escape(os, "\x01\x2a\x3b\xc4\xde\xfa\xb5\xcd\xef");
BOOST_CHECK(os.is_equal("%01%2A%3B%C4%DE%FA%B5%CD%EF"));
}
@@ -196,8 +195,7 @@
BOOST_CHECK_EQUAL(unescape("Bad %a"), "Bad %a");
output_test_stream os;
- const char str[] = "%01%2a%3B%c4%de%fA%B5%Cd%EF";
- unescape(os, str, std::strlen(str));
+ unescape(os, "%01%2a%3B%c4%de%fA%B5%Cd%EF");
BOOST_CHECK(os.is_equal("\x01\x2a\x3b\xc4\xde\xfa\xb5\xcd\xef"));
}