解释类对象的Gcov报告

Interpret Gcov report for class objects

本文关键字:Gcov 报告 对象 解释      更新时间:2023-10-16

我正在cashier课上做代码覆盖,我的老师就报告的含义进行了非常简短的教学,我认为这对我的软件工程技能的发展非常重要,因此我需要您对以下gcov报告的解释提出建议。我将感谢任何链接或文章,这将有助于我理解gcov

感谢

头文件

#ifndef CASHIER_H
#define CASHIER_H
#include <string>
using namespace std;

class cashier 
{
public:
void setID(string);
string getID();
void setPassword(string);
string getPassword();
void settries(int);
int gettries();
void increase_tries();
private:
string ID;
string Password;
int tries;

};
#endif  /* CASHIER_H */

实现文件

#include "cashier.h"


void cashier::setID(string value)
{
this->ID = value;
}
void cashier::setPassword(string value)
{
this->Password = value;
}
string cashier::getID()
{
return this->ID;
}
string cashier::getPassword()
{
return this->Password;
}
void cashier::settries(int value)
{
this->tries=value;
}
int cashier::gettries()
{
return this->tries;
}
void cashier::increase_tries()
{
this->tries = this->tries + 1 ;
}

我在命令提示符中键入以下命令,以便在类上使用gcov

gcov -b cashier.gnco

我得到了以下结果

File 'cashier.cpp'
Lines executed:100.00% of 18 //what does the 18 mean 
No branches                  //what does no branches mean
Calls executed:100.00% of 4   // what does 4 mean ??
cashier.cpp:creating 'cashier.cpp.gcov'
File '/usr/include/c++/4.4/bits/basic_string.h' // Where did this come from ??
Lines executed:0.00% of 2
No branches
Calls executed:0.00% of 1
/usr/include/c++/4.4/bits/basic_string.h:creating 'basic_string.h.gcov

我键入以下命令

gcov -f cashier.gnco

我得到了以下结果B

Function '_ZN7cashier8settriesEi' // does this refer to the function :settries
Lines executed:100.00% of 3       // my teacher doesnt think so but i feel it refer
//to it , who is correct??
Function '_ZN7cashier8gettriesEv'
Lines executed:100.00% of 2
Function '_ZN7cashier14increase_triesEv'
Lines executed:100.00% of 3
Function '_ZN7cashier11getPasswordEv'
Lines executed:100.00% of 2
Function '_ZN7cashier5getIDEv'
Lines executed:100.00% of 2
Function '_ZNSsaSERKSs'
Lines executed:0.00% of 2
Function '_ZN7cashier11setPasswordESs'
Lines executed:100.00% of 3
Function '_ZN7cashier5setIDESs'
Lines executed:100.00% of 3
File 'cashier.cpp'
Lines executed:100.00% of 18
cashier.cpp:creating 'cashier.cpp.gcov'
File '/usr/include/c++/4.4/bits/basic_string.h'
Lines executed:0.00% of 2
/usr/include/c++/4.4/bits/basic_string.h:creating 'basic_string.h.gcov'

我对结果A的问题

1)18是什么意思及其在Lines executed:100.00% of 18中的意义

2)no branches是什么意思

3)4的含义及其在Calls executed:100.00% of 4中的意义

4) 整段是什么意思

File '/usr/include/c++/4.4/bits/basic_string.h'
Lines executed:0.00% of 2
No branches
Calls executed:0.00% of 1
/usr/include/c++/4.4/bits/basic_string.h:creating 'basic_string.h.gcov

我对结果B的问题

1) 所有函数名称etc::'_ZN7cashier8settriesEi'几乎与出纳函数名称etc:void settries(int)匹配,我认为它指的是同一个函数,但我的老师不这么认为,谁是对的??

2)Lines executed:100.00% of 3中的3对函数"_ZN7cashier8settriesEi"意味着什么

结果A:

  1. 程序启动后执行了18行代码(与出纳.cpp相关)
  2. 您的代码没有任何条件
  3. 从4个仪器中执行4行(与出纳有关。h)
  4. std::string的一些行成为覆盖范围的一部分。您应该从覆盖率报告中排除std::string(gcov-e)

结果B:

  1. 从3个仪器中执行了3行

p.S。如果你对gcov感兴趣,你可以安装lcov——它是gcov报表上的gui表示。

_ZN7cashier8settriesEi这样的名称是损坏的名称,在这种情况下,它们肯定指的是像cashier::settries()这样的函数。

Lines executed只是源文件中在程序运行期间经过的行数。您应该查看详细的结果(参见示例),以检查哪些是实际的可执行执行行。

No branches意味着代码中没有像if语句那样的决策点。

Calls executed是该文件中的函数对该文件的函数的调用。

void cashier::setID(string value)
{
this->ID = value; // call to string::operator=()
}
void cashier::setPassword(string value)
{
this->Password = value; // call to string::operator=()
}
string cashier::getID()
{
return this->ID; // call to copy-constructor of string
}
string cashier::getPassword()
{
return this->Password; // call to copy-constructor of string
}

这四个方法正在调用std::string的方法,尽管它不是显式编写的。(请参阅我对上面代码的评论。)其他三个方法处理基本类型int的变量,这不需要函数调用。