1 /*
2  * Copyright © 2010 Canonical Ltd.
3  *             By Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 3 as
7  * published by the Free Software Foundation.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18 
19 #include <glib.h>
20 #include <glib-object.h>
21 #include "zeitgeist.h"
22 
23 typedef struct
24 {
25 } Fixture;
26 
27 static void setup    (Fixture *fix, gconstpointer data);
28 static void teardown (Fixture *fix, gconstpointer data);
29 
30 static void
setup(Fixture * fix,gconstpointer data)31 setup (Fixture *fix, gconstpointer data)
32 {
33 }
34 
35 static void
teardown(Fixture * fix,gconstpointer data)36 teardown (Fixture *fix, gconstpointer data)
37 {
38 }
39 
40 static void
test_create(Fixture * fix,gconstpointer data)41 test_create (Fixture *fix, gconstpointer data)
42 {
43   ZeitgeistTimeRange *tr;
44   tr = zeitgeist_time_range_new (0, 1);
45 
46   g_assert_cmpint (0, ==, zeitgeist_time_range_get_start (tr));
47   g_assert_cmpint (1, ==, zeitgeist_time_range_get_end (tr));
48 
49   g_object_unref (tr);
50 }
51 
52 static void
test_anytime(Fixture * fix,gconstpointer data)53 test_anytime (Fixture *fix, gconstpointer data)
54 {
55   ZeitgeistTimeRange *tr;
56 
57   tr = zeitgeist_time_range_new_anytime ();
58 
59   g_assert (0 == zeitgeist_time_range_get_start (tr));
60   g_assert (G_MAXINT64 == zeitgeist_time_range_get_end (tr));
61 
62   g_object_unref (tr);
63 }
64 
65 static void
test_to_now(Fixture * fix,gconstpointer data)66 test_to_now (Fixture *fix, gconstpointer data)
67 {
68   ZeitgeistTimeRange *tr;
69 
70   tr = zeitgeist_time_range_new_to_now ();
71 
72   g_assert (0 == zeitgeist_time_range_get_start (tr));
73 
74   /* Since system time is unreliable we simply assert that the end timestamp
75    * is after 2000. This assueres that we catch any uint/int32 overflow
76    * at least */
77   g_assert (30*ZEITGEIST_TIMESTAMP_YEAR < zeitgeist_time_range_get_end (tr));
78 
79   g_object_unref (tr);
80 }
81 
82 static void
test_from_now(Fixture * fix,gconstpointer data)83 test_from_now (Fixture *fix, gconstpointer data)
84 {
85   ZeitgeistTimeRange *tr;
86 
87   tr = zeitgeist_time_range_new_from_now ();
88 
89   /* Since system time is unreliable we simply assert that the start timestamp
90    * is after 2000. This assueres that we catch any uint/int32 overflow
91    * at least */
92   g_assert (30*ZEITGEIST_TIMESTAMP_YEAR < zeitgeist_time_range_get_start (tr));
93   g_assert (G_MAXINT64 == zeitgeist_time_range_get_end (tr));
94 
95   g_object_unref (tr);
96 }
97 
98 static void
test_from_variant(Fixture * fix,gconstpointer data)99 test_from_variant (Fixture *fix, gconstpointer data)
100 {
101   ZeitgeistTimeRange *tr;
102   GVariant           *v;
103   gint64              i,j;
104   GError             **error;
105 
106   error = NULL;
107   v = g_variant_new ("(xx)",
108                      G_GINT64_CONSTANT(0), G_MAXINT64);
109   g_variant_get (v, "(xx)", &i, &j);
110   tr = zeitgeist_time_range_new_from_variant (v, error); // v freed
111 
112   g_assert (0 == zeitgeist_time_range_get_start (tr));
113   g_assert (G_MAXINT64 == zeitgeist_time_range_get_end (tr));
114 
115   g_object_unref (tr);
116 }
117 
118 static void
test_to_variant(Fixture * fix,gconstpointer data)119 test_to_variant (Fixture *fix, gconstpointer data)
120 {
121   ZeitgeistTimeRange *tr;
122   GVariant           *v;
123   gint64              i,j;
124 
125   tr = zeitgeist_time_range_new (0, G_MAXINT64);
126   v = zeitgeist_time_range_to_variant (tr); // tr freed
127   g_variant_get (v, "(xx)",
128                  &i, &j);
129 
130   g_assert (0 == i);
131   g_assert (G_MAXINT64 == j);
132 
133   g_variant_unref (v);
134 }
135 
136 int
main(int argc,char * argv[])137 main (int   argc,
138       char *argv[])
139 {
140   g_type_init();
141   g_test_init (&argc, &argv, NULL);
142 
143   g_test_add ("/Zeitgeist/TimeRange/Create", Fixture, NULL,
144               setup, test_create, teardown);
145   g_test_add ("/Zeitgeist/TimeRange/Anytime", Fixture, NULL,
146               setup, test_anytime, teardown);
147   g_test_add ("/Zeitgeist/TimeRange/ToNow", Fixture, NULL,
148               setup, test_to_now, teardown);
149   g_test_add ("/Zeitgeist/TimeRange/FromNow", Fixture, NULL,
150               setup, test_from_now, teardown);
151   g_test_add ("/Zeitgeist/TimeRange/FromVariant", Fixture, NULL,
152                 setup, test_from_variant, teardown);
153   g_test_add ("/Zeitgeist/TimeRange/ToVariant", Fixture, NULL,
154                   setup, test_to_variant, teardown);
155 
156   return g_test_run();
157 }
158