Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F156690367
D41970.id127734.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
D41970.id127734.diff
View Options
diff --git a/contrib/netbsd-tests/lib/libc/string/t_strcmp.c b/contrib/netbsd-tests/lib/libc/string/t_strcmp.c
--- a/contrib/netbsd-tests/lib/libc/string/t_strcmp.c
+++ b/contrib/netbsd-tests/lib/libc/string/t_strcmp.c
@@ -2,6 +2,10 @@
/*
* Written by J.T. Conklin <jtc@acorntoolworks.com>
+ *
+ * Portions of this software were developed by Robert Clausecker
+ * <fuz@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
+ *
* Public domain.
*/
@@ -10,6 +14,9 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
+#include <dlfcn.h>
+
+int (*volatile strcmp_fn)(const char *, const char *);
ATF_TC(strcmp_basic);
ATF_TC_HEAD(strcmp_basic, tc)
@@ -19,9 +26,6 @@
ATF_TC_BODY(strcmp_basic, tc)
{
- /* try to trick the compiler */
- int (*f)(const char *, const char *s) = strcmp;
-
unsigned int a0, a1, t;
char buf0[64];
char buf1[64];
@@ -81,7 +85,7 @@
memcpy(&buf1[a1], tab[t].val1,
strlen(tab[t].val1) + 1);
- ret = f(&buf0[a0], &buf1[a1]);
+ ret = strcmp_fn(&buf0[a0], &buf1[a1]);
if ((ret == 0 && tab[t].ret != 0) ||
(ret < 0 && tab[t].ret >= 0) ||
@@ -108,29 +112,116 @@
char buf1[10] = "xxx";
char buf2[10] = "xxy";
- ATF_CHECK(strcmp(buf1, buf1) == 0);
- ATF_CHECK(strcmp(buf2, buf2) == 0);
+ ATF_CHECK(strcmp_fn(buf1, buf1) == 0);
+ ATF_CHECK(strcmp_fn(buf2, buf2) == 0);
+
+ ATF_CHECK(strcmp_fn("x\xf6x", "xox") > 0);
+ ATF_CHECK(strcmp_fn("xxx", "xxxyyy") < 0);
+ ATF_CHECK(strcmp_fn("xxxyyy", "xxx") > 0);
+
+ ATF_CHECK(strcmp_fn(buf1 + 0, buf2 + 0) < 0);
+ ATF_CHECK(strcmp_fn(buf1 + 1, buf2 + 1) < 0);
+ ATF_CHECK(strcmp_fn(buf1 + 2, buf2 + 2) < 0);
+ ATF_CHECK(strcmp_fn(buf1 + 3, buf2 + 3) == 0);
+
+ ATF_CHECK(strcmp_fn(buf2 + 0, buf1 + 0) > 0);
+ ATF_CHECK(strcmp_fn(buf2 + 1, buf1 + 1) > 0);
+ ATF_CHECK(strcmp_fn(buf2 + 2, buf1 + 2) > 0);
+ ATF_CHECK(strcmp_fn(buf2 + 3, buf1 + 3) == 0);
+}
+
+ATF_TC(strcmp_alignments);
+ATF_TC_HEAD(strcmp_alignments, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test strcmp(3) with various alignments");
+}
+
+static void
+alignment_testcase(char *a, char *b, int want)
+{
+ int res;
+
+ res = strcmp_fn(a, b);
+ ATF_CHECK_MSG(want == (res > 0) - (res < 0),
+ "strcmp(%p \"%s\", %p \"%s\") = %d != %d",
+ (void *)a, a, (void *)b, b, res, want);
+}
+
+static void
+check_strcmp_alignments(char a[], char b[],
+ size_t a_off, size_t b_off, size_t len, size_t pos)
+{
+ char *a_str, *b_str, a_orig, b_orig;
+
+ a[a_off] = '\0';
+ b[b_off] = '\0';
+
+ a_str = a + a_off + 1;
+ b_str = b + b_off + 1;
+
+ a_str[len] = '\0';
+ b_str[len] = '\0';
+ a_str[len+1] = 'A';
+ b_str[len+1] = 'B';
- ATF_CHECK(strcmp("x\xf6x", "xox") > 0);
- ATF_CHECK(strcmp("xxx", "xxxyyy") < 0);
- ATF_CHECK(strcmp("xxxyyy", "xxx") > 0);
+ a_orig = a_str[pos];
+ b_orig = b_str[pos];
- ATF_CHECK(strcmp(buf1 + 0, buf2 + 0) < 0);
- ATF_CHECK(strcmp(buf1 + 1, buf2 + 1) < 0);
- ATF_CHECK(strcmp(buf1 + 2, buf2 + 2) < 0);
- ATF_CHECK(strcmp(buf1 + 3, buf2 + 3) == 0);
+ alignment_testcase(a_str, b_str, 0);
- ATF_CHECK(strcmp(buf2 + 0, buf1 + 0) > 0);
- ATF_CHECK(strcmp(buf2 + 1, buf1 + 1) > 0);
- ATF_CHECK(strcmp(buf2 + 2, buf1 + 2) > 0);
- ATF_CHECK(strcmp(buf2 + 3, buf1 + 3) == 0);
+ if (pos < len) {
+ a_str[pos] = '\0';
+ alignment_testcase(a_str, b_str, -1);
+ a_str[pos] = a_orig;
+ b_str[pos] = '\0';
+ alignment_testcase(a_str, b_str, 1);
+ b_str[pos] = b_orig;
+ }
+
+ a_str[pos] = 'X';
+ alignment_testcase(a_str, b_str, 1);
+ a_str[pos] = a_orig;
+ b_str[pos] = 'X';
+ alignment_testcase(a_str, b_str, -1);
+ b_str[pos] = b_orig;
+
+ a[a_off] = '-';
+ b[b_off] = '-';
+ a_str[len] = '-';
+ b_str[len] = '-';
+ a_str[len+1] = '-';
+ b_str[len+1] = '-';
+}
+
+ATF_TC_BODY(strcmp_alignments, tc)
+{
+ size_t a_off, b_off, len, pos;
+ char a[64+16+3], b[64+16+3];
+
+ memset(a, '-', sizeof(a));
+ memset(b, '-', sizeof(b));
+ a[sizeof(a) - 1] = '\0';
+ b[sizeof(b) - 1] = '\0';
+
+ for (a_off = 0; a_off < 16; a_off++)
+ for (b_off = 0; b_off < 16; b_off++)
+ for (len = 1; len <= 64; len++)
+ for (pos = 0; pos <= len; pos++)
+ check_strcmp_alignments(a, b, a_off, b_off, len, pos);
}
ATF_TP_ADD_TCS(tp)
{
+ void *dl_handle;
+
+ dl_handle = dlopen(NULL, RTLD_LAZY);
+ strcmp_fn = dlsym(dl_handle, "test_strcmp");
+ if (strcmp_fn == NULL)
+ strcmp_fn = strcmp;
ATF_TP_ADD_TC(tp, strcmp_basic);
ATF_TP_ADD_TC(tp, strcmp_simple);
+ ATF_TP_ADD_TC(tp, strcmp_alignments);
return atf_no_error();
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, May 16, 5:23 PM (5 m, 38 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
33140568
Default Alt Text
D41970.id127734.diff (4 KB)
Attached To
Mode
D41970: lib/libc/tests/string: add extended unit tests for strcmp()
Attached
Detach File
Event Timeline
Log In to Comment