File: test-timerange.c

package info (click to toggle)
zeitgeist 1.0.4-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,440 kB
  • sloc: python: 8,116; cpp: 3,006; ansic: 1,298; sql: 1,192; makefile: 951; javascript: 753; sh: 156
file content (157 lines) | stat: -rw-r--r-- 4,221 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright © 2010 Canonical Ltd.
 *             By Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
 *
 * 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/>.
 *
 */

#include <glib.h>
#include <glib-object.h>
#include "zeitgeist.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_create (Fixture *fix, gconstpointer data)
{
  ZeitgeistTimeRange *tr;
  tr = zeitgeist_time_range_new (0, 1);

  g_assert_cmpint (0, ==, zeitgeist_time_range_get_start (tr));
  g_assert_cmpint (1, ==, zeitgeist_time_range_get_end (tr));

  g_object_unref (tr);
}

static void
test_anytime (Fixture *fix, gconstpointer data)
{
  ZeitgeistTimeRange *tr;

  tr = zeitgeist_time_range_new_anytime ();

  g_assert (0 == zeitgeist_time_range_get_start (tr));
  g_assert (G_MAXINT64 == zeitgeist_time_range_get_end (tr));

  g_object_unref (tr);
}

static void
test_to_now (Fixture *fix, gconstpointer data)
{
  ZeitgeistTimeRange *tr;

  tr = zeitgeist_time_range_new_to_now ();

  g_assert (0 == zeitgeist_time_range_get_start (tr));

  /* Since system time is unreliable we simply assert that the end timestamp
   * is after 2000. This assueres that we catch any uint/int32 overflow
   * at least */
  g_assert (30*ZEITGEIST_TIMESTAMP_YEAR < zeitgeist_time_range_get_end (tr));

  g_object_unref (tr);
}

static void
test_from_now (Fixture *fix, gconstpointer data)
{
  ZeitgeistTimeRange *tr;

  tr = zeitgeist_time_range_new_from_now ();  

  /* Since system time is unreliable we simply assert that the start timestamp
   * is after 2000. This assueres that we catch any uint/int32 overflow
   * at least */
  g_assert (30*ZEITGEIST_TIMESTAMP_YEAR < zeitgeist_time_range_get_start (tr));
  g_assert (G_MAXINT64 == zeitgeist_time_range_get_end (tr));

  g_object_unref (tr);
}

static void
test_from_variant (Fixture *fix, gconstpointer data)
{
  ZeitgeistTimeRange *tr;
  GVariant           *v;
  gint64              i,j;
  GError             **error;

  error = NULL;
  v = g_variant_new ("(xx)",
                     G_GINT64_CONSTANT(0), G_MAXINT64);
  g_variant_get (v, "(xx)", &i, &j);
  tr = zeitgeist_time_range_new_from_variant (v, error); // v freed

  g_assert (0 == zeitgeist_time_range_get_start (tr));
  g_assert (G_MAXINT64 == zeitgeist_time_range_get_end (tr));

  g_object_unref (tr);
}

static void
test_to_variant (Fixture *fix, gconstpointer data)
{
  ZeitgeistTimeRange *tr;
  GVariant           *v;
  gint64              i,j;

  tr = zeitgeist_time_range_new (0, G_MAXINT64);
  v = zeitgeist_time_range_to_variant (tr); // tr freed
  g_variant_get (v, "(xx)",
                 &i, &j);

  g_assert (0 == i);
  g_assert (G_MAXINT64 == j);

  g_variant_unref (v);
}

int
main (int   argc,
      char *argv[])
{
  g_type_init();
  g_test_init (&argc, &argv, NULL);

  g_test_add ("/Zeitgeist/TimeRange/Create", Fixture, NULL,
              setup, test_create, teardown);
  g_test_add ("/Zeitgeist/TimeRange/Anytime", Fixture, NULL,
              setup, test_anytime, teardown);
  g_test_add ("/Zeitgeist/TimeRange/ToNow", Fixture, NULL,
              setup, test_to_now, teardown);
  g_test_add ("/Zeitgeist/TimeRange/FromNow", Fixture, NULL,
              setup, test_from_now, teardown);
  g_test_add ("/Zeitgeist/TimeRange/FromVariant", Fixture, NULL,
                setup, test_from_variant, teardown);
  g_test_add ("/Zeitgeist/TimeRange/ToVariant", Fixture, NULL,
                  setup, test_to_variant, teardown);

  return g_test_run();
}