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