t_gcov.sh revision 1.1 1 1.1 rillig # $NetBSD: t_gcov.sh,v 1.1 2025/01/18 22:31:22 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Copyright (c) 2025 The NetBSD Foundation, Inc.
4 1.1 rillig # All rights reserved.
5 1.1 rillig #
6 1.1 rillig # Redistribution and use in source and binary forms, with or without
7 1.1 rillig # modification, are permitted provided that the following conditions
8 1.1 rillig # are met:
9 1.1 rillig # 1. Redistributions of source code must retain the above copyright
10 1.1 rillig # notice, this list of conditions and the following disclaimer.
11 1.1 rillig # 2. Redistributions in binary form must reproduce the above copyright
12 1.1 rillig # notice, this list of conditions and the following disclaimer in the
13 1.1 rillig # documentation and/or other materials provided with the distribution.
14 1.1 rillig #
15 1.1 rillig # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 rillig # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 rillig # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 rillig # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 rillig # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 rillig # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 rillig # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 rillig # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 rillig # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 rillig # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 rillig # POSSIBILITY OF SUCH DAMAGE.
26 1.1 rillig #
27 1.1 rillig
28 1.1 rillig after_exec_head()
29 1.1 rillig {
30 1.1 rillig }
31 1.1 rillig after_exec_body()
32 1.1 rillig {
33 1.1 rillig atf_require_prog cat
34 1.1 rillig atf_require_prog gcc
35 1.1 rillig atf_require_prog gcov
36 1.1 rillig atf_require_prog grep
37 1.1 rillig
38 1.1 rillig cat <<EOF >prog.c
39 1.1 rillig #include <unistd.h>
40 1.1 rillig
41 1.1 rillig int
42 1.1 rillig main(void)
43 1.1 rillig {
44 1.1 rillig pid_t pid = vfork();
45 1.1 rillig switch (pid) {
46 1.1 rillig case 0:
47 1.1 rillig execl("/bin/sh", "sh", "-c", ":", (const char *)0);
48 1.1 rillig /* FALLTHROUGH */
49 1.1 rillig case -1:
50 1.1 rillig write(2, "error\n", 6);
51 1.1 rillig _exit(1);
52 1.1 rillig }
53 1.1 rillig
54 1.1 rillig write(1, "reached\n", 8);
55 1.1 rillig return 0;
56 1.1 rillig }
57 1.1 rillig EOF
58 1.1 rillig
59 1.1 rillig cat <<EOF >prog.c.gcov.expected
60 1.1 rillig -: 0:Source:prog.c
61 1.1 rillig -: 0:Graph:prog.gcno
62 1.1 rillig -: 0:Data:prog.gcda
63 1.1 rillig -: 0:Runs:1
64 1.1 rillig -: 1:#include <unistd.h>
65 1.1 rillig -: 2:
66 1.1 rillig -: 3:int
67 1.1 rillig 1: 4:main(void)
68 1.1 rillig -: 5:{
69 1.1 rillig 1: 6: pid_t pid = vfork();
70 1.1 rillig 1: 7: switch (pid) {
71 1.1 rillig 1: 8: case 0:
72 1.1 rillig 1: 9: execl("/bin/sh", "sh", "-c", ":", (const char *)0);
73 1.1 rillig -: 10: /* FALLTHROUGH */
74 1.1 rillig #####: 11: case -1:
75 1.1 rillig #####: 12: write(2, "error\n", 6);
76 1.1 rillig #####: 13: _exit(1);
77 1.1 rillig -: 14: }
78 1.1 rillig -: 15:
79 1.1 rillig #####: 16: write(1, "reached\n", 8);
80 1.1 rillig #####: 17: return 0;
81 1.1 rillig -: 18:}
82 1.1 rillig EOF
83 1.1 rillig
84 1.1 rillig atf_check \
85 1.1 rillig gcc --coverage -c prog.c
86 1.1 rillig atf_check \
87 1.1 rillig gcc --coverage -o prog prog.o
88 1.1 rillig atf_check -o inline:'reached\n' \
89 1.1 rillig ./prog
90 1.1 rillig atf_check -o ignore \
91 1.1 rillig gcov prog.c
92 1.1 rillig
93 1.1 rillig atf_check -o file:prog.c.gcov.expected \
94 1.1 rillig cat prog.c.gcov
95 1.1 rillig
96 1.1 rillig # FIXME: The code was reached once but is reported as unreached.
97 1.1 rillig atf_check -o ignore \
98 1.1 rillig grep "#####.*reached" prog.c.gcov
99 1.1 rillig }
100 1.1 rillig
101 1.1 rillig atf_init_test_cases()
102 1.1 rillig {
103 1.1 rillig atf_add_test_case after_exec
104 1.1 rillig }
105