1313535Sngie/*	$NetBSD: t_memmem.c,v 1.3 2017/01/11 18:07:37 christos Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2005 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Perry E. Metzger of Metzger, Dowdeswell & Co. LLC.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie
32272343Sngie#include <atf-c.h>
33272343Sngie
34272343Sngie#include <stdio.h>
35272343Sngie#include <stdlib.h>
36272343Sngie#include <string.h>
37272343Sngie
38272343Sngiechar p0[] = "";
39272343Sngieint lp0 = 0;
40272343Sngiechar p1[] = "0123";
41272343Sngieint lp1 = 4;
42272343Sngiechar p2[] = "456";
43272343Sngieint lp2 = 3;
44272343Sngiechar p3[] = "789";
45272343Sngieint lp3 = 3;
46272343Sngiechar p4[] = "abc";
47272343Sngieint lp4 = 3;
48272343Sngiechar p5[] = "0";
49272343Sngieint lp5 = 1;
50272343Sngiechar p6[] = "9";
51272343Sngieint lp6 = 1;
52272343Sngiechar p7[] = "654";
53272343Sngieint lp7 = 3;
54313513Sngiechar p8[] = "89abc";
55313513Sngieint lp8 = 5;
56272343Sngie
57272343Sngiechar b0[] = "";
58272343Sngieint lb0 = 0;
59272343Sngiechar b1[] = "0";
60272343Sngieint lb1 = 1;
61272343Sngiechar b2[] = "0123456789";
62272343Sngieint lb2 = 10;
63272343Sngie
64272343Sngie#define expect(b)							\
65272343Sngie	if (!(b)) {							\
66272343Sngie		fprintf(stderr, "failed on line %d\n", __LINE__);	\
67272343Sngie		atf_tc_fail("Check stderr for test id/line");		\
68272343Sngie	}
69272343Sngie
70272343SngieATF_TC(memmem_basic);
71272343SngieATF_TC_HEAD(memmem_basic, tc)
72272343Sngie{
73272343Sngie
74272343Sngie	atf_tc_set_md_var(tc, "descr", "Test memmem results");
75272343Sngie}
76272343Sngie
77272343SngieATF_TC_BODY(memmem_basic, tc)
78272343Sngie{
79272343Sngie
80313480Sngie#if defined(__darwin__)
81276478Sngie	expect(memmem(b2, lb2, p0, lp0) == NULL);
82276478Sngie	expect(memmem(b0, lb0, p0, lp0) == NULL);
83276478Sngie#else
84272343Sngie	expect(memmem(b2, lb2, p0, lp0) == b2);
85272343Sngie	expect(memmem(b0, lb0, p0, lp0) == b0);
86276478Sngie#endif
87272343Sngie	expect(memmem(b0, lb0, p1, lp1) == NULL);
88272343Sngie	expect(memmem(b1, lb1, p1, lp1) == NULL);
89272343Sngie
90272343Sngie	expect(memmem(b2, lb2, p1, lp1) == b2);
91272343Sngie	expect(memmem(b2, lb2, p2, lp2) == (b2 + 4));
92272343Sngie	expect(memmem(b2, lb2, p3, lp3) == (b2 + 7));
93272343Sngie
94272343Sngie	expect(memmem(b2, lb2, p5, lp5) == b2);
95272343Sngie	expect(memmem(b2, lb2, p6, lp6) == (b2 + 9));
96272343Sngie
97272343Sngie	expect(memmem(b2, lb2, p4, lp4) == NULL);
98272343Sngie	expect(memmem(b2, lb2, p7, lp7) == NULL);
99313513Sngie	expect(memmem(b2, lb2, p8, lp8) == NULL);
100272343Sngie}
101272343Sngie
102272343SngieATF_TP_ADD_TCS(tp)
103272343Sngie{
104272343Sngie
105272343Sngie	ATF_TP_ADD_TC(tp, memmem_basic);
106272343Sngie
107272343Sngie	return atf_no_error();
108272343Sngie}
109