2 * Automated Testing Framework (atf)
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "test_helpers.h"
39 /* ---------------------------------------------------------------------
40 * Test cases for the free functions.
41 * --------------------------------------------------------------------- */
43 /* TODO: Add checks for build_check_c_o and the macros defined in the
47 ATF_TC_HEAD(grep_string, tc)
49 atf_tc_set_md_var(tc, "descr", "Tests the grep_string helper "
52 ATF_TC_BODY(grep_string, tc)
56 atf_dynstr_init_fmt(&str, "a string - aaaabbbb");
57 ATF_CHECK(grep_string(&str, "a string"));
58 ATF_CHECK(grep_string(&str, "^a string"));
59 ATF_CHECK(grep_string(&str, "aaaabbbb$"));
60 ATF_CHECK(grep_string(&str, "aa.*bb"));
61 ATF_CHECK(!grep_string(&str, "foo"));
62 ATF_CHECK(!grep_string(&str, "bar"));
63 ATF_CHECK(!grep_string(&str, "aaaaa"));
65 atf_dynstr_fini(&str);
70 ATF_TC_HEAD(grep_file, tc)
72 atf_tc_set_md_var(tc, "descr", "Tests the grep_file helper function");
74 ATF_TC_BODY(grep_file, tc)
78 f = fopen("test.txt", "w");
80 fprintf(f, "line1\n");
81 fprintf(f, "the second line\n");
82 fprintf(f, "aaaabbbb\n");
85 ATF_CHECK(grep_file("test.txt", "line1"));
86 ATF_CHECK(grep_file("test.txt", "line%d", 1));
87 ATF_CHECK(grep_file("test.txt", "second line"));
88 ATF_CHECK(grep_file("test.txt", "aa.*bb"));
89 ATF_CHECK(!grep_file("test.txt", "foo"));
90 ATF_CHECK(!grep_file("test.txt", "bar"));
91 ATF_CHECK(!grep_file("test.txt", "aaaaa"));
95 ATF_TC_HEAD(read_line, tc)
97 atf_tc_set_md_var(tc, "descr", "Tests the read_line function");
99 ATF_TC_BODY(read_line, tc)
101 const char *l1 = "First line with % formatting % characters %";
102 const char *l2 = "Second line; much longer than the first one";
103 const char *l3 = "Last line, without terminator";
108 f = fopen("test", "w");
109 ATF_REQUIRE(f != NULL);
118 fd = open("test", O_RDONLY);
119 ATF_REQUIRE(fd != -1);
121 RE(atf_dynstr_init(&dest));
122 eof = read_line(fd, &dest);
124 atf_dynstr_fini(&dest);
130 f = fopen("test", "w");
131 ATF_REQUIRE(f != NULL);
133 fprintf(f, "%s\n", l1);
134 fprintf(f, "%s\n", l2);
135 fprintf(f, "%s", l3);
145 fd = open("test", O_RDONLY);
146 ATF_REQUIRE(fd != -1);
148 RE(atf_dynstr_init(&dest));
149 eof = read_line(fd, &dest);
151 printf("1st line: >%s<\n", atf_dynstr_cstring(&dest));
152 ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l1));
153 atf_dynstr_fini(&dest);
155 RE(atf_dynstr_init(&dest));
156 eof = read_line(fd, &dest);
158 printf("2nd line: >%s<\n", atf_dynstr_cstring(&dest));
159 ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l2));
160 atf_dynstr_fini(&dest);
162 RE(atf_dynstr_init(&dest));
163 eof = read_line(fd, &dest);
165 printf("3rd line: >%s<\n", atf_dynstr_cstring(&dest));
166 ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l3));
167 atf_dynstr_fini(&dest);
173 /* ---------------------------------------------------------------------
175 * --------------------------------------------------------------------- */
179 /* Add the tests for the free functions. */
180 ATF_TP_ADD_TC(tp, grep_string);
181 ATF_TP_ADD_TC(tp, grep_file);
182 ATF_TP_ADD_TC(tp, read_line);
184 return atf_no_error();