diff options
Diffstat (limited to 'lib/libc/tests/stdlib')
25 files changed, 2726 insertions, 0 deletions
diff --git a/lib/libc/tests/stdlib/Makefile b/lib/libc/tests/stdlib/Makefile new file mode 100644 index 000000000000..9d84becfbd1f --- /dev/null +++ b/lib/libc/tests/stdlib/Makefile @@ -0,0 +1,85 @@ +.include <src.opts.mk> + +ATF_TESTS_C+= clearenv_test +ATF_TESTS_C+= cxa_atexit_test +ATF_TESTS_C+= dynthr_test +ATF_TESTS_C+= getenv_r_test +ATF_TESTS_C+= heapsort_test +ATF_TESTS_C+= libc_exit_test +ATF_TESTS_C+= mergesort_test +ATF_TESTS_C+= qsort_test +.if ${COMPILER_FEATURES:Mblocks} +ATF_TESTS_C+= qsort_b_test +.endif +ATF_TESTS_C+= qsort_r_compat_test +ATF_TESTS_C+= qsort_r_test +ATF_TESTS_C+= qsort_s_test +ATF_TESTS_C+= qsort_bench +ATF_TESTS_C+= set_constraint_handler_s_test +ATF_TESTS_C+= strfmon_test +ATF_TESTS_C+= tsearch_test +ATF_TESTS_CXX+= cxa_thread_atexit_test +ATF_TESTS_CXX+= cxa_thread_atexit_nothr_test + +# All architectures on FreeBSD have fenv.h +CFLAGS+= -D__HAVE_FENV + +# Define __HAVE_LONG_DOUBLE for architectures whose long double has greater +# precision than their double. +.if ${MACHINE_CPUARCH} == "aarch64" || \ + ${MACHINE_CPUARCH} == "amd64" || \ + ${MACHINE_CPUARCH} == "i386" || \ + ${MACHINE_CPUARCH} == "riscv" +CFLAGS+= -D__HAVE_LONG_DOUBLE +.endif + +# TODO: t_getenv_thread, t_mi_vector_hash, t_strtoi +NETBSD_ATF_TESTS_C+= abs_test +NETBSD_ATF_TESTS_C+= atoi_test +NETBSD_ATF_TESTS_C+= div_test +NETBSD_ATF_TESTS_C+= getenv_test +NETBSD_ATF_TESTS_C+= exit_test +NETBSD_ATF_TESTS_C+= hsearch_test +NETBSD_ATF_TESTS_C+= posix_memalign_test +NETBSD_ATF_TESTS_C+= random_test +NETBSD_ATF_TESTS_C+= strtod_test +NETBSD_ATF_TESTS_C+= strtol_test +NETBSD_ATF_TESTS_C+= system_test + +# TODO: need to come up with a correct explanation of what the patch pho does +# with h_atexit +#ATF_TESTS_SH= atexit_test +NETBSD_ATF_TESTS_SH= getopt_test + +.include "../Makefile.netbsd-tests" + +BINDIR= ${TESTSDIR} + +# TODO: see comment above +#PROGS+= h_atexit +PROGS+= h_getopt h_getopt_long + +CFLAGS+= -I${.CURDIR} + +LIBADD.cxa_thread_atexit_test+= pthread + +# Tests that require blocks support +.for t in qsort_b_test +CFLAGS.${t}.c+= -fblocks +LIBADD.${t}+= BlocksRuntime +.endfor + +.for t in h_getopt h_getopt_long +CFLAGS.$t+= -I${LIBNETBSD_SRCDIR} -I${SRCTOP}/contrib/netbsd-tests +LDFLAGS.$t+= -L${LIBNETBSD_OBJDIR} + +LIBADD.${t}+= netbsd util +.endfor + +LIBADD.libc_exit_test+= pthread +LIBADD.strtod_test+= m + +SUBDIR+= dynthr_mod +SUBDIR+= libatexit + +.include <bsd.test.mk> diff --git a/lib/libc/tests/stdlib/Makefile.depend b/lib/libc/tests/stdlib/Makefile.depend new file mode 100644 index 000000000000..777d716dbd01 --- /dev/null +++ b/lib/libc/tests/stdlib/Makefile.depend @@ -0,0 +1,24 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/atf/libatf-c \ + lib/atf/libatf-c++ \ + lib/libc \ + lib/libc++ \ + lib/libcompiler_rt \ + lib/libcxxrt \ + lib/libnetbsd \ + lib/libthr \ + lib/libutil \ + lib/msun \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/lib/libc/tests/stdlib/clearenv_test.c b/lib/libc/tests/stdlib/clearenv_test.c new file mode 100644 index 000000000000..003535a00060 --- /dev/null +++ b/lib/libc/tests/stdlib/clearenv_test.c @@ -0,0 +1,173 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (C) 2021 Mariusz Zaborski <oshogbo@FreeBSD.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test for clearenv(3) routine. + */ + +#include <atf-c.h> + +#include <stdio.h> +#include <stdlib.h> + +#define TEST_VARIABLE "TEST_VAR" +#define TEST_SYSTEM_VARIABLE "PWD" + +extern char **environ; + +void +create_multiple_variables(int num) +{ + char name[64]; + char value[64]; + int i; + + for (i = 0; i < num; i++) { + snprintf(name, sizeof(name), "%s_%d", TEST_VARIABLE, i); + snprintf(value, sizeof(value), "%d", i); + ATF_CHECK(getenv(name) == NULL); + ATF_CHECK(setenv(name, value, 0) != -1); + ATF_CHECK_STREQ(getenv(name), value); + } +} + +void +check_if_nulled_variables(int num) +{ + char name[64]; + int i; + + for (i = 0; i < num; i++) { + snprintf(name, sizeof(name), "%s_%d", TEST_VARIABLE, i); + ATF_CHECK(getenv(name) == NULL); + } +} + +ATF_TC_WITHOUT_HEAD(clearenv__single_var_test); +ATF_TC_BODY(clearenv__single_var_test, tc) +{ + + ATF_CHECK(setenv(TEST_VARIABLE, "true", 0) != -1); + ATF_CHECK_STREQ(getenv(TEST_VARIABLE), "true"); + ATF_CHECK(clearenv() == 0); + ATF_CHECK(getenv(TEST_VARIABLE) == NULL); +} + +ATF_TC_WITHOUT_HEAD(clearenv__multiple_vars_test); +ATF_TC_BODY(clearenv__multiple_vars_test, tc) +{ + + create_multiple_variables(10); + ATF_CHECK(clearenv() == 0); + check_if_nulled_variables(10); +} + +ATF_TC_WITHOUT_HEAD(clearenv__recreated_vars_test); +ATF_TC_BODY(clearenv__recreated_vars_test, tc) +{ + + create_multiple_variables(10); + ATF_CHECK(clearenv() == 0); + check_if_nulled_variables(10); + create_multiple_variables(10); +} + +ATF_TC_WITHOUT_HEAD(clearenv__system_var_test); +ATF_TC_BODY(clearenv__system_var_test, tc) +{ + + ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) != NULL); + ATF_CHECK(clearenv() == 0); + ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) == NULL); +} + +ATF_TC_WITHOUT_HEAD(clearenv__recreated_system_var_test); +ATF_TC_BODY(clearenv__recreated_system_var_test, tc) +{ + + ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) != NULL); + ATF_CHECK(clearenv() == 0); + ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) == NULL); + ATF_CHECK(setenv(TEST_SYSTEM_VARIABLE, "test", 0) != -1); + ATF_CHECK_STREQ(getenv(TEST_SYSTEM_VARIABLE), "test"); +} + +ATF_TC_WITHOUT_HEAD(clearenv__double_clear_vars); +ATF_TC_BODY(clearenv__double_clear_vars, tc) +{ + + create_multiple_variables(10); + ATF_CHECK(clearenv() == 0); + check_if_nulled_variables(10); + ATF_CHECK(clearenv() == 0); + check_if_nulled_variables(10); + create_multiple_variables(10); +} + +ATF_TC_WITHOUT_HEAD(clearenv__environ_null); +ATF_TC_BODY(clearenv__environ_null, tc) +{ + + ATF_CHECK(clearenv() == 0); + ATF_CHECK(environ != NULL); +} + +ATF_TC_WITHOUT_HEAD(clearenv__putenv_vars); +ATF_TC_BODY(clearenv__putenv_vars, tc) +{ + char buf[64], ref[64]; + + snprintf(buf, sizeof(buf), "%s=1", TEST_VARIABLE); + strcpy(ref, buf); + + ATF_CHECK(getenv(TEST_VARIABLE) == NULL); + ATF_CHECK(putenv(buf) != -1); + ATF_CHECK(strcmp(getenv(TEST_VARIABLE), "1") == 0); + + ATF_CHECK(clearenv() == 0); + + ATF_CHECK(getenv(TEST_VARIABLE) == NULL); + ATF_CHECK(strcmp(buf, ref) == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, clearenv__single_var_test); + ATF_TP_ADD_TC(tp, clearenv__multiple_vars_test); + ATF_TP_ADD_TC(tp, clearenv__recreated_vars_test); + + ATF_TP_ADD_TC(tp, clearenv__system_var_test); + ATF_TP_ADD_TC(tp, clearenv__recreated_system_var_test); + + ATF_TP_ADD_TC(tp, clearenv__double_clear_vars); + ATF_TP_ADD_TC(tp, clearenv__environ_null); + + ATF_TP_ADD_TC(tp, clearenv__putenv_vars); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/cxa_atexit_test.c b/lib/libc/tests/stdlib/cxa_atexit_test.c new file mode 100644 index 000000000000..7e2cafbce850 --- /dev/null +++ b/lib/libc/tests/stdlib/cxa_atexit_test.c @@ -0,0 +1,132 @@ +/*- + * Copyright (c) 2025 Kyle Evans <kevans@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/wait.h> + +#include <dlfcn.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +#include <atf-c.h> + +#define ARBITRARY_EXIT_CODE 42 + +static char * +get_shlib(const char *srcdir) +{ + char *shlib; + + shlib = NULL; + if (asprintf(&shlib, "%s/libatexit.so", srcdir) < 0) + atf_tc_fail("failed to construct path to libatexit.so"); + return (shlib); +} + +static void +run_test(const atf_tc_t *tc, bool with_fatal_atexit, bool with_exit) +{ + pid_t p; + void (*set_fatal_atexit)(bool); + void (*set_exit_code)(int); + void *hdl; + char *shlib; + + shlib = get_shlib(atf_tc_get_config_var(tc, "srcdir")); + + hdl = dlopen(shlib, RTLD_LAZY); + ATF_REQUIRE_MSG(hdl != NULL, "dlopen: %s", dlerror()); + + free(shlib); + + if (with_fatal_atexit) { + set_fatal_atexit = dlsym(hdl, "set_fatal_atexit"); + ATF_REQUIRE_MSG(set_fatal_atexit != NULL, + "set_fatal_atexit: %s", dlerror()); + } + if (with_exit) { + set_exit_code = dlsym(hdl, "set_exit_code"); + ATF_REQUIRE_MSG(set_exit_code != NULL, "set_exit_code: %s", + dlerror()); + } + + p = atf_utils_fork(); + if (p == 0) { + /* + * Don't let the child clobber the results file; stderr/stdout + * have been replaced by atf_utils_fork() to capture it. We're + * intentionally using exit() instead of _exit() here to run + * __cxa_finalize at exit, otherwise we'd just leave it be. + */ + closefrom(3); + + if (with_fatal_atexit) + set_fatal_atexit(true); + if (with_exit) + set_exit_code(ARBITRARY_EXIT_CODE); + + dlclose(hdl); + + /* + * If the dtor was supposed to exit (most cases), then we should + * not have made it to this point. If it's not supposed to + * exit, then we just exit with success here because we might + * be expecting either a clean exit or a signal on our way out + * as the final __cxa_finalize tries to run a callback in the + * unloaded DSO. + */ + if (with_exit) + exit(1); + exit(0); + } + + dlclose(hdl); + atf_utils_wait(p, with_exit ? ARBITRARY_EXIT_CODE : 0, "", ""); +} + +ATF_TC_WITHOUT_HEAD(simple_cxa_atexit); +ATF_TC_BODY(simple_cxa_atexit, tc) +{ + /* + * This test exits in a global object's dtor so that we check for our + * dtor being run at dlclose() time. If it isn't, then the forked child + * will have a chance to exit(1) after dlclose() to raise a failure. + */ + run_test(tc, false, true); +} + +ATF_TC_WITHOUT_HEAD(late_cxa_atexit); +ATF_TC_BODY(late_cxa_atexit, tc) +{ + /* + * This test creates another global object during a __cxa_atexit handler + * invocation. It's been observed in the wild that we weren't executing + * it, then the DSO gets torn down and it was executed at application + * exit time instead. In the best case scenario we would crash if + * something else hadn't been mapped there. + */ + run_test(tc, true, false); +} + +ATF_TC_WITHOUT_HEAD(late_cxa_atexit_ran); +ATF_TC_BODY(late_cxa_atexit_ran, tc) +{ + /* + * This is a slight variation of the previous test where we trigger an + * exit() in our late-registered __cxa_atexit handler so that we can + * ensure it was ran *before* dlclose() finished and not through some + * weird chain of events afterwards. + */ + run_test(tc, true, true); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, simple_cxa_atexit); + ATF_TP_ADD_TC(tp, late_cxa_atexit); + ATF_TP_ADD_TC(tp, late_cxa_atexit_ran); + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/cxa_thread_atexit_nothr_test.cc b/lib/libc/tests/stdlib/cxa_thread_atexit_nothr_test.cc new file mode 100644 index 000000000000..0b3b9497a6bd --- /dev/null +++ b/lib/libc/tests/stdlib/cxa_thread_atexit_nothr_test.cc @@ -0,0 +1,99 @@ +/*- + * Copyright (c) 2016 Mahdi Mokhtari <mokhi64@gmail.com> + * Copyright (c) 2016 The FreeBSD Foundation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <dlfcn.h> +#include <atf-c++.hpp> +#include <cstdio> +#include <cstdlib> + +static FILE *output = NULL; + +struct Foo { + Foo() { ATF_REQUIRE(fprintf(output, "Created\n") > 0); } + ~Foo() { ATF_REQUIRE(fprintf(output, "Destroyed\n") > 0); } + void use() { ATF_REQUIRE(fprintf(output, "Used\n") > 0); } +}; + +static thread_local Foo f; + +/* + * This test must not be linked to libpthread. + */ +ATF_TEST_CASE_WITHOUT_HEAD(cxx__nothr); +ATF_TEST_CASE_BODY(cxx__nothr) +{ + void *libthr_handle; + + /* Avoid coredump during f construction. */ + output = stderr; + + libthr_handle = dlopen("libthr.so.3", RTLD_LAZY | RTLD_GLOBAL | + RTLD_NOLOAD); + ATF_REQUIRE(libthr_handle == NULL); +} + +static void +check_local_main(void) +{ + static const char out_log[] = "Created\nUsed\nDestroyed\n"; + + fflush(output); + ATF_REQUIRE(atf::utils::compare_file("test_main.txt", out_log)); +} + +ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_main); +ATF_TEST_CASE_BODY(cxx__thread_local_main) +{ + + ATF_REQUIRE((output = fopen("test_main.txt", "w")) != NULL); + f.use(); + atexit(check_local_main); +} + +extern "C" int __cxa_thread_atexit(void (*)(void *), void *, void *); + +static void +again(void *arg) +{ + + __cxa_thread_atexit(again, arg, &output); +} + +ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_inf_dtors); +ATF_TEST_CASE_BODY(cxx__thread_inf_dtors) +{ + + again(NULL); +} + +ATF_INIT_TEST_CASES(tcs) +{ + + ATF_ADD_TEST_CASE(tcs, cxx__nothr); + ATF_ADD_TEST_CASE(tcs, cxx__thread_local_main); + ATF_ADD_TEST_CASE(tcs, cxx__thread_inf_dtors); +} diff --git a/lib/libc/tests/stdlib/cxa_thread_atexit_test.cc b/lib/libc/tests/stdlib/cxa_thread_atexit_test.cc new file mode 100644 index 000000000000..628a70b510d1 --- /dev/null +++ b/lib/libc/tests/stdlib/cxa_thread_atexit_test.cc @@ -0,0 +1,177 @@ +/*- + * Copyright (c) 2016 Mahdi Mokhtari <mokhi64@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <dlfcn.h> +#include <atf-c++.hpp> +#include <cstdio> +#include <cstdlib> +#include <thread> + +static FILE *output = NULL; + +struct Foo { + Foo() { ATF_REQUIRE(fprintf(output, "Created\n") > 0); } + ~Foo() { ATF_REQUIRE(fprintf(output, "Destroyed\n") > 0); } + void use() { ATF_REQUIRE(fprintf(output, "Used\n") > 0); } +}; + +struct Bar { + Bar() {} + ~Bar() { + thread_local static Foo foo; + ATF_REQUIRE(fprintf(output, "DIED\n") > 0); + } + void use() {} +}; + +extern "C" int __cxa_thread_atexit(void (*)(void *), void *, void *); + +static void +again(void *arg) +{ + + __cxa_thread_atexit(again, arg, &output); +} + +struct Baz { + Baz() {} + ~Baz() { + again(NULL); + } + void use() {} +}; + +static thread_local Foo f; +static thread_local Foo g; +static thread_local Bar h; +static thread_local Baz e; + +/* + * This test must be linked to libpthread. + */ +ATF_TEST_CASE_WITHOUT_HEAD(cxx__thr); +ATF_TEST_CASE_BODY(cxx__thr) +{ + void *libthr_handle; + + /* Avoid coredump during f construction. */ + output = stderr; + + libthr_handle = dlopen("libthr.so.3", RTLD_LAZY | RTLD_GLOBAL | + RTLD_NOLOAD); + ATF_REQUIRE(libthr_handle != NULL); + dlclose(libthr_handle); +} + +/* + * In this test f.use() will test cxa_thread_atexit() in non-threaded mode. + * After f.use() main will be threaded and we'll have one additional thread + * with its own TLS data. + */ +ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_before); +ATF_TEST_CASE_BODY(cxx__thread_local_before) +{ + static const char out_log[] = "Created\nCreated\nUsed\nCreated\n" + "Created\nUsed\nCreated\nDIED\nDestroyed\nDestroyed\nDestroyed\n"; + + ATF_REQUIRE((output = fopen("test_before.txt", "w")) != NULL); + + f.use(); + std::thread t([]() { f.use(); }); + t.join(); + + fflush(output); + + ATF_REQUIRE(atf::utils::compare_file("test_before.txt", out_log)); +} + +/* + * In this test, f.use() will test __cxa_thread_atexit() + * in threaded mode (but still in main-threaed). + */ +ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_after); +ATF_TEST_CASE_BODY(cxx__thread_local_after) +{ + static const char out_log[] = "Created\nCreated\nUsed\nCreated\n" + "DIED\nDestroyed\nDestroyed\nDestroyed\nCreated\nCreated\nUsed\n"; + + ATF_REQUIRE((output = fopen("test_after.txt", "w")) != NULL); + + std::thread t([]() { g.use(); }); + t.join(); + sleep(1); + g.use(); + + fflush(output); + + ATF_REQUIRE(atf::utils::compare_file("test_after.txt", out_log)); +} + +/* + * In this test, we register a new dtor while dtors are being run + * in __cxa_thread_atexit(). + */ +ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_add_while_calling_dtors); +ATF_TEST_CASE_BODY(cxx__thread_local_add_while_calling_dtors) +{ + static const char out_log[] = "Created\nCreated\nCreated\nDIED\n" + "Destroyed\nDestroyed\nDestroyed\n"; + + ATF_REQUIRE((output = fopen("test_add_meanwhile.txt", "w")) != NULL); + + std::thread t([]() { h.use(); }); + t.join(); + sleep(1); + + fflush(output); + + ATF_REQUIRE(atf::utils::compare_file("test_add_meanwhile.txt", out_log)); +} + +ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_inf_dtors); +ATF_TEST_CASE_BODY(cxx__thread_inf_dtors) +{ + + /* + * Only added to make isolated run of this test not + * coredumping. Construction of Foo objects require filled + * output. + */ + output = stderr; + + std::thread t([]() { e.use(); }); + t.join(); +} + +ATF_INIT_TEST_CASES(tcs) +{ + + ATF_ADD_TEST_CASE(tcs, cxx__thr); + ATF_ADD_TEST_CASE(tcs, cxx__thread_local_before); + ATF_ADD_TEST_CASE(tcs, cxx__thread_local_after); + ATF_ADD_TEST_CASE(tcs, cxx__thread_local_add_while_calling_dtors); + ATF_ADD_TEST_CASE(tcs, cxx__thread_inf_dtors); +} diff --git a/lib/libc/tests/stdlib/dynthr_mod/Makefile b/lib/libc/tests/stdlib/dynthr_mod/Makefile new file mode 100644 index 000000000000..3602e81e5be1 --- /dev/null +++ b/lib/libc/tests/stdlib/dynthr_mod/Makefile @@ -0,0 +1,10 @@ +SHLIB_NAME= dynthr_mod.so +SHLIBDIR= ${TESTSDIR} +SRCS= dynthr_mod.c +LIBADD= pthread +PACKAGE= tests + +TESTSDIR:= ${TESTSBASE}/${RELDIR:C/libc\/tests/libc/:H} + + +.include <bsd.lib.mk> diff --git a/lib/libc/tests/stdlib/dynthr_mod/dynthr_mod.c b/lib/libc/tests/stdlib/dynthr_mod/dynthr_mod.c new file mode 100644 index 000000000000..eccffcd0bd49 --- /dev/null +++ b/lib/libc/tests/stdlib/dynthr_mod/dynthr_mod.c @@ -0,0 +1,71 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (C) 2019 Andrew Gierth + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Though this file is initially distributed under the 2-clause BSD license, + * the author grants permission for its redistribution under alternative + * licenses as set forth at <https://rhodiumtoad.github.io/RELICENSE.txt>. + * This paragraph and the RELICENSE.txt file are not part of the license and + * may be omitted in redistributions. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <unistd.h> +#include <pthread.h> + +void mod_main(int op); + +static pthread_t thr; + +static void * +mod_thread(void *ptr __unused) +{ + char *volatile dummy; + + dummy = malloc(500); + *dummy = 'a'; + return (NULL); +} + +void +mod_main(int op) +{ + int rc; + + switch (op) { + case 1: + rc = pthread_create(&thr, NULL, mod_thread, NULL); + if (rc != 0) + _exit(1); + break; + case 0: + pthread_join(thr, NULL); + break; + } +} + diff --git a/lib/libc/tests/stdlib/dynthr_test.c b/lib/libc/tests/stdlib/dynthr_test.c new file mode 100644 index 000000000000..5b0af718afc7 --- /dev/null +++ b/lib/libc/tests/stdlib/dynthr_test.c @@ -0,0 +1,90 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (C) 2019 Andrew Gierth + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Though this file is initially distributed under the 2-clause BSD license, + * the author grants permission for its redistribution under alternative + * licenses as set forth at <https://rhodiumtoad.github.io/RELICENSE.txt>. + * This paragraph and the RELICENSE.txt file are not part of the license and + * may be omitted in redistributions. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <unistd.h> +#include <dlfcn.h> + +#include <atf-c.h> + +typedef void (modfunc_t)(int op); + +/* + * Minimal test case for PR 235158; mutual dependencies between jemalloc and + * libthr causing issues in thread creation. Specifically to this case, libthr + * uses calloc to initialize pthread mutexes, and jemalloc uses pthread mutexes. + * + * Deferred initialization provided by jemalloc proved to be fragile, causing + * issues like in the referenced PR where thread creation in a shared object + * loaded via dlopen(3) would stall unless the calling application also linked + * against pthread. + */ +ATF_TC(maintc); +ATF_TC_HEAD(maintc, tc) +{ + + atf_tc_set_md_var(tc, "timeout", "3"); +} + +ATF_TC_BODY(maintc, tc) +{ + char *libpath; + modfunc_t *func; + void *mod_handle; + const char *srcdir; + dlfunc_t rawfunc; + + srcdir = atf_tc_get_config_var(tc, "srcdir"); + if (asprintf(&libpath, "%s/dynthr_mod.so", srcdir) < 0) + atf_tc_fail("failed to construct path to libthr"); + mod_handle = dlopen(libpath, RTLD_LOCAL); + free(libpath); + if (mod_handle == NULL) + atf_tc_fail("failed to open dynthr_mod.so: %s", dlerror()); + rawfunc = dlfunc(mod_handle, "mod_main"); + if (rawfunc == NULL) + atf_tc_fail("failed to resolve function mod_main"); + func = (modfunc_t *)rawfunc; + func(1); + func(0); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, maintc); + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/getenv_r_test.c b/lib/libc/tests/stdlib/getenv_r_test.c new file mode 100644 index 000000000000..8085b92b1064 --- /dev/null +++ b/lib/libc/tests/stdlib/getenv_r_test.c @@ -0,0 +1,69 @@ +/*- + * Copyright (c) 2025 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <errno.h> +#include <stdlib.h> +#include <string.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(getenv_r_ok); +ATF_TC_BODY(getenv_r_ok, tc) +{ + const char *ident = atf_tc_get_ident(tc); + char buf[256]; + + ATF_REQUIRE_EQ(0, setenv("ATF_TC_IDENT", ident, 1)); + ATF_REQUIRE_EQ(0, getenv_r("ATF_TC_IDENT", buf, sizeof(buf))); + ATF_REQUIRE_STREQ(ident, buf); +} + +ATF_TC_WITHOUT_HEAD(getenv_r_einval); +ATF_TC_BODY(getenv_r_einval, tc) +{ + char buf[256]; + + errno = 0; + ATF_REQUIRE_EQ(-1, getenv_r(NULL, buf, sizeof(buf))); + ATF_REQUIRE_EQ(EINVAL, errno); + errno = 0; + ATF_REQUIRE_EQ(-1, getenv_r("", buf, sizeof(buf))); + ATF_REQUIRE_EQ(EINVAL, errno); + errno = 0; + ATF_REQUIRE_EQ(-1, getenv_r("A=B", buf, sizeof(buf))); + ATF_REQUIRE_EQ(EINVAL, errno); +} + +ATF_TC_WITHOUT_HEAD(getenv_r_enoent); +ATF_TC_BODY(getenv_r_enoent, tc) +{ + char buf[256]; + + errno = 0; + ATF_REQUIRE_EQ(-1, getenv_r("no such variable", buf, sizeof(buf))); + ATF_REQUIRE_EQ(ENOENT, errno); +} + +ATF_TC_WITHOUT_HEAD(getenv_r_erange); +ATF_TC_BODY(getenv_r_erange, tc) +{ + const char *ident = atf_tc_get_ident(tc); + char buf[256]; + + ATF_REQUIRE_EQ(0, setenv("ATF_TC_IDENT", ident, 1)); + errno = 0; + ATF_REQUIRE_EQ(-1, getenv_r("ATF_TC_IDENT", buf, strlen(ident))); + ATF_REQUIRE_EQ(ERANGE, errno); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, getenv_r_ok); + ATF_TP_ADD_TC(tp, getenv_r_einval); + ATF_TP_ADD_TC(tp, getenv_r_enoent); + ATF_TP_ADD_TC(tp, getenv_r_erange); + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/heapsort_test.c b/lib/libc/tests/stdlib/heapsort_test.c new file mode 100644 index 000000000000..b6747ff5d3aa --- /dev/null +++ b/lib/libc/tests/stdlib/heapsort_test.c @@ -0,0 +1,68 @@ +/*- + * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test for heapsort() routine. + */ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +#include "test-sort.h" + +ATF_TC_WITHOUT_HEAD(heapsort_test); +ATF_TC_BODY(heapsort_test, tc) +{ + int sresvector[IVEC_LEN]; + int testvector[IVEC_LEN]; + int i, j; + + for (j = 2; j < IVEC_LEN; j++) { + /* Populate test vectors */ + for (i = 0; i < j; i++) + testvector[i] = sresvector[i] = initvector[i]; + + /* Sort using heapsort(3) */ + heapsort(testvector, j, sizeof(testvector[0]), sorthelp); + /* Sort using reference slow sorting routine */ + ssort(sresvector, j); + + /* Compare results */ + for (i = 0; i < j; i++) + ATF_CHECK_MSG(testvector[i] == sresvector[i], + "item at index %d didn't match: %d != %d", + i, testvector[i], sresvector[i]); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, heapsort_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/libatexit/Makefile b/lib/libc/tests/stdlib/libatexit/Makefile new file mode 100644 index 000000000000..9ba04c77af62 --- /dev/null +++ b/lib/libc/tests/stdlib/libatexit/Makefile @@ -0,0 +1,11 @@ +SHLIB_CXX= libatexit +SHLIB_NAME= libatexit.so +SHLIB_MAJOR= 1 +SHLIBDIR= ${TESTSDIR} +PACKAGE= tests +SRCS= libatexit.cc + +TESTSDIR:= ${TESTSBASE}/${RELDIR:C/libc\/tests/libc/:H} + + +.include <bsd.lib.mk> diff --git a/lib/libc/tests/stdlib/libatexit/libatexit.cc b/lib/libc/tests/stdlib/libatexit/libatexit.cc new file mode 100644 index 000000000000..bb286c97e421 --- /dev/null +++ b/lib/libc/tests/stdlib/libatexit/libatexit.cc @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2025 Kyle Evans <kevans@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + * + */ + +#include <unistd.h> + +static int exit_code = -1; +static bool fatal_atexit; + +extern "C" { + void set_fatal_atexit(bool); + void set_exit_code(int); +} + +void +set_fatal_atexit(bool fexit) +{ + fatal_atexit = fexit; +} + +void +set_exit_code(int code) +{ + exit_code = code; +} + +struct other_object { + ~other_object() { + + /* + * In previous versions of our __cxa_atexit handling, we would + * never actually execute this handler because it's added during + * ~object() below; __cxa_finalize would never revisit it. We + * will allow the caller to configure us to exit with a certain + * exit code so that it can run us twice: once to ensure we + * don't crash at the end, and again to make sure the handler + * actually ran. + */ + if (exit_code != -1) + _exit(exit_code); + } +}; + +void +create_staticobj() +{ + static other_object obj; +} + +struct object { + ~object() { + /* + * If we're doing the fatal_atexit behavior (i.e., create an + * object that will add its own dtor for __cxa_finalize), then + * we don't exit here. + */ + if (fatal_atexit) + create_staticobj(); + else if (exit_code != -1) + _exit(exit_code); + } +}; + +static object obj; diff --git a/lib/libc/tests/stdlib/libc_exit_test.c b/lib/libc/tests/stdlib/libc_exit_test.c new file mode 100644 index 000000000000..12965261bdb3 --- /dev/null +++ b/lib/libc/tests/stdlib/libc_exit_test.c @@ -0,0 +1,154 @@ +/*- + * Copyright (c) 2023 Klara, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <sys/wait.h> + +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include <atf-c.h> + +static void +func_a(void) +{ + if (write(STDOUT_FILENO, "a", 1) != 1) + _Exit(1); +} + +static void +func_b(void) +{ + if (write(STDOUT_FILENO, "b", 1) != 1) + _Exit(1); +} + +static void +func_c(void) +{ + if (write(STDOUT_FILENO, "c", 1) != 1) + _Exit(1); +} + +static void +child(void) +{ + /* this will be received by the parent */ + printf("hello, "); + fflush(stdout); + /* this won't, because quick_exit() does not flush */ + printf("world"); + /* these will be called in reverse order, producing "abc" */ + if (at_quick_exit(func_c) != 0 || + at_quick_exit(func_b) != 0 || + at_quick_exit(func_a) != 0) + _Exit(1); + quick_exit(0); +} + +ATF_TC_WITHOUT_HEAD(quick_exit); +ATF_TC_BODY(quick_exit, tc) +{ + char buf[100] = ""; + ssize_t len; + int p[2], wstatus = 0; + pid_t pid; + + ATF_REQUIRE(pipe(p) == 0); + pid = fork(); + if (pid == 0) { + if (dup2(p[1], STDOUT_FILENO) < 0) + _Exit(1); + (void)close(p[1]); + (void)close(p[0]); + child(); + _Exit(1); + } + ATF_REQUIRE_MSG(pid > 0, + "expect fork() to succeed"); + ATF_CHECK_EQ_MSG(pid, waitpid(pid, &wstatus, 0), + "expect to collect child process"); + ATF_CHECK_EQ_MSG(0, wstatus, + "expect child to exit cleanly"); + ATF_CHECK_MSG((len = read(p[0], buf, sizeof(buf))) > 0, + "expect to receive output from child"); + ATF_CHECK_STREQ("hello, abc", buf); +} + +static void +myatexit1(void) +{ + exit(12); +} + +ATF_TC_WITHOUT_HEAD(recursive_exit1); +ATF_TC_BODY(recursive_exit1, tc) +{ + pid_t pid; + int wstatus; + + pid = fork(); + if (pid == 0) { + atexit(myatexit1); + exit(1); + } + ATF_REQUIRE_MSG(pid > 0, + "expect fork() to succeed"); + ATF_CHECK_EQ_MSG(pid, waitpid(pid, &wstatus, 0), + "expect to collect child process"); + ATF_CHECK(WIFEXITED(wstatus)); + ATF_CHECK_EQ(WEXITSTATUS(wstatus), 12); +} + +static pthread_barrier_t barrier; + +static void +myatexit2(void) +{ + pthread_barrier_wait(&barrier); + exit(12); +} + +static void * +mythreadexit(void *arg) +{ + pthread_barrier_wait(&barrier); + exit(15); +} + +ATF_TC_WITHOUT_HEAD(recursive_exit2); +ATF_TC_BODY(recursive_exit2, tc) +{ + pid_t pid; + int wstatus; + + pid = fork(); + if (pid == 0) { + pthread_t thr; + + atexit(myatexit2); + + pthread_barrier_init(&barrier, NULL, 2); + pthread_create(&thr, NULL, mythreadexit, NULL); + + exit(1); + } + ATF_REQUIRE_MSG(pid > 0, + "expect fork() to succeed"); + ATF_CHECK_EQ_MSG(pid, waitpid(pid, &wstatus, 0), + "expect to collect child process"); + ATF_CHECK(WIFEXITED(wstatus)); + ATF_CHECK_EQ(WEXITSTATUS(wstatus), 12); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, quick_exit); + ATF_TP_ADD_TC(tp, recursive_exit1); + ATF_TP_ADD_TC(tp, recursive_exit2); + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/mergesort_test.c b/lib/libc/tests/stdlib/mergesort_test.c new file mode 100644 index 000000000000..156d6bb7c2c3 --- /dev/null +++ b/lib/libc/tests/stdlib/mergesort_test.c @@ -0,0 +1,68 @@ +/*- + * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test for mergesort() routine. + */ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +#include "test-sort.h" + +ATF_TC_WITHOUT_HEAD(mergesort_test); +ATF_TC_BODY(mergesort_test, tc) +{ + int sresvector[IVEC_LEN]; + int testvector[IVEC_LEN]; + int i, j; + + for (j = 2; j < IVEC_LEN; j++) { + /* Populate test vectors */ + for (i = 0; i < j; i++) + testvector[i] = sresvector[i] = initvector[i]; + + /* Sort using mergesort(3) */ + mergesort(testvector, j, sizeof(testvector[0]), sorthelp); + /* Sort using reference slow sorting routine */ + ssort(sresvector, j); + + /* Compare results */ + for (i = 0; i < j; i++) + ATF_CHECK_MSG(testvector[i] == sresvector[i], + "item at index %d didn't match: %d != %d", + i, testvector[i], sresvector[i]); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, mergesort_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/qsort_b_test.c b/lib/libc/tests/stdlib/qsort_b_test.c new file mode 100644 index 000000000000..60cd30ac222a --- /dev/null +++ b/lib/libc/tests/stdlib/qsort_b_test.c @@ -0,0 +1,76 @@ +/*- + * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test for qsort_b() routine. + */ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +#include "test-sort.h" + +ATF_TC_WITHOUT_HEAD(qsort_b_test); +ATF_TC_BODY(qsort_b_test, tc) +{ + int testvector[IVEC_LEN]; + int sresvector[IVEC_LEN]; + int i, j; + + for (j = 2; j < IVEC_LEN; j++) { + /* Populate test vectors */ + for (i = 0; i < j; i++) + testvector[i] = sresvector[i] = initvector[i]; + + /* Sort using qsort_b(3) */ + qsort_b(testvector, j, sizeof(testvector[0]), + ^(const void* a, const void* b) { + if (*(int *)a > *(int *)b) + return (1); + else if (*(int *)a < *(int *)b) + return (-1); + else + return (0); + }); + /* Sort using reference slow sorting routine */ + ssort(sresvector, j); + + /* Compare results */ + for (i = 0; i < j; i++) + ATF_CHECK_MSG(testvector[i] == sresvector[i], + "item at index %d didn't match: %d != %d", + i, testvector[i], sresvector[i]); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, qsort_b_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/qsort_bench.c b/lib/libc/tests/stdlib/qsort_bench.c new file mode 100644 index 000000000000..5f2cfae40140 --- /dev/null +++ b/lib/libc/tests/stdlib/qsort_bench.c @@ -0,0 +1,113 @@ +/*- + * Copyright (c) 2025 Dag-Erling Smørgrav <des@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <atf-c.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <unistd.h> + +/*- + * Measures qsort(3) runtime with pathological input and verify that it + * stays close to N * log2(N). + * + * Thanks to Vivian Hussey for the proof of concept. + * + * The input we construct is similar to a sweep from 0 to N where each + * half, except for the first element, has been reversed; for instance, + * with N = 8, we get { 0, 3, 2, 1, 4, 8, 7, 6 }. This triggers a bug in + * the BSD qsort(3) where it will switch to insertion sort if the pivots + * are sorted. + * + * This article goes into more detail about the bug and its origin: + * + * https://www.raygard.net/2022/02/26/Re-engineering-a-qsort-part-3 + * + * With this optimization (the `if (swap_cnt == 0)` block), qsort(3) needs + * roughly N * N / 4 comparisons to sort our pathological input. Without + * it, it needs only a little more than N * log2(N) comparisons. + */ + +/* we stop testing once a single takes longer than this */ +#define MAXRUNSECS 10 + +static bool debugging; + +static uintmax_t ncmp; + +static int +intcmp(const void *a, const void *b) +{ + ncmp++; + return ((*(int *)a > *(int *)b) - (*(int *)a < *(int *)b)); +} + +static void +qsort_bench(int log2n) +{ + uintmax_t n = 1LLU << log2n; + int *buf; + + /* fill an array with a pathological pattern */ + ATF_REQUIRE(buf = malloc(n * sizeof(*buf))); + buf[0] = 0; + buf[n / 2] = n / 2; + for (unsigned int i = 1; i < n / 2; i++) { + buf[i] = n / 2 - i; + buf[n / 2 + i] = n - i; + } + + ncmp = 0; + qsort(buf, n, sizeof(*buf), intcmp); + + /* check result and free array */ + if (debugging) { + for (unsigned int i = 1; i < n; i++) { + ATF_REQUIRE_MSG(buf[i] > buf[i - 1], + "array is not sorted"); + } + } + free(buf); + + /* check that runtime does not exceed N² */ + ATF_CHECK_MSG(ncmp / n < n, + "runtime %ju exceeds N² for N = %ju", ncmp, n); + + /* check that runtime does not exceed N log N by much */ + ATF_CHECK_MSG(ncmp / n <= log2n + 1, + "runtime %ju exceeds N log N for N = %ju", ncmp, n); +} + +ATF_TC_WITHOUT_HEAD(qsort_bench); +ATF_TC_BODY(qsort_bench, tc) +{ + struct timespec t0, t1; + uintmax_t tus; + + for (int i = 10; i <= 30; i++) { + clock_gettime(CLOCK_UPTIME, &t0); + qsort_bench(i); + clock_gettime(CLOCK_UPTIME, &t1); + tus = t1.tv_sec * 1000000 + t1.tv_nsec / 1000; + tus -= t0.tv_sec * 1000000 + t0.tv_nsec / 1000; + if (debugging) { + fprintf(stderr, "N = 2^%d in %ju.%06jus\n", + i, tus / 1000000, tus % 1000000); + } + /* stop once an individual run exceeds our limit */ + if (tus / 1000000 >= MAXRUNSECS) + break; + } +} + +ATF_TP_ADD_TCS(tp) +{ + debugging = !getenv("__RUNNING_INSIDE_ATF_RUN") && + isatty(STDERR_FILENO); + ATF_TP_ADD_TC(tp, qsort_bench); + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/qsort_r_compat_test.c b/lib/libc/tests/stdlib/qsort_r_compat_test.c new file mode 100644 index 000000000000..d7b06615292e --- /dev/null +++ b/lib/libc/tests/stdlib/qsort_r_compat_test.c @@ -0,0 +1,89 @@ +/*- + * Copyright (C) 2020 Edward Tomasz Napierala <trasz@FreeBSD.org> + * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test for historical qsort_r(3) routine. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "test-sort.h" + +#define THUNK 42 + +static int +sorthelp_r(void *thunk, const void *a, const void *b) +{ + const int *oa, *ob; + + ATF_REQUIRE_EQ(*(int *)thunk, THUNK); + + oa = a; + ob = b; + /* Don't use "return *oa - *ob" since it's easy to cause overflow! */ + if (*oa > *ob) + return (1); + if (*oa < *ob) + return (-1); + return (0); +} + +ATF_TC_WITHOUT_HEAD(qsort_r_compat_test); +ATF_TC_BODY(qsort_r_compat_test, tc) +{ + int testvector[IVEC_LEN]; + int sresvector[IVEC_LEN]; + int i, j; + int thunk = THUNK; + + for (j = 2; j < IVEC_LEN; j++) { + /* Populate test vectors */ + for (i = 0; i < j; i++) + testvector[i] = sresvector[i] = initvector[i]; + + /* Sort using qsort_r(3) */ + qsort_r(testvector, j, sizeof(testvector[0]), &thunk, + sorthelp_r); + /* Sort using reference slow sorting routine */ + ssort(sresvector, j); + + /* Compare results */ + for (i = 0; i < j; i++) + ATF_CHECK_MSG(testvector[i] == sresvector[i], + "item at index %d didn't match: %d != %d", + i, testvector[i], sresvector[i]); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, qsort_r_compat_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/qsort_r_test.c b/lib/libc/tests/stdlib/qsort_r_test.c new file mode 100644 index 000000000000..446d63279fbf --- /dev/null +++ b/lib/libc/tests/stdlib/qsort_r_test.c @@ -0,0 +1,89 @@ +/*- + * Copyright (C) 2020 Edward Tomasz Napierala <trasz@FreeBSD.org> + * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test for qsort_r(3) routine. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "test-sort.h" + +#define THUNK 42 + +static int +sorthelp_r(const void *a, const void *b, void *thunk) +{ + const int *oa, *ob; + + ATF_REQUIRE_EQ(*(int *)thunk, THUNK); + + oa = a; + ob = b; + /* Don't use "return *oa - *ob" since it's easy to cause overflow! */ + if (*oa > *ob) + return (1); + if (*oa < *ob) + return (-1); + return (0); +} + +ATF_TC_WITHOUT_HEAD(qsort_r_test); +ATF_TC_BODY(qsort_r_test, tc) +{ + int testvector[IVEC_LEN]; + int sresvector[IVEC_LEN]; + int i, j; + int thunk = THUNK; + + for (j = 2; j < IVEC_LEN; j++) { + /* Populate test vectors */ + for (i = 0; i < j; i++) + testvector[i] = sresvector[i] = initvector[i]; + + /* Sort using qsort_r(3) */ + qsort_r(testvector, j, sizeof(testvector[0]), sorthelp_r, + &thunk); + /* Sort using reference slow sorting routine */ + ssort(sresvector, j); + + /* Compare results */ + for (i = 0; i < j; i++) + ATF_CHECK_MSG(testvector[i] == sresvector[i], + "item at index %d didn't match: %d != %d", + i, testvector[i], sresvector[i]); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, qsort_r_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/qsort_s_test.c b/lib/libc/tests/stdlib/qsort_s_test.c new file mode 100644 index 000000000000..c3210caf24e1 --- /dev/null +++ b/lib/libc/tests/stdlib/qsort_s_test.c @@ -0,0 +1,254 @@ +/*- + * Copyright (C) 2020 Edward Tomasz Napierala <trasz@FreeBSD.org> + * Copyright (c) 2017 Juniper Networks. All rights reserved. + * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test for qsort_s(3) routine. + */ + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> + +#define THUNK 42 + +#include "test-sort.h" + +static errno_t e; + +static int +sorthelp_s(const void *a, const void *b, void *thunk) +{ + const int *oa, *ob; + + ATF_REQUIRE_EQ(*(int *)thunk, THUNK); + + oa = a; + ob = b; + /* Don't use "return *oa - *ob" since it's easy to cause overflow! */ + if (*oa > *ob) + return (1); + if (*oa < *ob) + return (-1); + return (0); +} + +void +h(const char * restrict msg __unused, void * restrict ptr __unused, errno_t error) +{ + e = error; +} + +/* nmemb < 0 */ +ATF_TC_WITHOUT_HEAD(qsort_s_nmemb_lt_zero); +ATF_TC_BODY(qsort_s_nmemb_lt_zero, tc) +{ + int thunk = THUNK; + int b; + + ATF_CHECK(qsort_s(&b, -1, sizeof(int), sorthelp_s, &thunk) != 0); +} + +/* nmemb > rmax */ +ATF_TC_WITHOUT_HEAD(qsort_s_nmemb_gt_rmax); +ATF_TC_BODY(qsort_s_nmemb_gt_rmax, tc) +{ + int thunk = THUNK; + int b; + + ATF_CHECK(qsort_s(&b, RSIZE_MAX + 1, sizeof(int), sorthelp_s, &thunk) != 0); +} + +/* size < 0 */ +ATF_TC_WITHOUT_HEAD(qsort_s_size_lt_zero); +ATF_TC_BODY(qsort_s_size_lt_zero, tc) +{ + int thunk = THUNK; + int b; + + ATF_CHECK(qsort_s(&b, 1, -1, sorthelp_s, &thunk) != 0); +} + +/* size > rmax */ +ATF_TC_WITHOUT_HEAD(qsort_s_size_gt_rmax); +ATF_TC_BODY(qsort_s_size_gt_rmax, tc) +{ + int thunk = THUNK; + int b; + + ATF_CHECK(qsort_s(&b, 1, RSIZE_MAX + 1, sorthelp_s, &thunk) != 0); +} + +/* NULL compar */ +ATF_TC_WITHOUT_HEAD(qsort_s_null_compar); +ATF_TC_BODY(qsort_s_null_compar, tc) +{ + int thunk = THUNK; + int b; + + ATF_CHECK(qsort_s(&b, 1, sizeof(int), NULL, &thunk) != 0); +} + +/* nmemb < 0, handler */ +ATF_TC_WITHOUT_HEAD(qsort_s_nmemb_lt_zero_h); +ATF_TC_BODY(qsort_s_nmemb_lt_zero_h, tc) +{ + int thunk = THUNK; + int b[] = {81, 4, 7}; + + e = 0; + set_constraint_handler_s(h); + ATF_CHECK(qsort_s(&b, -1, sizeof(int), sorthelp_s, &thunk) != 0); + ATF_CHECK(e > 0); + ATF_CHECK_EQ(b[0], 81); + ATF_CHECK_EQ(b[1], 4); + ATF_CHECK_EQ(b[2], 7); +} + +/* nmemb > rmax, handler */ +ATF_TC_WITHOUT_HEAD(qsort_s_nmemb_gt_rmax_h); +ATF_TC_BODY(qsort_s_nmemb_gt_rmax_h, tc) +{ + int thunk = THUNK; + int b[] = {81, 4, 7}; + + e = 0; + set_constraint_handler_s(h); + ATF_CHECK(qsort_s(&b, RSIZE_MAX + 1, sizeof(int), sorthelp_s, &thunk) != 0); + ATF_CHECK(e > 0); + ATF_CHECK_EQ(b[0], 81); + ATF_CHECK_EQ(b[1], 4); + ATF_CHECK_EQ(b[2], 7); +} + +/* size < 0, handler */ +ATF_TC_WITHOUT_HEAD(qsort_s_size_lt_zero_h); +ATF_TC_BODY(qsort_s_size_lt_zero_h, tc) +{ + int thunk = THUNK; + int b[] = {81, 4, 7}; + + e = 0; + set_constraint_handler_s(h); + ATF_CHECK(qsort_s(&b, nitems(b), -1, sorthelp_s, &thunk) != 0); + ATF_CHECK(e > 0); + ATF_CHECK_EQ(b[0], 81); + ATF_CHECK_EQ(b[1], 4); + ATF_CHECK_EQ(b[2], 7); +} + +/* size > rmax, handler */ +ATF_TC_WITHOUT_HEAD(qsort_s_size_gt_rmax_h); +ATF_TC_BODY(qsort_s_size_gt_rmax_h, tc) +{ + int thunk = THUNK; + int b[] = {81, 4, 7}; + + e = 0; + set_constraint_handler_s(h); + ATF_CHECK(qsort_s(&b, nitems(b), RSIZE_MAX + 1, sorthelp_s, &thunk) != 0); + ATF_CHECK(e > 0); + ATF_CHECK_EQ(b[0], 81); + ATF_CHECK_EQ(b[1], 4); + ATF_CHECK_EQ(b[2], 7); +} + +/* NULL compar, handler */ +ATF_TC_WITHOUT_HEAD(qsort_s_null_compar_h); +ATF_TC_BODY(qsort_s_null_compar_h, tc) +{ + int thunk = THUNK; + int b[] = {81, 4, 7}; + + e = 0; + set_constraint_handler_s(h); + ATF_CHECK(qsort_s(&b, nitems(b), sizeof(int), NULL, &thunk) != 0); + ATF_CHECK(e > 0); + ATF_CHECK_EQ(b[0], 81); + ATF_CHECK_EQ(b[1], 4); + ATF_CHECK_EQ(b[2], 7); +} + +ATF_TC_WITHOUT_HEAD(qsort_s_h); +ATF_TC_BODY(qsort_s_h, tc) +{ + int thunk = THUNK; + int b[] = {81, 4, 7}; + + e = 0; + set_constraint_handler_s(h); + ATF_CHECK(qsort_s(&b, nitems(b), sizeof(int), sorthelp_s, &thunk) == 0); + ATF_CHECK(e == 0); + ATF_CHECK_EQ(b[0], 4); + ATF_CHECK_EQ(b[1], 7); + ATF_CHECK_EQ(b[2], 81); +} + +ATF_TC_WITHOUT_HEAD(qsort_s_test); +ATF_TC_BODY(qsort_s_test, tc) +{ + int testvector[IVEC_LEN]; + int sresvector[IVEC_LEN]; + int i, j; + int thunk = THUNK; + + for (j = 2; j < IVEC_LEN; j++) { + /* Populate test vectors */ + for (i = 0; i < j; i++) + testvector[i] = sresvector[i] = initvector[i]; + + /* Sort using qsort_s(3) */ + qsort_s(testvector, j, sizeof(testvector[0]), + sorthelp_s, &thunk); + /* Sort using reference slow sorting routine */ + ssort(sresvector, j); + + /* Compare results */ + for (i = 0; i < j; i++) + ATF_CHECK_MSG(testvector[i] == sresvector[i], + "item at index %d didn't match: %d != %d", + i, testvector[i], sresvector[i]); + } +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, qsort_s_nmemb_lt_zero); + ATF_TP_ADD_TC(tp, qsort_s_nmemb_gt_rmax); + ATF_TP_ADD_TC(tp, qsort_s_size_lt_zero); + ATF_TP_ADD_TC(tp, qsort_s_size_gt_rmax); + ATF_TP_ADD_TC(tp, qsort_s_null_compar); + ATF_TP_ADD_TC(tp, qsort_s_nmemb_lt_zero_h); + ATF_TP_ADD_TC(tp, qsort_s_nmemb_gt_rmax_h); + ATF_TP_ADD_TC(tp, qsort_s_size_lt_zero_h); + ATF_TP_ADD_TC(tp, qsort_s_size_gt_rmax_h); + ATF_TP_ADD_TC(tp, qsort_s_null_compar_h); + ATF_TP_ADD_TC(tp, qsort_s_h); + ATF_TP_ADD_TC(tp, qsort_s_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/qsort_test.c b/lib/libc/tests/stdlib/qsort_test.c new file mode 100644 index 000000000000..a6202e55fd62 --- /dev/null +++ b/lib/libc/tests/stdlib/qsort_test.c @@ -0,0 +1,68 @@ +/*- + * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test for qsort() routine. + */ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +#include "test-sort.h" + +ATF_TC_WITHOUT_HEAD(qsort_test); +ATF_TC_BODY(qsort_test, tc) +{ + int testvector[IVEC_LEN]; + int sresvector[IVEC_LEN]; + int i, j; + + for (j = 2; j < IVEC_LEN; j++) { + /* Populate test vectors */ + for (i = 0; i < j; i++) + testvector[i] = sresvector[i] = initvector[i]; + + /* Sort using qsort(3) */ + qsort(testvector, j, sizeof(testvector[0]), sorthelp); + /* Sort using reference slow sorting routine */ + ssort(sresvector, j); + + /* Compare results */ + for (i = 0; i < j; i++) + ATF_CHECK_MSG(testvector[i] == sresvector[i], + "item at index %d didn't match: %d != %d", + i, testvector[i], sresvector[i]); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, qsort_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/set_constraint_handler_s_test.c b/lib/libc/tests/stdlib/set_constraint_handler_s_test.c new file mode 100644 index 000000000000..87db1f71ed4d --- /dev/null +++ b/lib/libc/tests/stdlib/set_constraint_handler_s_test.c @@ -0,0 +1,60 @@ +/*- + * Copyright (c) 2017 Juniper Networks. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <assert.h> +#include <stdlib.h> + +#include <atf-c.h> + +/* null */ +ATF_TC_WITHOUT_HEAD(null_handler); +ATF_TC_BODY(null_handler, tc) +{ + assert(set_constraint_handler_s(abort_handler_s) == NULL); +} + +/* abort handler */ +ATF_TC_WITHOUT_HEAD(abort_handler); +ATF_TC_BODY(abort_handler, tc) +{ + set_constraint_handler_s(abort_handler_s); + assert(set_constraint_handler_s(ignore_handler_s) == abort_handler_s); +} + +/* ignore handler */ +ATF_TC_WITHOUT_HEAD(ignore_handler); +ATF_TC_BODY(ignore_handler, tc) +{ + set_constraint_handler_s(ignore_handler_s); + assert(set_constraint_handler_s(abort_handler_s) == ignore_handler_s); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, null_handler); + ATF_TP_ADD_TC(tp, abort_handler); + ATF_TP_ADD_TC(tp, ignore_handler); + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/strfmon_test.c b/lib/libc/tests/stdlib/strfmon_test.c new file mode 100644 index 000000000000..86f6256dba0b --- /dev/null +++ b/lib/libc/tests/stdlib/strfmon_test.c @@ -0,0 +1,252 @@ +/*- + * Copyright (C) 2018 Conrad Meyer <cem@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/param.h> + +#include <locale.h> +#include <monetary.h> +#include <stdio.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(strfmon_locale_thousands); +ATF_TC_BODY(strfmon_locale_thousands, tc) +{ + char actual[40], expected[40]; + struct lconv *lc; + const char *ts; + double n; + + setlocale(LC_MONETARY, "sv_SE.UTF-8"); + + lc = localeconv(); + + ts = lc->mon_thousands_sep; + if (strlen(ts) == 0) + ts = lc->thousands_sep; + + if (strlen(ts) < 2) + atf_tc_skip("multi-byte thousands-separator not found"); + + n = 1234.56; + strfmon(actual, sizeof(actual) - 1, "%i", n); + + strcpy(expected, "1"); + strlcat(expected, ts, sizeof(expected)); + strlcat(expected, "234", sizeof(expected)); + + /* We're just testing the thousands separator, not all of strfmon. */ + actual[strlen(expected)] = '\0'; + ATF_CHECK_STREQ(expected, actual); +} + +ATF_TC_WITHOUT_HEAD(strfmon_examples); +ATF_TC_BODY(strfmon_examples, tc) +{ + const struct { + const char *format; + const char *expected; + } tests[] = { + { "%n", "[$123.45] [-$123.45] [$3,456.78]" }, + { "%11n", "[ $123.45] [ -$123.45] [ $3,456.78]" }, + { "%#5n", "[ $ 123.45] [-$ 123.45] [ $ 3,456.78]" }, + { "%=*#5n", "[ $***123.45] [-$***123.45] [ $*3,456.78]" }, + { "%=0#5n", "[ $000123.45] [-$000123.45] [ $03,456.78]" }, + { "%^#5n", "[ $ 123.45] [-$ 123.45] [ $ 3456.78]" }, + { "%^#5.0n", "[ $ 123] [-$ 123] [ $ 3457]" }, + { "%^#5.4n", "[ $ 123.4500] [-$ 123.4500] [ $ 3456.7810]" }, + { "%(#5n", "[ $ 123.45 ] [($ 123.45)] [ $ 3,456.78 ]" }, + { "%!(#5n", "[ 123.45 ] [( 123.45)] [ 3,456.78 ]" }, + { "%-14#5.4n", "[ $ 123.4500 ] [-$ 123.4500 ] [ $ 3,456.7810 ]" }, + { "%14#5.4n", "[ $ 123.4500] [ -$ 123.4500] [ $ 3,456.7810]" }, + }; + size_t i; + char actual[100], format[50]; + + if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL) + atf_tc_skip("unable to setlocale()"); + + for (i = 0; i < nitems(tests); ++i) { + snprintf(format, sizeof(format), "[%s] [%s] [%s]", + tests[i].format, tests[i].format, tests[i].format); + strfmon(actual, sizeof(actual) - 1, format, + 123.45, -123.45, 3456.781); + ATF_CHECK_STREQ_MSG(tests[i].expected, actual, + "[%s]", tests[i].format); + } +} + +ATF_TC(strfmon_cs_precedes_0); +ATF_TC_HEAD(strfmon_cs_precedes_0, tc) +{ + atf_tc_set_md_var(tc, "descr", + "sep_by_space x sign_posn when cs_precedes = 0"); +} +ATF_TC_BODY(strfmon_cs_precedes_0, tc) +{ + const struct { + const char *expected; + } tests[] = { + /* sep_by_space x sign_posn */ + { "[(123.00$)] [-123.00$] [123.00$-] [123.00-$] [123.00$-]" }, + { "[(123.00 $)] [-123.00 $] [123.00 $-] [123.00 -$] [123.00 $-]" }, + { "[(123.00$)] [- 123.00$] [123.00$ -] [123.00- $] [123.00$ -]" }, + }; + size_t i, j; + struct lconv *lc; + char actual[100], buf[100]; + + if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL) + atf_tc_skip("unable to setlocale()"); + + lc = localeconv(); + lc->n_cs_precedes = 0; + + for (i = 0; i < nitems(tests); ++i) { + actual[0] = '\0'; + lc->n_sep_by_space = i; + + for (j = 0; j < 5; ++j) { + lc->n_sign_posn = j; + + strfmon(buf, sizeof(buf) - 1, "[%n] ", -123.0); + strlcat(actual, buf, sizeof(actual)); + } + + actual[strlen(actual) - 1] = '\0'; + ATF_CHECK_STREQ_MSG(tests[i].expected, actual, + "sep_by_space = %zu", i); + } +} + +ATF_TC(strfmon_cs_precedes_1); +ATF_TC_HEAD(strfmon_cs_precedes_1, tc) +{ + atf_tc_set_md_var(tc, "descr", + "sep_by_space x sign_posn when cs_precedes = 1"); +} +ATF_TC_BODY(strfmon_cs_precedes_1, tc) +{ + const struct { + const char *expected; + } tests[] = { + /* sep_by_space x sign_posn */ + { "[($123.00)] [-$123.00] [$123.00-] [-$123.00] [$-123.00]" }, + { "[($ 123.00)] [-$ 123.00] [$ 123.00-] [-$ 123.00] [$- 123.00]" }, + { "[($123.00)] [- $123.00] [$123.00 -] [- $123.00] [$ -123.00]" }, + }; + size_t i, j; + struct lconv *lc; + char actual[100], buf[100]; + + if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL) + atf_tc_skip("unable to setlocale()"); + + lc = localeconv(); + lc->n_cs_precedes = 1; + + for (i = 0; i < nitems(tests); ++i) { + actual[0] = '\0'; + lc->n_sep_by_space = i; + + for (j = 0; j < 5; ++j) { + lc->n_sign_posn = j; + + strfmon(buf, sizeof(buf) - 1, "[%n] ", -123.0); + strlcat(actual, buf, sizeof(actual)); + } + + actual[strlen(actual) - 1] = '\0'; + ATF_CHECK_STREQ_MSG(tests[i].expected, actual, + "sep_by_space = %zu", i); + } +} + +ATF_TC_WITHOUT_HEAD(strfmon_international_currency_code); +ATF_TC_BODY(strfmon_international_currency_code, tc) +{ + const struct { + const char *locale; + const char *expected; + } tests[] = { + { "en_US.UTF-8", "[USD123.45]" }, + { "de_DE.UTF-8", "[123,45 EUR]" }, + { "C", "[123.45]" }, + }; + size_t i; + char actual[100]; + + for (i = 0; i < nitems(tests); ++i) { + if (setlocale(LC_MONETARY, tests[i].locale) == NULL) + atf_tc_skip("unable to setlocale()"); + + strfmon(actual, sizeof(actual) - 1, "[%i]", 123.45); + ATF_CHECK_STREQ(tests[i].expected, actual); + } +} + +ATF_TC(strfmon_l); +ATF_TC_HEAD(strfmon_l, tc) +{ + atf_tc_set_md_var(tc, "descr", + "checks strfmon_l under different locales"); +} +ATF_TC_BODY(strfmon_l, tc) +{ + const struct { + const char *locale; + const char *expected; + } tests[] = { + { "C", "[ **1234.57 ] [ **1234.57 ]" }, + { "de_DE.UTF-8", "[ **1234,57 €] [ **1.234,57 EUR]" }, + { "en_GB.UTF-8", "[ £**1234.57] [ GBP**1,234.57]" }, + }; + locale_t loc; + size_t i; + char buf[100]; + + for (i = 0; i < nitems(tests); ++i) { + loc = newlocale(LC_MONETARY_MASK, tests[i].locale, NULL); + ATF_REQUIRE(loc != NULL); + + strfmon_l(buf, sizeof(buf) - 1, loc, "[%^=*#6n] [%=*#6i]", + 1234.567, 1234.567); + ATF_REQUIRE_STREQ(tests[i].expected, buf); + + freelocale(loc); + } +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, strfmon_locale_thousands); + ATF_TP_ADD_TC(tp, strfmon_examples); + ATF_TP_ADD_TC(tp, strfmon_cs_precedes_0); + ATF_TP_ADD_TC(tp, strfmon_cs_precedes_1); + ATF_TP_ADD_TC(tp, strfmon_international_currency_code); + ATF_TP_ADD_TC(tp, strfmon_l); + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdlib/test-sort.h b/lib/libc/tests/stdlib/test-sort.h new file mode 100644 index 000000000000..562f92b6748c --- /dev/null +++ b/lib/libc/tests/stdlib/test-sort.h @@ -0,0 +1,271 @@ +/*- + * Copyright (C) 2004 Maxim Sobolev <sobomax@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _TEST_SORT_H +#define _TEST_SORT_H + +#include <sys/param.h> + +#include <atf-c.h> + +static int +sorthelp(const void *a, const void *b) +{ + const int *oa, *ob; + + oa = a; + ob = b; + /* Don't use "return *oa - *ob" since it's easy to cause overflow! */ + if (*oa > *ob) + return (1); + if (*oa < *ob) + return (-1); + return (0); +} + +/* Reference sorting routine (slooow!) */ +static void +ssort(int v[], int nmemb) +{ + int i, j, k; + + for (i = 0; i < nmemb; i++) { + for (j = i + 1; j < nmemb; j++) { + if (v[j] < v[i]) { + k = v[i]; + v[i] = v[j]; + v[j] = k; + } + } + } +} + +/* Some random data */ +static int initvector[1024] = { + 599853225, 371951333, -428880425, 1450668530, 85530178, -460170550, + -270680269, 717137591, 106244401, -1138072905, -113532586, + 599690958, 464993720, 107690177, 24249019, -1151018550, 794225588, + 1494905642, -62951716, -734531757, 1197251494, 1020063925, + -634536734, -216634704, 617366508, -18840190, 481010395, + -1555785724, 128221950, -1747743676, 1119012320, -252160437, + 617137855, 1159833073, -106647920, -1570719046, -769400990, + -130308848, 1186546396, 215253591, 445832488, 160263176, 777264170, + -274636650, -696134447, 1057957088, -626175254, 188632134, + -98941793, -1602334908, -373717125, 885013862, 571258221, + -399572192, 407761463, -733249776, 12094800, -367046815, 178974501, + -452638559, -516580143, -94282073, 1411115180, -1151792797, + 1442815268, 1106583674, 515269284, -74019972, 598818901, 696848474, + -657910766, -287309335, 1538095746, 143543003, 851444033, + -947573348, 296222973, 1339592140, -947505360, 872027891, + -418844714, -583075597, -324758197, -964531573, 1016795914, + -866842580, -304084402, -286930819, 338975583, 803597943, + -452483372, 1525283506, -1185830826, -596405894, 905191340, + -1638026278, 106749913, 582771197, -730713796, 56052422, + 1498040174, 644967266, 182541575, 280483468, -1932835017, + -435546874, 940106007, 1753388509, -340355019, -231577358, + -714879035, 1051182949, 204261785, 1321346243, -895289878, + -240369893, 566608506, -350777580, -1384849829, -876816409, + 1013787299, -1408035937, -222626650, 1708168486, -645148461, + 1854654, -393012507, 179327030, -1607975548, -715202732, 730588290, + 246968517, -550975254, -101136772, -174103176, 1188941016, + 2004650834, -1501389866, -2021225998, 1219042515, -464113898, + 268565342, 126451123, -1045877126, -198918003, 310177444, + 1578314812, 1828867082, 453336898, -908485523, 749777130, + -1028565802, 61360571, 241484853, -175693399, 1816211059, + 533940889, -1496933703, 385185125, -821132413, -8923391, + -1429546239, 46190992, 449247332, -20432494, 778761611, -178782699, + -811194695, -632276211, 70196094, 890817525, -1447776908, + -323783741, -62333222, 954929503, 247668507, -1394420605, + 367913886, 1364926759, 1298781924, 357923028, -73644747, + -319744305, 61718202, -1454549286, 387493522, 2018321449, + 861026653, 289973298, 1755939093, -84772204, 43425266, -1325753781, + -679938775, 1228500370, -763816405, 548903035, 1765563727, + 425183052, 1424621863, -188287424, 554385654, 751831998, + 1377088085, 66622090, 139224450, -1305816167, 650140724, 593988868, + -444913321, 589045097, 1261428989, 101292769, -780376786, + 559307348, 1644593236, -1838606833, 53570301, -680576100, + -44969538, 268718267, -632104125, 276904628, 626299449, -11761453, + 545899086, -1027343598, -432251872, 539353494, -399575006, + -568383580, -677802882, 1296513472, 801202048, 806425805, 1983363, + 850487421, 38046173, 1535399484, 1998285544, -1301751898, + -46561973, 56545408, -1107662676, 456041145, -452923904, + -262219453, -371849200, 392524281, 1650231392, 1185585356, + 126610511, -1125389657, 1015981177, -1318481342, -213835789, + 1263484054, -654361441, 1408272041, -231555284, -1121778152, + -395512897, 332277674, -349703586, 1809474399, -223731607, + -1342224737, 736165236, 67535587, 89879082, 471445838, 423788300, + -384354470, 907475706, 154886390, -1406736539, -8558766, + -203405718, -422127884, 1048520863, 747418501, 87438975, 727668160, + -914936981, 428266976, -455742009, -949014605, 487159058, + -262526850, -309451504, -76928461, 1072951428, -1370953830, + 238231681, -1831047244, -405735199, -35941848, 469048670, + 505307987, -660553590, 876537439, -314195871, 999234339, + -1405846240, -579885695, 629067031, 863481510, -742301385, + -1723403128, -153070386, 782682839, -343111818, -877101810, + 1438467302, -319951459, -1585702508, -338381888, 484108815, + -1726935191, -749923706, 1657932127, -816151395, -566321865, + -133542331, 84961164, 598275578, 1302065347, -533098653, + -1766383037, 53169714, -464201288, 617735133, 862536123, + -141730724, -1967377125, -1023736305, -1012511106, 64484992, + -1250856831, 426409332, -355670055, -298251663, -867634978, + -776124819, 74018333, -425385214, -108004251, -1192766130, + 1294193867, -109867195, -78667637, 1164855393, -826007552, + -522528066, 1160283046, -1095276403, -1218917472, -396475366, + -77394733, -425603169, 251928357, -393559355, 1488845184, + 898455978, -773338691, -37627855, -345014717, 204298578, 209084274, + 1047080643, -414348222, 26496664, 705759824, 575149152, 604904761, + 624988690, 1446041250, 1000757225, -1354896151, 1422958189, + -1607165311, -832757521, 365074225, 1171764999, 1200795217, + -203957828, 23166236, -846818698, -547439867, -790192498, + -122123561, 914298013, 66225525, -36163174, -480698856, + -1269421818, 624641703, 254923102, 1564329013, -583609221, + -649433551, -743396069, 1182455435, 102658744, 285599336, + 692480463, -852737563, -660090184, 1374350065, 72300985, 690541659, + -1194212957, -1151816525, 157415641, 487398246, 1030153072, + 933723020, 1359181027, -1303457764, -1543773272, 774306017, + -854382652, 755485667, -864943584, -63242545, -1271480354, + 157720215, -205001173, 889984130, -581583822, -473779111, + -932136577, 735326252, 428638717, 1888884222, 561493709, + -1966427364, -1710208603, 340121869, 918894036, 927172509, + 904178436, 1476687667, 90796414, 651591731, -550913123, -42218158, + 637756529, 1091478400, 124976584, -48739309, -798898083, 393581349, + -1078624722, -789291577, 1010582542, -512317005, 1222773656, + 466454593, 1183558811, 822782663, -851624703, -850404012, + 1473672600, 852527073, 1073953362, 137635828, 936104739, -86567759, + -882563252, 1845232980, -737978404, -104703380, 149802989, + -349118325, 1479797538, 1966007488, 1254228842, 414304661, + -790805785, 31583329, -76864151, 558202855, -1447047313, 716720607, + -404224238, -54107627, 1747385914, -657407390, 202940208, + 1083067056, -532861378, 163426933, -130289277, 1301785258, + -302920320, -637947485, -644895903, -1668027530, 391890467, + -126586485, -536836984, -1154536413, -339505118, -1187229462, + -670985870, -601310813, -1350055158, -482479784, 139847714, + -253955849, 5190414, -542857077, 1175835684, -584165057, + 1132775766, -592091269, -891445655, -340995936, 736395810, + 779967964, 515095845, -1138437307, -259226729, -167820100, + -801611617, -282451622, -1313679283, -1436126694, 1258773990, + -717601098, -583559645, -1307478759, 1238647247, 1589011223, + -1029216445, -107030650, 400152690, -1349079913, 1428582247, + 21546946, 208807686, -169450574, -1086213374, -1242715198, + 669098277, 416626722, -1122525014, -1389466669, -391843085, + -56318357, 421040094, 212303518, 738666684, -1185364904, + -506192760, 646735912, -1424722633, 745226976, 1019191717, + -190891744, -310320868, -373655022, -665117060, 830760000, + 583906512, -330536027, 611092636, -321344259, -1255008904, + -777959046, -523043472, 88896910, 346062204, -163734816, 336635665, + 906060268, -128891583, 740952564, 916767203, 296514859, 198670559, + 358136672, -152253676, 12038867, -1469916749, -1020980597, + -897143313, 354240970, -97959800, 814172811, 1983363241, 264990663, + 105154676, 1060463014, 430172143, 375111946, -824526943, + -708877751, -1377442616, -236559551, 684724584, 1602131358, + -42140186, -763499840, -605167, 98575879, -376577598, 1689746083, + -777973879, -1396984691, -187780326, 281040966, 1858859521, + 158395760, 1022807978, -218458812, 811779708, 1495113833, + 1192561226, 629539535, -1365196683, -1120253162, 1091681956, + 134286445, 1172218369, -34093658, -137087343, -27869226, + -180889478, 55705436, 52362743, -1745516232, -926564477, + -513701675, -1666272054, 1457952810, 843953275, -478275722, + -1240291080, 101859661, -1606687523, 916365333, 314713310, + -22002688, 1019022641, -1171741314, -129050627, -211955813, + -1020176299, 1357865293, -609819628, 724533854, -1141449545, + 22285231, -97731145, -302329687, 191910894, -1300709885, + -644951895, 640448036, -1289009824, 1445456129, 846416471, + 1821291740, -1639640821, -712724532, -447475807, 132156847, + 258067455, 324301319, 278586969, -1544082357, 636007277, 977122066, + 127462480, 365126129, 1086276825, -432124148, 896598926, + -388550179, 273357331, -845018695, -1089465389, 384439820, + -558419772, 1476422025, 730712769, 190665059, -764968927, + -789708218, 637873581, 348429858, 1386000168, -638123594, + -842010345, -607926448, 19535163, -742771490, -18323344, 246155488, + 350409468, 1290907730, -492566468, 300358636, 501876267, 252441807, + 1233551975, -1431067042, 517177374, -1540299707, -948380761, + 1253997663, 693795998, 148707823, 152894502, 98729538, -30042824, + -563057441, 723726409, 367338961, 1395435261, 217868876, + 1220652431, 953572083, -1134905155, -734486813, -587470130, + -864647866, 1030737023, 781798521, -321076732, -460548153, + 122681678, -873728847, -1286304399, -75472885, 113390881, + -1556849198, -1070802176, 924389470, 957478910, 5974049, 709413506, + 476334647, 572869787, 776298405, -8822420, -99326499, -707855342, + -1187216303, 668038414, 262454769, -1120293198, -32011040, + 249812104, -101835410, 1082281087, -570183855, -954535179, + 1384361473, -983199686, 2017611313, 328926483, -878162849, + -1202254181, -225604951, 966898955, 247213529, -1257544612, + -197005970, -1039902730, -1947925142, 1752780907, -313410699, + -464474556, 416580338, -1063356643, -292212702, 57509168, + 1034124629, 1059212593, 468522867, 845369497, 1872717306, + -1216544764, -1419907623, 1376869956, -66585488, -1590974467, + -367070705, -1456529060, 791844031, 336217635, -1106832215, + 1476739427, -751018210, -1411671555, -1880387375, -1714242595, + 1169006474, -479442380, -892267555, -1471250266, -267888858, + 808634418, 1324777189, -585449929, 1127186951, 468768901, + -2002989138, -52352849, 186493053, 1258464606, 117157186, + 445919258, 908401949, -1112221136, 863904453, -942718649, + 796754809, -38943966, -781978872, -56814078, 1273857459, + -1781920832, 209979504, 811828269, -1150814437, 850061883, + -532968763, 252506408, -885025629, 391567580, -1295702482, + 574894420, 1462248349, 1622584325, -88443443, -1122006092, + -169758578, 456282338, -443233309, 436693483, -956563815, + -480221349, 435252860, -1114099391, 1060053979, -470893945, + -1273682879, -734596176, 639950927, -1278648701, 306274747, + -410562146, 1137869228, -1970746553, 1313830798, 1248071822, + -247297704, 1015218053, -1607348116, -261817613, -821413148, + -782942639, -628711083, 240953091, -629550668, -1584736319, + 856616893, -186759752, 197905373, 541638393, 603407919, -278480495, + 410077039, 544065371, -1509627427, 402918436, -450463799, + -131169308, 249920630, 1079548609, -927911828, 1444577689, + -353002528, -224292462, 1247795279, -790844631, -40427503, + 59829765, -332587567, 1319862109, -1261340753, 121711726, + 1342756342, -643495787, 100326201, -934436714, -69134888, + -898880561, -379524737, -577234570, -805827092, -1575493557, + -289920678, -476605057, 1235714994, -317239591, 418553949, + 410053338, -204985448, 1206503615, 202610372, -932244873, + 782207875, -288661931, -806814809, 1270953679, 2060991595, + -311412846, 327279979, 1148562672, 55239149, -610114433, + -1511688434, 87619740, -433503545, 326150519, -581293393, + -97417688, -289736140, -1543886029, -1251976119, 1585774446, + 1570011421, 432602745, 486343696, -834680922, 265004849, + -1132107706, 502627197, -815873818, 249635389, 1985714127, + -1095817653, -130743522, -645266828, -334621094, 199921747, + 1059256177, 378031303, 1519740920, 925540689, 1405957844, + 1387748290, -56138046, -770637828, -187984510, -1361735163, + 1294823206, -608941238, 451860688, -510549867, 1143516283, + -779090703, 1459305047, -600335915, -1684168049, 1516622316, + -631733335, -4360068, 499778796, 587600402, -1296000335, -37959743, + -1084203927, 1162902556, 246374600, -515103645, 341724568, + -702303954, 452229900, 485108287, 1170048553, -1510148355, + 611133912, 1997927484, -142022671, -724776653, -336090522, + 708283514, -1409637378, -2052637687, 376055110, 226628105, + -1714452033, -1776158002, 369167930, 1800807012, 710680889, + -69951947, -1223849766, -1862239787, 218536127, -656411794, + -1202269188, 609634805, -224425858, 519797951, 284223080, + 869408930, 270750206, -544967439, 2097168510, 31650971, -600985417, + -165303097, -257809088, -1043223971, 1827891621, -156827355, + 499719603 +}; + +#define IVEC_LEN (nitems(initvector)) + +#endif diff --git a/lib/libc/tests/stdlib/tsearch_test.c b/lib/libc/tests/stdlib/tsearch_test.c new file mode 100644 index 000000000000..ee9deec588cb --- /dev/null +++ b/lib/libc/tests/stdlib/tsearch_test.c @@ -0,0 +1,156 @@ +/*- + * Copyright (c) 2015 Nuxi, https://nuxi.nl/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <atf-c.h> +#define _SEARCH_PRIVATE +#include <search.h> +#include <stdbool.h> +#include <stdlib.h> +#include <stdio.h> + +static int n_nodes = 0; +static int n_seen = 0; + +/* Validates the integrity of an AVL tree. */ +static inline unsigned int +tnode_assert(const posix_tnode *n) +{ + unsigned int height_left, height_right; + int balance; + + if (n == NULL) + return 0; + height_left = tnode_assert(n->llink); + height_right = tnode_assert(n->rlink); + balance = (int)height_left - (int)height_right; + ATF_CHECK(balance >= -1); + ATF_CHECK(balance <= 1); + ATF_CHECK_EQ(balance, n->balance); + return (height_left > height_right ? height_left : height_right) + 1; +} + +static int +compar(const void *a, const void *b) +{ + + return *(int *)a - *(int *)b; +} + +static void +treewalk(const posix_tnode *node, VISIT v, int level) +{ + + if (v == postorder || v == leaf) + n_seen++; +} + +ATF_TC_WITHOUT_HEAD(tsearch_test); +ATF_TC_BODY(tsearch_test, tc) +{ + /* + * Run the test below in a deterministic fashion to prevent this + * test from potentially flapping. We assume that this provides + * enough coverage. + */ +#if 0 + unsigned short random_state[3]; + arc4random_buf(random_state, sizeof(random_state)); +#else + unsigned short random_state[3] = { 26554, 13330, 3246 }; +#endif + +#define NKEYS 1000 + /* Create 1000 possible keys. */ + int keys[NKEYS]; + for (int i = 0; i < NKEYS; ++i) + keys[i] = i; + + /* Apply random operations on a binary tree and check the results. */ + posix_tnode *root = NULL; + bool present[NKEYS] = {}; + for (int i = 0; i < NKEYS * 10; ++i) { + int key = nrand48(random_state) % NKEYS; + int sample = i; + + /* + * Ensure each case is tested at least 10 times, plus a + * random sampling. + */ + if ((sample % NKEYS) > 3) + sample = nrand48(random_state) % 3; + + switch (sample) { + case 0: /* tdelete(). */ + if (present[key]) { + ATF_CHECK(tdelete(&key, &root, compar) != NULL); + present[key] = false; + ATF_CHECK(n_nodes > 0); + n_nodes--; + } else { + ATF_CHECK_EQ(NULL, + tdelete(&key, &root, compar)); + } + break; + case 1: /* tfind(). */ + if (present[key]) { + ATF_CHECK_EQ(&keys[key], + *(int **)tfind(&key, &root, compar)); + } else { + ATF_CHECK_EQ(NULL, tfind(&key, &root, compar)); + } + break; + case 2: /* tsearch(). */ + if (present[key]) { + ATF_CHECK_EQ(&keys[key], + *(int **)tsearch(&key, &root, compar)); + } else { + ATF_CHECK_EQ(&keys[key], *(int **)tsearch( + &keys[key], &root, compar)); + present[key] = true; + n_nodes++; + } + break; + } + tnode_assert(root); + } + + /* Walk the tree. */ + twalk(root, treewalk); + ATF_CHECK_EQ(n_nodes, n_seen); + + /* Remove all entries from the tree. */ + for (int key = 0; key < NKEYS; ++key) + if (present[key]) + ATF_CHECK(tdelete(&key, &root, compar) != NULL); + ATF_CHECK_EQ(NULL, root); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, tsearch_test); + + return (atf_no_error()); +} |