2 * test_bt_notification_heap.c
4 * bt_notification_heap tests
6 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <babeltrace/compiler-internal.h>
25 #include <babeltrace/ref.h>
26 #include <babeltrace/graph/notification-heap.h>
27 #include <babeltrace/graph/notification.h>
28 #include <babeltrace/graph/notification-internal.h>
32 struct dummy_notification
{
33 struct bt_notification parent
;
38 void dummy_notification_destroy(struct bt_object
*obj
)
43 /* Reproduced from internal notification.c code. */
44 void bt_notification_init(struct bt_notification
*notification
,
45 enum bt_notification_type type
,
46 bt_object_release_func release
)
48 assert(type
> BT_NOTIFICATION_TYPE_ALL
&&
49 type
< BT_NOTIFICATION_TYPE_NR
);
50 notification
->type
= type
;
51 bt_object_init(¬ification
->base
, release
);
55 struct bt_notification
*dummy_notification_create(uint64_t value
)
57 struct dummy_notification
*notification
;
59 notification
= g_new0(struct dummy_notification
, 1);
63 bt_notification_init(¬ification
->parent
,
64 BT_NOTIFICATION_TYPE_NR
- 1 /* dummy value */,
65 dummy_notification_destroy
);
66 notification
->value
= value
;
67 return ¬ification
->parent
;
73 bt_bool
compare_notifications(struct bt_notification
*a
, struct bt_notification
*b
,
76 uint64_t val_a
= ((struct dummy_notification
*) a
)->value
;
77 uint64_t val_b
= ((struct dummy_notification
*) b
)->value
;
86 int main(int argc
, char **argv
)
89 uint64_t last_read_value
= 0;
90 struct bt_notification_heap
*heap
= NULL
;
92 /* Initialize tap harness before any tests */
94 heap
= bt_notification_heap_create(compare_notifications
, NULL
);
95 ok(heap
, "Created a notification heap");
97 /* Insert 10 000 notifications with random values. */
98 for (i
= 0; i
< 10000; i
++) {
100 struct bt_notification
*notification
=
101 dummy_notification_create(rand());
104 diag("Dummy notification creation failed");
108 ret
= bt_notification_heap_insert(heap
, notification
);
110 diag("Failed to insert notification %i in heap", i
);
113 bt_put(notification
);
115 pass("Inserted 10 000 random notifications in notification heap");
117 /* Pop 5000 notifications, making sure the values read are ascending */
118 for (i
= 0; i
< 5000; i
++) {
119 struct bt_notification
*pop_notification
;
120 struct bt_notification
*peek_notification
;
121 struct dummy_notification
*dummy
;
123 peek_notification
= bt_notification_heap_peek(heap
);
124 if (!peek_notification
) {
125 fail("Failed to peek a notification");
129 pop_notification
= bt_notification_heap_pop(heap
);
130 if (!pop_notification
) {
131 fail("Failed to pop a notification");
135 if (peek_notification
!= pop_notification
) {
136 fail("bt_notification_heap_peek and bt_notification_heap_pop do not return the same notification");
137 bt_put(peek_notification
);
138 bt_put(pop_notification
);
142 dummy
= container_of(pop_notification
,
143 struct dummy_notification
, parent
);
144 if (dummy
->value
< last_read_value
) {
145 fail("Notification heap did not provide notifications in ascending order");
147 last_read_value
= dummy
->value
;
148 bt_put(peek_notification
);
149 bt_put(pop_notification
);
152 pass("bt_notification_heap_peek and bt_notification_heap_pop return the same notification");
153 pass("Notification heap provided 5 000 notifications in ascending order");
155 /* Insert 10 000 notifications with random values. */
156 for (i
= 0; i
< 10000; i
++) {
158 struct bt_notification
*notification
=
159 dummy_notification_create(rand());
162 diag("Dummy notification creation failed");
166 ret
= bt_notification_heap_insert(heap
, notification
);
168 diag("Failed to insert notification %i in heap", i
);
171 bt_put(notification
);
173 pass("Inserted 10 000 random notifications in notification heap after popping");
176 /* Pop remaining 15 000 notifications, making sure the values read are ascending */
177 for (i
= 0; i
< 15000; i
++) {
178 struct bt_notification
*pop_notification
;
179 struct dummy_notification
*dummy
;
181 pop_notification
= bt_notification_heap_pop(heap
);
182 if (!pop_notification
) {
183 fail("Failed to pop a notification");
186 dummy
= container_of(pop_notification
,
187 struct dummy_notification
, parent
);
188 if (dummy
->value
< last_read_value
) {
189 fail("Notification heap did not provide notifications in ascending order");
191 last_read_value
= dummy
->value
;
192 bt_put(pop_notification
);
194 pass("Popped remaining 15 000 notifications from heap in ascending order");
196 ok(!bt_notification_heap_peek(heap
), "No notifications left in heap");
199 return exit_status();