Branch data Line data Source code
1 : : /*
2 : : * Copyright © 2020 Canonical Ltd.
3 : : * Copyright © 2021 Alexandros Theodotou
4 : : *
5 : : * SPDX-License-Identifier: LicenseRef-old-glib-tests
6 : : *
7 : : * This work is provided "as is"; redistribution and modification
8 : : * in whole or in part, in any medium, physical or electronic is
9 : : * permitted without restriction.
10 : : *
11 : : * This work is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 : : *
15 : : * In no event shall the authors or contributors be liable for any
16 : : * direct, indirect, incidental, special, exemplary, or consequential
17 : : * damages (including, but not limited to, procurement of substitute
18 : : * goods or services; loss of use, data, or profits; or business
19 : : * interruption) however caused and on any theory of liability, whether
20 : : * in contract, strict liability, or tort (including negligence or
21 : : * otherwise) arising in any way out of the use of this software, even
22 : : * if advised of the possibility of such damage.
23 : : */
24 : :
25 : : #include "glib.h"
26 : :
27 : : static void
28 : 1 : test_strvbuilder_empty (void)
29 : : {
30 : : GStrvBuilder *builder;
31 : : GStrv result;
32 : :
33 : 1 : builder = g_strv_builder_new ();
34 : 1 : result = g_strv_builder_end (builder);
35 : 1 : g_assert_nonnull (result);
36 : 1 : g_assert_cmpint (g_strv_length (result), ==, 0);
37 : :
38 : 1 : g_strfreev (result);
39 : 1 : g_strv_builder_unref (builder);
40 : 1 : }
41 : :
42 : : static void
43 : 1 : test_strvbuilder_add (void)
44 : : {
45 : : GStrvBuilder *builder;
46 : : GStrv result;
47 : 1 : const gchar *expected[] = { "one", "two", "three", NULL };
48 : :
49 : 1 : builder = g_strv_builder_new ();
50 : 1 : g_strv_builder_add (builder, "one");
51 : 1 : g_strv_builder_add (builder, "two");
52 : 1 : g_strv_builder_add (builder, "three");
53 : 1 : result = g_strv_builder_end (builder);
54 : 1 : g_assert_nonnull (result);
55 : 1 : g_assert_true (g_strv_equal ((const gchar *const *) result, expected));
56 : :
57 : 1 : g_strfreev (result);
58 : 1 : g_strv_builder_unref (builder);
59 : 1 : }
60 : :
61 : : static void
62 : 1 : test_strvbuilder_addv (void)
63 : : {
64 : : GStrvBuilder *builder;
65 : : GStrv result;
66 : 1 : const gchar *expected[] = { "one", "two", "three", NULL };
67 : :
68 : 1 : builder = g_strv_builder_new ();
69 : 1 : g_strv_builder_addv (builder, expected);
70 : 1 : result = g_strv_builder_end (builder);
71 : 1 : g_assert_nonnull (result);
72 : 4 : g_assert_cmpstrv ((const gchar *const *) result, expected);
73 : :
74 : 1 : g_strfreev (result);
75 : 1 : g_strv_builder_unref (builder);
76 : 1 : }
77 : :
78 : : static void
79 : 1 : test_strvbuilder_add_many (void)
80 : : {
81 : : GStrvBuilder *builder;
82 : : GStrv result;
83 : 1 : const gchar *expected[] = { "one", "two", "three", NULL };
84 : :
85 : 1 : builder = g_strv_builder_new ();
86 : 1 : g_strv_builder_add_many (builder, "one", "two", "three", NULL);
87 : 1 : result = g_strv_builder_end (builder);
88 : 1 : g_assert_nonnull (result);
89 : 4 : g_assert_cmpstrv ((const gchar *const *) result, expected);
90 : :
91 : 1 : g_strfreev (result);
92 : 1 : g_strv_builder_unref (builder);
93 : 1 : }
94 : :
95 : : static void
96 : 1 : test_strvbuilder_take (void)
97 : : {
98 : : GStrvBuilder *builder;
99 : : GStrv result;
100 : 1 : const gchar *expected[] = { "one", "two", "three", NULL };
101 : :
102 : 1 : builder = g_strv_builder_new ();
103 : 1 : g_strv_builder_take (builder, g_strdup ("one"));
104 : 1 : g_strv_builder_add (builder, "two");
105 : 1 : g_strv_builder_take (builder, g_strdup ("three"));
106 : 1 : result = g_strv_builder_end (builder);
107 : 1 : g_assert_nonnull (result);
108 : 1 : g_assert_true (g_strv_equal ((const gchar *const *) result, expected));
109 : :
110 : 1 : g_strfreev (result);
111 : 1 : g_strv_builder_unref (builder);
112 : 1 : }
113 : :
114 : : static void
115 : 1 : test_strvbuilder_ref (void)
116 : : {
117 : : GStrvBuilder *builder;
118 : :
119 : 1 : builder = g_strv_builder_new ();
120 : 1 : g_strv_builder_ref (builder);
121 : 1 : g_strv_builder_unref (builder);
122 : 1 : g_strv_builder_unref (builder);
123 : 1 : }
124 : :
125 : : static void
126 : 1 : test_strvbuilder_unref_to_strv (void)
127 : : {
128 : 1 : GStrvBuilder *builder = g_strv_builder_new ();
129 : : GStrv result;
130 : :
131 : 1 : g_strv_builder_add_many (builder, "hello", "world", NULL);
132 : 1 : result = g_strv_builder_unref_to_strv (builder);
133 : :
134 : 1 : g_assert_true (g_strv_equal ((const char * const *) result,
135 : : (const char *[]) {
136 : : "hello",
137 : : "world",
138 : : NULL,
139 : : }));
140 : :
141 : 1 : g_strfreev (result);
142 : :
143 : 1 : builder = g_strv_builder_new ();
144 : 1 : result = g_strv_builder_unref_to_strv (builder);
145 : 1 : g_assert_null (result[0]);
146 : 1 : g_strfreev (result);
147 : 1 : }
148 : :
149 : : int
150 : 1 : main (int argc,
151 : : char *argv[])
152 : : {
153 : 1 : g_test_init (&argc, &argv, NULL);
154 : :
155 : 1 : g_test_add_func ("/strvbuilder/empty", test_strvbuilder_empty);
156 : 1 : g_test_add_func ("/strvbuilder/add", test_strvbuilder_add);
157 : 1 : g_test_add_func ("/strvbuilder/addv", test_strvbuilder_addv);
158 : 1 : g_test_add_func ("/strvbuilder/add_many", test_strvbuilder_add_many);
159 : 1 : g_test_add_func ("/strvbuilder/take", test_strvbuilder_take);
160 : 1 : g_test_add_func ("/strvbuilder/ref", test_strvbuilder_ref);
161 : 1 : g_test_add_func ("/strvbuilder/unref_to_strv", test_strvbuilder_unref_to_strv);
162 : :
163 : 1 : return g_test_run ();
164 : : }
|