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 * $FreeBSD$
27 */
28
29#include <sys/nv.h>
30
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36
37static 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
47int
48main(void)
49{
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;
54	unsigned int i;
55	size_t nitems;
56
57	printf("1..32\n");
58
59	nvl = nvlist_create(0);
60
61	for (i = 0; i < 16; i++)
62		nvlist_append_bool_array(nvl, "nvl/bool", i % 2 == 0);
63
64	CHECK(nvlist_error(nvl) == 0);
65	CHECK(!nvlist_empty(nvl));
66	CHECK(nvlist_exists_bool_array(nvl, "nvl/bool"));
67
68	bool_result = nvlist_get_bool_array(nvl, "nvl/bool", &nitems);
69	CHECK(nitems == 16);
70	CHECK(bool_result != NULL);
71	for (i = 0; i < nitems; i++)
72		CHECK(bool_result[i] == (i % 2 == 0));
73
74
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);
78	CHECK(nitems == 2);
79	CHECK(strcmp(string_result[0], "a") == 0);
80	CHECK(strcmp(string_result[1], "abc") == 0);
81
82
83	nvl1 = nvlist_create(0);
84	nvlist_add_string(nvl1, "key1", "test1");
85	nvlist_append_nvlist_array(nvl, "nvl/nvl", nvl1);
86	nvlist_destroy(nvl1);
87
88	nvl2 = nvlist_create(0);
89	nvlist_add_string(nvl2, "key2", "test2");
90	nvlist_append_nvlist_array(nvl, "nvl/nvl", nvl2);
91	nvlist_destroy(nvl2);
92
93	nvl_result = nvlist_get_nvlist_array(nvl, "nvl/nvl", &nitems);
94	CHECK(nitems == 2);
95	CHECK(strcmp(nvlist_get_string(nvl_result[0], "key1"), "test1") == 0);
96	CHECK(strcmp(nvlist_get_string(nvl_result[1], "key2"), "test2") == 0);
97
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);
102
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);
107
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);
113
114	nvlist_move_nvlist_array(nvl, "nvl/nvl_new_array", items, nitems);
115	CHECK(nvlist_error(nvl) == 0);
116
117	nvlist_destroy(nvl);
118
119	return (0);
120}
121