1/* $Id: t_bm.c,v 1.1 2014/06/23 10:53:20 shm Exp $ */
2/*-
3 * Copyright (c) 2014 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Mateusz Kocielski.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__RCSID("$Id: t_bm.c,v 1.1 2014/06/23 10:53:20 shm Exp $");
33
34#include <atf-c.h>
35#include <stdio.h>
36#include <sys/types.h>
37#include <bm.h>
38#include <string.h>
39#include <stdlib.h>
40
41ATF_TC(bm);
42ATF_TC_HEAD(bm, tc)
43{
44	atf_tc_set_md_var(tc, "descr", "Test bm(3)");
45}
46
47typedef struct {
48	const char *pattern;
49	const char *text;
50	const char *freq;
51	ssize_t match;
52} t_testcase;
53
54const t_testcase testcases[] = {
55	{"test", "test", NULL, 0},
56	{"test", "ttest", NULL, 1},
57	{"test", "tes", NULL, -1},
58	{"test", "testtesttest", NULL, 0},
59	{"test", "testtesttesttesttesttest", NULL, 0},
60	{"test", "------------------------", NULL, -1},
61	{"a", "a", NULL, 0},
62	{"a", "ba", NULL, 1},
63	{"a", "bba", NULL, 2},
64	{"bla", "bl", NULL, -1},
65	{"a", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", NULL, -1},
66	{"test", "qfwiofjqeiwofjioqewfjeiqwjfiqewjfioqewfjioewqjfioewqjfioewqjoi",
67	  NULL, -1},
68	{"needle", "haystack", NULL, -1},
69	{"netbsd", "freebsd netbsd openbsd", NULL, 8},
70};
71
72ATF_TC_BODY(bm, tc)
73{
74	size_t ts;
75	u_char *off;
76	char *text;
77	bm_pat *pattern;
78
79	for (ts = 0; ts < sizeof(testcases)/sizeof(t_testcase); ts++) {
80		ATF_CHECK(pattern = bm_comp((const u_char *)testcases[ts].pattern,
81		  strlen(testcases[ts].pattern), (const u_char *)testcases[ts].freq));
82
83		ATF_REQUIRE(text = strdup(testcases[ts].text));
84		off = bm_exec(pattern, (u_char *)text, strlen(text));
85
86		if (testcases[ts].match == -1)
87			ATF_CHECK_EQ(off, NULL);
88		else
89			ATF_CHECK_EQ(testcases[ts].match,
90			  (off-(u_char *)text));
91
92		bm_free(pattern);
93		free(text);
94	}
95}
96
97ATF_TP_ADD_TCS(tp)
98{
99
100	ATF_TP_ADD_TC(tp, bm);
101	return atf_no_error();
102}
103