Test bt_notification_heap
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 2 Nov 2016 20:11:20 +0000 (16:11 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sat, 27 May 2017 18:09:06 +0000 (14:09 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/Makefile.am
tests/lib/Makefile.am
tests/lib/test_bt_notification_heap.c [new file with mode: 0644]

index 54847f3e207f159b6154e43e05229fa75bc1a6ac..bc14009fe493c720278662ae8e5589c8deedd128 100644 (file)
@@ -17,7 +17,8 @@ TESTS = bin/test_trace_read \
        lib/test_ctf_ir_ref \
        lib/test_bt_ctf_field_type_validation \
        lib/test_ir_visit \
-       lib/test_trace_listener
+       lib/test_trace_listener \
+       lib/test_bt_notification_heap
 
 EXTRA_DIST = $(srcdir)/ctf-traces/** \
             $(srcdir)/debug-info-data/** \
index cbbc5dc407025a25ba44969f27776ead107e396a..ed89ea3e93c9ab26e2d6c939ce7f38cd8794f65b 100644 (file)
@@ -30,9 +30,11 @@ test_ir_visit_LDADD = $(COMMON_TEST_LDADD)
 
 test_trace_listener_LDADD = $(COMMON_TEST_LDADD)
 
+test_bt_notification_heap_LDADD = $(COMMON_TEST_LDADD)
+
 noinst_PROGRAMS = test_seek test_bitfield test_ctf_writer test_bt_values \
        test_ctf_ir_ref test_bt_ctf_field_type_validation test_ir_visit \
-       test_trace_listener
+       test_trace_listener test_bt_notification_heap
 
 test_seek_SOURCES = test_seek.c
 test_bitfield_SOURCES = test_bitfield.c
@@ -42,6 +44,7 @@ test_ctf_ir_ref_SOURCES = test_ctf_ir_ref.c
 test_bt_ctf_field_type_validation_SOURCES = test_bt_ctf_field_type_validation.c
 test_ir_visit_SOURCES = test_ir_visit.c
 test_trace_listener_SOURCES = test_trace_listener.c
+test_bt_notification_heap_SOURCES = test_bt_notification_heap.c
 
 check_SCRIPTS = test_seek_big_trace \
                test_seek_empty_packet \
diff --git a/tests/lib/test_bt_notification_heap.c b/tests/lib/test_bt_notification_heap.c
new file mode 100644 (file)
index 0000000..c200c63
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * test_bt_notification_heap.c
+ *
+ * bt_notification_heap tests
+ *
+ * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "tap/tap.h"
+#include <stdbool.h>
+#include <stdlib.h>
+#include <babeltrace/compiler.h>
+#include <babeltrace/ref.h>
+#include <babeltrace/plugin/notification/heap.h>
+#include <babeltrace/plugin/notification/notification.h>
+#include <babeltrace/plugin/notification/notification-internal.h>
+
+#define NR_TESTS 7
+
+struct dummy_notification {
+       struct bt_notification parent;
+       uint64_t value;
+};
+
+static
+void dummy_notification_destroy(struct bt_object *obj)
+{
+       g_free(obj);
+}
+
+/* Reproduced from internal notification.c code. */
+void bt_notification_init(struct bt_notification *notification,
+               enum bt_notification_type type,
+               bt_object_release_func release)
+{
+       assert(type > BT_NOTIFICATION_TYPE_ALL &&
+                       type < BT_NOTIFICATION_TYPE_NR);
+       notification->type = type;
+       bt_object_init(&notification->base, release);
+}
+
+static
+struct bt_notification *dummy_notification_create(uint64_t value)
+{
+       struct dummy_notification *notification;
+
+       notification = g_new0(struct dummy_notification, 1);
+       if (!notification) {
+               goto error;
+       }
+       bt_notification_init(&notification->parent,
+                       BT_NOTIFICATION_TYPE_NR - 1 /* dummy value */,
+                       dummy_notification_destroy);
+       notification->value = value;
+       return &notification->parent;
+error:
+       return NULL;
+}
+
+static
+bool compare_notifications(struct bt_notification *a, struct bt_notification *b)
+{
+       uint64_t val_a = ((struct dummy_notification *) a)->value;
+       uint64_t val_b = ((struct dummy_notification *) b)->value;
+
+       if (val_a == val_b) {
+               return a < b;
+       } else {
+               return val_a < val_b;
+       }
+}
+
+int main(int argc, char **argv)
+{
+       int i;
+       uint64_t last_read_value = 0;
+       struct bt_notification_heap *heap = NULL;
+
+       /* Initialize tap harness before any tests */
+       plan_tests(NR_TESTS);
+       heap = bt_notification_heap_create(compare_notifications);
+       ok(heap, "Created a notification heap");
+
+       /* Insert 10 000 notifications with random values. */
+       for (i = 0; i < 10000; i++) {
+               int ret;
+               struct bt_notification *notification =
+                               dummy_notification_create(rand());
+
+               if (!notification) {
+                       diag("Dummy notification creation failed");
+                       goto end;
+               }
+
+               ret = bt_notification_heap_insert(heap, notification);
+               if (ret) {
+                       diag("Failed to insert notification %i in heap", i);
+                       goto end;
+               }
+               bt_put(notification);
+       }
+       pass("Inserted 10 000 random notifications in notification heap");
+
+       /* Pop 5000 notifications, making sure the values read are ascending */
+       for (i = 0; i < 5000; i++) {
+               struct bt_notification *pop_notification;
+               struct bt_notification *peek_notification;
+               struct dummy_notification *dummy;
+
+               peek_notification = bt_notification_heap_peek(heap);
+               if (!peek_notification) {
+                       fail("Failed to peek a notification");
+                       goto end;
+               }
+
+               pop_notification = bt_notification_heap_pop(heap);
+               if (!pop_notification) {
+                       fail("Failed to pop a notification");
+                       goto end;
+               }
+
+               if (peek_notification != pop_notification) {
+                       fail("bt_notification_heap_peek and bt_notification_heap_pop do not return the same notification");
+                       bt_put(peek_notification);
+                       bt_put(pop_notification);
+                       goto end;
+               }
+
+               dummy = container_of(pop_notification,
+                               struct dummy_notification, parent);
+               if (dummy->value < last_read_value) {
+                       fail("Notification heap did not provide notifications in ascending order");
+               }
+               last_read_value = dummy->value;
+               bt_put(peek_notification);
+               bt_put(pop_notification);
+       }
+
+       pass("bt_notification_heap_peek and bt_notification_heap_pop return the same notification");
+       pass("Notification heap provided 5 000 notifications in ascending order");
+
+       /* Insert 10 000 notifications with random values. */
+       for (i = 0; i < 10000; i++) {
+               int ret;
+               struct bt_notification *notification =
+                               dummy_notification_create(rand());
+
+               if (!notification) {
+                       diag("Dummy notification creation failed");
+                       goto end;
+               }
+
+               ret = bt_notification_heap_insert(heap, notification);
+               if (ret) {
+                       diag("Failed to insert notification %i in heap", i);
+                       goto end;
+               }
+               bt_put(notification);
+       }
+       pass("Inserted 10 000 random notifications in notification heap after popping");
+
+       last_read_value = 0;
+       /* Pop remaining 15 000 notifications, making sure the values read are ascending */
+       for (i = 0; i < 15000; i++) {
+               struct bt_notification *pop_notification;
+               struct dummy_notification *dummy;
+
+               pop_notification = bt_notification_heap_pop(heap);
+               if (!pop_notification) {
+                       fail("Failed to pop a notification");
+                       goto end;
+               }
+               dummy = container_of(pop_notification,
+                               struct dummy_notification, parent);
+               if (dummy->value < last_read_value) {
+                       fail("Notification heap did not provide notifications in ascending order");
+               }
+               last_read_value = dummy->value;
+               bt_put(pop_notification);
+       }
+       pass("Popped remaining 15 000 notifications from heap in ascending order");
+
+       ok(!bt_notification_heap_peek(heap), "No notifications left in heap");
+end:
+       bt_put(heap);
+       return exit_status();
+}
This page took 0.026723 seconds and 4 git commands to generate.