xref: /freebsd/lib/libnv/tests/nvlist_free_test.c (revision 0e33efe4e4b5d24e2d416938af8bc6e6e4160ec8)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <sys/nv.h>
33 
34 #include <stdio.h>
35 #include <unistd.h>
36 
37 static int ntest = 1;
38 
39 #define	CHECK(expr)	do {						\
40 	if ((expr))							\
41 		printf("ok # %d %s:%u\n", ntest, __FILE__, __LINE__);	\
42 	else								\
43 		printf("not ok # %d %s:%u\n", ntest, __FILE__, __LINE__);\
44 	ntest++;							\
45 } while (0)
46 
47 int
48 main(void)
49 {
50 	nvlist_t *nvl;
51 
52 	printf("1..114\n");
53 
54 	nvl = nvlist_create(0);
55 
56 	nvlist_add_null(nvl, "nvlist/null");
57 	nvlist_add_bool(nvl, "nvlist/bool", true);
58 	nvlist_add_number(nvl, "nvlist/number", 0);
59 	nvlist_add_string(nvl, "nvlist/string", "test");
60 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
61 	nvlist_add_descriptor(nvl, "nvlist/descriptor", STDERR_FILENO);
62 	nvlist_add_binary(nvl, "nvlist/binary", "test", 4);
63 
64 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
65 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
66 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
67 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
68 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
69 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
70 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
71 
72 	nvlist_free_null(nvl, "nvlist/null");
73 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
74 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
75 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
76 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
77 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
78 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
79 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
80 
81 	nvlist_free_bool(nvl, "nvlist/bool");
82 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
83 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
84 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
85 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
86 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
87 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
88 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
89 
90 	nvlist_free_number(nvl, "nvlist/number");
91 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
92 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
93 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
94 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
95 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
96 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
97 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
98 
99 	nvlist_free_string(nvl, "nvlist/string");
100 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
101 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
102 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
103 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
104 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
105 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
106 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
107 
108 	nvlist_free_nvlist(nvl, "nvlist/nvlist");
109 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
110 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
111 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
112 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
113 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
114 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
115 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
116 
117 	nvlist_free_descriptor(nvl, "nvlist/descriptor");
118 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
119 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
120 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
121 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
122 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
123 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
124 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
125 
126 	nvlist_free_binary(nvl, "nvlist/binary");
127 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
128 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
129 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
130 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
131 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
132 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
133 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary"));
134 
135 	CHECK(nvlist_empty(nvl));
136 
137 	nvlist_add_null(nvl, "nvlist/null");
138 	nvlist_add_bool(nvl, "nvlist/bool", true);
139 	nvlist_add_number(nvl, "nvlist/number", 0);
140 	nvlist_add_string(nvl, "nvlist/string", "test");
141 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
142 	nvlist_add_descriptor(nvl, "nvlist/descriptor", STDERR_FILENO);
143 	nvlist_add_binary(nvl, "nvlist/binary", "test", 4);
144 
145 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
146 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
147 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
148 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
149 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
150 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
151 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
152 
153 	nvlist_free(nvl, "nvlist/null");
154 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
155 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
156 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
157 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
158 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
159 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
160 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
161 
162 	nvlist_free(nvl, "nvlist/bool");
163 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
164 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
165 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
166 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
167 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
168 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
169 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
170 
171 	nvlist_free(nvl, "nvlist/number");
172 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
173 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
174 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
175 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
176 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
177 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
178 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
179 
180 	nvlist_free(nvl, "nvlist/string");
181 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
182 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
183 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
184 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
185 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
186 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
187 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
188 
189 	nvlist_free(nvl, "nvlist/nvlist");
190 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
191 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
192 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
193 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
194 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
195 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
196 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
197 
198 	nvlist_free(nvl, "nvlist/descriptor");
199 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
200 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
201 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
202 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
203 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
204 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
205 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
206 
207 	nvlist_free(nvl, "nvlist/binary");
208 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
209 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
210 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
211 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
212 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
213 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
214 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary"));
215 
216 	CHECK(nvlist_empty(nvl));
217 
218 	nvlist_destroy(nvl);
219 
220 	return (0);
221 }
222