From 26232fbeb5d0605a015eecc848b840a7735b648f Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Thu, 27 Mar 2025 10:58:30 +0000 Subject: implement exists_type() --- libnvxx/const_nv_list.cc | 6 ++++++ libnvxx/nvxx.3 | 8 ++++++++ libnvxx/nvxx_base.h | 5 +++++ libnvxx/tests/nvxx_basic.cc | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/libnvxx/const_nv_list.cc b/libnvxx/const_nv_list.cc index 883eebb..0ed83b2 100644 --- a/libnvxx/const_nv_list.cc +++ b/libnvxx/const_nv_list.cc @@ -73,6 +73,12 @@ __const_nv_list::exists(std::string_view key) const return ::nvlist_exists(__m_nv, std::string(key).c_str()); } +bool +__const_nv_list::exists_type(std::string_view key, int type) const +{ + return ::nvlist_exists_type(__m_nv, std::string(key).c_str(), type); +} + bool __const_nv_list::empty() const noexcept { diff --git a/libnvxx/nvxx.3 b/libnvxx/nvxx.3 index 26fa5eb..8a43649 100644 --- a/libnvxx/nvxx.3 +++ b/libnvxx/nvxx.3 @@ -66,6 +66,7 @@ struct const_nv_list : { void send(int fd) const; bool exists(std::string_view key) const; + bool exists_type(std::string_view key, int) const; bool exists_null(std::string_view key) const; bool exists_bool(std::string_view key) const; bool exists_number(std::string_view key) const; @@ -361,6 +362,13 @@ member function returns if a key by the given name exists, otherwise .Dv false . .Pp +The +.Fn exists_type +member function returns +.Dv true +if a key by the given name exists with the specified type, which should be one +of the type constants defined in . +.Pp The .Fn exists_null , .Fn exists_bool , diff --git a/libnvxx/nvxx_base.h b/libnvxx/nvxx_base.h index df97893..aa1f176 100644 --- a/libnvxx/nvxx_base.h +++ b/libnvxx/nvxx_base.h @@ -161,6 +161,11 @@ struct __const_nv_list : virtual __nv_list_base { */ [[nodiscard]] bool exists(std::string_view __key) const; + /* + * If a key of the given type with the given name exists, return true. + */ + [[nodiscard]] bool exists_type(std::string_view __key, int type) const; + // TODO: exists_type() /* exists */ diff --git a/libnvxx/tests/nvxx_basic.cc b/libnvxx/tests/nvxx_basic.cc index d8faea3..d73a54b 100644 --- a/libnvxx/tests/nvxx_basic.cc +++ b/libnvxx/tests/nvxx_basic.cc @@ -46,6 +46,35 @@ TEST_CASE(nvxx_const_ctor_error) auto nvl = bsd::const_nv_list(nvp)); } +/* + * exists(_type) + */ + +TEST_CASE(nvxx_exists) +{ + using namespace std::literals; + auto key = "test number"sv; + auto value = 42u; + + auto nvl = bsd::nv_list{}; + nvl.add_number(key, value); + ATF_REQUIRE_EQ(true, nvl.exists(key)); + ATF_REQUIRE_EQ(false, nvl.exists("nonesuch")); +} + +TEST_CASE(nvxx_exists_type) +{ + using namespace std::literals; + auto key = "test number"sv; + auto value = 42u; + + auto nvl = bsd::nv_list{}; + nvl.add_number(key, value); + ATF_REQUIRE_EQ(true, nvl.exists_type(key, NV_TYPE_NUMBER)); + ATF_REQUIRE_EQ(false, nvl.exists_type(key, NV_TYPE_STRING)); + ATF_REQUIRE_EQ(false, nvl.exists_type("nonesuch", NV_TYPE_NUMBER)); +} + /* * test the NV_FLAG_IGNORE_CASE flag. */ @@ -975,6 +1004,9 @@ ATF_INIT_TEST_CASES(tcs) ATF_ADD_TEST_CASE(tcs, nvxx_const_ctor_error); ATF_ADD_TEST_CASE(tcs, nvxx_ignore_case); + ATF_ADD_TEST_CASE(tcs, nvxx_exists); + ATF_ADD_TEST_CASE(tcs, nvxx_exists_type); + ATF_ADD_TEST_CASE(tcs, nvxx_add_null); ATF_ADD_TEST_CASE(tcs, nvxx_add_null_error); ATF_ADD_TEST_CASE(tcs, nvxx_add_duplicate_null); -- cgit v1.2.3