tests: add a chip iterator test case
authorBartosz Golaszewski <bartekgola@gmail.com>
Mon, 20 Feb 2017 14:57:24 +0000 (15:57 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 20 Feb 2017 15:03:36 +0000 (16:03 +0100)
Add a test case checking the chip iterators and the accompanying
gpiod_foreach_chip() macro.

Since we're adding a new file: make the cleanup helpers reusable from
all test files.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
tests/unit/Makefile.am
tests/unit/gpiod-unit.c
tests/unit/gpiod-unit.h
tests/unit/tests-chip.c
tests/unit/tests-iter.c [new file with mode: 0644]

index b92e101b1e7013306c53c5dded93051e8f6c29cb..260b22e612d0253102eb8617cf01b61cd3126e37 100644 (file)
@@ -13,4 +13,4 @@ DEPENDENCIES = libgpiod.la
 
 check_PROGRAMS = gpiod-unit
 
-gpiod_unit_SOURCES = gpiod-unit.c tests-chip.c
+gpiod_unit_SOURCES = gpiod-unit.c tests-chip.c tests-iter.c
index a5b77bb74d646ea3e6e6025a7fac46475c3b0012..04995c1a86a9267e9a176f0ed31839481ee77428 100644 (file)
@@ -402,3 +402,21 @@ int main(int argc GU_UNUSED, char **argv GU_UNUSED)
 
        return globals.tests_failed ? EXIT_FAILURE : EXIT_SUCCESS;
 }
+
+void gu_close_chip(struct gpiod_chip **chip)
+{
+       if (*chip)
+               gpiod_chip_close(*chip);
+}
+
+void gu_free_str(char **str)
+{
+       if (*str)
+               free(*str);
+}
+
+void gu_free_chip_iter(struct gpiod_chip_iter **iter)
+{
+       if (*iter)
+               gpiod_chip_iter_free(*iter);
+}
index ffefabe5976be170cf5a1c07a0759be45715d1b2..4c0aff9ebd6598399a07eea4add0b7a2dac6a21d 100644 (file)
@@ -11,9 +11,8 @@
 #ifndef __GPIOD_UNIT_H__
 #define __GPIOD_UNIT_H__
 
-#include <stdlib.h>
+#include <gpiod.h>
 #include <string.h>
-#include <stdbool.h>
 
 #define GU_INIT                        __attribute__((constructor))
 #define GU_UNUSED              __attribute__((unused))
@@ -74,6 +73,10 @@ const char * gu_chip_path(unsigned int index);
 const char * gu_chip_name(unsigned int index);
 unsigned int gu_chip_num(unsigned int index);
 
+void gu_close_chip(struct gpiod_chip **chip);
+void gu_free_str(char **str);
+void gu_free_chip_iter(struct gpiod_chip_iter **iter);
+
 #define GU_ASSERT(statement)                                           \
        do {                                                            \
                if (!(statement)) {                                     \
index 514da800b54473771526442b7fdcf484778608f3..bfba6f798869d729bf5d3c471451354a375774be 100644 (file)
@@ -9,26 +9,13 @@
  */
 
 #include "gpiod-unit.h"
-#include <gpiod.h>
 
 #include <stdio.h>
 #include <errno.h>
 
-static void close_chip(struct gpiod_chip **chip)
-{
-       if (*chip)
-               gpiod_chip_close(*chip);
-}
-
-static void free_str(char **str)
-{
-       if (*str)
-               free(*str);
-}
-
 static void chip_open_good(void)
 {
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip = NULL;
 
        chip = gpiod_chip_open(gu_chip_path(0));
        GU_ASSERT_NOT_NULL(chip);
@@ -63,7 +50,7 @@ GU_DEFINE_TEST(chip_open_notty,
 
 static void chip_open_by_number_good(void)
 {
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip = NULL;
 
        chip = gpiod_chip_open_by_number(gu_chip_num(0));
        GU_ASSERT_NOT_NULL(chip);
@@ -74,10 +61,10 @@ GU_DEFINE_TEST(chip_open_by_number_good,
 
 static void chip_open_lookup(void)
 {
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip_by_name = NULL;
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip_by_path = NULL;
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip_by_num = NULL;
-       GU_CLEANUP(free_str) char *chip_num;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_name = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_path = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_num = NULL;
+       GU_CLEANUP(gu_free_str) char *chip_num;
 
        GU_ASSERT(asprintf(&chip_num, "%u", gu_chip_num(0)) > 0);
 
@@ -95,9 +82,9 @@ GU_DEFINE_TEST(chip_open_lookup,
 
 static void chip_name(void)
 {
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip0 = NULL;
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip1 = NULL;
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip2 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip0 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip1 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip2 = NULL;
 
        chip0 = gpiod_chip_open(gu_chip_path(0));
        chip1 = gpiod_chip_open(gu_chip_path(1));
@@ -114,9 +101,9 @@ GU_DEFINE_TEST(chip_name, "gpiod_chip_name()", GU_LINES_UNNAMED, { 8, 8, 8 });
 
 static void chip_label(void)
 {
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip0 = NULL;
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip1 = NULL;
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip2 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip0 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip1 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip2 = NULL;
 
        chip0 = gpiod_chip_open(gu_chip_path(0));
        chip1 = gpiod_chip_open(gu_chip_path(1));
@@ -134,11 +121,11 @@ GU_DEFINE_TEST(chip_label, "gpiod_chip_label()",
 
 static void chip_num_lines(void)
 {
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip0 = NULL;
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip1 = NULL;
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip2 = NULL;
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip3 = NULL;
-       GU_CLEANUP(close_chip) struct gpiod_chip *chip4 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip0 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip1 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip2 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip3 = NULL;
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip4 = NULL;
 
        chip0 = gpiod_chip_open(gu_chip_path(0));
        chip1 = gpiod_chip_open(gu_chip_path(1));
diff --git a/tests/unit/tests-iter.c b/tests/unit/tests-iter.c
new file mode 100644 (file)
index 0000000..4bc5574
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * GPIO chip test cases for libgpiod.
+ *
+ * Copyright (C) 2017 Bartosz Golaszewski <bartekgola@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License
+ * as published by the Free Software Foundation.
+ */
+
+#include "gpiod-unit.h"
+
+static void chip_iter(void)
+{
+       GU_CLEANUP(gu_free_chip_iter) struct gpiod_chip_iter *iter = NULL;
+       struct gpiod_chip *chip;
+       bool A, B, C;
+
+       A = B = C = false;
+
+       iter = gpiod_chip_iter_new();
+       GU_ASSERT_NOT_NULL(iter);
+
+       gpiod_foreach_chip(iter, chip) {
+               GU_ASSERT(!gpiod_chip_iter_err(iter));
+
+               if (strcmp(gpiod_chip_label(chip), "gpio-mockup-A") == 0)
+                       A = true;
+               else if (strcmp(gpiod_chip_label(chip), "gpio-mockup-B") == 0)
+                       B = true;
+               else if (strcmp(gpiod_chip_label(chip), "gpio-mockup-C") == 0)
+                       C = true;
+       }
+
+       GU_ASSERT(A);
+       GU_ASSERT(B);
+       GU_ASSERT(C);
+}
+GU_DEFINE_TEST(chip_iter, "gpiod_chip iterator",
+              GU_LINES_UNNAMED, { 8, 8, 8 });