1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00
Files
libgpiod/tests/tests-misc.c
Bartosz Golaszewski d30bcd7b2e tests: update library API version string tests
The version string returned by gpiod_api_version() is now stricter and
will always be of the form: x.y.z where x, y and z are unsigned integer
numbers. We no longer have to parse various suffixes so update the test
cases tree-wide.

Acked-by: Erik Wierich <erik@riscstar.com>
Link: https://lore.kernel.org/r/20250903-api-version-detach-v1-4-027ca5928518@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-09-09 10:24:00 +02:00

101 lines
2.4 KiB
C

// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <bartekgola@gmail.com>
#include <errno.h>
#include <glib.h>
#include <gpiod.h>
#include <gpiod-test.h>
#include <gpiod-test-common.h>
#include <gpiosim-glib.h>
#include <unistd.h>
#include "helpers.h"
#define GPIOD_TEST_GROUP "misc"
GPIOD_TEST_CASE(is_gpiochip_bad)
{
g_assert_false(gpiod_is_gpiochip_device("/dev/null"));
g_assert_cmpint(errno, ==, 0);
g_assert_false(gpiod_is_gpiochip_device("/dev/nonexistent"));
g_assert_cmpint(errno, ==, 0);
}
GPIOD_TEST_CASE(is_gpiochip_good)
{
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL);
g_assert_true(gpiod_is_gpiochip_device(
g_gpiosim_chip_get_dev_path(sim)));
}
GPIOD_TEST_CASE(is_gpiochip_link_bad)
{
g_autofree gchar *link = NULL;
gint ret;
link = g_strdup_printf("/tmp/gpiod-test-link.%u", getpid());
ret = symlink("/dev/null", link);
g_assert_cmpint(ret, ==, 0);
gpiod_test_return_if_failed();
g_assert_false(gpiod_is_gpiochip_device(link));
ret = unlink(link);
g_assert_cmpint(ret, ==, 0);
}
GPIOD_TEST_CASE(is_gpiochip_link_good)
{
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL);
g_autofree gchar *link = NULL;
gint ret;
link = g_strdup_printf("/tmp/gpiod-test-link.%u", getpid());
ret = symlink(g_gpiosim_chip_get_dev_path(sim), link);
g_assert_cmpint(ret, ==, 0);
gpiod_test_return_if_failed();
g_assert_true(gpiod_is_gpiochip_device(link));
ret = unlink(link);
g_assert_cmpint(ret, ==, 0);
}
GPIOD_TEST_CASE(is_gpiochip_null_path)
{
g_assert_false(gpiod_is_gpiochip_device(NULL));
gpiod_test_expect_errno(0);
}
GPIOD_TEST_CASE(version_string)
{
static const gchar *const pattern = "^\\d+\\.\\d+\\.\\d+";
g_autoptr(GError) err = NULL;
g_autoptr(GRegex) regex = NULL;
g_autoptr(GMatchInfo) match = NULL;
g_autofree gchar *res = NULL;
const gchar *ver;
gboolean ret;
ver = gpiod_api_version();
g_assert_nonnull(ver);
gpiod_test_return_if_failed();
regex = g_regex_new(pattern, 0, 0, &err);
g_assert_nonnull(regex);
g_assert_no_error(err);
gpiod_test_return_if_failed();
ret = g_regex_match(regex, ver, 0, &match);
g_assert_true(ret);
gpiod_test_return_if_failed();
g_assert_true(g_match_info_matches(match));
res = g_match_info_fetch(match, 0);
g_assert_nonnull(res);
g_assert_cmpstr(res, ==, ver);
g_match_info_next(match, &err);
g_assert_no_error(err);
g_assert_false(g_match_info_matches(match));
}