1313680Sngie/*	$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;
54272343Sngie
55272343Sngiechar b0[] = "";
56272343Sngieint lb0 = 0;
57272343Sngiechar b1[] = "0";
58272343Sngieint lb1 = 1;
59272343Sngiechar b2[] = "0123456789";
60272343Sngieint lb2 = 10;
61272343Sngie
62272343Sngie#define expect(b)							\
63272343Sngie	if (!(b)) {							\
64272343Sngie		fprintf(stderr, "failed on line %d\n", __LINE__);	\
65272343Sngie		atf_tc_fail("Check stderr for test id/line");		\
66272343Sngie	}
67272343Sngie
68272343SngieATF_TC(memmem_basic);
69272343SngieATF_TC_HEAD(memmem_basic, tc)
70272343Sngie{
71272343Sngie
72272343Sngie	atf_tc_set_md_var(tc, "descr", "Test memmem results");
73272343Sngie}
74272343Sngie
75272343SngieATF_TC_BODY(memmem_basic, tc)
76272343Sngie{
77272343Sngie
78283584Semaste#if defined(__darwin__)
79273020Sngie	expect(memmem(b2, lb2, p0, lp0) == NULL);
80273020Sngie	expect(memmem(b0, lb0, p0, lp0) == NULL);
81273020Sngie#else
82272343Sngie	expect(memmem(b2, lb2, p0, lp0) == b2);
83272343Sngie	expect(memmem(b0, lb0, p0, lp0) == b0);
84273020Sngie#endif
85272343Sngie	expect(memmem(b0, lb0, p1, lp1) == NULL);
86272343Sngie	expect(memmem(b1, lb1, p1, lp1) == NULL);
87272343Sngie
88272343Sngie	expect(memmem(b2, lb2, p1, lp1) == b2);
89272343Sngie	expect(memmem(b2, lb2, p2, lp2) == (b2 + 4));
90272343Sngie	expect(memmem(b2, lb2, p3, lp3) == (b2 + 7));
91272343Sngie
92272343Sngie	expect(memmem(b2, lb2, p5, lp5) == b2);
93272343Sngie	expect(memmem(b2, lb2, p6, lp6) == (b2 + 9));
94272343Sngie
95272343Sngie	expect(memmem(b2, lb2, p4, lp4) == NULL);
96272343Sngie	expect(memmem(b2, lb2, p7, lp7) == NULL);
97272343Sngie}
98272343Sngie
99272343SngieATF_TP_ADD_TCS(tp)
100272343Sngie{
101272343Sngie
102272343Sngie	ATF_TP_ADD_TC(tp, memmem_basic);
103272343Sngie
104272343Sngie	return atf_no_error();
105272343Sngie}
106