Easily take C program coverage with configure's build options!

I've written the unit test code, but are there really enough test cases? ** Coverage **, which is a visible index at such times, Linux includes a library called gcov as standard. Since it's a big deal, let's switch it with the build option so that it can be used at any time!

** Coverage acquisition: gcov **

** How to use **

The procedure is simple.

--Add -fprofile-arcs -ftest-coverage to your build options. --Set the library link -lgcov.

You can rebuild it with this.

** Add to build options! ** **

I think there are various build option switching methods, but I will introduce the method with the self-made configure that I am currently using. Please refer to here etc. when installing configure.

First, add the ** --enable option **.

  1. Add option with AC_ARG_ENABLE (option name, [help wording], [script when specifying option], default value)
  2. If the option is valid, add gcov library option with ʻAC_CHECK_LIB and build option GCOV_OPT for gcov with ʻAC_SUBST ([GCOV_OPT]).

configure.ac


###
# Unit test option
AC_ARG_ENABLE(coverage,
[  --enable-coverage      enable coverage test, only for developer of this package. Please "Don't" use this package if you install it, gcov is GPL license [[default=no]]],
[\
case "${enableval}" in
 yes) enable_coverage=yes ;;
 *)   AC_MSG_ERROR(bad value for --enable-) ;;
esac],
enable_coverage=no)

#check flag, and add gcov option
if test x"${enable_coverage}" = x"yes"; then
  AC_CHECK_LIB([gcov], [main])
  GCOV_OPT="-fprofile-arcs -ftest-coverage"
  AC_SUBST([GCOV_OPT])
fi

After that, it is OK if you pass the GCOV_OPT option set for gcov to Makefile.am etc. The default is empty, so you can add it. In this example, it is set to the common setting to be read in Makefile.am.

am.conf


AM_CPPFLAGS=-I$(top_srcdir)/design_pattern_util/include/ -I$(top_srcdir)/design_pattern_util/include/ $(GCOV_OPT)

Looking at the help, it looks like this. It's fun to get help. (If you write the wording with AC_HELP_STRING, you can output more beautifully)

python


$ ./configure --help
  --enable-coverage      enable coverage test, only for developer of this package. Please "Don't" use this package if you install it, gcov is GPL license [default=no]

If gcov is used successfully, you will get this * .gcno file next to the code after build.

$ ls chain_of_responsibility/lib/
chain_element.c     chain_element.h   chain_element.o            chain_of_responsibility.gcno  chain_of_responsibility.map  libchainofresp.la  Makefile.am
chain_element.gcno  chain_element.lo  chain_of_responsibility.c  chain_of_responsibility.lo    chain_of_responsibility.o    Makefile           Makefile.in

Then run the test and you'll see the coverage. It seems that automatic coverage acquisition can be done with this.

** Check coverage: lcov **

I tested it now. Let's see the result! Even if it becomes, the result is output as binary data, so it is only when you do not make it visible to humans. The easiest way to understand the result is to use lcov.

Refer to the procedure of here

  1. Download and unpack tar.gz on the original site. The inside was already built. Install with make install
  2. Execute the following directly under the code. An html file will be created in the code.
$lcov -c -d . -o XXX.info
$genhtml -o . XXX.info

When you open ʻindex.html`, you can see the coverage rate like this. Line Coverage is the so-called C0 coverage.

In this example, the test code is of course error-free, so the rate is low, but lib is 90% or more. If you open the inside, you can see the code that does not pass, but since it is also an abnormal system such as malloc failure, it can be said that there is no problem with test coverage from the viewpoint of coverage. It's like that.

** Impression **

The next time I touch it, I want to dig a little deeper and link it with the test script so that the C0 coverage rate can be checked automatically!

reference

gcov http://iyukki.blog56.fc2.com/blog-entry-154.html

lcov http://d.hatena.ne.jp/t-kado/20100622

Recommended Posts

Easily take C program coverage with configure's build options!
Easily build CNN with Keras
Easily build virtual machines with Vagrant
Easily build a development environment with Laragon