~elementary-os/elementaryos/os-patch-notify-osd-precise

« back to all changes in this revision

Viewing changes to tests/test-stack.c

  • Committer: Sergey "Shnatsel" Davidoff
  • Date: 2012-06-18 21:08:31 UTC
  • Revision ID: shnatsel@gmail.com-20120618210831-g6k5y7vecjdylgic
Initial import, version 0.9.34-0ubuntu2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
**3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
 
3
**      10        20        30        40        50        60        70        80
 
4
**
 
5
** notify-osd
 
6
**
 
7
** test-stack.c - unit-tests for stack class
 
8
**
 
9
** Copyright 2009 Canonical Ltd.
 
10
**
 
11
** Authors:
 
12
**    Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
 
13
**    David Barth <david.barth@canonical.com>
 
14
**
 
15
** This program is free software: you can redistribute it and/or modify it
 
16
** under the terms of the GNU General Public License version 3, as published
 
17
** by the Free Software Foundation.
 
18
**
 
19
** This program is distributed in the hope that it will be useful, but
 
20
** WITHOUT ANY WARRANTY; without even the implied warranties of
 
21
** MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
22
** PURPOSE.  See the GNU General Public License for more details.
 
23
**
 
24
** You should have received a copy of the GNU General Public License along
 
25
** with this program.  If not, see <http://www.gnu.org/licenses/>.
 
26
**
 
27
*******************************************************************************/
 
28
 
 
29
#include <glib.h>
 
30
 
 
31
#include "stack.h"
 
32
#include "defaults.h"
 
33
#include "bubble.h"
 
34
 
 
35
static
 
36
void
 
37
test_stack_new ()
 
38
{
 
39
        Stack*    stack = NULL;
 
40
        Defaults* defaults = defaults_new ();
 
41
        Observer* observer = observer_new ();
 
42
 
 
43
        stack = stack_new (defaults, observer);
 
44
        g_assert (stack != NULL);
 
45
}
 
46
 
 
47
static
 
48
void
 
49
test_stack_del ()
 
50
{
 
51
        Stack*    stack = NULL;
 
52
        Defaults* defaults = defaults_new ();
 
53
        Observer* observer = observer_new ();
 
54
 
 
55
        stack = stack_new (defaults, observer);
 
56
        stack_del (stack);
 
57
}
 
58
 
 
59
static
 
60
void
 
61
test_stack_push ()
 
62
{
 
63
        Stack*       stack = NULL;
 
64
        Defaults* defaults = defaults_new ();
 
65
        Observer* observer = observer_new ();
 
66
        Bubble*     bubble = bubble_new (defaults);
 
67
        guint           id = -1;
 
68
        guint  replaced_id = -1;
 
69
 
 
70
        stack = stack_new (defaults, observer);
 
71
        id = stack_push_bubble (stack, bubble);
 
72
 
 
73
        g_assert (id > 0);
 
74
 
 
75
        replaced_id = stack_push_bubble (stack, bubble);
 
76
 
 
77
        g_assert (replaced_id == id);
 
78
 
 
79
        g_object_unref (G_OBJECT (stack));
 
80
}
 
81
 
 
82
static void
 
83
test_stack_slots ()
 
84
{
 
85
        Stack*    stack = NULL;
 
86
        Defaults* defaults = defaults_new ();
 
87
        Observer* observer = observer_new ();
 
88
        gint      x;
 
89
        gint      y;
 
90
        Bubble*   one;
 
91
        Bubble*   two;
 
92
 
 
93
        stack = stack_new (defaults, observer);
 
94
 
 
95
        // check if stack_is_slot_vacant() can take "crap" without crashing
 
96
        g_assert_cmpint (stack_is_slot_vacant (NULL, SLOT_TOP), ==, FALSE);
 
97
        g_assert_cmpint (stack_is_slot_vacant (stack, 832), ==, FALSE);
 
98
        g_assert_cmpint (stack_is_slot_vacant (NULL, 4321), ==, FALSE);
 
99
 
 
100
        // check if stack_get_slot_position can take "crap" without crashing
 
101
        stack_get_slot_position (NULL, SLOT_TOP, 0, &x, &y);
 
102
        g_assert_cmpint (x, ==, -1);
 
103
        g_assert_cmpint (y, ==, -1);
 
104
        stack_get_slot_position (stack, 4711, 0, &x, &y);
 
105
        g_assert_cmpint (x, ==, -1);
 
106
        g_assert_cmpint (y, ==, -1);
 
107
        stack_get_slot_position (NULL, 42, 0, NULL, NULL);
 
108
 
 
109
        // check if stack_allocate_slot() can take "crap" without crashing
 
110
        one = bubble_new (defaults);
 
111
        two = bubble_new (defaults);
 
112
        g_assert_cmpint (stack_allocate_slot (NULL, one, SLOT_TOP), ==, FALSE);
 
113
        g_assert_cmpint (stack_allocate_slot (stack, NULL, SLOT_TOP), ==, FALSE);
 
114
        g_assert_cmpint (stack_allocate_slot (stack, one, 4711), ==, FALSE);
 
115
 
 
116
        // check if stack_free_slot() can take "crap" without crashing
 
117
        g_assert_cmpint (stack_free_slot (NULL, two), ==, FALSE);
 
118
        g_assert_cmpint (stack_free_slot (stack, NULL), ==, FALSE);
 
119
 
 
120
        // initially both slots should be empty
 
121
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_TOP), ==, VACANT);
 
122
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_BOTTOM), ==, VACANT);
 
123
        g_object_unref (one);
 
124
        g_object_unref (two);
 
125
 
 
126
        // fill top slot, verify it's occupied, free it, verify again
 
127
        one = bubble_new (defaults);
 
128
        g_assert_cmpint (stack_allocate_slot (stack, one, SLOT_TOP), ==, TRUE);
 
129
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_TOP), ==, OCCUPIED);
 
130
        g_assert_cmpint (stack_free_slot (stack, one), ==, TRUE);
 
131
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_TOP), ==, VACANT);
 
132
        g_object_unref (one);
 
133
 
 
134
        // fill bottom slot, verify it's occupied, free it, verify again
 
135
        two = bubble_new (defaults);
 
136
        g_assert_cmpint (stack_allocate_slot (stack, two, SLOT_BOTTOM), ==, TRUE);
 
137
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_BOTTOM), ==, OCCUPIED);
 
138
        g_assert_cmpint (stack_free_slot (stack, two), ==, TRUE);
 
139
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_BOTTOM), ==, VACANT);
 
140
        g_object_unref (two);
 
141
 
 
142
        // try to free vacant slots
 
143
        one = bubble_new (defaults);
 
144
        two = bubble_new (defaults);
 
145
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_TOP), ==, VACANT);
 
146
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_BOTTOM), ==, VACANT);
 
147
        g_assert_cmpint (stack_free_slot (stack, one), ==, FALSE);
 
148
        g_assert_cmpint (stack_free_slot (stack, two), ==, FALSE);
 
149
        g_object_unref (one);
 
150
        g_object_unref (two);
 
151
 
 
152
        // allocate top slot, verify, try to allocate top slot again
 
153
        one = bubble_new (defaults);
 
154
        two = bubble_new (defaults);
 
155
        g_assert_cmpint (stack_allocate_slot (stack, one, SLOT_TOP), ==, TRUE);
 
156
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_TOP), ==, OCCUPIED);
 
157
        g_assert_cmpint (stack_allocate_slot (stack, two, SLOT_TOP), ==, FALSE);
 
158
        g_assert_cmpint (stack_free_slot (stack, one), ==, TRUE);
 
159
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_TOP), ==, VACANT);
 
160
        g_object_unref (one);
 
161
        g_object_unref (two);
 
162
 
 
163
        // allocate bottom slot, verify, try to allocate bottom slot again
 
164
        one = bubble_new (defaults);
 
165
        two = bubble_new (defaults);
 
166
        g_assert_cmpint (stack_allocate_slot (stack, one, SLOT_BOTTOM), ==, TRUE);
 
167
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_BOTTOM), ==, OCCUPIED);
 
168
        g_assert_cmpint (stack_allocate_slot (stack, two, SLOT_BOTTOM), ==, FALSE);
 
169
        g_assert_cmpint (stack_free_slot (stack, one), ==, TRUE);
 
170
        g_assert_cmpint (stack_is_slot_vacant (stack, SLOT_BOTTOM), ==, VACANT);
 
171
        g_object_unref (one);
 
172
        g_object_unref (two);
 
173
 
 
174
        // check if we can get reasonable values from stack_get_slot_position()
 
175
        // FIXME: disabled this test for the moment, hopefully it works within
 
176
        // a real environment
 
177
        /*stack_get_slot_position (stack, SLOT_TOP, &x, &y);
 
178
        g_assert_cmpint (x, >, -1);
 
179
        g_assert_cmpint (y, >, -1);
 
180
        stack_get_slot_position (stack, SLOT_BOTTOM, &x, &y);
 
181
        g_assert_cmpint (x, >, -1);
 
182
        g_assert_cmpint (y, >, -1);*/
 
183
 
 
184
        g_object_unref (G_OBJECT (stack));
 
185
}
 
186
 
 
187
GTestSuite *
 
188
test_stack_create_test_suite (void)
 
189
{
 
190
        GTestSuite *ts = NULL;
 
191
 
 
192
        ts = g_test_create_suite ("stack");
 
193
 
 
194
#define TC(x) g_test_create_case(#x, 0, NULL, NULL, x, NULL)
 
195
 
 
196
        g_test_suite_add(ts, TC(test_stack_new));
 
197
        g_test_suite_add(ts, TC(test_stack_del));
 
198
        g_test_suite_add(ts, TC(test_stack_push));
 
199
        g_test_suite_add(ts, TC(test_stack_slots));
 
200
 
 
201
        return ts;
 
202
}