Path: blob/main/lib/libc/tests/string/strcmp_test.c
39485 views
/*-1* Copyright (c) 2023 The FreeBSD Foundation2*3* This software was developed by Robert Clausecker <[email protected]>4* under sponsorship from the FreeBSD Foundation.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE26*/2728#include <sys/cdefs.h>2930#include <atf-c.h>31#include <dlfcn.h>32#include <string.h>3334int (*volatile strcmp_fn)(const char *, const char *);3536ATF_TC(strcmp_alignments);37ATF_TC_HEAD(strcmp_alignments, tc)38{39atf_tc_set_md_var(tc, "descr", "Test strcmp(3) with various alignments");40}4142static void43alignment_testcase(char *a, char *b, int want)44{45int res;4647res = strcmp_fn(a, b);48ATF_CHECK_MSG(want == (res > 0) - (res < 0),49"strcmp(%p \"%s\", %p \"%s\") = %d != %d",50(void *)a, a, (void *)b, b, res, want);51}5253static void54check_strcmp_alignments(char a[], char b[],55size_t a_off, size_t b_off, size_t len, size_t pos)56{57char *a_str, *b_str, a_orig, b_orig;5859a[a_off] = '\0';60b[b_off] = '\0';6162a_str = a + a_off + 1;63b_str = b + b_off + 1;6465a_str[len] = '\0';66b_str[len] = '\0';67a_str[len+1] = 'A';68b_str[len+1] = 'B';6970a_orig = a_str[pos];71b_orig = b_str[pos];7273alignment_testcase(a_str, b_str, 0);7475if (pos < len) {76a_str[pos] = '\0';77alignment_testcase(a_str, b_str, -1);78a_str[pos] = a_orig;79b_str[pos] = '\0';80alignment_testcase(a_str, b_str, 1);81b_str[pos] = b_orig;82}8384a_str[pos] = 'X';85alignment_testcase(a_str, b_str, 1);86a_str[pos] = a_orig;87b_str[pos] = 'X';88alignment_testcase(a_str, b_str, -1);89b_str[pos] = b_orig;9091a[a_off] = '-';92b[b_off] = '-';93a_str[len] = '-';94b_str[len] = '-';95a_str[len+1] = '-';96b_str[len+1] = '-';97}9899ATF_TC_BODY(strcmp_alignments, tc)100{101size_t a_off, b_off, len, pos;102/* 16B alignment offset + 64B buffer + sentinel before/after + NUL */103char a[64+16+3], b[64+16+3];104105memset(a, '-', sizeof(a));106memset(b, '-', sizeof(b));107a[sizeof(a) - 1] = '\0';108b[sizeof(b) - 1] = '\0';109110/* check alignment offsets relevant for SSE routines */111for (a_off = 0; a_off < 16; a_off++)112for (b_off = 0; b_off < 16; b_off++)113/* ensure main loop (@ 32B) is completed at least once */114for (len = 1; len <= 64; len++)115for (pos = 0; pos <= len; pos++)116check_strcmp_alignments(a, b, a_off, b_off, len, pos);117}118119ATF_TP_ADD_TCS(tp)120{121void *dl_handle;122123dl_handle = dlopen(NULL, RTLD_LAZY);124strcmp_fn = dlsym(dl_handle, "test_strcmp");125if (strcmp_fn == NULL)126strcmp_fn = strcmp;127128ATF_TP_ADD_TC(tp, strcmp_alignments);129130return atf_no_error();131}132133134