xref: /minix3/tests/lib/libc/string/t_strcmp.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_strcmp.c,v 1.4 2012/03/25 08:17:54 joerg Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*
4*11be35a1SLionel Sambuc  * Written by J.T. Conklin <jtc@acorntoolworks.com>
5*11be35a1SLionel Sambuc  * Public domain.
6*11be35a1SLionel Sambuc  */
7*11be35a1SLionel Sambuc 
8*11be35a1SLionel Sambuc #include <atf-c.h>
9*11be35a1SLionel Sambuc #include <string.h>
10*11be35a1SLionel Sambuc #include <unistd.h>
11*11be35a1SLionel Sambuc #include <stdio.h>
12*11be35a1SLionel Sambuc #include <stdlib.h>
13*11be35a1SLionel Sambuc 
14*11be35a1SLionel Sambuc ATF_TC(strcmp_basic);
ATF_TC_HEAD(strcmp_basic,tc)15*11be35a1SLionel Sambuc ATF_TC_HEAD(strcmp_basic, tc)
16*11be35a1SLionel Sambuc {
17*11be35a1SLionel Sambuc         atf_tc_set_md_var(tc, "descr", "Test strcmp(3) results, #1");
18*11be35a1SLionel Sambuc }
19*11be35a1SLionel Sambuc 
ATF_TC_BODY(strcmp_basic,tc)20*11be35a1SLionel Sambuc ATF_TC_BODY(strcmp_basic, tc)
21*11be35a1SLionel Sambuc {
22*11be35a1SLionel Sambuc 	/* try to trick the compiler */
23*11be35a1SLionel Sambuc 	int (*f)(const char *, const char *s) = strcmp;
24*11be35a1SLionel Sambuc 
25*11be35a1SLionel Sambuc 	unsigned int a0, a1, t;
26*11be35a1SLionel Sambuc 	char buf0[64];
27*11be35a1SLionel Sambuc 	char buf1[64];
28*11be35a1SLionel Sambuc 	int ret;
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc 	struct tab {
31*11be35a1SLionel Sambuc 		const char*	val0;
32*11be35a1SLionel Sambuc 		const char*	val1;
33*11be35a1SLionel Sambuc 		int		ret;
34*11be35a1SLionel Sambuc 	};
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc 	const struct tab tab[] = {
37*11be35a1SLionel Sambuc 		{ "",			"",			0 },
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc 		{ "a",			"a",			0 },
40*11be35a1SLionel Sambuc 		{ "a",			"b",			-1 },
41*11be35a1SLionel Sambuc 		{ "b",			"a",			+1 },
42*11be35a1SLionel Sambuc 		{ "",			"a",			-1 },
43*11be35a1SLionel Sambuc 		{ "a",			"",			+1 },
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc 		{ "aa",			"aa",			0 },
46*11be35a1SLionel Sambuc 		{ "aa",			"ab",			-1 },
47*11be35a1SLionel Sambuc 		{ "ab",			"aa",			+1 },
48*11be35a1SLionel Sambuc 		{ "a",			"aa",			-1 },
49*11be35a1SLionel Sambuc 		{ "aa",			"a",			+1 },
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc 		{ "aaa",		"aaa",			0 },
52*11be35a1SLionel Sambuc 		{ "aaa",		"aab",			-1 },
53*11be35a1SLionel Sambuc 		{ "aab",		"aaa",			+1 },
54*11be35a1SLionel Sambuc 		{ "aa",			"aaa",			-1 },
55*11be35a1SLionel Sambuc 		{ "aaa",		"aa",			+1 },
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc 		{ "aaaa",		"aaaa",			0 },
58*11be35a1SLionel Sambuc 		{ "aaaa",		"aaab",			-1 },
59*11be35a1SLionel Sambuc 		{ "aaab",		"aaaa",			+1 },
60*11be35a1SLionel Sambuc 		{ "aaa",		"aaaa",			-1 },
61*11be35a1SLionel Sambuc 		{ "aaaa",		"aaa",			+1 },
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc 		{ "aaaaa",		"aaaaa",		0 },
64*11be35a1SLionel Sambuc 		{ "aaaaa",		"aaaab",		-1 },
65*11be35a1SLionel Sambuc 		{ "aaaab",		"aaaaa",		+1 },
66*11be35a1SLionel Sambuc 		{ "aaaa",		"aaaaa",		-1 },
67*11be35a1SLionel Sambuc 		{ "aaaaa",		"aaaa",			+1 },
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc 		{ "aaaaaa",		"aaaaaa",		0 },
70*11be35a1SLionel Sambuc 		{ "aaaaaa",		"aaaaab",		-1 },
71*11be35a1SLionel Sambuc 		{ "aaaaab",		"aaaaaa",		+1 },
72*11be35a1SLionel Sambuc 		{ "aaaaa",		"aaaaaa",		-1 },
73*11be35a1SLionel Sambuc 		{ "aaaaaa",		"aaaaa",		+1 },
74*11be35a1SLionel Sambuc 	};
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc 	for (a0 = 0; a0 < sizeof(long); ++a0) {
77*11be35a1SLionel Sambuc 		for (a1 = 0; a1 < sizeof(long); ++a1) {
78*11be35a1SLionel Sambuc 			for (t = 0; t < __arraycount(tab); ++t) {
79*11be35a1SLionel Sambuc 				memcpy(&buf0[a0], tab[t].val0,
80*11be35a1SLionel Sambuc 				       strlen(tab[t].val0) + 1);
81*11be35a1SLionel Sambuc 				memcpy(&buf1[a1], tab[t].val1,
82*11be35a1SLionel Sambuc 				       strlen(tab[t].val1) + 1);
83*11be35a1SLionel Sambuc 
84*11be35a1SLionel Sambuc 				ret = f(&buf0[a0], &buf1[a1]);
85*11be35a1SLionel Sambuc 
86*11be35a1SLionel Sambuc 				if ((ret == 0 && tab[t].ret != 0) ||
87*11be35a1SLionel Sambuc 				    (ret <  0 && tab[t].ret >= 0) ||
88*11be35a1SLionel Sambuc 				    (ret >  0 && tab[t].ret <= 0)) {
89*11be35a1SLionel Sambuc 					fprintf(stderr, "a0 %d, a1 %d, t %d\n",
90*11be35a1SLionel Sambuc 					    a0, a1, t);
91*11be35a1SLionel Sambuc 					fprintf(stderr, "\"%s\" \"%s\" %d\n",
92*11be35a1SLionel Sambuc 					    &buf0[a0], &buf1[a1], ret);
93*11be35a1SLionel Sambuc 					atf_tc_fail("Check stderr for details");
94*11be35a1SLionel Sambuc 				}
95*11be35a1SLionel Sambuc 			}
96*11be35a1SLionel Sambuc 		}
97*11be35a1SLionel Sambuc 	}
98*11be35a1SLionel Sambuc }
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc ATF_TC(strcmp_simple);
ATF_TC_HEAD(strcmp_simple,tc)101*11be35a1SLionel Sambuc ATF_TC_HEAD(strcmp_simple, tc)
102*11be35a1SLionel Sambuc {
103*11be35a1SLionel Sambuc         atf_tc_set_md_var(tc, "descr", "Test strcmp(3) results, #2");
104*11be35a1SLionel Sambuc }
105*11be35a1SLionel Sambuc 
ATF_TC_BODY(strcmp_simple,tc)106*11be35a1SLionel Sambuc ATF_TC_BODY(strcmp_simple, tc)
107*11be35a1SLionel Sambuc {
108*11be35a1SLionel Sambuc 	char buf1[10] = "xxx";
109*11be35a1SLionel Sambuc 	char buf2[10] = "xxy";
110*11be35a1SLionel Sambuc 
111*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf1, buf1) == 0);
112*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf2, buf2) == 0);
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp("x\xf6x", "xox") > 0);
115*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp("xxx", "xxxyyy") < 0);
116*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp("xxxyyy", "xxx") > 0);
117*11be35a1SLionel Sambuc 
118*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf1 + 0, buf2 + 0) < 0);
119*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf1 + 1, buf2 + 1) < 0);
120*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf1 + 2, buf2 + 2) < 0);
121*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf1 + 3, buf2 + 3) == 0);
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf2 + 0, buf1 + 0) > 0);
124*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf2 + 1, buf1 + 1) > 0);
125*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf2 + 2, buf1 + 2) > 0);
126*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf2 + 3, buf1 + 3) == 0);
127*11be35a1SLionel Sambuc }
128*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)129*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
130*11be35a1SLionel Sambuc {
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, strcmp_basic);
133*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, strcmp_simple);
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc 	return atf_no_error();
136*11be35a1SLionel Sambuc }
137