mirror of
https://github.com/libratbag/libratbag.git
synced 2026-11-07 15:14:38 +01:00
test: add basic context init tests
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -42,4 +42,5 @@ test-build-linker
|
||||
test-build-pedantic-c99
|
||||
test-build-std-gnuc90
|
||||
test-suite.log
|
||||
test-context
|
||||
ratbag-command
|
||||
|
||||
@@ -9,7 +9,8 @@ AM_CXXFLAGS = $(GCC_CXXFLAGS)
|
||||
|
||||
TEST_LIBS = $(CHECK_LIBS) $(LIBEVDEV_LIBS) $(top_builddir)/src/libratbag.la
|
||||
|
||||
run_tests =
|
||||
run_tests = \
|
||||
test-context
|
||||
|
||||
build_tests = \
|
||||
test-build-cxx \
|
||||
@@ -17,10 +18,14 @@ build_tests = \
|
||||
test-build-pedantic-c99 \
|
||||
test-build-std-gnuc90
|
||||
|
||||
noinst_PROGRAMS = $(build_tests)
|
||||
noinst_PROGRAMS = $(build_tests) $(run_tests)
|
||||
noinst_SCRIPTS = symbols-leak-test
|
||||
TESTS = $(run_tests) symbols-leak-test
|
||||
|
||||
test_context_SOURCES = test-context.c
|
||||
test_context_LDADD = $(TEST_LIBS)
|
||||
test_context_LDFLAGS = -no-install
|
||||
|
||||
# build-test only
|
||||
test_build_pedantic_c99_SOURCES = build-pedantic.c
|
||||
test_build_pedantic_c99_CFLAGS = -std=c99 -pedantic -Werror
|
||||
|
||||
148
test/test-context.c
Normal file
148
test/test-context.c
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright © 2015 Red Hat, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <check.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "libratbag.h"
|
||||
|
||||
static int
|
||||
open_restricted(const char *path, int flags, void *user_data)
|
||||
{
|
||||
int fd = open(path, flags);
|
||||
|
||||
if (fd < 0)
|
||||
fprintf(stderr, "Failed to open %s (%s)\n",
|
||||
path, strerror(errno));
|
||||
|
||||
return fd < 0 ? -errno : fd;
|
||||
}
|
||||
|
||||
static void
|
||||
close_restricted(int fd, void *user_data)
|
||||
{
|
||||
close(fd);
|
||||
}
|
||||
|
||||
struct libratbag_interface simple_iface = {
|
||||
.open_restricted = open_restricted,
|
||||
.close_restricted = close_restricted,
|
||||
};
|
||||
|
||||
START_TEST(context_init_NULL)
|
||||
{
|
||||
struct libratbag *lr;
|
||||
lr = libratbag_create_context(NULL, NULL);
|
||||
ck_assert(lr == NULL);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(context_init_bad_iface)
|
||||
{
|
||||
struct libratbag *lr;
|
||||
struct libratbag_interface iface = {
|
||||
.open_restricted = NULL,
|
||||
.close_restricted = NULL,
|
||||
};
|
||||
|
||||
lr = libratbag_create_context(&iface, NULL);
|
||||
ck_assert(lr == NULL);
|
||||
|
||||
iface.open_restricted = open_restricted;
|
||||
lr = libratbag_create_context(&iface, NULL);
|
||||
ck_assert(lr == NULL);
|
||||
|
||||
iface.open_restricted = NULL;
|
||||
iface.close_restricted = close_restricted;
|
||||
lr = libratbag_create_context(&iface, NULL);
|
||||
ck_assert(lr == NULL);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(context_init)
|
||||
{
|
||||
struct libratbag *lr;
|
||||
|
||||
lr = libratbag_create_context(&simple_iface, NULL);
|
||||
ck_assert(lr != NULL);
|
||||
libratbag_unref(lr);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(context_ref)
|
||||
{
|
||||
struct libratbag *lr;
|
||||
struct libratbag *lr2;
|
||||
|
||||
lr = libratbag_create_context(&simple_iface, NULL);
|
||||
ck_assert(lr != NULL);
|
||||
|
||||
lr2 = libratbag_ref(lr);
|
||||
ck_assert_ptr_eq(lr, lr2);
|
||||
|
||||
lr2 = libratbag_unref(lr2);
|
||||
ck_assert_ptr_eq(lr2, lr);
|
||||
|
||||
lr2 = libratbag_unref(lr2);
|
||||
ck_assert_ptr_eq(lr2, NULL);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
static Suite *
|
||||
test_context_suite(void)
|
||||
{
|
||||
TCase *tc;
|
||||
Suite *s;
|
||||
|
||||
s = suite_create("context");
|
||||
tc = tcase_create("init");
|
||||
tcase_add_test(tc, context_init_NULL);
|
||||
tcase_add_test(tc, context_init_bad_iface);
|
||||
tcase_add_test(tc, context_init);
|
||||
tcase_add_test(tc, context_ref);
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int nfailed;
|
||||
Suite *s;
|
||||
SRunner *sr;
|
||||
|
||||
s = test_context_suite();
|
||||
sr = srunner_create(s);
|
||||
|
||||
srunner_run_all(sr, CK_ENV);
|
||||
nfailed = srunner_ntests_failed(sr);
|
||||
srunner_free(sr);
|
||||
|
||||
return (nfailed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
Reference in New Issue
Block a user