tests: add line value enums for gpiosim GLib wrapper
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Mon, 13 Feb 2023 10:53:42 +0000 (11:53 +0100)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Wed, 15 Feb 2023 12:29:09 +0000 (13:29 +0100)
libgpiosim has a dedicated enum type for line values but the GLib wrapper
still uses magic numbers (0 and 1). Add an enum type for the wrapper and
use it across all test cases.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
tests/gpiod-test-sim.c
tests/gpiod-test-sim.h
tests/tests-line-config.c
tests/tests-line-request.c

index 39398256ed58622e9457d11148f316b38d051324..161a09f7708b1aaab3d9612c346d113b6e47ddb7 100644 (file)
@@ -416,25 +416,32 @@ const gchar *g_gpiosim_chip_get_name(GPIOSimChip *self)
        return g_gpiosim_chip_get_string_prop(self, "name");
 }
 
-gint _g_gpiosim_chip_get_value(GPIOSimChip *chip, guint offset, GError **err)
+GPIOSimValue
+_g_gpiosim_chip_get_value(GPIOSimChip *chip, guint offset, GError **err)
 {
-       gint val;
+       enum gpiosim_value val;
 
        val = gpiosim_bank_get_value(chip->bank, offset);
-       if (val < 0) {
+       switch (val) {
+       case GPIOSIM_VALUE_ERROR:
                g_set_error(err, G_GPIOSIM_ERROR,
                            G_GPIOSIM_ERR_GET_VALUE_FAILED,
                            "Unable to read the line value: %s",
                            g_strerror(errno));
-               return -1;
+               return G_GPIOSIM_VALUE_ERROR;
+       case GPIOSIM_VALUE_INACTIVE:
+               return G_GPIOSIM_VALUE_INACTIVE;
+       case GPIOSIM_VALUE_ACTIVE:
+               return G_GPIOSIM_VALUE_ACTIVE;
        }
 
-       return val;
+       g_error("Invalid line value returned by gpiosim");
 }
 
 void g_gpiosim_chip_set_pull(GPIOSimChip *chip, guint offset, GPIOSimPull pull)
 {
-       gint ret, sim_pull;
+       enum gpiosim_pull sim_pull;
+       gint ret;
 
        switch (pull) {
        case G_GPIOSIM_PULL_DOWN:
index 1293ea7d520f9c87e9d7c265999cb4340f6bc98a..f6a4bf03be30ae0f6d1f952b79cbc4485b6964ee 100644 (file)
 
 G_BEGIN_DECLS
 
+typedef enum {
+       G_GPIOSIM_VALUE_ERROR = -1,
+       G_GPIOSIM_VALUE_INACTIVE = 0,
+       G_GPIOSIM_VALUE_ACTIVE = 1,
+} GPIOSimValue;
+
 typedef enum {
        G_GPIOSIM_PULL_UP = 1,
        G_GPIOSIM_PULL_DOWN,
@@ -54,7 +60,8 @@ G_DECLARE_FINAL_TYPE(GPIOSimChip, g_gpiosim_chip, G_GPIOSIM, CHIP, GObject);
 const gchar *g_gpiosim_chip_get_dev_path(GPIOSimChip *self);
 const gchar *g_gpiosim_chip_get_name(GPIOSimChip *self);
 
-gint _g_gpiosim_chip_get_value(GPIOSimChip *self, guint offset, GError **err);
+GPIOSimValue
+_g_gpiosim_chip_get_value(GPIOSimChip *self, guint offset, GError **err);
 void g_gpiosim_chip_set_pull(GPIOSimChip *self, guint offset, GPIOSimPull pull);
 
 #define g_gpiosim_chip_get_value(self, offset) \
index 78c4d6b76ce0296abbf7acbe8a0ccd6663240a0c..446cccb6ca509d22a96434fcf0f113a403d3aba5 100644 (file)
@@ -310,13 +310,13 @@ GPIOD_TEST_CASE(set_global_output_values)
        request = gpiod_test_request_lines_or_fail(chip, NULL, config);
 
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==,
-                       GPIOD_LINE_VALUE_ACTIVE);
+                       G_GPIOSIM_VALUE_ACTIVE);
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==,
-                       GPIOD_LINE_VALUE_INACTIVE);
+                       G_GPIOSIM_VALUE_INACTIVE);
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==,
-                       GPIOD_LINE_VALUE_ACTIVE);
+                       G_GPIOSIM_VALUE_ACTIVE);
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==,
-                       GPIOD_LINE_VALUE_INACTIVE);
+                       G_GPIOSIM_VALUE_INACTIVE);
 }
 
 GPIOD_TEST_CASE(read_back_global_output_values)
index 49e97915c5db3573737e76ec346c773e4ff70074..89402116182727f0fb06d24799f46c3350de3f94 100644 (file)
@@ -158,10 +158,10 @@ GPIOD_TEST_CASE(default_output_value)
 
        for (i = 0; i < 4; i++)
                g_assert_cmpint(g_gpiosim_chip_get_value(sim, offsets[i]), ==,
-                               GPIOD_LINE_VALUE_ACTIVE);
+                               G_GPIOSIM_VALUE_ACTIVE);
 
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==,
-                       GPIOD_LINE_VALUE_INACTIVE);
+                       G_GPIOSIM_VALUE_INACTIVE);
 }
 
 GPIOD_TEST_CASE(read_all_values)
@@ -243,6 +243,13 @@ GPIOD_TEST_CASE(set_all_values)
                GPIOD_LINE_VALUE_ACTIVE,
                GPIOD_LINE_VALUE_ACTIVE
        };
+       static const enum gpiod_line_value sim_values[] = {
+               G_GPIOSIM_VALUE_ACTIVE,
+               G_GPIOSIM_VALUE_INACTIVE,
+               G_GPIOSIM_VALUE_ACTIVE,
+               G_GPIOSIM_VALUE_ACTIVE,
+               G_GPIOSIM_VALUE_ACTIVE
+       };
 
        g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
        g_autoptr(struct_gpiod_chip) chip = NULL;
@@ -269,7 +276,7 @@ GPIOD_TEST_CASE(set_all_values)
 
        for (i = 0; i < 5; i++)
                g_assert_cmpint(g_gpiosim_chip_get_value(sim, offsets[i]), ==,
-                               values[i]);
+                               sim_values[i]);
 }
 
 GPIOD_TEST_CASE(set_values_subset_of_lines)
@@ -306,11 +313,11 @@ GPIOD_TEST_CASE(set_values_subset_of_lines)
        gpiod_test_return_if_failed();
 
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==,
-                       GPIOD_LINE_VALUE_ACTIVE);
+                       G_GPIOSIM_VALUE_ACTIVE);
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==,
-                       GPIOD_LINE_VALUE_INACTIVE);
+                       G_GPIOSIM_VALUE_INACTIVE);
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==,
-                       GPIOD_LINE_VALUE_ACTIVE);
+                       G_GPIOSIM_VALUE_ACTIVE);
 }
 
 GPIOD_TEST_CASE(set_line_after_requesting)
@@ -338,10 +345,14 @@ GPIOD_TEST_CASE(set_line_after_requesting)
 
        gpiod_line_request_set_value(request, 1, GPIOD_LINE_VALUE_ACTIVE);
 
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==, 0);
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==, 1);
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, 0);
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 4), ==, 0);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==,
+                       G_GPIOSIM_VALUE_INACTIVE);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==,
+                       G_GPIOSIM_VALUE_ACTIVE);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==,
+                       G_GPIOSIM_VALUE_INACTIVE);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 4), ==,
+                       G_GPIOSIM_VALUE_INACTIVE);
 }
 
 GPIOD_TEST_CASE(request_survives_parent_chip)
@@ -442,7 +453,8 @@ GPIOD_TEST_CASE(active_low_read_value)
        value = gpiod_line_request_get_value(request, 2);
        g_assert_cmpint(value, ==, GPIOD_LINE_VALUE_ACTIVE);
 
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, 0);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==,
+                       G_GPIOSIM_VALUE_INACTIVE);
 }
 
 GPIOD_TEST_CASE(reconfigure_lines)
@@ -478,10 +490,14 @@ GPIOD_TEST_CASE(reconfigure_lines)
 
        request = gpiod_test_request_lines_or_fail(chip, NULL, line_cfg);
 
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==, 1);
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==, 0);
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==, 1);
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, 0);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==,
+                       G_GPIOSIM_VALUE_ACTIVE);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==,
+                       G_GPIOSIM_VALUE_INACTIVE);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==,
+                       G_GPIOSIM_VALUE_ACTIVE);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==,
+                       G_GPIOSIM_VALUE_INACTIVE);
 
        gpiod_line_config_reset(line_cfg);
 
@@ -501,10 +517,14 @@ GPIOD_TEST_CASE(reconfigure_lines)
        g_assert_cmpint(ret, ==, 0);
        gpiod_test_return_if_failed();
 
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==, 0);
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==, 1);
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==, 0);
-       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, 1);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==,
+                       G_GPIOSIM_VALUE_INACTIVE);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==,
+                       G_GPIOSIM_VALUE_ACTIVE);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==,
+                       G_GPIOSIM_VALUE_INACTIVE);
+       g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==,
+                       G_GPIOSIM_VALUE_ACTIVE);
 }
 
 GPIOD_TEST_CASE(request_lines_with_unordered_offsets)
@@ -543,17 +563,17 @@ GPIOD_TEST_CASE(request_lines_with_unordered_offsets)
        gpiod_line_request_set_values_subset(request, 4, set_offsets, values);
 
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==,
-                       GPIOD_LINE_VALUE_INACTIVE);
+                       G_GPIOSIM_VALUE_INACTIVE);
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==,
-                       GPIOD_LINE_VALUE_ACTIVE);
+                       G_GPIOSIM_VALUE_ACTIVE);
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==,
-                       GPIOD_LINE_VALUE_ACTIVE);
+                       G_GPIOSIM_VALUE_ACTIVE);
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 5), ==,
-                       GPIOD_LINE_VALUE_ACTIVE);
+                       G_GPIOSIM_VALUE_ACTIVE);
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 6), ==,
-                       GPIOD_LINE_VALUE_INACTIVE);
+                       G_GPIOSIM_VALUE_INACTIVE);
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 7), ==,
-                       GPIOD_LINE_VALUE_INACTIVE);
+                       G_GPIOSIM_VALUE_INACTIVE);
 }
 
 GPIOD_TEST_CASE(request_with_bias_set_to_pull_up)
@@ -578,7 +598,7 @@ GPIOD_TEST_CASE(request_with_bias_set_to_pull_up)
        request = gpiod_test_request_lines_or_fail(chip, NULL, line_cfg);
 
        g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==,
-                       GPIOD_LINE_VALUE_ACTIVE);
+                       G_GPIOSIM_VALUE_ACTIVE);
 }
 
 GPIOD_TEST_CASE(get_requested_offsets_less_and_more)