1*d3a20667Skre /* $Id: t_bm.c,v 1.3 2024/07/11 03:55:58 kre Exp $ */
2509eb314Sshm /*-
3509eb314Sshm * Copyright (c) 2014 The NetBSD Foundation, Inc.
4509eb314Sshm * All rights reserved.
5509eb314Sshm *
6509eb314Sshm * This code is derived from software contributed to The NetBSD Foundation
7509eb314Sshm * by Mateusz Kocielski.
8509eb314Sshm *
9509eb314Sshm * Redistribution and use in source and binary forms, with or without
10509eb314Sshm * modification, are permitted provided that the following conditions
11509eb314Sshm * are met:
12509eb314Sshm * 1. Redistributions of source code must retain the above copyright
13509eb314Sshm * notice, this list of conditions and the following disclaimer.
14509eb314Sshm * 2. Redistributions in binary form must reproduce the above copyright
15509eb314Sshm * notice, this list of conditions and the following disclaimer in the
16509eb314Sshm * documentation and/or other materials provided with the distribution.
17509eb314Sshm *
18509eb314Sshm * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19509eb314Sshm * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20509eb314Sshm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21509eb314Sshm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22509eb314Sshm * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23509eb314Sshm * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24509eb314Sshm * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25509eb314Sshm * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26509eb314Sshm * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27509eb314Sshm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28509eb314Sshm * POSSIBILITY OF SUCH DAMAGE.
29509eb314Sshm */
30509eb314Sshm
31509eb314Sshm #include <sys/cdefs.h>
32*d3a20667Skre __RCSID("$Id: t_bm.c,v 1.3 2024/07/11 03:55:58 kre Exp $");
33509eb314Sshm
34509eb314Sshm #include <atf-c.h>
35509eb314Sshm #include <stdio.h>
36509eb314Sshm #include <sys/types.h>
37509eb314Sshm #include <bm.h>
38509eb314Sshm #include <string.h>
39509eb314Sshm #include <stdlib.h>
40509eb314Sshm
41509eb314Sshm ATF_TC(bm);
ATF_TC_HEAD(bm,tc)42509eb314Sshm ATF_TC_HEAD(bm, tc)
43509eb314Sshm {
44509eb314Sshm atf_tc_set_md_var(tc, "descr", "Test bm(3)");
45509eb314Sshm }
46509eb314Sshm
47509eb314Sshm typedef struct {
48509eb314Sshm const char *pattern;
49509eb314Sshm const char *text;
50509eb314Sshm const char *freq;
51509eb314Sshm ssize_t match;
52509eb314Sshm } t_testcase;
53509eb314Sshm
54509eb314Sshm const t_testcase testcases[] = {
55509eb314Sshm {"test", "test", NULL, 0},
56509eb314Sshm {"test", "ttest", NULL, 1},
57509eb314Sshm {"test", "tes", NULL, -1},
58509eb314Sshm {"test", "testtesttest", NULL, 0},
59509eb314Sshm {"test", "testtesttesttesttesttest", NULL, 0},
60509eb314Sshm {"test", "------------------------", NULL, -1},
61509eb314Sshm {"a", "a", NULL, 0},
62509eb314Sshm {"a", "ba", NULL, 1},
63509eb314Sshm {"a", "bba", NULL, 2},
64509eb314Sshm {"bla", "bl", NULL, -1},
65509eb314Sshm {"a", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", NULL, -1},
66509eb314Sshm {"test", "qfwiofjqeiwofjioqewfjeiqwjfiqewjfioqewfjioewqjfioewqjfioewqjoi",
67509eb314Sshm NULL, -1},
68509eb314Sshm {"needle", "haystack", NULL, -1},
69509eb314Sshm {"netbsd", "freebsd netbsd openbsd", NULL, 8},
70509eb314Sshm };
71509eb314Sshm
ATF_TC_BODY(bm,tc)72509eb314Sshm ATF_TC_BODY(bm, tc)
73509eb314Sshm {
74509eb314Sshm size_t ts;
75509eb314Sshm u_char *off;
76509eb314Sshm char *text;
77509eb314Sshm bm_pat *pattern;
78509eb314Sshm
79509eb314Sshm for (ts = 0; ts < sizeof(testcases)/sizeof(t_testcase); ts++) {
80*d3a20667Skre ATF_CHECK((pattern = bm_comp((const u_char *)testcases[ts].pattern,
81*d3a20667Skre strlen(testcases[ts].pattern), (const u_char *)testcases[ts].freq)));
82509eb314Sshm
8306493b9bSkre ATF_REQUIRE((text = strdup(testcases[ts].text)));
84509eb314Sshm off = bm_exec(pattern, (u_char *)text, strlen(text));
85509eb314Sshm
86509eb314Sshm if (testcases[ts].match == -1)
87509eb314Sshm ATF_CHECK_EQ(off, NULL);
88509eb314Sshm else
89509eb314Sshm ATF_CHECK_EQ(testcases[ts].match,
90509eb314Sshm (off-(u_char *)text));
91509eb314Sshm
92509eb314Sshm bm_free(pattern);
93509eb314Sshm free(text);
94509eb314Sshm }
95509eb314Sshm }
96509eb314Sshm
ATF_TP_ADD_TCS(tp)97509eb314Sshm ATF_TP_ADD_TCS(tp)
98509eb314Sshm {
99509eb314Sshm
100509eb314Sshm ATF_TP_ADD_TC(tp, bm);
101509eb314Sshm return atf_no_error();
102509eb314Sshm }
103