From 95fa063da2b7cce7486480ed80c70b45f298dff7 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Fri, 24 Feb 2017 11:40:37 +0100 Subject: [PATCH] tests: add test cases for the high-level API For now we only test the get and set operations. We'll add event tests once the support for that is merged in the kernel. Signed-off-by: Bartosz Golaszewski --- tests/unit/Makefile.am | 6 +- tests/unit/tests-simple-api.c | 116 ++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/unit/tests-simple-api.c diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 6d72d16..6b49d33 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -13,4 +13,8 @@ DEPENDENCIES = libgpiod.la check_PROGRAMS = gpiod-unit -gpiod_unit_SOURCES = gpiod-unit.c tests-chip.c tests-iter.c tests-line.c +gpiod_unit_SOURCES = gpiod-unit.c \ + tests-chip.c \ + tests-iter.c \ + tests-line.c \ + tests-simple-api.c diff --git a/tests/unit/tests-simple-api.c b/tests/unit/tests-simple-api.c new file mode 100644 index 0000000..b0a8f3e --- /dev/null +++ b/tests/unit/tests-simple-api.c @@ -0,0 +1,116 @@ +/* + * Simple API test cases for libgpiod. + * + * Copyright (C) 2017 Bartosz Golaszewski + * + * 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 simple_set_get_value(void) +{ + int ret; + + ret = gpiod_simple_get_value("gpiod-unit", gu_chip_name(0), 3, false); + GU_ASSERT_EQ(ret, 0); + + ret = gpiod_simple_set_value("gpiod-unit", gu_chip_name(0), + 3, 1, false, NULL, NULL); + GU_ASSERT_RET_OK(ret); + + ret = gpiod_simple_get_value("gpiod-unit", gu_chip_name(0), 3, false); + GU_ASSERT_EQ(ret, 1); +} +GU_DEFINE_TEST(simple_set_get_value, + "simple set/get value", + GU_LINES_UNNAMED, { 8 }); + +static void simple_set_get_value_multiple(void) +{ + unsigned int offsets[] = { 0, 1, 2, 3, 4, 5, 6, 12, 13, 15 }; + int values[10], ret; + + ret = gpiod_simple_get_value_multiple("gpiod-unit", gu_chip_name(0), + offsets, values, 10, false); + GU_ASSERT_RET_OK(ret); + + GU_ASSERT_EQ(values[0], 0); + GU_ASSERT_EQ(values[1], 0); + GU_ASSERT_EQ(values[2], 0); + GU_ASSERT_EQ(values[3], 0); + GU_ASSERT_EQ(values[4], 0); + GU_ASSERT_EQ(values[5], 0); + GU_ASSERT_EQ(values[6], 0); + GU_ASSERT_EQ(values[7], 0); + GU_ASSERT_EQ(values[8], 0); + GU_ASSERT_EQ(values[9], 0); + + values[0] = 1; + values[1] = 1; + values[2] = 1; + values[3] = 0; + values[4] = 0; + values[5] = 1; + values[6] = 0; + values[7] = 1; + values[8] = 0; + values[9] = 0; + + ret = gpiod_simple_set_value_multiple("gpiod-unit", gu_chip_name(0), + offsets, values, 10, false, + NULL, NULL); + GU_ASSERT_RET_OK(ret); + + ret = gpiod_simple_get_value_multiple("gpiod-unit", gu_chip_name(0), + offsets, values, 10, false); + GU_ASSERT_RET_OK(ret); + + GU_ASSERT_EQ(values[0], 1); + GU_ASSERT_EQ(values[1], 1); + GU_ASSERT_EQ(values[2], 1); + GU_ASSERT_EQ(values[3], 0); + GU_ASSERT_EQ(values[4], 0); + GU_ASSERT_EQ(values[5], 1); + GU_ASSERT_EQ(values[6], 0); + GU_ASSERT_EQ(values[7], 1); + GU_ASSERT_EQ(values[8], 0); + GU_ASSERT_EQ(values[9], 0); +} +GU_DEFINE_TEST(simple_set_get_value_multiple, + "simple set/get value multiple", + GU_LINES_UNNAMED, { 16 }); + +static void simple_get_value_multiple_max_lines(void) +{ + unsigned int offsets[GPIOD_REQUEST_MAX_LINES + 1]; + int values[GPIOD_REQUEST_MAX_LINES + 1], ret; + + ret = gpiod_simple_get_value_multiple("gpiod-unit", gu_chip_name(0), + offsets, values, + GPIOD_REQUEST_MAX_LINES + 1, + false); + GU_ASSERT_NOTEQ(ret, 0); + GU_ASSERT_EQ(gpiod_errno(), GPIOD_ELINEMAX); +} +GU_DEFINE_TEST(simple_get_value_multiple_max_lines, + "gpiod_simple_get_value_multiple() max lines", + GU_LINES_UNNAMED, { 128 }); + +static void simple_set_value_multiple_max_lines(void) +{ + unsigned int offsets[GPIOD_REQUEST_MAX_LINES + 1]; + int values[GPIOD_REQUEST_MAX_LINES + 1], ret; + + ret = gpiod_simple_set_value_multiple("gpiod-unit", gu_chip_name(0), + offsets, values, + GPIOD_REQUEST_MAX_LINES + 1, + false, NULL, NULL); + GU_ASSERT_NOTEQ(ret, 0); + GU_ASSERT_EQ(gpiod_errno(), GPIOD_ELINEMAX); +} +GU_DEFINE_TEST(simple_set_value_multiple_max_lines, + "gpiod_simple_set_value_multiple() max lines", + GU_LINES_UNNAMED, { 128 }); -- 2.30.2