gtest 调试问题

调试问题 

1.invalid conversion from ‘uint16_t*’ {aka ‘short unsigned int*’} to ‘long long unsigned int’ [-fpermissive] 148 | EXPECT_EQ(std::to_string(dev_config.xref_component_index), | ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~ | | | uint16_t* {aka short unsigned int*} 

分析:

uint16_t xref_component_index[COMPONENT_INDEX_NUMBER] = {0};是一个数组(指针),用其中的某个元素(类型为long long unsigned int).

2.将  EXPECT_STRNE("", dev_msg.data().xref_component_index13().display_name().c_str());
  EXPECT_STRNE("", dev_msg.data().xref_component_index13().description().c_str());
  EXPECT_EQ(analyzer_proto::DataType::FLOAT,
            dev_msg.data().xref_component_index13().data_type());
  EXPECT_EQ(std::to_string(dev_config.xref_component_index[13]),
            dev_msg.data().xref_component_index13().default_value().c_str());  

中xref_component_index0-15写成循环执行

方案:

使用 std::ostringstream 来动态生成函数名,并尝试通过反射或模板元编程来调用这些函数。然而,C++ 标准库并没有直接支持通过字符串来调用成员函数。因此,我们需要使用一些技巧来实现这一目的。

由于您想要通过字符串来调用成员函数,您可以使用模板元编程来生成对应的函数调用。下面是一个使用模板元编程的示例,它展示了如何动态地生成函数调用:cpp

#include <gtest/gtest.h>
#include <sstream>
#include <string>

class DevConfig {
public:
    // 假设 dev_config.xref_component_index 是一个数组
    uint16_t xref_component_index[16];
};

class DeviceMessage {
public:
    // 假设有一个系列的成员函数,这里以 xref_component_index0 为例
    struct ComponentIndex {
        std::string display_name() const { return "Name"; }
        std::string description() const { return "Description"; }
        analyzer_proto::DataType data_type() const { return analyzer_proto::DataType::FLOAT; }
        std::string default_value() const { return "0"; }
    } xref_component_index0, xref_component_index1, /* ... 其他成员 ... */;

    // ... 其他成员函数 xref_component_index2 到 xref_component_index15 ...
};

// 定义一个模板函数来调用对应的成员函数
template<int Index>
void expectComponentEq(const DeviceMessage& dev_msg, const DevConfig& dev_config) {
    EXPECT_STRNE("", dev_msg.data().xref_component_index##Index().display_name().c_str());
    EXPECT_STRNE("", dev_msg.data().xref_component_index##Index().description().c_str());
    EXPECT_EQ(analyzer_proto::DataType::FLOAT, dev_msg.data().xref_component_index##Index().data_type());
    EXPECT_EQ(std::to_string(dev_config.xref_component_index[Index]), dev_msg.data().xref_component_index##Index().default_value().c_str());
}

// 使用模板进行循环遍历并调用函数
TEST(DeviceMessageTest, TestXrefComponentIndices) {
    DevConfig dev_config;
    DeviceMessage dev_msg;
    for (int i = 0; i < 16; ++i) {
        expectComponentEq<i>(dev_msg, dev_config);
    }
}

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
报错:
 error: stray ‘##’ in program
  506 | component_index[Index]), dev_msg.data().xref_component_index##Index().default_value().c_str());

3./abb/Device-Modules/unit_tests/analyzer/config/initial/analyzer_dev_config_initial_tests.cpp文件下:bison::Message<analyzer_proto::DevListConfig> m_dev_list_config_msg_;为什么和/abb/Device-Modules/unit_tests/coriolis/config/initial/coriolis_dev_list_config_initial_tests.cpp文件下bison::Message<coriolis_proto::DevListConfig> m_dev_list_config_msg_;编译时冲突?

4.为什么只能std::to_string(device_config.xref_component_index[4]),转换成string而不能在用的时候,例如:std::to_string(data_def.data().set_value(test_error_value_list[j]))

5.Expected equality of these values:
  new_mole_percent_hi_limit
    Which is: 8.9
  it->mole_percent_hi_limit
    Which is: 8.9
这个错我不知道什么问题

解决:浮点型比较,有特定的函数的

6.不明白  EXPECT_CALL(*(this->mock_analyzer_peripheral_msg_manager_),
              ParsePeripheralMessage(peripheral_msg, &com_config, _))
      .Times(1)
      .WillOnce(testing::Return(false));
  on_acquire_peripheral_check(peripheral_msg);
  // on_error shall be called
  EXPECT_EQ(++err_check, err_num);

  // check while parse msg success
  EXPECT_CALL(*(this->mock_analyzer_peripheral_msg_manager_),
              ParsePeripheralMessage(peripheral_msg, &com_config, _))
      .Times(1)
      .WillOnce(testing::Return(true));这两个都会被调用吗?

7.试图循环赋值,报错

   for (int i = 0; i < COMPONENT_INDEX_NUMBER; i++) {

     std::string xref_component_index = AnalyzerConfigChangeParam::XREF_COMPONENT_INDEX + to_string(i);

     int j=13+i;

     ccr[xref_component_index] = std::std::to_string(get<j>(GetParam());
error: the value of ‘j’ is not usable in a constant expression
   91 |     ccr[xref_component_index] = std::std::to_string(get<j>(GetParam());
      |                                                                      ^
/abb/Device-Modules/unit_tests/analyzer/implementation/configchange/adddevice/add_device_config_change_validator_tests.cpp:90:9: note: ‘int j’ is not const
   90 |     int j=13+i;
      |         ^

get<j>(GetParam()中只能是常量

8.循环xref_component_index1-16,数据太多跑不动

规范问题

1.调试的时候添加了注释,提交时没删掉

2.提交时 git diff一下看看是不是自己改的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_52541394

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值