1 /*
2  * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19 
20 #include <time.h>
21 #include <string.h>
22 
23 #include <glib-object.h>
24 
25 #include <libtracker-miners-common/tracker-type-utils.h>
26 
27 static void
test_string_in_string_list_failures_subprocess(void)28 test_string_in_string_list_failures_subprocess (void)
29 {
30 	const gchar *complete = "This is an extract of text with different terms an props like Audio:Title ...";
31 	gchar **pieces;
32 
33 	pieces = g_strsplit (complete, " ", -1);
34 
35 	g_assert_cmpint (tracker_string_in_string_list (NULL, pieces), ==, -1);
36 
37 	g_strfreev (pieces);
38 }
39 
40 static void
test_string_in_string_list_failures(void)41 test_string_in_string_list_failures (void)
42 {
43 
44 	g_test_trap_subprocess ("/libtracker-common/tracker-type-utils/string_in_string_list_failures/subprocess", 0, 0);
45 	g_test_trap_assert_failed ();
46 	g_test_trap_assert_stderr ("*assertion 'str != NULL' failed*");
47 }
48 
49 static void
test_string_in_string_list(void)50 test_string_in_string_list (void)
51 {
52 	const gchar *complete = "This is an extract of text with different terms an props like Audio:Title ...";
53 	gchar **pieces;
54 
55 	pieces = g_strsplit (complete, " ", -1);
56 
57 	g_assert_cmpint (tracker_string_in_string_list ("is", pieces), ==, 1);
58 	g_assert_cmpint (tracker_string_in_string_list ("Audio:Title", pieces), ==, 12);
59 
60 	g_assert_cmpint (tracker_string_in_string_list ("terms", NULL), ==, -1);
61     g_strfreev (pieces);
62 }
63 
64 static void
test_string_in_gslist(void)65 test_string_in_gslist (void)
66 {
67         GSList *input = NULL;
68 
69         input = g_slist_prepend (input, g_strdup ("one"));
70         input = g_slist_prepend (input, g_strdup ("two"));
71         input = g_slist_prepend (input, g_strdup ("three"));
72         input = g_slist_prepend (input, g_strdup ("four"));
73 
74         g_assert (tracker_string_in_gslist ("one", input));
75         g_assert (tracker_string_in_gslist ("two", input));
76         g_assert (tracker_string_in_gslist ("three", input));
77         g_assert (tracker_string_in_gslist ("four", input));
78         g_assert (!tracker_string_in_gslist ("five", input));
79 
80         g_slist_foreach (input, (GFunc)g_free, NULL);
81         g_slist_free (input);
82 }
83 
84 static void
test_gslist_to_string_list(void)85 test_gslist_to_string_list (void)
86 {
87 	GSList *input = NULL;
88 	gchar **result;
89 
90 	input = g_slist_prepend (input, (gpointer) "four");
91 	input = g_slist_prepend (input, (gpointer) "three");
92 	input = g_slist_prepend (input, (gpointer) "two");
93 	input = g_slist_prepend (input, (gpointer) "one");
94 
95 	result = tracker_gslist_to_string_list (input);
96 
97 	g_assert_cmpstr (result[0], ==, "one");
98 	g_assert_cmpstr (result[1], ==, "two");
99         g_assert_cmpstr (result[2], ==, "three");
100 	g_assert_cmpstr (result[3], ==, "four");
101 
102 	g_strfreev (result);
103 
104 	result = tracker_gslist_to_string_list (NULL);
105 	g_assert (result != NULL);
106 	g_strfreev (result);
107 }
108 
109 static void
test_string_list_to_gslist(void)110 test_string_list_to_gslist (void)
111 {
112         const gchar  *input [] = {"one", "two", "three", "four", NULL};
113         GSList *result = NULL;
114 
115         result = tracker_string_list_to_gslist ((gchar **)input, -1);
116         g_assert (result);
117         g_assert_cmpint (g_slist_length (result), ==, 4);
118 
119         /* This function is tested in other test, so it should work or fail there also */
120         g_assert (tracker_string_in_gslist ("one", result));
121         g_assert (tracker_string_in_gslist ("two", result));
122         g_assert (tracker_string_in_gslist ("three", result));
123         g_assert (tracker_string_in_gslist ("four", result));
124 
125         g_slist_foreach (result, (GFunc)g_free, NULL);
126         g_slist_free (result);
127 
128         result = tracker_string_list_to_gslist ((gchar **)input, 2);
129         g_assert (result);
130         g_assert_cmpint (g_slist_length (result), ==, 2);
131 
132         g_assert (tracker_string_in_gslist ("one", result));
133         g_assert (tracker_string_in_gslist ("two", result));
134         g_assert (!tracker_string_in_gslist ("three", result));
135         g_assert (!tracker_string_in_gslist ("four", result));
136 }
137 
138 static void
test_string_to_string_list(void)139 test_string_to_string_list (void)
140 {
141         const gchar *input = "first line";
142         gchar **result;
143 
144         result = tracker_string_to_string_list (input);
145         g_assert_cmpint (g_strv_length (result), ==, 1);
146         g_assert_cmpstr (result [0], ==, "first line");
147         g_strfreev (result);
148 }
149 
150 static void
test_gslist_with_string_data_equal(void)151 test_gslist_with_string_data_equal (void)
152 {
153         GSList *list1 = NULL;
154         GSList *list2 = NULL;
155         GSList *shorty = NULL;
156         GSList *list3 = NULL;
157 
158         list1 = g_slist_prepend (list1, g_strdup ("one"));
159         list1 = g_slist_prepend (list1, g_strdup ("two"));
160         list1 = g_slist_prepend (list1, g_strdup ("three"));
161 
162         g_assert (tracker_gslist_with_string_data_equal (list1, list1));
163 
164         shorty = g_slist_prepend (shorty, g_strdup ("one"));
165         g_assert (!tracker_gslist_with_string_data_equal (list1, shorty));
166         g_assert (!tracker_gslist_with_string_data_equal (shorty, list1));
167 
168         list2 = g_slist_prepend (list2, g_strdup ("one"));
169         list2 = g_slist_prepend (list2, g_strdup ("two"));
170         list2 = g_slist_prepend (list2, g_strdup ("three"));
171         g_assert (tracker_gslist_with_string_data_equal (list1, list2));
172         g_assert (tracker_gslist_with_string_data_equal (list2, list1));
173 
174         list3 = g_slist_prepend (list3, g_strdup ("one"));
175         list3 = g_slist_prepend (list3, g_strdup ("something different"));
176         list3 = g_slist_prepend (list3, g_strdup ("three"));
177         g_assert (!tracker_gslist_with_string_data_equal (list1, list3));
178         g_assert (!tracker_gslist_with_string_data_equal (list3, list1));
179 
180         g_slist_foreach (list1, (GFunc)g_free, NULL);
181         g_slist_foreach (list2, (GFunc)g_free, NULL);
182         g_slist_foreach (shorty, (GFunc)g_free, NULL);
183         g_slist_foreach (list3, (GFunc)g_free, NULL);
184 
185         g_slist_free (list1);
186         g_slist_free (list2);
187         g_slist_free (shorty);
188         g_slist_free (list3);
189 }
190 
191 int
main(int argc,char ** argv)192 main (int argc, char **argv)
193 {
194 	gint result;
195 
196 	g_test_init (&argc, &argv, NULL);
197 
198 	g_test_add_func ("/libtracker-common/tracker-type-utils/string_list_as_list",
199 	                 test_string_to_string_list);
200 	g_test_add_func ("/libtracker-common/tracker-type-utils/gslist_to_string_list",
201 	                 test_gslist_to_string_list);
202 	g_test_add_func ("/libtracker-common/tracker-type-utils/string_in_string_list",
203 	                 test_string_in_string_list);
204 	g_test_add_func ("/libtracker-common/tracker-type-utils/string_in_string_list_failures",
205 	                 test_string_in_string_list_failures);
206 	g_test_add_func ("/libtracker-common/tracker-type-utils/string_in_string_list_failures/subprocess",
207 	                 test_string_in_string_list_failures_subprocess);
208         g_test_add_func ("/libtracker-common/tracker-type-utils/string_in_gslist",
209                          test_string_in_gslist);
210         g_test_add_func ("/libtracker-common/tracker-type-utils/string_list_to_gslist",
211                          test_string_list_to_gslist);
212 	g_test_add_func ("/libtracker-common/tracker-type-utils/gslist_with_string_data_equal",
213 	                 test_gslist_with_string_data_equal);
214 	result = g_test_run ();
215 
216 	return result;
217 }
218