tracker-0.16.2/tests/libtracker-common/tracker-type-utils-test.c

Location Tool Test ID Function Issue
tracker-type-utils-test.c:117:3 clang-analyzer Value stored to 'rc' is never read
tracker-type-utils-test.c:122:2 clang-analyzer Value stored to 'rc' is never read
  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-common/tracker-type-utils.h>
 26 
 27 static void
 28 test_long_to_string (void)
 29 {
 30 	glong n;
 31 	gchar *result;
 32 
 33 	n = 10050;
 34 	result = tracker_glong_to_string (n);
 35 	g_assert_cmpstr (result, ==, "10050");
 36 	g_free (result);
 37 
 38 	n = -9950;
 39 	result = tracker_glong_to_string (n);
 40 	g_assert_cmpstr (result, ==, "-9950");
 41 	g_free (result);
 42 }
 43 
 44 static void
 45 test_int_to_string (void)
 46 {
 47 	gint n;
 48 	gchar *result;
 49 
 50 	n = 654;
 51 	result = tracker_gint_to_string (n);
 52 	g_assert_cmpstr (result, ==, "654");
 53 	g_free (result);
 54 
 55 	n = -963;
 56 	result = tracker_gint_to_string (n);
 57 	g_assert_cmpstr (result, ==, "-963");
 58 	g_free (result);
 59 
 60 }
 61 
 62 static void
 63 test_uint_to_string (void)
 64 {
 65 	guint n;
 66 	gchar *result;
 67 
 68 	n = 100;
 69 	result = tracker_guint_to_string (n);
 70 	g_assert_cmpstr (result, ==, "100");
 71 	g_free (result);
 72 }
 73 
 74 static void
 75 test_gint32_to_string (void)
 76 {
 77 	gint32 n;
 78 	gchar *result;
 79 
 80 	n = 100;
 81 	result = tracker_gint32_to_string (n);
 82 	g_assert_cmpstr (result, ==, "100");
 83 	g_free (result);
 84 
 85 	n = -96;
 86 	result = tracker_gint32_to_string (n);
 87 	g_assert_cmpstr (result, ==, "-96");
 88 	g_free (result);
 89 
 90 }
 91 
 92 static void
 93 test_guint32_to_string (void)
 94 {
 95 	guint32 n;
 96 	gchar *result;
 97 
 98 	n = 100;
 99 	result = tracker_guint32_to_string (n);
100 	g_assert_cmpstr (result, ==, "100");
101 	g_free (result);
102 
103 }
104 
105 static void
106 test_string_to_uint (void)
107 {
108 	guint num_result, rc;
109 
110 	rc = tracker_string_to_uint ("10", &num_result);
111 
112 	g_assert (rc);
113 	g_assert_cmpint (num_result, ==, 10);
114 
115 
116 	if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
117 		rc = tracker_string_to_uint (NULL, &num_result);
Value stored to 'rc' is never read
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

118 } 119 g_test_trap_assert_failed (); 120 121 /* ???? FIXME */ 122 rc = tracker_string_to_uint ("-20", &num_result);
Value stored to 'rc' is never read
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

123 124 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { 125 tracker_string_to_uint (NULL, &num_result); 126 } 127 g_test_trap_assert_failed (); 128 129 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { 130 tracker_string_to_uint ("199", NULL); 131 } 132 g_test_trap_assert_failed (); 133 134 rc = tracker_string_to_uint ("i am not a number", &num_result); 135 g_assert (!rc); 136 g_assert_cmpint (rc, ==, 0); 137 } 138 139 static void 140 test_string_in_string_list (void) 141 { 142 const gchar *complete = "This is an extract of text with different terms an props like Audio:Title ..."; 143 gchar **pieces; 144 145 pieces = g_strsplit (complete, " ", -1); 146 147 g_assert_cmpint (tracker_string_in_string_list ("is", pieces), ==, 1); 148 g_assert_cmpint (tracker_string_in_string_list ("Audio:Title", pieces), ==, 12); 149 150 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { 151 g_assert_cmpint (tracker_string_in_string_list (NULL, pieces), ==, -1); 152 } 153 g_test_trap_assert_failed (); 154 155 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { 156 g_assert_cmpint (tracker_string_in_string_list ("terms", NULL), ==, -1); 157 } 158 g_test_trap_assert_failed (); 159 } 160 161 static void 162 test_string_in_gslist (void) 163 { 164 GSList *input = NULL; 165 166 input = g_slist_prepend (input, g_strdup ("one")); 167 input = g_slist_prepend (input, g_strdup ("two")); 168 input = g_slist_prepend (input, g_strdup ("three")); 169 input = g_slist_prepend (input, g_strdup ("four")); 170 171 g_assert (tracker_string_in_gslist ("one", input)); 172 g_assert (tracker_string_in_gslist ("two", input)); 173 g_assert (tracker_string_in_gslist ("three", input)); 174 g_assert (tracker_string_in_gslist ("four", input)); 175 g_assert (!tracker_string_in_gslist ("five", input)); 176 177 g_slist_foreach (input, (GFunc)g_free, NULL); 178 g_slist_free (input); 179 } 180 181 static void 182 test_gslist_to_string_list (void) 183 { 184 GSList *input = NULL; 185 gchar **result; 186 187 input = g_slist_prepend (input, (gpointer) "four"); 188 input = g_slist_prepend (input, (gpointer) "three"); 189 input = g_slist_prepend (input, (gpointer) "two"); 190 input = g_slist_prepend (input, (gpointer) "one"); 191 192 result = tracker_gslist_to_string_list (input); 193 194 g_assert_cmpstr (result[0], ==, "one"); 195 g_assert_cmpstr (result[1], ==, "two"); 196 g_assert_cmpstr (result[2], ==, "three"); 197 g_assert_cmpstr (result[3], ==, "four"); 198 199 g_strfreev (result); 200 201 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { 202 result = tracker_gslist_to_string_list (NULL); 203 g_strfreev (result); 204 } 205 206 g_test_trap_assert_failed (); 207 } 208 209 static void 210 test_string_list_to_gslist (void) 211 { 212 const gchar *input [] = {"one", "two", "three", "four", NULL}; 213 GSList *result = NULL; 214 215 result = tracker_string_list_to_gslist ((gchar **)input, -1); 216 g_assert (result); 217 g_assert_cmpint (g_slist_length (result), ==, 4); 218 219 /* This function is tested in other test, so it should work or fail there also */ 220 g_assert (tracker_string_in_gslist ("one", result)); 221 g_assert (tracker_string_in_gslist ("two", result)); 222 g_assert (tracker_string_in_gslist ("three", result)); 223 g_assert (tracker_string_in_gslist ("four", result)); 224 225 g_slist_foreach (result, (GFunc)g_free, NULL); 226 g_slist_free (result); 227 228 result = tracker_string_list_to_gslist ((gchar **)input, 2); 229 g_assert (result); 230 g_assert_cmpint (g_slist_length (result), ==, 2); 231 232 g_assert (tracker_string_in_gslist ("one", result)); 233 g_assert (tracker_string_in_gslist ("two", result)); 234 g_assert (!tracker_string_in_gslist ("three", result)); 235 g_assert (!tracker_string_in_gslist ("four", result)); 236 237 } 238 239 static void 240 test_string_list_to_string (void) 241 { 242 const gchar *input = "one two three four"; 243 gchar **pieces; 244 gchar *result; 245 246 pieces = g_strsplit (input, " ", 4); 247 248 result = tracker_string_list_to_string (pieces, 4, ' '); 249 g_assert_cmpstr (input, ==, result); 250 g_free (result); 251 252 result = tracker_string_list_to_string (pieces, 3, '_'); 253 g_assert_cmpstr ("one_two_three", ==, result); 254 g_free (result); 255 256 257 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { 258 result = tracker_string_list_to_string (NULL, 6, 'x'); 259 g_free (result); 260 } 261 g_test_trap_assert_failed (); 262 263 result = tracker_string_list_to_string (pieces, -1, ' '); 264 g_assert_cmpstr (input, ==, result); 265 g_free (result); 266 267 result = tracker_string_list_to_string (pieces, 6, ' '); 268 g_assert_cmpstr (input, ==, result); 269 g_free (result); 270 271 g_strfreev (pieces); 272 } 273 274 static void 275 test_string_to_string_list (void) 276 { 277 const gchar *input = "first line"; 278 gchar **result; 279 280 result = tracker_string_to_string_list (input); 281 g_assert_cmpint (g_strv_length (result), ==, 1); 282 g_assert_cmpstr (result [0], ==, "first line"); 283 } 284 285 static void 286 test_boolean_as_text_to_number (void) 287 { 288 gchar *result; 289 290 /* Correct true values */ 291 result = tracker_string_boolean_to_string_gint ("True"); 292 g_assert_cmpstr (result, ==, "1"); 293 g_free (result); 294 295 296 result = tracker_string_boolean_to_string_gint ("TRUE"); 297 g_assert_cmpstr (result, ==, "1"); 298 g_free (result); 299 300 result = tracker_string_boolean_to_string_gint ("true"); 301 g_assert_cmpstr (result, ==, "1"); 302 g_free (result); 303 304 /* Correct false values */ 305 result = tracker_string_boolean_to_string_gint ("False"); 306 g_assert_cmpstr (result, ==, "0"); 307 g_free (result); 308 309 result = tracker_string_boolean_to_string_gint ("FALSE"); 310 g_assert_cmpstr (result, ==, "0"); 311 g_free (result); 312 313 result = tracker_string_boolean_to_string_gint ("false"); 314 g_assert_cmpstr (result, ==, "0"); 315 g_free (result); 316 317 /* Invalid values */ 318 result = tracker_string_boolean_to_string_gint ("Thrue"); 319 g_assert_cmpstr (result, ==, "Thrue"); 320 g_free (result); 321 322 result = tracker_string_boolean_to_string_gint ("Falsez"); 323 g_assert_cmpstr (result, ==, "Falsez"); 324 g_free (result); 325 326 result = tracker_string_boolean_to_string_gint ("Other invalid value"); 327 g_assert_cmpstr (result, ==, "Other invalid value"); 328 g_free (result); 329 330 331 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { 332 result = tracker_string_boolean_to_string_gint (NULL); 333 g_free (result); 334 } 335 g_test_trap_assert_failed (); 336 } 337 338 static void 339 test_gslist_with_string_data_equal (void) 340 { 341 GSList *list1 = NULL; 342 GSList *list2 = NULL; 343 GSList *shorty = NULL; 344 GSList *list3 = NULL; 345 346 list1 = g_slist_prepend (list1, g_strdup ("one")); 347 list1 = g_slist_prepend (list1, g_strdup ("two")); 348 list1 = g_slist_prepend (list1, g_strdup ("three")); 349 350 g_assert (tracker_gslist_with_string_data_equal (list1, list1)); 351 352 shorty = g_slist_prepend (shorty, g_strdup ("one")); 353 g_assert (!tracker_gslist_with_string_data_equal (list1, shorty)); 354 g_assert (!tracker_gslist_with_string_data_equal (shorty, list1)); 355 356 list2 = g_slist_prepend (list2, g_strdup ("one")); 357 list2 = g_slist_prepend (list2, g_strdup ("two")); 358 list2 = g_slist_prepend (list2, g_strdup ("three")); 359 g_assert (tracker_gslist_with_string_data_equal (list1, list2)); 360 g_assert (tracker_gslist_with_string_data_equal (list2, list1)); 361 362 list3 = g_slist_prepend (list3, g_strdup ("one")); 363 list3 = g_slist_prepend (list3, g_strdup ("something different")); 364 list3 = g_slist_prepend (list3, g_strdup ("three")); 365 g_assert (!tracker_gslist_with_string_data_equal (list1, list3)); 366 g_assert (!tracker_gslist_with_string_data_equal (list3, list1)); 367 368 g_slist_foreach (list1, (GFunc)g_free, NULL); 369 g_slist_foreach (list2, (GFunc)g_free, NULL); 370 g_slist_foreach (shorty, (GFunc)g_free, NULL); 371 g_slist_foreach (list3, (GFunc)g_free, NULL); 372 373 g_slist_free (list1); 374 g_slist_free (list2); 375 g_slist_free (shorty); 376 g_slist_free (list3); 377 } 378 379 static void 380 test_glist_copy_with_string_data (void) 381 { 382 GList *input = NULL; 383 GList *result = NULL; 384 385 input = g_list_prepend (input, g_strdup ("one")); 386 input = g_list_prepend (input, g_strdup ("two")); 387 388 result = tracker_glist_copy_with_string_data (input); 389 g_assert (result != input); 390 g_assert_cmpint (g_list_length (result), ==, 2); 391 392 /* Further checks... that the contents are actually the same */ 393 } 394 395 int 396 main (int argc, char **argv) 397 { 398 gint result; 399 400 g_test_init (&argc, &argv, NULL); 401 402 g_test_add_func ("/libtracker-common/tracker-type-utils/boolean_as_text_to_number", 403 test_boolean_as_text_to_number); 404 g_test_add_func ("/libtracker-common/tracker-type-utils/string_list_as_list", 405 test_string_list_to_string); 406 g_test_add_func ("/libtracker-common/tracker-type-utils/string_list_as_list", 407 test_string_to_string_list); 408 g_test_add_func ("/libtracker-common/tracker-type-utils/gslist_to_string_list", 409 test_gslist_to_string_list); 410 g_test_add_func ("/libtracker-common/tracker-type-utils/string_in_string_list", 411 test_string_in_string_list); 412 g_test_add_func ("/libtracker-common/tracker-type-utils/string_in_gslist", 413 test_string_in_gslist); 414 g_test_add_func ("/libtracker-common/tracker-type-utils/string_list_to_gslist", 415 test_string_list_to_gslist); 416 g_test_add_func ("/libtracker-common/tracker-type-utils/string_to_uint", 417 test_string_to_uint); 418 g_test_add_func ("/libtracker-common/tracker-type-utils/guint32_to_string", 419 test_guint32_to_string); 420 g_test_add_func ("/libtracker-common/tracker-type-utils/gint32_to_string", 421 test_gint32_to_string); 422 g_test_add_func ("/libtracker-common/tracker-type-utils/uint_to_string", 423 test_uint_to_string); 424 g_test_add_func ("/libtracker-common/tracker-type-utils/int_to_string", 425 test_int_to_string); 426 g_test_add_func ("/libtracker-common/tracker-type-utils/long_to_string", 427 test_long_to_string); 428 g_test_add_func ("/libtracker-common/tracker-type-utils/gslist_with_string_data_equal", 429 test_gslist_with_string_data_equal); 430 g_test_add_func ("/libtracker-common/tracker-type-utils/glist_copy_with_string_data", 431 test_glist_copy_with_string_data); 432 result = g_test_run (); 433 434 return result; 435 }