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