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.
25 #include <babeltrace/compiler.h>
26 #include <babeltrace/ref.h>
27 #include <babeltrace/plugin/notification/heap.h>
28 #include <babeltrace/plugin/notification/notification.h>
29 #include <babeltrace/plugin/notification/notification-internal.h>
33 struct dummy_notification
{
34 struct bt_notification parent
;
39 void dummy_notification_destroy(struct bt_object
*obj
)
44 /* Reproduced from internal notification.c code. */
45 void bt_notification_init(struct bt_notification
*notification
,
46 enum bt_notification_type type
,
47 bt_object_release_func release
)
49 assert(type
> BT_NOTIFICATION_TYPE_ALL
&&
50 type
< BT_NOTIFICATION_TYPE_NR
);
51 notification
->type
= type
;
52 bt_object_init(¬ification
->base
, release
);
56 struct bt_notification
*dummy_notification_create(uint64_t value
)
58 struct dummy_notification
*notification
;
60 notification
= g_new0(struct dummy_notification
, 1);
64 bt_notification_init(¬ification
->parent
,
65 BT_NOTIFICATION_TYPE_NR
- 1 /* dummy value */,
66 dummy_notification_destroy
);
67 notification
->value
= value
;
68 return ¬ification
->parent
;
74 bool compare_notifications(struct bt_notification
*a
, struct bt_notification
*b
,
77 uint64_t val_a
= ((struct dummy_notification
*) a
)->value
;
78 uint64_t val_b
= ((struct dummy_notification
*) b
)->value
;
87 int main(int argc
, char **argv
)
90 uint64_t last_read_value
= 0;
91 struct bt_notification_heap
*heap
= NULL
;
93 /* Initialize tap harness before any tests */
95 heap
= bt_notification_heap_create(compare_notifications
, NULL
);
96 ok(heap
, "Created a notification heap");
98 /* Insert 10 000 notifications with random values. */
99 for (i
= 0; i
< 10000; i
++) {
101 struct bt_notification
*notification
=
102 dummy_notification_create(rand());
105 diag("Dummy notification creation failed");
109 ret
= bt_notification_heap_insert(heap
, notification
);
111 diag("Failed to insert notification %i in heap", i
);
114 bt_put(notification
);
116 pass("Inserted 10 000 random notifications in notification heap");
118 /* Pop 5000 notifications, making sure the values read are ascending */
119 for (i
= 0; i
< 5000; i
++) {
120 struct bt_notification
*pop_notification
;
121 struct bt_notification
*peek_notification
;
122 struct dummy_notification
*dummy
;
124 peek_notification
= bt_notification_heap_peek(heap
);
125 if (!peek_notification
) {
126 fail("Failed to peek a notification");
130 pop_notification
= bt_notification_heap_pop(heap
);
131 if (!pop_notification
) {
132 fail("Failed to pop a notification");
136 if (peek_notification
!= pop_notification
) {
137 fail("bt_notification_heap_peek and bt_notification_heap_pop do not return the same notification");
138 bt_put(peek_notification
);
139 bt_put(pop_notification
);
143 dummy
= container_of(pop_notification
,
144 struct dummy_notification
, parent
);
145 if (dummy
->value
< last_read_value
) {
146 fail("Notification heap did not provide notifications in ascending order");
148 last_read_value
= dummy
->value
;
149 bt_put(peek_notification
);
150 bt_put(pop_notification
);
153 pass("bt_notification_heap_peek and bt_notification_heap_pop return the same notification");
154 pass("Notification heap provided 5 000 notifications in ascending order");
156 /* Insert 10 000 notifications with random values. */
157 for (i
= 0; i
< 10000; i
++) {
159 struct bt_notification
*notification
=
160 dummy_notification_create(rand());
163 diag("Dummy notification creation failed");
167 ret
= bt_notification_heap_insert(heap
, notification
);
169 diag("Failed to insert notification %i in heap", i
);
172 bt_put(notification
);
174 pass("Inserted 10 000 random notifications in notification heap after popping");
177 /* Pop remaining 15 000 notifications, making sure the values read are ascending */
178 for (i
= 0; i
< 15000; i
++) {
179 struct bt_notification
*pop_notification
;
180 struct dummy_notification
*dummy
;
182 pop_notification
= bt_notification_heap_pop(heap
);
183 if (!pop_notification
) {
184 fail("Failed to pop a notification");
187 dummy
= container_of(pop_notification
,
188 struct dummy_notification
, parent
);
189 if (dummy
->value
< last_read_value
) {
190 fail("Notification heap did not provide notifications in ascending order");
192 last_read_value
= dummy
->value
;
193 bt_put(pop_notification
);
195 pass("Popped remaining 15 000 notifications from heap in ascending order");
197 ok(!bt_notification_heap_peek(heap
), "No notifications left in heap");
200 return exit_status();