From 7441c365654f9015c998fb2f6aa4d9465c214a9d Mon Sep 17 00:00:00 2001 From: Davide Madrisan Date: Mon, 25 Jul 2022 22:02:09 +0200 Subject: [PATCH] Add some unit tests for lib/xstrton Signed-off-by: Davide Madrisan --- tests/Makefile.am | 13 +++-- tests/tslibxstrton_agetoint64.c | 92 ++++++++++++++++++++++++++++++++ tests/tslibxstrton_sizetoint64.c | 91 +++++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+), 5 deletions(-) create mode 100644 tests/tslibxstrton_agetoint64.c create mode 100644 tests/tslibxstrton_sizetoint64.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 0f923479..29ec4789 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -57,7 +57,9 @@ test_programs = \ tslibmessages \ tslibperfdata \ tslibpressure \ - tsliburlencode + tsliburlencode \ + tslibxstrton_agetoint64 \ + tslibxstrton_sizetoint64 if !HAVE_LIBPROCPS test_programs += \ tslibvminfo @@ -85,7 +87,6 @@ TSLIBS_LDFLAGS = -module -avoid-version \ tslibcontainer_docker_count_SOURCES = $(test_utils) tslibcontainer_docker_count.c tslibcontainer_docker_count_LDADD = $(LDADDS) - tslibcontainer_docker_memory_SOURCES = $(test_utils) tslibcontainer_docker_memory.c tslibcontainer_docker_memory_LDADD = $(LDADDS) @@ -101,10 +102,8 @@ tslibkernelver_LDADD = $(LDADDS) tslibmeminfo_conversions_SOURCES = $(test_utils) tslibmeminfo_conversions.c tslibmeminfo_conversions_LDADD = $(LDADDS) - tslibmeminfo_interface_SOURCES = $(test_utils) tslibmeminfo_interface.c tslibmeminfo_interface_LDADD = $(LDADDS) - tslibmeminfo_procparser_SOURCES = $(test_utils) tslibmeminfo_procparser.c tslibmeminfo_procparser_LDADD = $(LDADDS) @@ -120,6 +119,11 @@ tslibpressure_LDADD = $(LDADDS) tsliburlencode_SOURCES = $(test_utils) tsliburlencode.c tsliburlencode_LDADD = $(LDADDS) +tslibxstrton_agetoint64_SOURCES = $(test_utils) tslibxstrton_agetoint64.c +tslibxstrton_agetoint64_LDADD = $(LDADDS) +tslibxstrton_sizetoint64_SOURCES = $(test_utils) tslibxstrton_sizetoint64.c +tslibxstrton_sizetoint64_LDADD = $(LDADDS) + tslibvminfo_SOURCES = $(test_utils) tslibvminfo.c tslibvminfo_LDADD = $(LDADDS) @@ -134,7 +138,6 @@ tsintr_LDADD = $(LDADDS) tsload_normalize_SOURCES = $(test_utils) tsload_normalize.c tsload_normalize_LDADD = $(LDADDS) - tsload_thresholds_SOURCES = $(test_utils) tsload_thresholds.c tsload_thresholds_LDADD = $(LDADDS) diff --git a/tests/tslibxstrton_agetoint64.c b/tests/tslibxstrton_agetoint64.c new file mode 100644 index 00000000..a780dd10 --- /dev/null +++ b/tests/tslibxstrton_agetoint64.c @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + * License: GPLv3+ + * Copyright (c) 2022 Davide Madrisan + * + * Unit test for lib/xstrton.c + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "testutils.h" + +# define NPL_TESTING +# include "../lib/xstrton.c" +# undef NPL_TESTING + +typedef struct test_data +{ + char *age; + int64_t expect_value; +} test_data; + +static int +test_agetoint64 (const void *tdata) +{ + const struct test_data *data = tdata; + int64_t result; + char *errmegs; + int ret = 0; + + agetoint64 (data->age, &result, &errmegs); + TEST_ASSERT_EQUAL_NUMERIC (result, data->expect_value); + return ret; +} + +static int +mymain (void) +{ + int ret = 0; + +# define DO_TEST(AGE, EXPECT_VALUE) \ + do \ + { \ + test_data data = { \ + .age = AGE, \ + .expect_value = EXPECT_VALUE, \ + }; \ + if (test_run("check function agetoint64 with arg " AGE, \ + test_agetoint64, (&data)) < 0) \ + ret = -1; \ + } \ + while (0) + + /* test the function agetoint64() */ + +#define ONE_MIN 60 +#define ONE_HOUR (60 * ONE_MIN) +#define ONE_DAY (24 * ONE_HOUR) +#define ONE_WEEK (7 * ONE_DAY) +#define ONE_YEAR (365.25 * ONE_DAY) + + DO_TEST ("100s", 100); + DO_TEST ("30m", 30 * ONE_MIN); + DO_TEST ("4h", 4 * ONE_HOUR); + DO_TEST ("10d", 10 * ONE_DAY); + DO_TEST ("0.5d", 12 * ONE_HOUR); + DO_TEST ("4w", 4 * ONE_WEEK); + DO_TEST ("1y", ONE_YEAR); + + DO_TEST ("100S", 100); + DO_TEST ("30M", 30 * ONE_MIN); + DO_TEST ("4H", 4 * ONE_HOUR); + DO_TEST ("10D", 10 * ONE_DAY); + DO_TEST ("0.5D", 12 * ONE_HOUR); + DO_TEST ("4W", 4 * ONE_WEEK); + DO_TEST ("1Y", ONE_YEAR); + + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} +TEST_MAIN (mymain) diff --git a/tests/tslibxstrton_sizetoint64.c b/tests/tslibxstrton_sizetoint64.c new file mode 100644 index 00000000..247414a5 --- /dev/null +++ b/tests/tslibxstrton_sizetoint64.c @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + * License: GPLv3+ + * Copyright (c) 2022 Davide Madrisan + * + * Unit test for lib/xstrton.c + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "testutils.h" + +# define NPL_TESTING +# include "../lib/xstrton.c" +# undef NPL_TESTING + +typedef struct test_data +{ + char *size; + int64_t expect_value; +} test_data; + +static int +test_sizetoint64 (const void *tdata) +{ + const struct test_data *data = tdata; + int64_t result; + char *errmegs; + int ret = 0; + + sizetoint64 (data->size, &result, &errmegs); + TEST_ASSERT_EQUAL_NUMERIC (result, data->expect_value); + return ret; +} + +static int +mymain (void) +{ + int ret = 0; + +# define DO_TEST(SIZE, EXPECT_VALUE) \ + do \ + { \ + test_data data = { \ + .size = SIZE, \ + .expect_value = EXPECT_VALUE, \ + }; \ + if (test_run("check function sizetoint64 with arg " SIZE, \ + test_sizetoint64, (&data)) < 0) \ + ret = -1; \ + } \ + while (0) + + /* test the function sizetoint64() */ + +#define ONE_KILOBYTE 1000UL +#define ONE_MEGABYTE (1000UL * ONE_KILOBYTE) +#define ONE_GIGABYTE (1000UL * ONE_MEGABYTE) +#define ONE_TERABYTE (1000UL * ONE_GIGABYTE) +#define ONE_PETABYTE (1000UL * ONE_TERABYTE) + + DO_TEST ("1024b", 1024); + DO_TEST ("8k", 8 * ONE_KILOBYTE); + DO_TEST ("50m", 50 * ONE_MEGABYTE); + DO_TEST ("2g", 2 * ONE_GIGABYTE); + DO_TEST ("3t", 3 * ONE_TERABYTE); + DO_TEST ("2p", 2 * ONE_PETABYTE); + + DO_TEST ("1024B", 1024); + DO_TEST ("8K", 8 * ONE_KILOBYTE); + DO_TEST ("50M", 50 * ONE_MEGABYTE); + DO_TEST ("2G", 2 * ONE_GIGABYTE); + DO_TEST ("3T", 3 * ONE_TERABYTE); + DO_TEST ("2P", 2 * ONE_PETABYTE); + + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +TEST_MAIN (mymain)