tracker-0.16.2/tests/libtracker-common/tracker-date-time-test.c

Location Tool Test ID Function Issue
tracker-date-time-test.c:71:3 clang-analyzer Value stored to 'result_time_t' is never read
  1 /*
  2  * Copyright (C) 2011, 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.h>
 24 #include <glib-object.h>
 25 
 26 #include <libtracker-common/tracker-date-time.h>
 27 
 28 /* This define was committed in glib 18.07.2011
 29  * https://bugzilla.gnome.org/show_bug.cgi?id=577231
 30  */
 31 #ifndef G_VALUE_INIT
 32 #define G_VALUE_INIT { 0, { { 0 } } }
 33 #endif
 34 
 35 static void
 36 test_string_to_date (void)
 37 {
 38 	GDate     *expected;
 39 	GDate     *result;
 40 	time_t     result_time_t;
 41 	const gchar  *input = "2008-06-16T11:10:10+0600";
 42 	gchar  *timezone = g_strdup (g_getenv ("TZ"));
 43 	GError *error = NULL;
 44 
 45 	if (! g_setenv ("TZ", "UTC", TRUE)) {
 46 		g_test_message ("unable to set timezone, test results are invalid, skipping\n");
 47 		if (timezone) {
 48 			g_free (timezone);
 49 		}
 50 		return;
 51 	}
 52 
 53 	expected = g_date_new_dmy (16, G_DATE_JUNE, 2008);
 54 
 55 	result_time_t = tracker_string_to_date (input, NULL, &error);
 56 	g_assert_no_error (error);
 57 
 58 	result = g_date_new ();
 59 	g_date_set_time_t (result, result_time_t);
 60 
 61 	g_setenv ("TZ", timezone, TRUE);
 62 	if (timezone) {
 63 		g_free (timezone);
 64 	}
 65 
 66 	g_assert_cmpint (g_date_get_year (expected), ==, g_date_get_year (result));
 67 	g_assert_cmpint (g_date_get_day (expected), ==, g_date_get_day (result));
 68 	g_assert_cmpint (g_date_get_month (expected), ==, g_date_get_month (result));
 69 
 70 	if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
 71 		result_time_t = tracker_string_to_date (NULL, NULL, NULL);
Value stored to 'result_time_t' is never read
(emitted by clang-analyzer)

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

72 } 73 g_test_trap_assert_failed (); 74 75 result_time_t = tracker_string_to_date ("", NULL, &error); 76 g_assert_cmpint (result_time_t, ==, -1); 77 g_assert_error (error, TRACKER_DATE_ERROR, TRACKER_DATE_ERROR_INVALID_ISO8601); 78 g_error_free (error); 79 error = NULL; 80 81 result_time_t = tracker_string_to_date ("i am not a date", NULL, &error); 82 g_assert_cmpint (result_time_t, ==, -1); 83 g_assert_error (error, TRACKER_DATE_ERROR, TRACKER_DATE_ERROR_INVALID_ISO8601); 84 g_error_free (error); 85 error = NULL; 86 87 /* Fails! Check the code 88 result_time_t = tracker_string_to_date ("2008-06-32T04:23:10+0000", NULL); 89 g_assert_cmpint (result_time_t, ==, -1); 90 */ 91 92 /* More cases of string->date are tested in tracker_date_time_from_string... 93 * it is more convinient to test them there 94 */ 95 } 96 97 static void 98 test_date_to_string (void) 99 { 100 struct tm *original; 101 time_t input; 102 gchar *result; 103 104 original = g_new0 (struct tm, 1); 105 original->tm_sec = 10; 106 original->tm_min = 53; 107 original->tm_hour = 23; 108 original->tm_mday = 16; 109 original->tm_mon = 5; 110 original->tm_year = 108; 111 original->tm_isdst = 0; 112 113 #if !(defined(__FreeBSD__) || defined(__OpenBSD__)) 114 input = mktime (original) - timezone; 115 #else 116 input = timegm (original); 117 #endif 118 119 result = tracker_date_to_string (input); 120 121 g_assert (result != NULL && strncmp (result, "2008-06-16T23:53:10Z", 19) == 0); 122 } 123 124 static void 125 test_date_time_get_set () 126 { 127 GValue value = G_VALUE_INIT; 128 GValue copy = G_VALUE_INIT; 129 130 g_value_init (&value, TRACKER_TYPE_DATE_TIME); 131 g_value_init (&copy, TRACKER_TYPE_DATE_TIME); 132 133 tracker_date_time_set (&value, 123456789, 3600); 134 135 g_assert_cmpint (tracker_date_time_get_time (&value), ==, 123456789); 136 g_assert_cmpint (tracker_date_time_get_offset (&value), ==, 3600); 137 138 g_value_copy (&value, &copy); 139 140 g_assert_cmpint (tracker_date_time_get_time (&copy), ==, 123456789); 141 g_assert_cmpint (tracker_date_time_get_offset (&copy), ==, 3600); 142 } 143 144 static void 145 test_date_time_from_string () 146 { 147 GValue value = G_VALUE_INIT; 148 GError *error = NULL; 149 150 g_value_init (&value, TRACKER_TYPE_DATE_TIME); 151 152 tracker_date_time_set_from_string (&value, "2011-10-28T17:43:00+03:00", &error); 153 g_assert (!error); 154 g_assert_cmpint (tracker_date_time_get_time (&value), ==, 1319812980); 155 g_assert_cmpint (tracker_date_time_get_offset (&value), ==, 10800); 156 157 158 /* Negative offset */ 159 tracker_date_time_set_from_string (&value, "2011-10-28T17:43:00-03:00", &error); 160 g_assert (!error); 161 g_assert_cmpint (tracker_date_time_get_time (&value), ==, 1319834580); 162 g_assert_cmpint (tracker_date_time_get_offset (&value), ==, -10800); 163 164 /* No offset */ 165 tracker_date_time_set_from_string (&value, "2011-10-28T17:43:00", &error); 166 g_assert (!error); 167 g_assert_cmpint (tracker_date_time_get_time (&value), ==, 1319823780); 168 g_assert_cmpint (tracker_date_time_get_offset (&value), ==, 0); 169 170 /* Invalid format */ 171 tracker_date_time_set_from_string (&value, "2011-10-28T17:43:00Z0900", &error); 172 g_assert (error); 173 g_error_free (error); 174 error = NULL; 175 176 /* There are no 28 months... */ 177 tracker_date_time_set_from_string (&value, "2011-28-10T17:43:00Z0900", &error); 178 g_assert (error); 179 g_error_free (error); 180 error = NULL; 181 182 /* ... nor more than +-12 offsets */ 183 tracker_date_time_set_from_string (&value, "2011-28-10T17:43:00+17:00", &error); 184 g_assert (error); 185 g_error_free (error); 186 error = NULL; 187 188 /* ... the same for the glory of the branch % */ 189 tracker_date_time_set_from_string (&value, "2011-28-10T17:43:00-17:00", &error); 190 g_assert (error); 191 g_error_free (error); 192 error = NULL; 193 } 194 195 static void 196 test_date_time_get_local_date () 197 { 198 GValue value = G_VALUE_INIT; 199 GError *error = NULL; 200 201 g_value_init (&value, TRACKER_TYPE_DATE_TIME); 202 203 tracker_date_time_set_from_string (&value, "2011-10-28T17:43:00+03:00", &error); 204 g_assert (!error); 205 206 g_assert_cmpint (tracker_date_time_get_local_date (&value), ==, 15275); 207 } 208 209 static void 210 test_date_time_get_local_time () 211 { 212 GValue value = G_VALUE_INIT; 213 GError *error = NULL; 214 215 g_value_init (&value, TRACKER_TYPE_DATE_TIME); 216 217 tracker_date_time_set_from_string (&value, "2011-10-28T17:43:00+03:00", &error); 218 g_assert (!error); 219 220 g_assert_cmpint (tracker_date_time_get_local_time (&value), ==, 63780); 221 } 222 223 gint 224 main (gint argc, gchar **argv) 225 { 226 g_test_init (&argc, &argv, NULL); 227 228 g_test_add_func ("/libtracker-common/date-time/date_to_string", 229 test_date_to_string); 230 g_test_add_func ("/libtracker-common/date-time/string_to_date", 231 test_string_to_date); 232 g_test_add_func ("/libtracker-common/date-time/get_set", 233 test_date_time_get_set); 234 g_test_add_func ("/libtracker-common/date-time/from_string", 235 test_date_time_from_string); 236 g_test_add_func ("/libtracker-common/date-time/get_local_date", 237 test_date_time_get_local_date); 238 g_test_add_func ("/libtracker-common/date-time/get_local_time", 239 test_date_time_get_local_time); 240 241 return g_test_run (); 242 }