diff options
| author | Lexi Winter <lexi@hemlock.eden.le-fay.org> | 2025-03-29 06:16:58 +0000 |
|---|---|---|
| committer | Lexi Winter <lexi@hemlock.eden.le-fay.org> | 2025-03-29 06:16:58 +0000 |
| commit | 5cb84b0deeb55ba052f39900f8bab93ce917914f (patch) | |
| tree | 3e7fa1feb30a2c33219f68d6e8f4367f15e45468 | |
| parent | 61fc4a0aec687a4e52608f90c3613c41dc12ab5a (diff) | |
| download | libnvxx-5cb84b0deeb55ba052f39900f8bab93ce917914f.tar.gz libnvxx-5cb84b0deeb55ba052f39900f8bab93ce917914f.tar.bz2 | |
add tests for operator= and nv_list::release()
| -rw-r--r-- | libnvxx/nv_list.cc | 23 | ||||
| -rw-r--r-- | libnvxx/nvxx_base.h | 6 | ||||
| -rw-r--r-- | libnvxx/tests/nvxx_basic.cc | 80 |
3 files changed, 99 insertions, 10 deletions
diff --git a/libnvxx/nv_list.cc b/libnvxx/nv_list.cc index 03916d9..6eadd36 100644 --- a/libnvxx/nv_list.cc +++ b/libnvxx/nv_list.cc @@ -56,14 +56,21 @@ nv_list::nv_list(nv_list &&other) noexcept nv_list & nv_list::operator=(nv_list const &other) { - if (this != &other) { - auto *clone = nvlist_clone(other.__m_nv); - if (clone == nullptr) - throw std::system_error(std::error_code(errno, std::system_category())); - __free_nv(); - __m_nv = clone; - __m_owning = __detail::__nvlist_owning::__owning; - } + if (this != &other) + *this = const_nv_list(other); + + return (*this); +} + +nv_list & +nv_list::operator=(const_nv_list const &other) +{ + auto *clone = nvlist_clone(other.ptr()); + if (clone == nullptr) + throw std::system_error(std::error_code(errno, std::system_category())); + __free_nv(); + __m_nv = clone; + __m_owning = __detail::__nvlist_owning::__owning; return (*this); } diff --git a/libnvxx/nvxx_base.h b/libnvxx/nvxx_base.h index 257c217..12490e8 100644 --- a/libnvxx/nvxx_base.h +++ b/libnvxx/nvxx_base.h @@ -383,6 +383,12 @@ 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 * accessed other than to destruct it. diff --git a/libnvxx/tests/nvxx_basic.cc b/libnvxx/tests/nvxx_basic.cc index b428611..1d293af 100644 --- a/libnvxx/tests/nvxx_basic.cc +++ b/libnvxx/tests/nvxx_basic.cc @@ -135,6 +135,76 @@ TEST_CASE(nvxx_const_nv_list_ctor_copy) } /* + * operator= + */ + +TEST_CASE(nvxx_nv_list_assign_copy) +{ + using namespace std::literals; + auto constexpr key = "test"sv; + auto constexpr value = 42u; + + auto nvl = bsd::nv_list(); + nvl.add_number(key, value); + + auto nvl2 = bsd::nv_list(); + nvl2 = nvl; + ATF_REQUIRE_EQ(false, nvl.ptr() == nvl2.ptr()); + ATF_REQUIRE_EQ(value, nvl2.get_number(key)); +} + +TEST_CASE(nvxx_nv_list_assign_move) +{ + using namespace std::literals; + auto constexpr key = "test"sv; + auto constexpr value = 42u; + + auto nvl = bsd::nv_list(); + nvl.add_number(key, value); + auto old_ptr = nvl.ptr(); + + auto nvl2 = bsd::nv_list(); + nvl2 = std::move(nvl); + ATF_REQUIRE_THROW(std::logic_error, nvl.ptr()); + ATF_REQUIRE_EQ(old_ptr, nvl2.ptr()); + ATF_REQUIRE_EQ(value, nvl2.get_number(key)); +} + +TEST_CASE(nvxx_nv_list_assign_const_nv_list) +{ + 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 nvl2 = bsd::nv_list(); + nvl2 = cnv; + ATF_REQUIRE_EQ(false, nvl2.ptr() == cnv.ptr()); + ATF_REQUIRE_EQ(value, nvl2.get_number(key)); +} + +/* + * release + */ +TEST_CASE(nvxx_nv_list_release) +{ + 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 = std::move(nvl).release(); + ATF_REQUIRE_EQ(value, ::nvlist_get_number(nv, std::string(key).c_str())); + ATF_REQUIRE_THROW(std::logic_error, nvl.ptr()); +} + +/* * error/set_error */ @@ -1474,7 +1544,7 @@ TEST_CASE(nvxx_add_duplicate_descriptor) auto guard = std::unique_ptr<int[], decltype([](auto fds) { ::close(fds[0]); - ::close(fds[1]); + ::close(fds[1]); })>(&fds[0]); auto nvl = bsd::nv_list{}; @@ -1497,7 +1567,7 @@ TEST_CASE(nvxx_free_descriptor_array) auto guard = std::unique_ptr<int[], decltype([](auto fds) { ::close(fds[0]); - ::close(fds[1]); + ::close(fds[1]); })>(&fds[0]); auto nvl = bsd::nv_list{}; @@ -1681,6 +1751,12 @@ ATF_INIT_TEST_CASES(tcs) ATF_ADD_TEST_CASE(tcs, nvxx_const_nv_list_ctor_nv_list); ATF_ADD_TEST_CASE(tcs, nvxx_const_nv_list_ctor_nvlist_t); + ATF_ADD_TEST_CASE(tcs, nvxx_nv_list_assign_copy); + 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_release); + ATF_ADD_TEST_CASE(tcs, nvxx_ignore_case); ATF_ADD_TEST_CASE(tcs, nvxx_set_error); |
