2 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
39 #define CHECK(expr) do { \
41 printf("ok # %d %s:%u\n", ntest, __FILE__, __LINE__); \
43 printf("not ok # %d %s:%u\n", ntest, __FILE__, __LINE__);\
50 const bool *bool_result;
51 const char * const *string_result;
52 const nvlist_t * const *nvl_result;
53 nvlist_t *nvl, *nvl1, *nvl2, **items;
59 nvl = nvlist_create(0);
61 for (i = 0; i < 16; i++)
62 nvlist_append_bool_array(nvl, "nvl/bool", i % 2 == 0);
64 CHECK(nvlist_error(nvl) == 0);
65 CHECK(!nvlist_empty(nvl));
66 CHECK(nvlist_exists_bool_array(nvl, "nvl/bool"));
68 bool_result = nvlist_get_bool_array(nvl, "nvl/bool", &nitems);
70 CHECK(bool_result != NULL);
71 for (i = 0; i < nitems; i++)
72 CHECK(bool_result[i] == (i % 2 == 0));
75 nvlist_append_string_array(nvl, "nvl/string", "a");
76 nvlist_append_string_array(nvl, "nvl/string", "abc");
77 string_result = nvlist_get_string_array(nvl, "nvl/string", &nitems);
79 CHECK(strcmp(string_result[0], "a") == 0);
80 CHECK(strcmp(string_result[1], "abc") == 0);
83 nvl1 = nvlist_create(0);
84 nvlist_add_string(nvl1, "key1", "test1");
85 nvlist_append_nvlist_array(nvl, "nvl/nvl", nvl1);
88 nvl2 = nvlist_create(0);
89 nvlist_add_string(nvl2, "key2", "test2");
90 nvlist_append_nvlist_array(nvl, "nvl/nvl", nvl2);
93 nvl_result = nvlist_get_nvlist_array(nvl, "nvl/nvl", &nitems);
95 CHECK(strcmp(nvlist_get_string(nvl_result[0], "key1"), "test1") == 0);
96 CHECK(strcmp(nvlist_get_string(nvl_result[1], "key2"), "test2") == 0);
98 nvl1 = nvlist_create(0);
99 nvlist_add_number(nvl1, "key1", 10);
100 nvlist_append_nvlist_array(nvl, "nvl/nvl_array", nvl1);
101 nvlist_destroy(nvl1);
103 nvl2 = nvlist_create(0);
104 nvlist_add_number(nvl2, "key1", 20);
105 nvlist_append_nvlist_array(nvl, "nvl/nvl_array", nvl2);
106 nvlist_destroy(nvl2);
108 items = nvlist_take_nvlist_array(nvl, "nvl/nvl_array", &nitems);
109 CHECK(nvlist_get_number(items[0], "key1") == 10);
110 CHECK(nvlist_get_number(items[1], "key1") == 20);
111 CHECK(nvlist_error(items[0]) == 0);
112 CHECK(nvlist_error(items[1]) == 0);
114 nvlist_move_nvlist_array(nvl, "nvl/nvl_new_array", items, nitems);
115 CHECK(nvlist_error(nvl) == 0);