2 #include "gvdb-reader.h"
5 test_reader_open_error (void)
10 table
= gvdb_table_new (SRCDIR
"/gvdbs/does_not_exist", TRUE
, &error
);
11 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_NOENT
);
12 g_assert (table
== NULL
);
13 g_clear_error (&error
);
15 table
= gvdb_table_new (SRCDIR
"/gvdbs/file_empty", TRUE
, &error
);
16 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_INVAL
);
17 g_assert (table
== NULL
);
18 g_clear_error (&error
);
20 table
= gvdb_table_new (SRCDIR
"/gvdbs/invalid_header", TRUE
, &error
);
21 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_INVAL
);
22 g_assert (table
== NULL
);
23 g_clear_error (&error
);
25 table
= gvdb_table_new (SRCDIR
"/gvdbs/file_too_small", TRUE
, &error
);
26 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_INVAL
);
27 g_assert (table
== NULL
);
28 g_clear_error (&error
);
32 test_reader_empty (void)
34 const gchar
* strings
[] = { "", "value", "/value", ".", NULL
};
39 table
= gvdb_table_new (SRCDIR
"/gvdbs/empty_gvdb", TRUE
, &error
);
40 g_assert_no_error (error
);
41 g_assert (table
!= NULL
);
43 g_assert (gvdb_table_is_valid (table
));
45 for (i
= 0; strings
[i
]; i
++)
47 const gchar
*key
= strings
[i
];
53 sub
= gvdb_table_get_table (table
, key
);
54 g_assert (sub
== NULL
);
56 has
= gvdb_table_has_value (table
, key
);
59 val
= gvdb_table_get_value (table
, key
);
60 g_assert (val
== NULL
);
62 val
= gvdb_table_get_raw_value (table
, key
);
63 g_assert (val
== NULL
);
65 list
= gvdb_table_list (table
, key
);
66 g_assert (list
== NULL
);
69 gvdb_table_unref (table
);
73 verify_table (GvdbTable
*table
)
79 list
= gvdb_table_list (table
, "/");
80 g_assert (list
!= NULL
);
81 g_assert_cmpint (g_strv_length (list
), ==, 1);
82 g_assert_cmpstr (list
[0], ==, "values/");
85 list
= gvdb_table_list (table
, "/values/");
86 g_assert (list
!= NULL
);
87 g_assert_cmpint (g_strv_length (list
), ==, 3);
88 g_assert_cmpstr (list
[0], ==, "boolean");
89 g_assert_cmpstr (list
[1], ==, "int32");
90 g_assert_cmpstr (list
[2], ==, "string");
93 /* A directory is not a value */
94 has
= gvdb_table_has_value (table
, "/");
96 has
= gvdb_table_has_value (table
, "/values/");
99 has
= gvdb_table_has_value (table
, "/int32");
101 has
= gvdb_table_has_value (table
, "values/int32");
103 has
= gvdb_table_has_value (table
, "/values/int32");
106 value
= gvdb_table_get_value (table
, "/");
107 g_assert (value
== NULL
);
108 value
= gvdb_table_get_value (table
, "/values/");
109 g_assert (value
== NULL
);
110 value
= gvdb_table_get_value (table
, "/int32");
111 g_assert (value
== NULL
);
112 value
= gvdb_table_get_value (table
, "values/int32");
113 g_assert (value
== NULL
);
115 value
= gvdb_table_get_value (table
, "/values/boolean");
116 g_assert (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_BOOLEAN
));
117 g_assert (g_variant_get_boolean (value
));
118 g_variant_unref (value
);
120 value
= gvdb_table_get_raw_value (table
, "/values/boolean");
121 g_assert (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_BOOLEAN
));
122 g_assert (g_variant_get_boolean (value
));
123 g_variant_unref (value
);
125 value
= gvdb_table_get_value (table
, "/values/int32");
126 g_assert (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_INT32
));
127 g_assert_cmpint (g_variant_get_int32 (value
), ==, 0x44332211);
128 g_variant_unref (value
);
130 value
= gvdb_table_get_value (table
, "/values/string");
131 g_assert (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_STRING
));
132 g_assert_cmpstr (g_variant_get_string (value
, NULL
), ==, "a string");
133 g_variant_unref (value
);
135 value
= gvdb_table_get_raw_value (table
, "/values/string");
136 g_assert (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_STRING
));
137 g_assert_cmpstr (g_variant_get_string (value
, NULL
), ==, "a string");
138 g_variant_unref (value
);
142 test_reader_values (void)
144 GError
*error
= NULL
;
147 table
= gvdb_table_new (SRCDIR
"/gvdbs/example_gvdb", TRUE
, &error
);
148 g_assert_no_error (error
);
149 verify_table (table
);
151 #if G_BYTE_ORDER == G_BIG_ENDIAN
155 value
= gvdb_table_get_raw_value (table
, "/values/int32");
156 g_assert (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_INT32
));
157 g_assert_cmpint (g_variant_get_int32 (value
), ==, 0x11223344);
158 g_variant_unref (value
);
162 gvdb_table_unref (table
);
166 test_reader_values_bigendian (void)
168 GError
*error
= NULL
;
171 table
= gvdb_table_new (SRCDIR
"/gvdbs/example_gvdb.big-endian", TRUE
, &error
);
172 g_assert_no_error (error
);
173 verify_table (table
);
175 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
179 value
= gvdb_table_get_raw_value (table
, "/values/int32");
180 g_assert (value
!= NULL
&& g_variant_is_of_type (value
, G_VARIANT_TYPE_INT32
));
181 g_assert_cmpint (g_variant_get_int32 (value
), ==, 0x11223344);
182 g_variant_unref (value
);
186 gvdb_table_unref (table
);
189 static gint accept_this_many_opens
;
192 walk_open (const gchar
*name
,
196 GString
*log
= user_data
;
198 g_string_append (log
, "'");
199 g_string_append_len (log
, name
, name_len
);
200 g_string_append_printf (log
, "\'(%zd): {", name_len
);
202 if (accept_this_many_opens
)
204 accept_this_many_opens
--;
208 g_string_append (log
, "rejected}");
214 walk_value (const gchar
*name
,
219 GString
*log
= user_data
;
222 printed
= g_variant_print (value
, FALSE
);
224 g_string_append (log
, "'");
225 g_string_append_len (log
, name
, name_len
);
226 g_string_append_printf (log
, "\'(%zd): %s", name_len
, printed
);
231 walk_close (gsize name_len
,
234 GString
*log
= user_data
;
236 g_string_append_printf (log
, "(%zd)}", name_len
);
240 verify_walk (GvdbTable
*table
)
244 log
= g_string_new (NULL
);
245 accept_this_many_opens
= 2;
246 gvdb_table_walk (table
, "/", walk_open
, walk_value
, walk_close
, log
);
247 g_assert_cmpstr (log
->str
, ==,
251 "'int32'(5): 1144201745"
252 "'string'(6): 'a string'"
255 g_string_truncate (log
, 0);
257 accept_this_many_opens
= 1;
258 gvdb_table_walk (table
, "/", walk_open
, walk_value
, walk_close
, log
);
259 g_assert_cmpstr (log
->str
, ==,
261 "'values/'(7): {rejected}"
263 g_string_truncate (log
, 0);
265 accept_this_many_opens
= 0;
266 gvdb_table_walk (table
, "/", walk_open
, walk_value
, walk_close
, log
);
267 g_assert_cmpstr (log
->str
, ==, "'/'(1): {rejected}");
268 g_string_free (log
, TRUE
);
272 test_reader_walk (void)
274 GError
*error
= NULL
;
277 table
= gvdb_table_new (SRCDIR
"/gvdbs/example_gvdb", TRUE
, &error
);
278 g_assert_no_error (error
);
282 gvdb_table_unref (table
);
286 test_reader_walk_bigendian (void)
288 GError
*error
= NULL
;
291 table
= gvdb_table_new (SRCDIR
"/gvdbs/example_gvdb.big-endian", TRUE
, &error
);
292 g_assert_no_error (error
);
296 gvdb_table_unref (table
);
300 main (int argc
, char **argv
)
302 g_test_init (&argc
, &argv
, NULL
);
304 g_test_add_func ("/gvdb/reader/open-error", test_reader_open_error
);
305 g_test_add_func ("/gvdb/reader/empty", test_reader_empty
);
306 g_test_add_func ("/gvdb/reader/values", test_reader_values
);
307 g_test_add_func ("/gvdb/reader/values/big-endian", test_reader_values_bigendian
);
308 g_test_add_func ("/gvdb/reader/walk", test_reader_walk
);
309 g_test_add_func ("/gvdb/reader/walk/big-endian", test_reader_walk_bigendian
);
311 return g_test_run ();