diff options
| author | Lexi Winter <lexi@hemlock.eden.le-fay.org> | 2025-03-24 22:54:47 +0000 |
|---|---|---|
| committer | Lexi Winter <lexi@hemlock.eden.le-fay.org> | 2025-03-24 22:54:47 +0000 |
| commit | b82e66c2f41f9f8fb14391926e9e716c2ab18271 (patch) | |
| tree | 9dfbe6780418e7524f5a6835ab3609bbdcf88005 | |
| parent | 10881c992810727ca7a1be7de864f400f160b50f (diff) | |
| download | libnvxx-b82e66c2f41f9f8fb14391926e9e716c2ab18271.tar.gz libnvxx-b82e66c2f41f9f8fb14391926e9e716c2ab18271.tar.bz2 | |
test all the basic type in nvxx_iterator test
| -rw-r--r-- | libnvxx/nvxx_iterator.cc | 7 | ||||
| -rw-r--r-- | libnvxx/tests/nvxx_iterator.cc | 47 |
2 files changed, 53 insertions, 1 deletions
diff --git a/libnvxx/nvxx_iterator.cc b/libnvxx/nvxx_iterator.cc index 5a8f04f..9e0781b 100644 --- a/libnvxx/nvxx_iterator.cc +++ b/libnvxx/nvxx_iterator.cc @@ -123,25 +123,31 @@ nv_list_iterator::__advance() case NV_TYPE_NULL: __current = std::make_pair(name, nullptr); break; + case NV_TYPE_BOOL: __current = std::make_pair(name, cnvlist_get_bool(__cookie)); break; + case NV_TYPE_NUMBER: __current = std::make_pair(name, cnvlist_get_number(__cookie)); break; + case NV_TYPE_STRING: __current = std::make_pair(name, std::string_view( cnvlist_get_string(__cookie))); break; + case NV_TYPE_NVLIST: __current = std::make_pair(name, const_nv_list(cnvlist_get_nvlist(__cookie))); break; + case NV_TYPE_DESCRIPTOR: __current = std::make_pair(name, cnvlist_get_descriptor(__cookie)); break; + case NV_TYPE_BINARY: { auto nitems = std::size_t{}; auto ptr = cnvlist_get_binary(__cookie, &nitems); @@ -149,6 +155,7 @@ nv_list_iterator::__advance() __current = std::make_pair(name, span); break; } + default: std::abort(); } diff --git a/libnvxx/tests/nvxx_iterator.cc b/libnvxx/tests/nvxx_iterator.cc index df78bdf..fd96b38 100644 --- a/libnvxx/tests/nvxx_iterator.cc +++ b/libnvxx/tests/nvxx_iterator.cc @@ -40,10 +40,30 @@ TEST_CASE(nvxx_basic_iterate) { using namespace std::literals; auto nvl = bsd::nv_list(); + int fds[2]; + auto binary = std::array<std::byte, 4>{ + static_cast<std::byte>(1), + static_cast<std::byte>(2), + static_cast<std::byte>(3), + static_cast<std::byte>(4) + }; + + auto ret = ::pipe(fds); + ATF_REQUIRE_EQ(0, ret); + + nvl.add_null("a null"); nvl.add_number("a number", 42); nvl.add_string("a string", "a test string"); nvl.add_bool("a bool", true); + nvl.add_binary("a binary", binary); + + auto fdesc = fds[0]; + nvl.move_descriptor("an fd", fdesc); + + auto nvl2 = bsd::nv_list(); + nvl2.add_number("child number", 666); + nvl.add_nvlist("an nvlist", nvl2); auto begin = std::ranges::begin(nvl); auto end = std::ranges::end(nvl); @@ -56,20 +76,45 @@ TEST_CASE(nvxx_basic_iterate) if (std::holds_alternative<std::uint64_t>(value)) { ATF_REQUIRE_EQ("a number"sv, name); ATF_REQUIRE_EQ(42, std::get<std::uint64_t>(value)); + + } else if (std::holds_alternative<nullptr_t>(value)) { + ATF_REQUIRE_EQ("a null"sv, name); + ATF_REQUIRE_EQ(nullptr, + std::get<nullptr_t>(value)); + } else if (std::holds_alternative<std::string_view>(value)) { ATF_REQUIRE_EQ("a string"sv, name); ATF_REQUIRE_EQ("a test string", std::get<std::string_view>(value)); + } else if (std::holds_alternative<bool>(value)) { ATF_REQUIRE_EQ("a bool"sv, name); ATF_REQUIRE_EQ(true, std::get<bool>(value)); + + } else if (std::holds_alternative<int>(value)) { + ATF_REQUIRE_EQ("an fd"sv, name); + ATF_REQUIRE_EQ(fdesc, std::get<int>(value)); + + } else if (std::holds_alternative< + std::span<std::byte const>>(value)) { + ATF_REQUIRE_EQ("a binary"sv, name); + auto data = std::get<std::span<std::byte const>>(value); + ATF_REQUIRE_EQ(true, std::ranges::equal(binary, data)); + + } else if (std::holds_alternative<bsd::const_nv_list>(value)) { + ATF_REQUIRE_EQ("an nvlist"sv, name); + ATF_REQUIRE_EQ(666, + std::get<bsd::const_nv_list>(value) + .get_number("child number")); + } else ATF_REQUIRE_EQ(true, false); ++i; ++begin; } - ATF_REQUIRE_EQ(3, i); + + ATF_REQUIRE_EQ(7, i); } ATF_INIT_TEST_CASES(tcs) |
