From 6ae3fb279c468a6f58ff817703cfb483589f61a2 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sat, 29 Mar 2025 06:29:01 +0000 Subject: add tests for ptr() --- libnvxx/nvxx_base.h | 12 +++---- libnvxx/tests/nvxx_basic.cc | 87 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/libnvxx/nvxx_base.h b/libnvxx/nvxx_base.h index 12490e8..68d749e 100644 --- a/libnvxx/nvxx_base.h +++ b/libnvxx/nvxx_base.h @@ -382,12 +382,6 @@ struct nv_list final */ nv_list &operator=(nv_list const &); - /* - * Replace the wrapped nv_list with a copy of the RHS const_nv_list - * using nvlist_clone(). On failure, throws std::system_error. - */ - nv_list &operator=(const_nv_list const &); - /* * Replace the wrapped nv_list by moving from another nv_list. The * moved-from nv_list is left in an undefined state and must not be @@ -395,6 +389,12 @@ struct nv_list final */ nv_list &operator=(nv_list &&) noexcept; + /* + * Replace the wrapped nv_list with a copy of the RHS const_nv_list + * using nvlist_clone(). On failure, throws std::system_error. + */ + nv_list &operator=(const_nv_list const &); + /* * Return the pointer stored by this nv_list, without releasing it. * The pointer may be used to modify the nvlist, but must not be diff --git a/libnvxx/tests/nvxx_basic.cc b/libnvxx/tests/nvxx_basic.cc index 1d293af..4ea2e40 100644 --- a/libnvxx/tests/nvxx_basic.cc +++ b/libnvxx/tests/nvxx_basic.cc @@ -204,6 +204,85 @@ TEST_CASE(nvxx_nv_list_release) ATF_REQUIRE_THROW(std::logic_error, nvl.ptr()); } +/* + * ptr + */ + +TEST_CASE(nvxx_nv_list_ptr) +{ + using namespace std::literals; + auto constexpr key = "test"sv; + auto constexpr value = 42u; + + auto nvl = bsd::nv_list(); + nvl.add_number(key, value); + + auto *nv = nvl.ptr(); + ATF_REQUIRE_EQ(value, ::nvlist_get_number(nv, std::string(key).c_str())); + ::nvlist_add_number(nv, "test2", 666); + ATF_REQUIRE_EQ(666, nvl.get_number("test2")); +} + +TEST_CASE(nvxx_nv_list_ptr_const) +{ + using namespace std::literals; + auto constexpr key = "test"sv; + auto constexpr value = 42u; + + auto nvl = bsd::nv_list(); + nvl.add_number(key, value); + + auto *nv = static_cast(nvl).ptr(); + static_assert(std::is_const_v>); + ATF_REQUIRE_EQ(value, ::nvlist_get_number(nv, std::string(key).c_str())); +} + +TEST_CASE(nvxx_nv_list_ptr_empty) +{ + auto nvl = bsd::nv_list(); + auto nvl2 = std::move(nvl); + + ATF_REQUIRE_THROW(std::logic_error, nvl.ptr()); +} + +TEST_CASE(nvxx_nv_list_ptr_error) +{ + auto nvl = bsd::nv_list(); + nvl.set_error(std::errc::invalid_argument); + ATF_REQUIRE_EQ(true, nvl.ptr() != nullptr); +} + +TEST_CASE(nvxx_const_nv_list_ptr) +{ + using namespace std::literals; + auto constexpr key = "test"sv; + auto constexpr value = 42u; + + auto nvl = bsd::nv_list(); + nvl.add_number(key, value); + + auto cnv = bsd::const_nv_list(nvl); + auto *nv = cnv.ptr(); + static_assert(std::is_const_v>); + ATF_REQUIRE_EQ(nvl.ptr(), nv); + ATF_REQUIRE_EQ(value, ::nvlist_get_number(nv, std::string(key).c_str())); +} + +TEST_CASE(nvxx_const_nv_list_ptr_empty) +{ + auto cnv = bsd::const_nv_list(); + ATF_REQUIRE_THROW(std::logic_error, cnv.ptr()); +} + +TEST_CASE(nvxx_const_nv_list_ptr_error) +{ + auto nvl = bsd::nv_list(); + nvl.set_error(std::errc::invalid_argument); + + auto cnv = bsd::const_nv_list(nvl); + ATF_REQUIRE_EQ(true, cnv.ptr() != nullptr); +} + /* * error/set_error */ @@ -1755,8 +1834,16 @@ ATF_INIT_TEST_CASES(tcs) ATF_ADD_TEST_CASE(tcs, nvxx_nv_list_assign_move); ATF_ADD_TEST_CASE(tcs, nvxx_nv_list_assign_const_nv_list); + ATF_ADD_TEST_CASE(tcs, nvxx_nv_list_ptr); + ATF_ADD_TEST_CASE(tcs, nvxx_nv_list_ptr_const); + ATF_ADD_TEST_CASE(tcs, nvxx_nv_list_ptr_empty); + ATF_ADD_TEST_CASE(tcs, nvxx_nv_list_ptr_error); ATF_ADD_TEST_CASE(tcs, nvxx_nv_list_release); + ATF_ADD_TEST_CASE(tcs, nvxx_const_nv_list_ptr); + ATF_ADD_TEST_CASE(tcs, nvxx_const_nv_list_ptr_empty); + ATF_ADD_TEST_CASE(tcs, nvxx_const_nv_list_ptr_error); + ATF_ADD_TEST_CASE(tcs, nvxx_ignore_case); ATF_ADD_TEST_CASE(tcs, nvxx_set_error); -- cgit v1.2.3