1272343Sngie/* $NetBSD: t_basic.c,v 1.4 2011/04/20 20:02:58 martin Exp $ */
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 2008 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie/*
30272343Sngie * Written by Jason Thorpe 5/26/2006.
31272343Sngie * Public domain.
32272343Sngie */
33272343Sngie
34272343Sngie#include <sys/cdefs.h>
35272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008\
36272343Sngie The NetBSD Foundation, inc. All rights reserved.");
37272343Sngie__RCSID("$NetBSD: t_basic.c,v 1.4 2011/04/20 20:02:58 martin Exp $");
38272343Sngie
39272343Sngie#include <stdlib.h>
40272343Sngie#include <string.h>
41272343Sngie#include <prop/proplib.h>
42272343Sngie
43272343Sngie#include <atf-c.h>
44272343Sngie
45272343Sngiestatic const char compare1[] =
46272343Sngie"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
47272343Sngie"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
48272343Sngie"<plist version=\"1.0\">\n"
49272343Sngie"<dict>\n"
50272343Sngie"	<key>false-val</key>\n"
51272343Sngie"	<false/>\n"
52272343Sngie"	<key>one</key>\n"
53272343Sngie"	<integer>1</integer>\n"
54272343Sngie"	<key>three</key>\n"
55272343Sngie"	<array>\n"
56272343Sngie"		<dict>\n"
57272343Sngie"			<key>one</key>\n"
58272343Sngie"			<integer>1</integer>\n"
59272343Sngie"			<key>two</key>\n"
60272343Sngie"			<string>number-two</string>\n"
61272343Sngie"		</dict>\n"
62272343Sngie"		<dict>\n"
63272343Sngie"			<key>one</key>\n"
64272343Sngie"			<integer>1</integer>\n"
65272343Sngie"			<key>two</key>\n"
66272343Sngie"			<string>number-two</string>\n"
67272343Sngie"		</dict>\n"
68272343Sngie"		<dict>\n"
69272343Sngie"			<key>one</key>\n"
70272343Sngie"			<integer>1</integer>\n"
71272343Sngie"			<key>two</key>\n"
72272343Sngie"			<string>number-two</string>\n"
73272343Sngie"		</dict>\n"
74272343Sngie"	</array>\n"
75272343Sngie"	<key>true-val</key>\n"
76272343Sngie"	<true/>\n"
77272343Sngie"	<key>two</key>\n"
78272343Sngie"	<string>number-two</string>\n"
79272343Sngie"</dict>\n"
80272343Sngie"</plist>\n";
81272343Sngie
82272343SngieATF_TC(prop_basic);
83272343SngieATF_TC_HEAD(prop_basic, tc)
84272343Sngie{
85272343Sngie	atf_tc_set_md_var(tc, "descr", "A basic test of proplib(3)");
86272343Sngie}
87272343Sngie
88272343SngieATF_TC_BODY(prop_basic, tc)
89272343Sngie{
90272343Sngie	prop_dictionary_t dict;
91272343Sngie	char *ext1;
92272343Sngie
93272343Sngie	dict = prop_dictionary_create();
94272343Sngie	ATF_REQUIRE(dict != NULL);
95272343Sngie
96272343Sngie	{
97272343Sngie		prop_number_t num = prop_number_create_integer(1);
98272343Sngie		ATF_REQUIRE(num != NULL);
99272343Sngie
100272343Sngie		ATF_REQUIRE_EQ(prop_dictionary_set(dict, "one", num), true);
101272343Sngie		prop_object_release(num);
102272343Sngie	}
103272343Sngie
104272343Sngie	{
105272343Sngie		prop_string_t str = prop_string_create_cstring("number-two");
106272343Sngie		ATF_REQUIRE(str != NULL);
107272343Sngie
108272343Sngie		ATF_REQUIRE_EQ(prop_dictionary_set(dict, "two", str), true);
109272343Sngie		prop_object_release(str);
110272343Sngie	}
111272343Sngie
112272343Sngie	{
113272343Sngie		prop_array_t arr;
114272343Sngie		prop_dictionary_t dict_copy;
115272343Sngie		int i;
116272343Sngie
117272343Sngie		arr = prop_array_create();
118272343Sngie		ATF_REQUIRE(arr != NULL);
119272343Sngie
120272343Sngie		for (i = 0; i < 3; ++i) {
121272343Sngie			dict_copy = prop_dictionary_copy(dict);
122272343Sngie			ATF_REQUIRE(dict_copy != NULL);
123272343Sngie			ATF_REQUIRE_EQ(prop_array_add(arr, dict_copy), true);
124272343Sngie			prop_object_release(dict_copy);
125272343Sngie		}
126272343Sngie
127272343Sngie		ATF_REQUIRE_EQ(prop_dictionary_set(dict, "three", arr), true);
128272343Sngie		prop_object_release(arr);
129272343Sngie	}
130272343Sngie
131272343Sngie	{
132272343Sngie		prop_bool_t val = prop_bool_create(true);
133272343Sngie		ATF_REQUIRE(val != NULL);
134272343Sngie		ATF_REQUIRE_EQ(prop_dictionary_set(dict, "true-val", val), true);
135272343Sngie		prop_object_release(val);
136272343Sngie
137272343Sngie		val = prop_bool_create(false);
138272343Sngie		ATF_REQUIRE(val != NULL);
139272343Sngie		ATF_REQUIRE_EQ(prop_dictionary_set(dict, "false-val", val), true);
140272343Sngie		prop_object_release(val);
141272343Sngie	}
142272343Sngie
143272343Sngie	ext1 = prop_dictionary_externalize(dict);
144272343Sngie	ATF_REQUIRE(ext1 != NULL);
145272343Sngie	ATF_REQUIRE_STREQ(compare1, ext1);
146272343Sngie
147272343Sngie	{
148272343Sngie		prop_dictionary_t dict2;
149272343Sngie		char *ext2;
150272343Sngie
151272343Sngie		dict2 = prop_dictionary_internalize(ext1);
152272343Sngie		ATF_REQUIRE(dict2 != NULL);
153272343Sngie		ext2 = prop_dictionary_externalize(dict2);
154272343Sngie		ATF_REQUIRE(ext2 != NULL);
155272343Sngie		ATF_REQUIRE_STREQ(ext1, ext2);
156272343Sngie		prop_object_release(dict2);
157272343Sngie		free(ext2);
158272343Sngie	}
159272343Sngie
160272343Sngie	prop_object_release(dict);
161272343Sngie	free(ext1);
162272343Sngie}
163272343Sngie
164272343SngieATF_TC(prop_dictionary_equals);
165272343SngieATF_TC_HEAD(prop_dictionary_equals, tc)
166272343Sngie{
167272343Sngie	atf_tc_set_md_var(tc, "descr", "Test prop_dictionary_equals(3)");
168272343Sngie}
169272343Sngie
170272343SngieATF_TC_BODY(prop_dictionary_equals, tc)
171272343Sngie{
172272343Sngie	prop_dictionary_t c, d;
173272343Sngie
174272343Sngie	/*
175272343Sngie	 * Fixed, should not fail any more...
176272343Sngie	 *
177272343Sngie	atf_tc_expect_death("PR lib/43964");
178272343Sngie	 *
179272343Sngie	 */
180272343Sngie
181272343Sngie	d = prop_dictionary_internalize(compare1);
182272343Sngie
183272343Sngie	ATF_REQUIRE(d != NULL);
184272343Sngie
185272343Sngie	c = prop_dictionary_copy(d);
186272343Sngie
187272343Sngie	ATF_REQUIRE(c != NULL);
188272343Sngie
189272343Sngie	if (prop_dictionary_equals(c, d) != true)
190272343Sngie		atf_tc_fail("dictionaries are not equal");
191272343Sngie
192272343Sngie	prop_object_release(c);
193272343Sngie	prop_object_release(d);
194272343Sngie}
195272343Sngie
196272343SngieATF_TP_ADD_TCS(tp)
197272343Sngie{
198272343Sngie
199272343Sngie	ATF_TP_ADD_TC(tp, prop_basic);
200272343Sngie	ATF_TP_ADD_TC(tp, prop_dictionary_equals);
201272343Sngie
202272343Sngie	return atf_no_error();
203272343Sngie}
204