| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <gpiod-glib.h> |
| 5 |
#include <gpiod-test.h> |
| 6 |
#include <gpiod-test-common.h> |
| 7 |
#include <gpiosim-glib.h> |
| 8 |
|
| 9 |
#include "helpers.h" |
| 10 |
|
| 11 |
#define GPIOD_TEST_GROUP "glib/line-config" |
| 12 |
|
| 13 |
GPIOD_TEST_CASE(too_many_lines) |
| 14 |
{ |
| 15 |
g_autoptr(GpiodglibLineSettings) settings = NULL; |
| 16 |
g_autoptr(GpiodglibLineConfig) config = NULL; |
| 17 |
g_autoptr(GArray) offsets = NULL; |
| 18 |
g_autoptr(GError) err = NULL; |
| 19 |
gboolean ret; |
| 20 |
guint i; |
| 21 |
|
| 22 |
settings = gpiodglib_line_settings_new(NULL); |
| 23 |
config = gpiodglib_line_config_new(); |
| 24 |
offsets = g_array_new(FALSE, TRUE, sizeof(guint)); |
| 25 |
|
| 26 |
for (i = 0; i < 65; i++) |
| 27 |
g_array_append_val(offsets, i); |
| 28 |
|
| 29 |
ret = gpiodglib_line_config_add_line_settings(config, offsets, |
| 30 |
settings, &err); |
| 31 |
g_assert_false(ret); |
| 32 |
g_assert_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_E2BIG); |
| 33 |
} |
| 34 |
|
| 35 |
GPIOD_TEST_CASE(get_line_settings) |
| 36 |
{ |
| 37 |
static const guint offset_vals[] = { 0, 1, 2, 3 }; |
| 38 |
|
| 39 |
g_autoptr(GpiodglibLineSettings) settings = NULL; |
| 40 |
g_autoptr(GpiodglibLineSettings) retrieved = NULL; |
| 41 |
g_autoptr(GpiodglibLineConfig) config = NULL; |
| 42 |
g_autoptr(GArray) offsets = NULL; |
| 43 |
|
| 44 |
settings = gpiodglib_line_settings_new( |
| 45 |
"direction", GPIODGLIB_LINE_DIRECTION_INPUT, |
| 46 |
"bias", GPIODGLIB_LINE_BIAS_PULL_DOWN, |
| 47 |
NULL); |
| 48 |
config = gpiodglib_line_config_new(); |
| 49 |
offsets = gpiodglib_test_array_from_const(offset_vals, 4, |
| 50 |
sizeof(guint)); |
| 51 |
|
| 52 |
gpiodglib_test_line_config_add_line_settings_or_fail(config, offsets, |
| 53 |
settings); |
| 54 |
|
| 55 |
retrieved = gpiodglib_test_line_config_get_line_settings_or_fail( |
| 56 |
config, 2); |
| 57 |
g_assert_cmpint(gpiodglib_line_settings_get_direction(retrieved), ==, |
| 58 |
GPIODGLIB_LINE_DIRECTION_INPUT); |
| 59 |
g_assert_cmpint(gpiodglib_line_settings_get_bias(retrieved), ==, |
| 60 |
GPIODGLIB_LINE_BIAS_PULL_DOWN); |
| 61 |
} |
| 62 |
|
| 63 |
GPIOD_TEST_CASE(null_settings) |
| 64 |
{ |
| 65 |
static const guint offset_vals[] = { 0, 1, 2, 3 }; |
| 66 |
|
| 67 |
g_autoptr(GpiodglibLineConfig) config = NULL; |
| 68 |
g_autoptr(GpiodglibLineSettings) settings = NULL; |
| 69 |
g_autoptr(GArray) offsets = NULL; |
| 70 |
|
| 71 |
config = gpiodglib_line_config_new(); |
| 72 |
offsets = gpiodglib_test_array_from_const(offset_vals, 4, |
| 73 |
sizeof(guint)); |
| 74 |
|
| 75 |
gpiodglib_test_line_config_add_line_settings_or_fail(config, offsets, |
| 76 |
NULL); |
| 77 |
|
| 78 |
settings = gpiodglib_test_line_config_get_line_settings_or_fail(config, |
| 79 |
2); |
| 80 |
|
| 81 |
g_assert_cmpint(gpiodglib_line_settings_get_drive(settings), ==, |
| 82 |
GPIODGLIB_LINE_DIRECTION_AS_IS); |
| 83 |
} |
| 84 |
|
| 85 |
GPIOD_TEST_CASE(null_offsets) |
| 86 |
{ |
| 87 |
g_autoptr(GpiodglibLineConfig) config = NULL; |
| 88 |
g_autoptr(GpiodglibLineSettings) settings = NULL; |
| 89 |
g_autoptr(GArray) offsets = NULL; |
| 90 |
g_autoptr(GError) err = NULL; |
| 91 |
gboolean ret; |
| 92 |
|
| 93 |
settings = gpiodglib_line_settings_new(NULL); |
| 94 |
config = gpiodglib_line_config_new(); |
| 95 |
offsets = g_array_new(FALSE, TRUE, sizeof(guint)); |
| 96 |
|
| 97 |
ret = gpiodglib_line_config_add_line_settings(config, NULL, settings, |
| 98 |
&err); |
| 99 |
g_assert_false(ret); |
| 100 |
g_assert_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL); |
| 101 |
} |
| 102 |
|
| 103 |
GPIOD_TEST_CASE(zero_offsets) |
| 104 |
{ |
| 105 |
g_autoptr(GpiodglibLineConfig) config = NULL; |
| 106 |
g_autoptr(GpiodglibLineSettings) settings = NULL; |
| 107 |
g_autoptr(GArray) offsets = NULL; |
| 108 |
g_autoptr(GError) err = NULL; |
| 109 |
gboolean ret; |
| 110 |
|
| 111 |
settings = gpiodglib_line_settings_new(NULL); |
| 112 |
config = gpiodglib_line_config_new(); |
| 113 |
offsets = g_array_new(FALSE, TRUE, sizeof(guint)); |
| 114 |
|
| 115 |
ret = gpiodglib_line_config_add_line_settings(config, offsets, settings, |
| 116 |
&err); |
| 117 |
g_assert_false(ret); |
| 118 |
g_assert_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL); |
| 119 |
} |
| 120 |
|
| 121 |
GPIOD_TEST_CASE(set_global_output_values) |
| 122 |
{ |
| 123 |
static const guint offset_vals[] = { 0, 1, 2, 3 }; |
| 124 |
static const GpiodglibLineValue output_values[] = { |
| 125 |
GPIODGLIB_LINE_VALUE_ACTIVE, |
| 126 |
GPIODGLIB_LINE_VALUE_INACTIVE, |
| 127 |
GPIODGLIB_LINE_VALUE_ACTIVE, |
| 128 |
GPIODGLIB_LINE_VALUE_INACTIVE, |
| 129 |
}; |
| 130 |
|
| 131 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); |
| 132 |
g_autoptr(GpiodglibChip) chip = NULL; |
| 133 |
g_autoptr(GpiodglibLineConfig) config = NULL; |
| 134 |
g_autoptr(GpiodglibLineSettings) settings = NULL; |
| 135 |
g_autoptr(GArray) offsets = NULL; |
| 136 |
g_autoptr(GArray) values = NULL; |
| 137 |
g_autoptr(GpiodglibLineRequest) request = NULL; |
| 138 |
|
| 139 |
chip = gpiodglib_test_new_chip_or_fail( |
| 140 |
g_gpiosim_chip_get_dev_path(sim)); |
| 141 |
settings = gpiodglib_line_settings_new("direction", |
| 142 |
GPIODGLIB_LINE_DIRECTION_OUTPUT, |
| 143 |
NULL); |
| 144 |
config = gpiodglib_line_config_new(); |
| 145 |
offsets = gpiodglib_test_array_from_const(offset_vals, 4, |
| 146 |
sizeof(guint)); |
| 147 |
values = gpiodglib_test_array_from_const(output_values, 4, |
| 148 |
sizeof(GpiodglibLineValue)); |
| 149 |
|
| 150 |
gpiodglib_test_line_config_add_line_settings_or_fail(config, offsets, |
| 151 |
settings); |
| 152 |
gpiodglib_test_line_config_set_output_values_or_fail(config, values); |
| 153 |
|
| 154 |
request = gpiodglib_test_chip_request_lines_or_fail(chip, NULL, |
| 155 |
config); |
| 156 |
|
| 157 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==, |
| 158 |
G_GPIOSIM_VALUE_ACTIVE); |
| 159 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==, |
| 160 |
G_GPIOSIM_VALUE_INACTIVE); |
| 161 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==, |
| 162 |
G_GPIOSIM_VALUE_ACTIVE); |
| 163 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, |
| 164 |
G_GPIOSIM_VALUE_INACTIVE); |
| 165 |
} |
| 166 |
|
| 167 |
GPIOD_TEST_CASE(handle_duplicate_offsets) |
| 168 |
{ |
| 169 |
static const guint offset_vals[] = { 0, 2, 2, 3 }; |
| 170 |
|
| 171 |
g_autoptr(GpiodglibLineConfig) config = NULL; |
| 172 |
g_autoptr(GArray) offsets = NULL; |
| 173 |
g_autoptr(GArray) retrieved = NULL; |
| 174 |
|
| 175 |
config = gpiodglib_line_config_new(); |
| 176 |
offsets = gpiodglib_test_array_from_const(offset_vals, 4, |
| 177 |
sizeof(guint)); |
| 178 |
|
| 179 |
gpiodglib_test_line_config_add_line_settings_or_fail(config, offsets, |
| 180 |
NULL); |
| 181 |
|
| 182 |
retrieved = gpiodglib_line_config_get_configured_offsets(config); |
| 183 |
g_assert_cmpuint(retrieved->len, ==, 3); |
| 184 |
g_assert_cmpuint(g_array_index(retrieved, guint, 0), ==, 0); |
| 185 |
g_assert_cmpuint(g_array_index(retrieved, guint, 1), ==, 2); |
| 186 |
g_assert_cmpuint(g_array_index(retrieved, guint, 2), ==, 3); |
| 187 |
} |