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