Do not use `bool` type; use new `bt_bool` instead
[babeltrace.git] / tests / lib / test_bt_notification_heap.c
1 /*
2 * test_bt_notification_heap.c
3 *
4 * bt_notification_heap tests
5 *
6 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
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.
11 *
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.
16 *
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.
20 */
21
22 #include "tap/tap.h"
23 #include <stdlib.h>
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>
29
30 #define NR_TESTS 7
31
32 struct dummy_notification {
33 struct bt_notification parent;
34 uint64_t value;
35 };
36
37 static
38 void dummy_notification_destroy(struct bt_object *obj)
39 {
40 g_free(obj);
41 }
42
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)
47 {
48 assert(type > BT_NOTIFICATION_TYPE_ALL &&
49 type < BT_NOTIFICATION_TYPE_NR);
50 notification->type = type;
51 bt_object_init(&notification->base, release);
52 }
53
54 static
55 struct bt_notification *dummy_notification_create(uint64_t value)
56 {
57 struct dummy_notification *notification;
58
59 notification = g_new0(struct dummy_notification, 1);
60 if (!notification) {
61 goto error;
62 }
63 bt_notification_init(&notification->parent,
64 BT_NOTIFICATION_TYPE_NR - 1 /* dummy value */,
65 dummy_notification_destroy);
66 notification->value = value;
67 return &notification->parent;
68 error:
69 return NULL;
70 }
71
72 static
73 bt_bool compare_notifications(struct bt_notification *a, struct bt_notification *b,
74 void *unused)
75 {
76 uint64_t val_a = ((struct dummy_notification *) a)->value;
77 uint64_t val_b = ((struct dummy_notification *) b)->value;
78
79 if (val_a == val_b) {
80 return a < b;
81 } else {
82 return val_a < val_b;
83 }
84 }
85
86 int main(int argc, char **argv)
87 {
88 int i;
89 uint64_t last_read_value = 0;
90 struct bt_notification_heap *heap = NULL;
91
92 /* Initialize tap harness before any tests */
93 plan_tests(NR_TESTS);
94 heap = bt_notification_heap_create(compare_notifications, NULL);
95 ok(heap, "Created a notification heap");
96
97 /* Insert 10 000 notifications with random values. */
98 for (i = 0; i < 10000; i++) {
99 int ret;
100 struct bt_notification *notification =
101 dummy_notification_create(rand());
102
103 if (!notification) {
104 diag("Dummy notification creation failed");
105 goto end;
106 }
107
108 ret = bt_notification_heap_insert(heap, notification);
109 if (ret) {
110 diag("Failed to insert notification %i in heap", i);
111 goto end;
112 }
113 bt_put(notification);
114 }
115 pass("Inserted 10 000 random notifications in notification heap");
116
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;
122
123 peek_notification = bt_notification_heap_peek(heap);
124 if (!peek_notification) {
125 fail("Failed to peek a notification");
126 goto end;
127 }
128
129 pop_notification = bt_notification_heap_pop(heap);
130 if (!pop_notification) {
131 fail("Failed to pop a notification");
132 goto end;
133 }
134
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);
139 goto end;
140 }
141
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");
146 }
147 last_read_value = dummy->value;
148 bt_put(peek_notification);
149 bt_put(pop_notification);
150 }
151
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");
154
155 /* Insert 10 000 notifications with random values. */
156 for (i = 0; i < 10000; i++) {
157 int ret;
158 struct bt_notification *notification =
159 dummy_notification_create(rand());
160
161 if (!notification) {
162 diag("Dummy notification creation failed");
163 goto end;
164 }
165
166 ret = bt_notification_heap_insert(heap, notification);
167 if (ret) {
168 diag("Failed to insert notification %i in heap", i);
169 goto end;
170 }
171 bt_put(notification);
172 }
173 pass("Inserted 10 000 random notifications in notification heap after popping");
174
175 last_read_value = 0;
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;
180
181 pop_notification = bt_notification_heap_pop(heap);
182 if (!pop_notification) {
183 fail("Failed to pop a notification");
184 goto end;
185 }
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");
190 }
191 last_read_value = dummy->value;
192 bt_put(pop_notification);
193 }
194 pass("Popped remaining 15 000 notifications from heap in ascending order");
195
196 ok(!bt_notification_heap_peek(heap), "No notifications left in heap");
197 end:
198 bt_put(heap);
199 return exit_status();
200 }
This page took 0.035621 seconds and 4 git commands to generate.