1272343Sngie/* $Id: t_bm.c,v 1.1 2014/06/23 10:53:20 shm Exp $ */
2272343Sngie/*-
3272343Sngie * Copyright (c) 2014 The NetBSD Foundation, Inc.
4272343Sngie * All rights reserved.
5272343Sngie *
6272343Sngie * This code is derived from software contributed to The NetBSD Foundation
7272343Sngie * by Mateusz Kocielski.
8272343Sngie *
9272343Sngie * Redistribution and use in source and binary forms, with or without
10272343Sngie * modification, are permitted provided that the following conditions
11272343Sngie * are met:
12272343Sngie * 1. Redistributions of source code must retain the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer.
14272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
15272343Sngie *    notice, this list of conditions and the following disclaimer in the
16272343Sngie *    documentation and/or other materials provided with the distribution.
17272343Sngie *
18272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28272343Sngie * POSSIBILITY OF SUCH DAMAGE.
29272343Sngie */
30272343Sngie
31272343Sngie#include <sys/cdefs.h>
32272343Sngie__RCSID("$Id: t_bm.c,v 1.1 2014/06/23 10:53:20 shm Exp $");
33272343Sngie
34272343Sngie#include <atf-c.h>
35272343Sngie#include <stdio.h>
36272343Sngie#include <sys/types.h>
37272343Sngie#include <bm.h>
38272343Sngie#include <string.h>
39272343Sngie#include <stdlib.h>
40272343Sngie
41272343SngieATF_TC(bm);
42272343SngieATF_TC_HEAD(bm, tc)
43272343Sngie{
44272343Sngie	atf_tc_set_md_var(tc, "descr", "Test bm(3)");
45272343Sngie}
46272343Sngie
47272343Sngietypedef struct {
48272343Sngie	const char *pattern;
49272343Sngie	const char *text;
50272343Sngie	const char *freq;
51272343Sngie	ssize_t match;
52272343Sngie} t_testcase;
53272343Sngie
54272343Sngieconst t_testcase testcases[] = {
55272343Sngie	{"test", "test", NULL, 0},
56272343Sngie	{"test", "ttest", NULL, 1},
57272343Sngie	{"test", "tes", NULL, -1},
58272343Sngie	{"test", "testtesttest", NULL, 0},
59272343Sngie	{"test", "testtesttesttesttesttest", NULL, 0},
60272343Sngie	{"test", "------------------------", NULL, -1},
61272343Sngie	{"a", "a", NULL, 0},
62272343Sngie	{"a", "ba", NULL, 1},
63272343Sngie	{"a", "bba", NULL, 2},
64272343Sngie	{"bla", "bl", NULL, -1},
65272343Sngie	{"a", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", NULL, -1},
66272343Sngie	{"test", "qfwiofjqeiwofjioqewfjeiqwjfiqewjfioqewfjioewqjfioewqjfioewqjoi",
67272343Sngie	  NULL, -1},
68272343Sngie	{"needle", "haystack", NULL, -1},
69272343Sngie	{"netbsd", "freebsd netbsd openbsd", NULL, 8},
70272343Sngie};
71272343Sngie
72272343SngieATF_TC_BODY(bm, tc)
73272343Sngie{
74272343Sngie	size_t ts;
75272343Sngie	u_char *off;
76272343Sngie	char *text;
77272343Sngie	bm_pat *pattern;
78272343Sngie
79272343Sngie	for (ts = 0; ts < sizeof(testcases)/sizeof(t_testcase); ts++) {
80272343Sngie		ATF_CHECK(pattern = bm_comp((const u_char *)testcases[ts].pattern,
81272343Sngie		  strlen(testcases[ts].pattern), (const u_char *)testcases[ts].freq));
82272343Sngie
83272343Sngie		ATF_REQUIRE(text = strdup(testcases[ts].text));
84272343Sngie		off = bm_exec(pattern, (u_char *)text, strlen(text));
85272343Sngie
86272343Sngie		if (testcases[ts].match == -1)
87272343Sngie			ATF_CHECK_EQ(off, NULL);
88272343Sngie		else
89272343Sngie			ATF_CHECK_EQ(testcases[ts].match,
90272343Sngie			  (off-(u_char *)text));
91272343Sngie
92272343Sngie		bm_free(pattern);
93272343Sngie		free(text);
94272343Sngie	}
95272343Sngie}
96272343Sngie
97272343SngieATF_TP_ADD_TCS(tp)
98272343Sngie{
99272343Sngie
100272343Sngie	ATF_TP_ADD_TC(tp, bm);
101272343Sngie	return atf_no_error();
102272343Sngie}
103