From 39550fe8d4457b9df7ac197b468c764f020930f3 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Thu, 25 Mar 2021 11:01:17 -0400 Subject: [PATCH] Add unit tests for utils macros Change-Id: I1908e7031f8b4493813f7a5fe6ac1ab90d062afc Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- .gitignore | 2 + configure.ac | 1 + tests/Makefile.am | 8 +- tests/unit/Makefile.am | 3 +- tests/unit/ust-utils/Makefile.am | 15 +++ tests/unit/ust-utils/ust-utils-common.h | 128 ++++++++++++++++++++++++ tests/unit/ust-utils/ust-utils-cxx.cpp | 19 ++++ tests/unit/ust-utils/ust-utils.c | 17 ++++ 8 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 tests/unit/ust-utils/Makefile.am create mode 100644 tests/unit/ust-utils/ust-utils-common.h create mode 100644 tests/unit/ust-utils/ust-utils-cxx.cpp create mode 100644 tests/unit/ust-utils/ust-utils.c diff --git a/.gitignore b/.gitignore index 00588540..8732df01 100644 --- a/.gitignore +++ b/.gitignore @@ -86,6 +86,8 @@ tests/unit/pthread_name/test_pthread_name tests/unit/snprintf/test_snprintf tests/unit/ust-elf/ust-elf tests/unit/ust-error/test_ust_error +tests/unit/ust-utils/test_ust_utils +tests/unit/ust-utils/test_ust_utils_cxx # Java agent library *.class diff --git a/configure.ac b/configure.ac index 5e4f8671..6bb13b88 100644 --- a/configure.ac +++ b/configure.ac @@ -563,6 +563,7 @@ AC_CONFIG_FILES([ tests/unit/snprintf/Makefile tests/unit/ust-elf/Makefile tests/unit/ust-error/Makefile + tests/unit/ust-utils/Makefile tests/utils/Makefile lttng-ust.pc lttng-ust-ctl.pc diff --git a/tests/Makefile.am b/tests/Makefile.am index d3ba1f63..b2c1c2b5 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -15,7 +15,13 @@ TESTS = \ unit/pthread_name/test_pthread_name \ unit/snprintf/test_snprintf \ unit/ust-elf/test_ust_elf \ - unit/ust-error/test_ust_error + unit/ust-error/test_ust_error \ + unit/ust-utils/test_ust_utils + +if HAVE_CXX +TESTS += \ + unit/ust-utils/test_ust_utils_cxx +endif EXTRA_DIST = README diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index c8f52c3c..47cec612 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -7,4 +7,5 @@ SUBDIRS = \ pthread_name \ snprintf \ ust-elf \ - ust-error + ust-error \ + ust-utils diff --git a/tests/unit/ust-utils/Makefile.am b/tests/unit/ust-utils/Makefile.am new file mode 100644 index 00000000..ec9ebe23 --- /dev/null +++ b/tests/unit/ust-utils/Makefile.am @@ -0,0 +1,15 @@ +# SPDX-License-Identifier: LGPL-2.1-only + +AM_CPPFLAGS += -I$(top_srcdir)/tests/utils + +noinst_PROGRAMS = test_ust_utils +test_ust_utils_SOURCES = ust-utils.c ust-utils-common.h +test_ust_utils_LDADD = $(top_builddir)/liblttng-ust/liblttng-ust.la \ + $(top_builddir)/tests/utils/libtap.a + +if HAVE_CXX +noinst_PROGRAMS += test_ust_utils_cxx +test_ust_utils_cxx_SOURCES = ust-utils-cxx.cpp ust-utils-common.h +test_ust_utils_cxx_LDADD = $(top_builddir)/liblttng-ust/liblttng-ust.la \ + $(top_builddir)/tests/utils/libtap.a +endif diff --git a/tests/unit/ust-utils/ust-utils-common.h b/tests/unit/ust-utils/ust-utils-common.h new file mode 100644 index 00000000..0792a10e --- /dev/null +++ b/tests/unit/ust-utils/ust-utils-common.h @@ -0,0 +1,128 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * Copyright (C) 2021 Michael Jeanson + */ + +#include "tap.h" + +#define NUM_TESTS 60 + +static +void test_ust_stringify(void) +{ + ok(strcmp(lttng_ust_stringify(1), "1") == 0, "lttng_ust_stringify - literal integer"); + ok(strcmp(lttng_ust_stringify(random_identifier), "random_identifier") == 0, "lttng_ust_stringify - identifier"); +} + +#define ok_is_signed(_type) \ + ok(lttng_ust_is_signed_type(_type) == true, "lttng_ust_is_signed_type - '" lttng_ust_stringify(_type) "' is signed") + +#define ok_is_unsigned(_type) \ + ok(lttng_ust_is_signed_type(_type) == false, "lttng_ust_is_signed_type - '" lttng_ust_stringify(_type) "' is unsigned") + +static +void test_ust_is_signed(void) +{ + /* + * Signed types + */ + + ok_is_signed(signed char); + ok_is_signed(short); + ok_is_signed(int); + ok_is_signed(long); + ok_is_signed(long long); + ok_is_signed(float); + ok_is_signed(double); + ok_is_signed(long double); + + ok_is_signed(int8_t); + ok_is_signed(int16_t); + ok_is_signed(int32_t); + ok_is_signed(int64_t); + ok_is_signed(intmax_t); + + ok_is_signed(ssize_t); + ok_is_signed(ptrdiff_t); + ok_is_signed(intptr_t); + + /* + * Unsigned types + */ + + ok_is_unsigned(unsigned char); + ok_is_unsigned(unsigned short); + ok_is_unsigned(unsigned int); + ok_is_unsigned(unsigned long); + ok_is_unsigned(unsigned long long); + + ok_is_unsigned(uint8_t); + ok_is_unsigned(uint16_t); + ok_is_unsigned(uint32_t); + ok_is_unsigned(uint64_t); + ok_is_unsigned(uintmax_t); + + ok_is_unsigned(bool); + ok_is_unsigned(size_t); + + ok_is_unsigned(void *); +} + + +#define ok_is_integer_type(_type) \ + ok(lttng_ust_is_integer_type(_type) == true, "lttng_ust_is_integer_type - '" lttng_ust_stringify(_type) "' is an integer") + +#define ok_is_not_integer_type(_type) \ + ok(lttng_ust_is_integer_type(_type) == false, "lttng_ust_is_integer_type - '" lttng_ust_stringify(_type) "' is not an integer") + +static +void test_ust_is_integer_type(void) +{ + ok_is_integer_type(char); + ok_is_integer_type(short); + ok_is_integer_type(int); + ok_is_integer_type(long); + ok_is_integer_type(long long); + + ok_is_integer_type(signed char); + ok_is_integer_type(signed short); + ok_is_integer_type(signed int); + ok_is_integer_type(signed long); + ok_is_integer_type(signed long long); + + ok_is_integer_type(unsigned char); + ok_is_integer_type(unsigned short); + ok_is_integer_type(unsigned int); + ok_is_integer_type(unsigned long); + ok_is_integer_type(unsigned long long); + + ok_is_integer_type(int8_t); + ok_is_integer_type(int16_t); + ok_is_integer_type(int32_t); + ok_is_integer_type(int64_t); + ok_is_integer_type(intmax_t); + + ok_is_integer_type(uint8_t); + ok_is_integer_type(uint16_t); + ok_is_integer_type(uint32_t); + ok_is_integer_type(uint64_t); + ok_is_integer_type(uintmax_t); + + ok_is_not_integer_type(float); + ok_is_not_integer_type(double); + ok_is_not_integer_type(long double); + + ok_is_not_integer_type(void *); +} + +int main(void) +{ + plan_tests(NUM_TESTS); + + test_ust_stringify(); + test_ust_is_signed(); + test_ust_is_integer_type(); + + return exit_status(); +} diff --git a/tests/unit/ust-utils/ust-utils-cxx.cpp b/tests/unit/ust-utils/ust-utils-cxx.cpp new file mode 100644 index 00000000..05ee3025 --- /dev/null +++ b/tests/unit/ust-utils/ust-utils-cxx.cpp @@ -0,0 +1,19 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * Copyright (C) 2021 Michael Jeanson + */ + +#include +#include +#include +#include + +#include + +extern "C" { + /* + * Share test code with C test + */ + #include "./ust-utils-common.h" +} diff --git a/tests/unit/ust-utils/ust-utils.c b/tests/unit/ust-utils/ust-utils.c new file mode 100644 index 00000000..25fcfbef --- /dev/null +++ b/tests/unit/ust-utils/ust-utils.c @@ -0,0 +1,17 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * Copyright (C) 2021 Michael Jeanson + */ + +#include +#include +#include +#include + +#include + +/* + * Share test code with CXX test + */ +#include "./ust-utils-common.h" -- 2.34.1