11.1Sjruoho/* $NetBSD: t_strrchr.c,v 1.1 2011/07/07 08:59:33 jruoho Exp $ */
21.1Sjruoho
31.1Sjruoho/*
41.1Sjruoho * Written by J.T. Conklin <jtc@acorntoolworks.com>
51.1Sjruoho * Public domain.
61.1Sjruoho */
71.1Sjruoho
81.1Sjruoho#include <atf-c.h>
91.1Sjruoho#include <string.h>
101.1Sjruoho#include <unistd.h>
111.1Sjruoho#include <stdio.h>
121.1Sjruoho#include <stdlib.h>
131.1Sjruoho
141.1SjruohoATF_TC(strrchr_basic);
151.1SjruohoATF_TC_HEAD(strrchr_basic, tc)
161.1Sjruoho{
171.1Sjruoho        atf_tc_set_md_var(tc, "descr", "Test strrchr(3) results");
181.1Sjruoho}
191.1Sjruoho
201.1SjruohoATF_TC_BODY(strrchr_basic, tc)
211.1Sjruoho{
221.1Sjruoho	/* try to trick the compiler */
231.1Sjruoho	char * (*f)(const char *, int) = strrchr;
241.1Sjruoho
251.1Sjruoho	unsigned int a, t;
261.1Sjruoho	char *off, *off2;
271.1Sjruoho	char buf[32];
281.1Sjruoho
291.1Sjruoho	struct tab {
301.1Sjruoho		const char*	val;
311.1Sjruoho		char		match;
321.1Sjruoho		ssize_t		f_off;	/* offset of first match */
331.1Sjruoho		ssize_t		l_off;	/* offset of last match */
341.1Sjruoho	};
351.1Sjruoho
361.1Sjruoho	const struct tab tab[] = {
371.1Sjruoho		{ "",			0, 0, 0 },
381.1Sjruoho		{ "a",			0, 0, 0 },
391.1Sjruoho		{ "aa",			0, 0, 0 },
401.1Sjruoho		{ "abc",		0, 0, 0 },
411.1Sjruoho		{ "abcd",		0, 0, 0 },
421.1Sjruoho		{ "abcde",		0, 0, 0 },
431.1Sjruoho		{ "abcdef",		0, 0, 0 },
441.1Sjruoho		{ "abcdefg",		0, 0, 0 },
451.1Sjruoho		{ "abcdefgh",		0, 0, 0 },
461.1Sjruoho
471.1Sjruoho		{ "/",			1, 0, 0 },
481.1Sjruoho		{ "//",                 1, 0, 1 },
491.1Sjruoho		{ "/a",                 1, 0, 0 },
501.1Sjruoho		{ "/a/",                1, 0, 2 },
511.1Sjruoho		{ "/ab",                1, 0, 0 },
521.1Sjruoho		{ "/ab/",               1, 0, 3 },
531.1Sjruoho		{ "/abc",               1, 0, 0 },
541.1Sjruoho		{ "/abc/",              1, 0, 4 },
551.1Sjruoho		{ "/abcd",              1, 0, 0 },
561.1Sjruoho		{ "/abcd/",             1, 0, 5 },
571.1Sjruoho		{ "/abcde",             1, 0, 0 },
581.1Sjruoho		{ "/abcde/",            1, 0, 6 },
591.1Sjruoho		{ "/abcdef",            1, 0, 0 },
601.1Sjruoho		{ "/abcdef/",           1, 0, 7 },
611.1Sjruoho		{ "/abcdefg",           1, 0, 0 },
621.1Sjruoho		{ "/abcdefg/",          1, 0, 8 },
631.1Sjruoho		{ "/abcdefgh",          1, 0, 0 },
641.1Sjruoho		{ "/abcdefgh/",         1, 0, 9 },
651.1Sjruoho
661.1Sjruoho		{ "a/",                 1, 1, 1 },
671.1Sjruoho		{ "a//",                1, 1, 2 },
681.1Sjruoho		{ "a/a",                1, 1, 1 },
691.1Sjruoho		{ "a/a/",               1, 1, 3 },
701.1Sjruoho		{ "a/ab",               1, 1, 1 },
711.1Sjruoho		{ "a/ab/",              1, 1, 4 },
721.1Sjruoho		{ "a/abc",              1, 1, 1 },
731.1Sjruoho		{ "a/abc/",             1, 1, 5 },
741.1Sjruoho		{ "a/abcd",             1, 1, 1 },
751.1Sjruoho		{ "a/abcd/",            1, 1, 6 },
761.1Sjruoho		{ "a/abcde",            1, 1, 1 },
771.1Sjruoho		{ "a/abcde/",           1, 1, 7 },
781.1Sjruoho		{ "a/abcdef",           1, 1, 1 },
791.1Sjruoho		{ "a/abcdef/",          1, 1, 8 },
801.1Sjruoho		{ "a/abcdefg",          1, 1, 1 },
811.1Sjruoho		{ "a/abcdefg/",         1, 1, 9 },
821.1Sjruoho		{ "a/abcdefgh",         1, 1, 1 },
831.1Sjruoho		{ "a/abcdefgh/",        1, 1, 10 },
841.1Sjruoho
851.1Sjruoho		{ "ab/",                1, 2, 2 },
861.1Sjruoho		{ "ab//",               1, 2, 3 },
871.1Sjruoho		{ "ab/a",               1, 2, 2 },
881.1Sjruoho		{ "ab/a/",              1, 2, 4 },
891.1Sjruoho		{ "ab/ab",              1, 2, 2 },
901.1Sjruoho		{ "ab/ab/",             1, 2, 5 },
911.1Sjruoho		{ "ab/abc",             1, 2, 2 },
921.1Sjruoho		{ "ab/abc/",            1, 2, 6 },
931.1Sjruoho		{ "ab/abcd",            1, 2, 2 },
941.1Sjruoho		{ "ab/abcd/",           1, 2, 7 },
951.1Sjruoho		{ "ab/abcde",           1, 2, 2 },
961.1Sjruoho		{ "ab/abcde/",          1, 2, 8 },
971.1Sjruoho		{ "ab/abcdef",          1, 2, 2 },
981.1Sjruoho		{ "ab/abcdef/",         1, 2, 9 },
991.1Sjruoho		{ "ab/abcdefg",         1, 2, 2 },
1001.1Sjruoho		{ "ab/abcdefg/",        1, 2, 10 },
1011.1Sjruoho		{ "ab/abcdefgh",        1, 2, 2 },
1021.1Sjruoho		{ "ab/abcdefgh/",       1, 2, 11 },
1031.1Sjruoho
1041.1Sjruoho		{ "abc/",               1, 3, 3 },
1051.1Sjruoho		{ "abc//",              1, 3, 4 },
1061.1Sjruoho		{ "abc/a",              1, 3, 3 },
1071.1Sjruoho		{ "abc/a/",             1, 3, 5 },
1081.1Sjruoho		{ "abc/ab",             1, 3, 3 },
1091.1Sjruoho		{ "abc/ab/",            1, 3, 6 },
1101.1Sjruoho		{ "abc/abc",            1, 3, 3 },
1111.1Sjruoho		{ "abc/abc/",           1, 3, 7 },
1121.1Sjruoho		{ "abc/abcd",           1, 3, 3 },
1131.1Sjruoho		{ "abc/abcd/",          1, 3, 8 },
1141.1Sjruoho		{ "abc/abcde",          1, 3, 3 },
1151.1Sjruoho		{ "abc/abcde/",         1, 3, 9 },
1161.1Sjruoho		{ "abc/abcdef",         1, 3, 3 },
1171.1Sjruoho		{ "abc/abcdef/",        1, 3, 10 },
1181.1Sjruoho		{ "abc/abcdefg",        1, 3, 3 },
1191.1Sjruoho		{ "abc/abcdefg/",       1, 3, 11 },
1201.1Sjruoho		{ "abc/abcdefgh",       1, 3, 3 },
1211.1Sjruoho		{ "abc/abcdefgh/",      1, 3, 12 },
1221.1Sjruoho
1231.1Sjruoho		{ "abcd/",              1, 4, 4 },
1241.1Sjruoho		{ "abcd//",             1, 4, 5 },
1251.1Sjruoho		{ "abcd/a",             1, 4, 4 },
1261.1Sjruoho		{ "abcd/a/",            1, 4, 6 },
1271.1Sjruoho		{ "abcd/ab",            1, 4, 4 },
1281.1Sjruoho		{ "abcd/ab/",           1, 4, 7 },
1291.1Sjruoho		{ "abcd/abc",           1, 4, 4 },
1301.1Sjruoho		{ "abcd/abc/",          1, 4, 8 },
1311.1Sjruoho		{ "abcd/abcd",          1, 4, 4 },
1321.1Sjruoho		{ "abcd/abcd/",         1, 4, 9 },
1331.1Sjruoho		{ "abcd/abcde",         1, 4, 4 },
1341.1Sjruoho		{ "abcd/abcde/",        1, 4, 10 },
1351.1Sjruoho		{ "abcd/abcdef",        1, 4, 4 },
1361.1Sjruoho		{ "abcd/abcdef/",       1, 4, 11 },
1371.1Sjruoho		{ "abcd/abcdefg",       1, 4, 4 },
1381.1Sjruoho		{ "abcd/abcdefg/",      1, 4, 12 },
1391.1Sjruoho		{ "abcd/abcdefgh",      1, 4, 4 },
1401.1Sjruoho		{ "abcd/abcdefgh/",     1, 4, 13 },
1411.1Sjruoho
1421.1Sjruoho		{ "abcde/",             1, 5, 5 },
1431.1Sjruoho		{ "abcde//",            1, 5, 6 },
1441.1Sjruoho		{ "abcde/a",            1, 5, 5 },
1451.1Sjruoho		{ "abcde/a/",           1, 5, 7 },
1461.1Sjruoho		{ "abcde/ab",           1, 5, 5 },
1471.1Sjruoho		{ "abcde/ab/",          1, 5, 8 },
1481.1Sjruoho		{ "abcde/abc",          1, 5, 5 },
1491.1Sjruoho		{ "abcde/abc/",         1, 5, 9 },
1501.1Sjruoho		{ "abcde/abcd",         1, 5, 5 },
1511.1Sjruoho		{ "abcde/abcd/",        1, 5, 10 },
1521.1Sjruoho		{ "abcde/abcde",        1, 5, 5 },
1531.1Sjruoho		{ "abcde/abcde/",       1, 5, 11 },
1541.1Sjruoho		{ "abcde/abcdef",       1, 5, 5 },
1551.1Sjruoho		{ "abcde/abcdef/",      1, 5, 12 },
1561.1Sjruoho		{ "abcde/abcdefg",      1, 5, 5 },
1571.1Sjruoho		{ "abcde/abcdefg/",     1, 5, 13 },
1581.1Sjruoho		{ "abcde/abcdefgh",     1, 5, 5 },
1591.1Sjruoho		{ "abcde/abcdefgh/",    1, 5, 14 },
1601.1Sjruoho
1611.1Sjruoho		{ "abcdef/",            1, 6, 6 },
1621.1Sjruoho		{ "abcdef//",           1, 6, 7 },
1631.1Sjruoho		{ "abcdef/a",           1, 6, 6 },
1641.1Sjruoho		{ "abcdef/a/",          1, 6, 8 },
1651.1Sjruoho		{ "abcdef/ab",          1, 6, 6 },
1661.1Sjruoho		{ "abcdef/ab/",         1, 6, 9 },
1671.1Sjruoho		{ "abcdef/abc",         1, 6, 6 },
1681.1Sjruoho		{ "abcdef/abc/",        1, 6, 10 },
1691.1Sjruoho		{ "abcdef/abcd",        1, 6, 6 },
1701.1Sjruoho		{ "abcdef/abcd/",       1, 6, 11 },
1711.1Sjruoho		{ "abcdef/abcde",       1, 6, 6 },
1721.1Sjruoho		{ "abcdef/abcde/",      1, 6, 12 },
1731.1Sjruoho		{ "abcdef/abcdef",      1, 6, 6 },
1741.1Sjruoho		{ "abcdef/abcdef/",     1, 6, 13 },
1751.1Sjruoho		{ "abcdef/abcdefg",     1, 6, 6 },
1761.1Sjruoho		{ "abcdef/abcdefg/",    1, 6, 14 },
1771.1Sjruoho		{ "abcdef/abcdefgh",    1, 6, 6 },
1781.1Sjruoho		{ "abcdef/abcdefgh/",   1, 6, 15 },
1791.1Sjruoho
1801.1Sjruoho		{ "abcdefg/",           1, 7, 7 },
1811.1Sjruoho		{ "abcdefg//",          1, 7, 8 },
1821.1Sjruoho		{ "abcdefg/a",          1, 7, 7 },
1831.1Sjruoho		{ "abcdefg/a/",         1, 7, 9 },
1841.1Sjruoho		{ "abcdefg/ab",         1, 7, 7 },
1851.1Sjruoho		{ "abcdefg/ab/",        1, 7, 10 },
1861.1Sjruoho		{ "abcdefg/abc",        1, 7, 7 },
1871.1Sjruoho		{ "abcdefg/abc/",       1, 7, 11 },
1881.1Sjruoho		{ "abcdefg/abcd",       1, 7, 7 },
1891.1Sjruoho		{ "abcdefg/abcd/",      1, 7, 12 },
1901.1Sjruoho		{ "abcdefg/abcde",      1, 7, 7 },
1911.1Sjruoho		{ "abcdefg/abcde/",     1, 7, 13 },
1921.1Sjruoho		{ "abcdefg/abcdef",     1, 7, 7 },
1931.1Sjruoho		{ "abcdefg/abcdef/",    1, 7, 14 },
1941.1Sjruoho		{ "abcdefg/abcdefg",    1, 7, 7 },
1951.1Sjruoho		{ "abcdefg/abcdefg/",   1, 7, 15 },
1961.1Sjruoho		{ "abcdefg/abcdefgh",   1, 7, 7 },
1971.1Sjruoho		{ "abcdefg/abcdefgh/",  1, 7, 16 },
1981.1Sjruoho
1991.1Sjruoho		{ "abcdefgh/",          1, 8, 8 },
2001.1Sjruoho		{ "abcdefgh//",         1, 8, 9 },
2011.1Sjruoho		{ "abcdefgh/a",         1, 8, 8 },
2021.1Sjruoho		{ "abcdefgh/a/",        1, 8, 10 },
2031.1Sjruoho		{ "abcdefgh/ab",        1, 8, 8 },
2041.1Sjruoho		{ "abcdefgh/ab/",       1, 8, 11 },
2051.1Sjruoho		{ "abcdefgh/abc",       1, 8, 8 },
2061.1Sjruoho		{ "abcdefgh/abc/",      1, 8, 12 },
2071.1Sjruoho		{ "abcdefgh/abcd",      1, 8, 8 },
2081.1Sjruoho		{ "abcdefgh/abcd/",     1, 8, 13 },
2091.1Sjruoho		{ "abcdefgh/abcde",     1, 8, 8 },
2101.1Sjruoho		{ "abcdefgh/abcde/",    1, 8, 14 },
2111.1Sjruoho		{ "abcdefgh/abcdef",    1, 8, 8 },
2121.1Sjruoho		{ "abcdefgh/abcdef/",   1, 8, 15 },
2131.1Sjruoho		{ "abcdefgh/abcdefg",   1, 8, 8 },
2141.1Sjruoho		{ "abcdefgh/abcdefg/",  1, 8, 16 },
2151.1Sjruoho		{ "abcdefgh/abcdefgh",  1, 8, 8 },
2161.1Sjruoho		{ "abcdefgh/abcdefgh/", 1, 8, 17 },
2171.1Sjruoho	};
2181.1Sjruoho
2191.1Sjruoho	for (a = 0; a < sizeof(long); ++a) {
2201.1Sjruoho		for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
2211.1Sjruoho			strcpy(&buf[a], tab[t].val);
2221.1Sjruoho
2231.1Sjruoho			off = f(&buf[a], '/');
2241.1Sjruoho			if (tab[t].match == 0) {
2251.1Sjruoho				if (off != 0) {
2261.1Sjruoho					fprintf(stderr, "a %d, t %d\n", a, t);
2271.1Sjruoho					atf_tc_fail("strrchr should not have "
2281.1Sjruoho					    "found the character");
2291.1Sjruoho				}
2301.1Sjruoho			} else if (tab[t].match == 1) {
2311.1Sjruoho				 if (tab[t].l_off != (off - &buf[a])) {
2321.1Sjruoho					fprintf(stderr, "a %d, t %d\n", a, t);
2331.1Sjruoho					atf_tc_fail("strrchr returns wrong "
2341.1Sjruoho					    "offset");
2351.1Sjruoho				}
2361.1Sjruoho			} else {
2371.1Sjruoho				fprintf(stderr, "a %d, t %d\n", a, t);
2381.1Sjruoho				atf_tc_fail("bad test case data");
2391.1Sjruoho			}
2401.1Sjruoho
2411.1Sjruoho			/* check zero extension of char arg */
2421.1Sjruoho			off2 = f(&buf[a], 0xffffff00 | '/');
2431.1Sjruoho			if (off != off2) {
2441.1Sjruoho				fprintf(stderr, "a %d, t %d\n", a, t);
2451.1Sjruoho				atf_tc_fail("zero extension of char arg fails");
2461.1Sjruoho			}
2471.1Sjruoho		}
2481.1Sjruoho	}
2491.1Sjruoho}
2501.1Sjruoho
2511.1SjruohoATF_TP_ADD_TCS(tp)
2521.1Sjruoho{
2531.1Sjruoho
2541.1Sjruoho	ATF_TP_ADD_TC(tp, strrchr_basic);
2551.1Sjruoho
2561.1Sjruoho	return atf_no_error();
2571.1Sjruoho}
258