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