Home | History | Annotate | Line # | Download | only in string
t_memchr.c revision 1.1
      1 /* $NetBSD: t_memchr.c,v 1.1 2011/07/07 08:59:32 jruoho Exp $ */
      2 
      3 /*
      4  * Written by J.T. Conklin <jtc (at) 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(memchr_basic);
     15 ATF_TC_HEAD(memchr_basic, tc)
     16 {
     17         atf_tc_set_md_var(tc, "descr", "Test memchr(3) results");
     18 }
     19 
     20 ATF_TC_BODY(memchr_basic, tc)
     21 {
     22 	/* try to trick the compiler */
     23 	void * (*f)(const void *, int, size_t) = memchr;
     24 
     25 	unsigned int a, t;
     26 	void *off, *off2;
     27 	char buf[32];
     28 
     29 	struct tab {
     30 		const char	*val;
     31 		size_t  	 len;
     32 		char		 match;
     33 		ssize_t		 off;
     34 	};
     35 
     36 	const struct tab tab[] = {
     37 		{ "",			0, 0, 0 },
     38 
     39 		{ "/",			0, 0, 0 },
     40 		{ "/",			1, 1, 0 },
     41 		{ "/a",			2, 1, 0 },
     42 		{ "/ab",		3, 1, 0 },
     43 		{ "/abc",		4, 1, 0 },
     44 		{ "/abcd",		5, 1, 0 },
     45 		{ "/abcde",		6, 1, 0 },
     46 		{ "/abcdef",		7, 1, 0 },
     47 		{ "/abcdefg",		8, 1, 0 },
     48 
     49 		{ "a/",			1, 0, 0 },
     50 		{ "a/",			2, 1, 1 },
     51 		{ "a/b",		3, 1, 1 },
     52 		{ "a/bc",		4, 1, 1 },
     53 		{ "a/bcd",		5, 1, 1 },
     54 		{ "a/bcde",		6, 1, 1 },
     55 		{ "a/bcdef",		7, 1, 1 },
     56 		{ "a/bcdefg",		8, 1, 1 },
     57 
     58 		{ "ab/",		2, 0, 0 },
     59 		{ "ab/",		3, 1, 2 },
     60 		{ "ab/c",		4, 1, 2 },
     61 		{ "ab/cd",		5, 1, 2 },
     62 		{ "ab/cde",		6, 1, 2 },
     63 		{ "ab/cdef",		7, 1, 2 },
     64 		{ "ab/cdefg",		8, 1, 2 },
     65 
     66 		{ "abc/",		3, 0, 0 },
     67 		{ "abc/",		4, 1, 3 },
     68 		{ "abc/d",		5, 1, 3 },
     69 		{ "abc/de",		6, 1, 3 },
     70 		{ "abc/def",		7, 1, 3 },
     71 		{ "abc/defg",		8, 1, 3 },
     72 
     73 		{ "abcd/",		4, 0, 0 },
     74 		{ "abcd/",		5, 1, 4 },
     75 		{ "abcd/e",		6, 1, 4 },
     76 		{ "abcd/ef",		7, 1, 4 },
     77 		{ "abcd/efg",		8, 1, 4 },
     78 
     79 		{ "abcde/",		5, 0, 0 },
     80 		{ "abcde/",		6, 1, 5 },
     81 		{ "abcde/f",		7, 1, 5 },
     82 		{ "abcde/fg",		8, 1, 5 },
     83 
     84 		{ "abcdef/",		6, 0, 0 },
     85 		{ "abcdef/",		7, 1, 6 },
     86 		{ "abcdef/g",		8, 1, 6 },
     87 
     88 		{ "abcdefg/",		7, 0, 0 },
     89 		{ "abcdefg/",		8, 1, 7 },
     90 
     91 		{ "\xff\xff\xff\xff" "efg/",	8, 1, 7 },
     92 		{ "a" "\xff\xff\xff\xff" "fg/",	8, 1, 7 },
     93 		{ "ab" "\xff\xff\xff\xff" "g/",	8, 1, 7 },
     94 		{ "abc" "\xff\xff\xff\xff" "/",	8, 1, 7 },
     95 	};
     96 
     97 	for (a = 1; a < 1 + sizeof(long); ++a) {
     98 		for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
     99 			buf[a-1] = '/';
    100 			strcpy(&buf[a], tab[t].val);
    101 
    102 			off = f(&buf[a], '/', tab[t].len);
    103 			if (tab[t].match == 0) {
    104 				if (off != 0) {
    105 					fprintf(stderr, "a = %d, t = %d\n",
    106 					    a, t);
    107 					atf_tc_fail("should not have found "
    108 					    " char past len");
    109 				}
    110 			} else if (tab[t].match == 1) {
    111 				if (tab[t].off != ((char*)off - &buf[a])) {
    112 					fprintf(stderr, "a = %d, t = %d\n",
    113 					    a, t);
    114 					atf_tc_fail("char not found at "
    115 					    "correct offset");
    116 				}
    117 	    		} else {
    118 				fprintf(stderr, "a = %d, t = %d\n", a, t);
    119 				atf_tc_fail("Corrupt test case data");
    120 			}
    121 
    122 			/* check zero extension of char arg */
    123 			off2 = f(&buf[a], 0xffffff00 | '/', tab[t].len);
    124 			if (off2 != off)
    125 				atf_tc_fail("zero extension of char arg "
    126 				    "failed");
    127 		}
    128 	}
    129 }
    130 
    131 ATF_TP_ADD_TCS(tp)
    132 {
    133 
    134 	ATF_TP_ADD_TC(tp, memchr_basic);
    135 
    136 	return atf_no_error();
    137 }
    138