Commit | Line | Data |
---|---|---|
e7ba301a JG |
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 <stdbool.h> | |
24 | #include <stdlib.h> | |
25 | #include <babeltrace/compiler.h> | |
26 | #include <babeltrace/ref.h> | |
b2e0c907 PP |
27 | #include <babeltrace/graph/notification-heap.h> |
28 | #include <babeltrace/graph/notification.h> | |
29 | #include <babeltrace/graph/notification-internal.h> | |
e7ba301a JG |
30 | |
31 | #define NR_TESTS 7 | |
32 | ||
33 | struct dummy_notification { | |
34 | struct bt_notification parent; | |
35 | uint64_t value; | |
36 | }; | |
37 | ||
38 | static | |
39 | void dummy_notification_destroy(struct bt_object *obj) | |
40 | { | |
41 | g_free(obj); | |
42 | } | |
43 | ||
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) | |
48 | { | |
49 | assert(type > BT_NOTIFICATION_TYPE_ALL && | |
50 | type < BT_NOTIFICATION_TYPE_NR); | |
51 | notification->type = type; | |
52 | bt_object_init(¬ification->base, release); | |
53 | } | |
54 | ||
55 | static | |
56 | struct bt_notification *dummy_notification_create(uint64_t value) | |
57 | { | |
58 | struct dummy_notification *notification; | |
59 | ||
60 | notification = g_new0(struct dummy_notification, 1); | |
61 | if (!notification) { | |
62 | goto error; | |
63 | } | |
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; | |
69 | error: | |
70 | return NULL; | |
71 | } | |
72 | ||
73 | static | |
352fe16f JG |
74 | bool compare_notifications(struct bt_notification *a, struct bt_notification *b, |
75 | void *unused) | |
e7ba301a JG |
76 | { |
77 | uint64_t val_a = ((struct dummy_notification *) a)->value; | |
78 | uint64_t val_b = ((struct dummy_notification *) b)->value; | |
79 | ||
80 | if (val_a == val_b) { | |
81 | return a < b; | |
82 | } else { | |
83 | return val_a < val_b; | |
84 | } | |
85 | } | |
86 | ||
87 | int main(int argc, char **argv) | |
88 | { | |
89 | int i; | |
90 | uint64_t last_read_value = 0; | |
91 | struct bt_notification_heap *heap = NULL; | |
92 | ||
93 | /* Initialize tap harness before any tests */ | |
94 | plan_tests(NR_TESTS); | |
352fe16f | 95 | heap = bt_notification_heap_create(compare_notifications, NULL); |
e7ba301a JG |
96 | ok(heap, "Created a notification heap"); |
97 | ||
98 | /* Insert 10 000 notifications with random values. */ | |
99 | for (i = 0; i < 10000; i++) { | |
100 | int ret; | |
101 | struct bt_notification *notification = | |
102 | dummy_notification_create(rand()); | |
103 | ||
104 | if (!notification) { | |
105 | diag("Dummy notification creation failed"); | |
106 | goto end; | |
107 | } | |
108 | ||
109 | ret = bt_notification_heap_insert(heap, notification); | |
110 | if (ret) { | |
111 | diag("Failed to insert notification %i in heap", i); | |
112 | goto end; | |
113 | } | |
114 | bt_put(notification); | |
115 | } | |
116 | pass("Inserted 10 000 random notifications in notification heap"); | |
117 | ||
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; | |
123 | ||
124 | peek_notification = bt_notification_heap_peek(heap); | |
125 | if (!peek_notification) { | |
126 | fail("Failed to peek a notification"); | |
127 | goto end; | |
128 | } | |
129 | ||
130 | pop_notification = bt_notification_heap_pop(heap); | |
131 | if (!pop_notification) { | |
132 | fail("Failed to pop a notification"); | |
133 | goto end; | |
134 | } | |
135 | ||
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); | |
140 | goto end; | |
141 | } | |
142 | ||
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"); | |
147 | } | |
148 | last_read_value = dummy->value; | |
149 | bt_put(peek_notification); | |
150 | bt_put(pop_notification); | |
151 | } | |
152 | ||
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"); | |
155 | ||
156 | /* Insert 10 000 notifications with random values. */ | |
157 | for (i = 0; i < 10000; i++) { | |
158 | int ret; | |
159 | struct bt_notification *notification = | |
160 | dummy_notification_create(rand()); | |
161 | ||
162 | if (!notification) { | |
163 | diag("Dummy notification creation failed"); | |
164 | goto end; | |
165 | } | |
166 | ||
167 | ret = bt_notification_heap_insert(heap, notification); | |
168 | if (ret) { | |
169 | diag("Failed to insert notification %i in heap", i); | |
170 | goto end; | |
171 | } | |
172 | bt_put(notification); | |
173 | } | |
174 | pass("Inserted 10 000 random notifications in notification heap after popping"); | |
175 | ||
176 | last_read_value = 0; | |
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; | |
181 | ||
182 | pop_notification = bt_notification_heap_pop(heap); | |
183 | if (!pop_notification) { | |
184 | fail("Failed to pop a notification"); | |
185 | goto end; | |
186 | } | |
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"); | |
191 | } | |
192 | last_read_value = dummy->value; | |
193 | bt_put(pop_notification); | |
194 | } | |
195 | pass("Popped remaining 15 000 notifications from heap in ascending order"); | |
196 | ||
197 | ok(!bt_notification_heap_peek(heap), "No notifications left in heap"); | |
198 | end: | |
199 | bt_put(heap); | |
200 | return exit_status(); | |
201 | } |