"C + +" Non-original | Statistical code coverage (i)

Source: Internet
Author: User

PHP code on the network coverage statistics are particularly many, C/s is rare, probably because of C/E + + charges? Don't know = =

Thank the original author, the original micro-blog address:http://www.cnblogs.com/turtle-fly/archive/2013/01/09/2851474.html

http://ltp.sourceforge.net/coverage/lcov.php Demo

The main operation is: 1 with Gcov to complete the coverage of the C + + file; 2 generate coverage reports through Lcov; 3 integrated into Jenkins Automation case (need to do it yourself)

This chapter only writes the simplest of applications: compiling a single C file, generating a Lcov report

A Built Environment

I run the program directly under Linux, so just install the Gcov and Lcov environments

    • Installing the Gcov environment
      • I'm ashamed to have it on my server, so it's not installed. The versions are as follows: Gcov-v (with GCC bindings)                 
    •     Installation Lcov Environment: https://sourceforge.net/projects/ltp/can download different versions
    •  cd/usr /localwget http:  sourceforge.net/projects/ltp/files/coverage%20analysis/lcov-1.12/lcov-1.12.tar.gz  TAR-XZVF Lcov-1.12  .tar.gzcd lcov -1.12  make Installlcov -V View version: Lcov:lcov version 1.12  is installed successfully 
      do not need to compile, install directly. Lcov, gendesc, genhtml, Geninfo, Genpng will be installed into the/usr/bin directory.

       

Two write a simple C + + file . FIB.C (Fibonacci series)

1#include <stdio.h>2 intFibonacciintn);3 4 intMain ()5 {6    intfib;7    intN;8 9     for(n =0; N <=Ten; n++) {TenFIB =Fibonacci (n); Oneprintf"Fibonnaci (%d) =%d\n", N, fib); A    } -  -    return 0; the } -  - intFibonacciintN) - { +    intfib; -    if(N <=0) { +FIB =0; A    } at    Else if(n = =1) { -FIB =1; -    } -    Else { -fib = Fibonacci (N-1) + Fibonacci (N-2); -    } in  -    returnfib; to}

Three-Gcov Overlay code

1. Compilation:gcc-c Fib.c-ftest-coverage-fprofile-arcs

The check found that the Fib.gcno and FIB.O files were generated, i.e. the correct

The. Gcno is generated by-ftest-coverage and contains information about rebuilding the base block diagram and the line number of the corresponding block's source code.

2 Links: Choose one of the following three to generate the executable file fib

GCC fib.o-o fib---o fib-o Fib-fprofile-arcs

3 Run the FIB:. /fib

Generate the Gcda file,. GCDA is generated by running the compiled file with the-fprofile-arcs compilation parameters, which contains the number of arcs and other summary information. See attached 1

4 Generating Gcov Report:Gcov FIB.C

The generated Fib.c.gcov file contains the statistics that the code covers, and the number represents the number of times each line of code is executed and the line number.    

1-:0: SOURCE:FIB.C2-:0: Graph:fib.gcno3-:0:D ATA:FIB.GCDA4-:0: Runs:1  5-:0:P Rograms:1  6-:1: #include <stdio.h>7-:2:  8-:3:intFibonacciintN); 9-:4: Ten         1:5:intMain () One-:6:{  A-:7:intfib; --:8:intN; --:9:  the         A:Ten: for(n =0; N <=Ten; n++) {  -         One: One: fib =Fibonacci (n); -         One: A: printf ("Fibonnaci (%d) =%d\n", N, fib);  --: -:   }  +-: -:  -         1: the:return 0;  +-: -:}  A-: -:  at       453: -:intFibonacciintN) --: +:{  --: -:intfib; -       453: +:if(N <=0) {  -         the: A: fib =0;  --: at:   }  in       364: -:Else if(n = =1) {  -       143: -: fib =1;  to-: -:   }  +-: -:Else {  -       221: -: fib = Fibonacci (N-1) + Fibonacci (N-2);  the-: in:   }  *-: -:  $       453: to:returnfib;Panax Notoginseng-: +:}

Mans

Four Lcov finishing Coverage data

1. Use the generated. Gcon. gcda file to generate the coverage data Fib.info file lcov-c-o fib.info-d.

Where:-c Lcov operation, indicating to capture the coverage data

-O output file, followed by target file

-d source file. Gcno. gcda folder, where '. ' meaning the current folder

[Email protected]*** gtest] $lcov-C-O fib.info-d.
Capturing coverage data from.
Found Gcov version:4.8.3
Scanning. For. gcda files ...
Found 1 data files in.
Processing FIB.GCDA
Finished. Info-file creation//indicates success

2. Generate HTML-formatted reports: genhtml fib.info-o fib_result

file Fib.info generated HTML output to Fig_result folder    

[Email protected]*** gtest] $genhtml fib.info-1"***cpptest" Writing. css and. png files. Generating output. Processing file gtest/fib.cwriting Directory View page. Overall coverage Rate:  100.0% (  lines)  //code logic line coverage   100.0% (22 functions)//code method coverage

3. Download the compressed file to the Windows system and open the View Web version

Tar cvf fib_result.tar.gz fib_result/sz  fib_result.tar.gz   // download windows path

Five questions

1 when executing the Lcov command, the error "Out of memory!", the resulting Fib.info folder is empty. is because the version does not match

Workaround: Review the version below and need to update the version of Lcov. More than 1.10 of Lcov support 4.8 Gcov

[Email protected]******* gtest] $gcov-Vgcov (GCC)4.8.3 20140911(Red Hat4.8.3-9) Copyright (C) -Free software Foundation, Inc.this isFree software; See the source forcopying conditions. There isNO warranty; Not even formerchantability or FITNESS for A particular PURPOSE. [[Email protected]-******* Gtest] $lcov-Vlcov:lcov version1.9

2 Follow-up re-replenishment

The attachments are parsed for each file that is compiled.

Attached 1 od-t x4-w16 FIB.GCDA

http://www.linuxidc.com/Linux/2011-05/36537.htm

"C + +" Non-original | Statistical code coverage (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.