Debian Codegraph @

src/libsysprof-capture/tests/test-capture-cursor.c

raw
1 /* test-capture-cursor.c
2 *
3 * Copyright 2016o-2019 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include <glib.h>
22 #include <glib/gstdio.h>
23 #include <sysprof-capture.h>
24
25 static bool
26 increment (const SysprofCaptureFrame *frame,
27 void *user_data)
28 {
29 gint *count= user_data;
30 (*count)++;
31 return true;
32 }
33
34 static void
35 test_cursor_basic (void)
36 {
37 SysprofCaptureReader *reader;
38 SysprofCaptureWriter *writer;
39 SysprofCaptureCursor *cursor;
40 gint64 t = SYSPROF_CAPTURE_CURRENT_TIME;
41 guint i;
42 gint r;
43 gint count = 0;
44
45 writer = sysprof_capture_writer_new ("capture-cursor-file", 0);
46 g_assert_nonnull (writer);
47
48 sysprof_capture_writer_flush (writer);
49
50 reader = sysprof_capture_reader_new ("capture-cursor-file");
51 g_assert_nonnull (reader);
52
53 for (i = 0; i < 100; i++)
54 {
55 r = sysprof_capture_writer_add_timestamp (writer, t, i, -1);
56 g_assert_cmpint (r, ==, TRUE);
57 }
58
59 sysprof_capture_writer_flush (writer);
60
61 cursor = sysprof_capture_cursor_new (reader);
62 sysprof_capture_cursor_foreach (cursor, increment, &count);
63 g_assert_cmpint (count, ==, 100);
64 g_clear_pointer (&cursor, sysprof_capture_cursor_unref);
65
66 sysprof_capture_reader_unref (reader);
67 sysprof_capture_writer_unref (writer);
68
69 g_unlink ("capture-cursor-file");
70 }
71
72 static void
73 test_cursor_null (void)
74 {
75 SysprofCaptureCursor *cursor = sysprof_capture_cursor_new (NULL);
76 gint count = 0;
77 sysprof_capture_cursor_foreach (cursor, increment, &count);
78 g_assert_cmpint (count, ==, 0);
79 g_clear_pointer (&cursor, sysprof_capture_cursor_unref);
80 }
81
82 int
83 main (int argc,
84 char *argv[])
85 {
86 sysprof_clock_init ();
87 g_test_init (&argc, &argv, NULL);
88 g_test_add_func ("/SysprofCaptureCursor/basic", test_cursor_basic);
89 g_test_add_func ("/SysprofCaptureCursor/null", test_cursor_null);
90 return g_test_run ();
91 }