xref: /freebsd/lib/libnv/tests/nvlist_append_test.c (revision 401ab69cff8fa2320a9f8ea4baa114a6da6c952b)
1 /*-
2  * Copyright (c) 2018 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26 
27 #include <sys/nv.h>
28 
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 static int ntest = 1;
36 
37 #define	CHECK(expr)	do {						\
38 	if ((expr))							\
39 		printf("ok # %d %s:%u\n", ntest, __FILE__, __LINE__);	\
40 	else								\
41 		printf("not ok # %d %s:%u\n", ntest, __FILE__, __LINE__);\
42 	ntest++;							\
43 } while (0)
44 
45 int
46 main(void)
47 {
48 	const bool *bool_result;
49 	const char * const *string_result;
50 	const nvlist_t * const *nvl_result;
51 	nvlist_t *nvl, *nvl1, *nvl2, **items;
52 	unsigned int i;
53 	size_t nitems;
54 
55 	printf("1..32\n");
56 
57 	nvl = nvlist_create(0);
58 
59 	for (i = 0; i < 16; i++)
60 		nvlist_append_bool_array(nvl, "nvl/bool", i % 2 == 0);
61 
62 	CHECK(nvlist_error(nvl) == 0);
63 	CHECK(!nvlist_empty(nvl));
64 	CHECK(nvlist_exists_bool_array(nvl, "nvl/bool"));
65 
66 	bool_result = nvlist_get_bool_array(nvl, "nvl/bool", &nitems);
67 	CHECK(nitems == 16);
68 	CHECK(bool_result != NULL);
69 	for (i = 0; i < nitems; i++)
70 		CHECK(bool_result[i] == (i % 2 == 0));
71 
72 
73 	nvlist_append_string_array(nvl, "nvl/string", "a");
74 	nvlist_append_string_array(nvl, "nvl/string", "abc");
75 	string_result = nvlist_get_string_array(nvl, "nvl/string", &nitems);
76 	CHECK(nitems == 2);
77 	CHECK(strcmp(string_result[0], "a") == 0);
78 	CHECK(strcmp(string_result[1], "abc") == 0);
79 
80 
81 	nvl1 = nvlist_create(0);
82 	nvlist_add_string(nvl1, "key1", "test1");
83 	nvlist_append_nvlist_array(nvl, "nvl/nvl", nvl1);
84 	nvlist_destroy(nvl1);
85 
86 	nvl2 = nvlist_create(0);
87 	nvlist_add_string(nvl2, "key2", "test2");
88 	nvlist_append_nvlist_array(nvl, "nvl/nvl", nvl2);
89 	nvlist_destroy(nvl2);
90 
91 	nvl_result = nvlist_get_nvlist_array(nvl, "nvl/nvl", &nitems);
92 	CHECK(nitems == 2);
93 	CHECK(strcmp(nvlist_get_string(nvl_result[0], "key1"), "test1") == 0);
94 	CHECK(strcmp(nvlist_get_string(nvl_result[1], "key2"), "test2") == 0);
95 
96 	nvl1 = nvlist_create(0);
97 	nvlist_add_number(nvl1, "key1", 10);
98 	nvlist_append_nvlist_array(nvl, "nvl/nvl_array", nvl1);
99 	nvlist_destroy(nvl1);
100 
101 	nvl2 = nvlist_create(0);
102 	nvlist_add_number(nvl2, "key1", 20);
103 	nvlist_append_nvlist_array(nvl, "nvl/nvl_array", nvl2);
104 	nvlist_destroy(nvl2);
105 
106 	items = nvlist_take_nvlist_array(nvl, "nvl/nvl_array", &nitems);
107 	CHECK(nvlist_get_number(items[0], "key1") == 10);
108 	CHECK(nvlist_get_number(items[1], "key1") == 20);
109 	CHECK(nvlist_error(items[0]) == 0);
110 	CHECK(nvlist_error(items[1]) == 0);
111 
112 	nvlist_move_nvlist_array(nvl, "nvl/nvl_new_array", items, nitems);
113 	CHECK(nvlist_error(nvl) == 0);
114 
115 	nvlist_destroy(nvl);
116 
117 	return (0);
118 }
119