写一个谷歌测试程序

*************

c++

topic: gtest 单元测试

*************

Taking a gtest is a necessary step before you upload your codes. The gtest can easily tells you wether your code realize the functions.

The basic form of gtest is like as follow.

#include <gtest/gtest.h>

// 测试套件(Test Suite)声明
TEST(TestSuiteName, TestName) 
{
    // 测试代码
    EXPECT_EQ(expected_value, actual_value);  // 断言
}

I wrote a function to calculate the real power of the system in power_calculator.h. This is a head file.

// power_calculator.h
double CalculatePower(double voltage, double current) 
{
    return voltage * current;
}

feed data to the program.

// power_calculator_test.cpp
#include <gtest/gtest.h>
#include "power_calculator.h"

// 测试用例:正常情况下的功率计算
TEST(PowerCalculatorTest, NormalCase) 
{
    //给程序喂数据
    double voltage = 10.0;
    double current = 5.0;

    //  期待的值
    double expected_power = 50.0;

    // 调用函数并计算结果
    double actual_power = CalculatePower(voltage, current);

    // 比较计算结果和期待的值
    EXPECT_EQ(expected_power, actual_power);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值