1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*
* Copyright (C) 2010 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
*
*/
#include <glib.h>
#include <glib-object.h>
#include "zeitgeist-timestamp.h"
typedef struct
{
} Fixture;
static void setup (Fixture *fix, gconstpointer data);
static void teardown (Fixture *fix, gconstpointer data);
static void
setup (Fixture *fix, gconstpointer data)
{
}
static void
teardown (Fixture *fix, gconstpointer data)
{
}
static void
test_from_iso8601 (Fixture *fix, gconstpointer data)
{
const gchar *orig = "2010-06-17T00:00:00Z";
gint64 from_iso = zeitgeist_timestamp_from_iso8601 (orig);
gchar *d = zeitgeist_timestamp_to_iso8601 (from_iso);
g_assert_cmpstr (orig, ==, d);
g_free (d);
}
static void
test_inc_year (Fixture *fix, gconstpointer data)
{
gchar *d = zeitgeist_timestamp_to_iso8601 (ZEITGEIST_TIMESTAMP_YEAR);
// Since ZEITGEIST_TIMESTAMP_YEAR accounts for leap years we wont exactly
// match "1971-01-01T00:00:00Z" on the hour
g_assert (g_str_has_prefix (d, "1971-01-01T"));
g_free (d);
}
static void
test_from_date (Fixture *fix, gconstpointer data)
{
GDate date = { 0 };
g_date_set_dmy (&date, 25, G_DATE_JUNE, 2000);
gint64 from_date = zeitgeist_timestamp_from_date (&date);
gchar *d = zeitgeist_timestamp_to_iso8601 (from_date);
/* API guarantees that the timestamp is rounded to midnight */
g_assert_cmpstr ("2000-06-25T00:00:00Z", ==, d);
}
/* Assert that now() is after 2000. This will catch any overflow bugs
* for uints of int32 */
static void
test_now (Fixture *fix, gconstpointer data)
{
g_assert (30*ZEITGEIST_TIMESTAMP_YEAR < zeitgeist_timestamp_for_now ());
}
/* If a timestamp is divisible by 1000 we should have lossless conversion
* to and from GTimeVals */
static void
test_timeval_conversion (Fixture *fix, gconstpointer data)
{
GTimeVal tv = { 0 };
gint64 ts, _ts;
/* Check 0 */
ts = 0;
zeitgeist_timestamp_to_timeval (ts, &tv);
_ts = zeitgeist_timestamp_from_timeval (&tv);
g_assert (ts == _ts);
/* Check a low number */
ts = 10000;
zeitgeist_timestamp_to_timeval (ts, &tv);
_ts = zeitgeist_timestamp_from_timeval (&tv);
g_assert (ts == _ts);
/* Check 2010-06-18 */
ts = G_GINT64_CONSTANT (1276849717119);
zeitgeist_timestamp_to_timeval (ts, &tv);
_ts = zeitgeist_timestamp_from_timeval (&tv);
g_assert (ts == _ts);
/* Note : G_MAXINT64 wont work because GTimeVal uses glongs internally
* to track the numbers */
}
static void
test_prev_midnight (Fixture *fix, gconstpointer data)
{
gint64 ts, midnight;
gchar *iso;
/* Check 2010-06-23T11:19:07Z */
ts = G_GINT64_CONSTANT (1277284743659);
/* Now the actual test */
midnight = zeitgeist_timestamp_prev_midnight (ts);
iso = zeitgeist_timestamp_to_iso8601(midnight);
g_assert(g_str_has_prefix (iso, "2010-06-23T00:00:00"));
g_free (iso);
/* Pre midnight of 'midnight' should go one day back */
midnight = zeitgeist_timestamp_prev_midnight (midnight);
iso = zeitgeist_timestamp_to_iso8601(midnight);
g_assert(g_str_has_prefix (iso, "2010-06-22T00:00:00"));
g_free (iso);
}
static void
test_next_midnight (Fixture *fix, gconstpointer data)
{
gint64 ts, midnight;
gchar *iso;
/* Check 2010-06-23T11:19:07Z */
ts = G_GINT64_CONSTANT (1277284743659);
/* Now the actual test */
midnight = zeitgeist_timestamp_next_midnight (ts);
iso = zeitgeist_timestamp_to_iso8601(midnight);
g_assert(g_str_has_prefix (iso, "2010-06-24T00:00:00"));
g_free (iso);
/* Pre midnight of 'midnight' should go one day back */
midnight = zeitgeist_timestamp_next_midnight (midnight);
iso = zeitgeist_timestamp_to_iso8601(midnight);
g_assert(g_str_has_prefix (iso, "2010-06-25T00:00:00"));
g_free (iso);
}
int
main (int argc,
char *argv[])
{
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add ("/Zeitgeist/Timestamp/FromISO8601", Fixture, NULL,
setup, test_from_iso8601, teardown);
g_test_add ("/Zeitgeist/Timestamp/IncrementYear", Fixture, NULL,
setup, test_inc_year, teardown);
g_test_add ("/Zeitgeist/Timestamp/FromDate", Fixture, NULL,
setup, test_from_date, teardown);
g_test_add ("/Zeitgeist/Timestamp/Now", Fixture, NULL,
setup, test_now, teardown);
g_test_add ("/Zeitgeist/Timestamp/TimeValConversion", Fixture, NULL,
setup, test_timeval_conversion, teardown);
g_test_add ("/Zeitgeist/Timestamp/PrevMidnight", Fixture, NULL,
setup, test_prev_midnight, teardown);
g_test_add ("/Zeitgeist/Timestamp/NextMidnight", Fixture, NULL,
setup, test_next_midnight, teardown);
return g_test_run();
}
|