【问题标题】:Interpret Gcov report for class objects解释类对象的 Gcov 报告
【发布时间】:2014-01-16 16:32:11
【问题描述】:

我正在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

我得到了以下结果A

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)Lines executed:100.00% of 1818是什么意思及意义

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) 所有功能名称等::'_ZN7cashier8settriesEi' 几乎与收银功能名称等匹配:void settries(int) ,我认为它指的是同一个功能,但我的老师感觉不同,谁对吗??

2) Lines executed:100.00% of 3for 函数中的 3 是什么意思:'_ZN7cashier8settriesEi'

【问题讨论】:

    标签: c++ class code-coverage gcov


    【解决方案1】:

    结果 A:

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

    结果 B:

    1. 是的
    2. 3 行从 3 次检测中执行。

    附: 如果您对 gcov 感兴趣,您可以安装 lcov - 它是 gcov 报告上的 gui 表示。

    【讨论】:

    • B.1) 的答案是这些被破坏,正如@Melebius 已经指出的那样。 Computernerds 老师是正确的,除非错位名称完全相等,否则它们所指的不是同一个函数。
    • @RobertJørgensgaardEngdahl 那么这是否意味着损坏的函数名称指的是cashier.h中的函数??
    • 我相信他们会这样做,是的。
    • @RobertJørgensgaardEngdahl 你说的指令是什么意思??
    【解决方案2】:

    _ZN7cashier8settriesEi 之类的名称是mangled 名称,在这种情况下,它们肯定是指cashier::settries() 之类的函数。

    Lines executed 只是源文件中程序运行期间经过的行数。您应该查看详细结果(请参阅example)以检查哪些是实际的 executableexecuted 行。

    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的方法,虽然没有明确写出来。 (请参阅上面代码的我的 cmets。) 其他三个方法操作基本类型为int 的变量,这不需要函数调用。

    【讨论】:

    • 我在cashier.h中有7个函数但只有4个函数调用,执行了哪四个函数调用??
    • @Computernerd 查看更新。很抱歉我对Calls executed 的错误,现在已更正和解释。
    • 为什么其他四种方法不需要函数调用??
    • @Computernerd 这四个我复制了进程std::string 变量,而其他三个进程只是int 变量。 int 是一个内置类型,编译器知道如何分配或复制它们。但是,std::string 是一个类。必须告诉编译器如何使用复制构造函数和 operator=() 等方法来执行这些操作。
    猜你喜欢
    • 2017-03-18
    • 2021-08-13
    • 2012-11-17
    • 2020-08-21
    • 1970-01-01
    • 2021-08-31
    • 2015-06-04
    • 2021-09-05
    • 2011-08-07
    相关资源
    最近更新 更多