Debian Codegraph @

tests/helpers.h

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> */
3
4 #ifndef __GPIOD_TEST_HELPERS_H__
5 #define __GPIOD_TEST_HELPERS_H__
6
7 #include <glib.h>
8 #include <gpiod.h>
9 #include <gpiod-test-common.h>
10
11 /*
12 * These typedefs are needed to make g_autoptr work - it doesn't accept
13 * regular 'struct typename' syntax.
14 */
15
16 typedef struct gpiod_chip struct_gpiod_chip;
17 G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_chip, gpiod_chip_close);
18
19 typedef struct gpiod_chip_info struct_gpiod_chip_info;
20 G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_chip_info, gpiod_chip_info_free);
21
22 typedef struct gpiod_line_info struct_gpiod_line_info;
23 G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_line_info, gpiod_line_info_free);
24
25 typedef struct gpiod_info_event struct_gpiod_info_event;
26 G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_info_event, gpiod_info_event_free);
27
28 typedef struct gpiod_line_config struct_gpiod_line_config;
29 G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_line_config, gpiod_line_config_free);
30
31 typedef struct gpiod_line_settings struct_gpiod_line_settings;
32 G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_line_settings,
33 gpiod_line_settings_free);
34
35 typedef struct gpiod_request_config struct_gpiod_request_config;
36 G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_request_config,
37 gpiod_request_config_free);
38
39 typedef struct gpiod_line_request struct_gpiod_line_request;
40 G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_line_request,
41 gpiod_line_request_release);
42
43 typedef struct gpiod_edge_event struct_gpiod_edge_event;
44 G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_edge_event, gpiod_edge_event_free);
45
46 typedef struct gpiod_edge_event_buffer struct_gpiod_edge_event_buffer;
47 G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_edge_event_buffer,
48 gpiod_edge_event_buffer_free);
49
50 #define gpiod_test_open_chip_or_fail(_path) \
51 ({ \
52 struct gpiod_chip *_chip = gpiod_chip_open(_path); \
53 g_assert_nonnull(_chip); \
54 gpiod_test_return_if_failed(); \
55 _chip; \
56 })
57
58 #define gpiod_test_chip_get_info_or_fail(_chip) \
59 ({ \
60 struct gpiod_chip_info *_info = gpiod_chip_get_info(_chip); \
61 g_assert_nonnull(_info); \
62 gpiod_test_return_if_failed(); \
63 _info; \
64 })
65
66 #define gpiod_test_chip_get_line_info_or_fail(_chip, _offset) \
67 ({ \
68 struct gpiod_line_info *_info = \
69 gpiod_chip_get_line_info(_chip, _offset); \
70 g_assert_nonnull(_info); \
71 gpiod_test_return_if_failed(); \
72 _info; \
73 })
74
75 #define gpiod_test_chip_watch_line_info_or_fail(_chip, _offset) \
76 ({ \
77 struct gpiod_line_info *_info = \
78 gpiod_chip_watch_line_info(_chip, _offset); \
79 g_assert_nonnull(_info); \
80 gpiod_test_return_if_failed(); \
81 _info; \
82 })
83
84 #define gpiod_test_create_line_settings_or_fail() \
85 ({ \
86 struct gpiod_line_settings *_settings = \
87 gpiod_line_settings_new(); \
88 g_assert_nonnull(_settings); \
89 gpiod_test_return_if_failed(); \
90 _settings; \
91 })
92
93 #define gpiod_test_create_line_config_or_fail() \
94 ({ \
95 struct gpiod_line_config *_config = \
96 gpiod_line_config_new(); \
97 g_assert_nonnull(_config); \
98 gpiod_test_return_if_failed(); \
99 _config; \
100 })
101
102 #define gpiod_test_create_edge_event_buffer_or_fail(_capacity) \
103 ({ \
104 struct gpiod_edge_event_buffer *_buffer = \
105 gpiod_edge_event_buffer_new(_capacity); \
106 g_assert_nonnull(_buffer); \
107 gpiod_test_return_if_failed(); \
108 _buffer; \
109 })
110
111 #define gpiod_test_line_config_add_line_settings_or_fail(_line_cfg, _offsets, \
112 _num_offsets, \
113 _settings) \
114 do { \
115 gint _ret = gpiod_line_config_add_line_settings(_line_cfg, \
116 _offsets, \
117 _num_offsets, \
118 _settings); \
119 g_assert_cmpint(_ret, ==, 0); \
120 gpiod_test_return_if_failed(); \
121 } while (0)
122
123 #define gpiod_test_line_config_get_line_settings_or_fail(_line_cfg, _offset) \
124 ({ \
125 struct gpiod_line_settings *_settings = \
126 gpiod_line_config_get_line_settings(_line_cfg, \
127 _offset); \
128 g_assert_nonnull(_settings); \
129 gpiod_test_return_if_failed(); \
130 _settings; \
131 })
132
133 #define gpiod_test_line_config_set_output_values_or_fail(_line_cfg, _values, \
134 _num_values) \
135 do { \
136 gint _ret = gpiod_line_config_set_output_values(_line_cfg, \
137 _values, \
138 _num_values); \
139 g_assert_cmpint(_ret, ==, 0); \
140 gpiod_test_return_if_failed(); \
141 } while (0)
142
143 #define gpiod_test_create_request_config_or_fail() \
144 ({ \
145 struct gpiod_request_config *_config = \
146 gpiod_request_config_new(); \
147 g_assert_nonnull(_config); \
148 gpiod_test_return_if_failed(); \
149 _config; \
150 })
151
152 #define gpiod_test_chip_request_lines_or_fail(_chip, _req_cfg, _line_cfg) \
153 ({ \
154 struct gpiod_line_request *_request = \
155 gpiod_chip_request_lines(_chip, \
156 _req_cfg, _line_cfg); \
157 g_assert_nonnull(_request); \
158 gpiod_test_return_if_failed(); \
159 _request; \
160 })
161
162 #define gpiod_test_line_request_reconfigure_lines_or_fail(_request, _line_cfg) \
163 do { \
164 gint _ret = gpiod_line_request_reconfigure_lines(_request, \
165 _line_cfg); \
166 g_assert_cmpint(_ret, ==, 0); \
167 gpiod_test_return_if_failed(); \
168 } while (0)
169
170 #define gpiod_test_expect_errno(_expected) \
171 g_assert_cmpint((_expected), ==, errno)
172
173 #endif /* __GPIOD_TEST_HELPERS_H__ */