GoogleTest 单元测试

假设我们有两个函数 complexFunction 和 helperFunction,其中 complexFunction 调用了 helperFunction。我们将编写测试 complexFunction 的单元测试,并在调用 helperFunction 的地方打桩。

// 复杂函数示例
int helperFunction(int x) {
   
    return x * 2;
}

// 调用了 helperFunction 的复杂函数
int complexFunction(int a, int b) {
   
    if (a > 0) {
   
        for (int i = 0; i < a; ++i) {
   
            if (i % 2 == 0) {
   
                b += helperFunction(i);
            }
        }
    } else {
   
        b = -1;
    }

    return b;
}

现在,我们将编写一个单元测试来测试 complexFunction 并在调用 helperFunction 的地方打桩。

#include <gtest/gtest.h>
#include <gmock/gmock.h>

// 导入要测试的函数
#include "complex_function.h" // 假设你的函数保存在 complex_function.h 文件中

// 引入命名空间
using ::testing::Return;

// 模拟 helperFunction 函数
class MockHelperFunction {
   
public:
    MOCK_METHOD(int, helperFunction, (int), (const));
};

// 单元测试套件
class ComplexFunctionTest : public ::testing::Test {
   
protected:
    // 在每个测试用例运行之前调用
    void SetUp() override {
   
        // 可以在这里进行初始化操作
    }

    // 在每个测试用例运行之后调用
    void TearDown() override {
   
        // 可以在这里进行资源释放等操作
    }

    // 模拟的 helperFunction
    MockHelperFunction mock_helper;
};

// 测试用例:测试 complexFunction 函数
TEST_F(ComplexFunctionTest, TestComplexFunction) {
   
    // 准备测试数据
    int a = 3, b = 2;
    // 设置预期调用
    EXPECT_CALL(mock_helper, helperFunction(::testing::_))
        .Times(3)
        .WillRepeatedly(Return(4)); // 模拟 helperFunction 返回值

    // 调用 complexFunction 函数
    int result = complexFunction(a, b);

    // 验证结果
    EXPECT_EQ(result, 14);
}

// 运行所有测试
int main(int argc, char **argv) {
   
    // 初始化 Google 测试
    ::testing::InitGoogleTest(&argc, argv);
    // 运行所有测试用例
    return RUN_ALL_TESTS();
}


// 函数的测试
#include <gtest/gtest.h>
#include <array>

#include "geoalg.h"

TEST(Vector2DTest, Subtract) 
{
   
  const std::array<double, 2> v0 = {
   3.0, 4.0};
  const std::array<double, 2> v1 = {
   1.0, 2.0};
  std::array<double, 2> result = v0 - v1;
  std::array<double, 2> expected = {
   2.0, 2.0};
  EXPECT_EQ(result, expected);
}


TEST(Vector2DTest, Add) 
{
   
  const std::array<double, 2> v0 = {
   3.0, 4.0};
  const std::array<double, 2> v1 = {
   1.0, 2.0};
  std::array<double, 2> result = v0 + v1;
  std::array<double, 2> expected = {
   4.0, 6.0};
  EXPECT_EQ(result, expected);
}

TEST(Vector2DTest, MultiplyByScalar) 
{
   
  const std::array<double, 2> v = {
   3.0, 4.0};
  double a = 2.0;
  std::array<double, 2> result = a * v;
  std::array<double, 2> expected = {
   6.0, 8.0};
  EXPECT_EQ(result, expected);
}

TEST(Vector2DTest, DivideByScalar) 
{
   
  const std::array<double, 2> v = {
   6.0, 8.0};
  double a = 2.0;
  std::array<double, 2> result = v / a;
  std::array<double, 2> expected = {
   3.0, 4.0};
  EXPECT_EQ(result, expected);

  //double b = 0.0;
  //std::array<double, 2> u = {6.0, 8.0};
  //ASSERT_THROW({u / b;}, std::runtime_error);
  //ASSERT_THROW({u /= b;}, std::runtime_error);

}

TEST(Vector2DTest, CrossProduct) 
{
   
  const std::array<double, 2> v0 = {
   3.0, 4.0};
  const std::array<double, 2> v1 = {
   1.0, 2.0};
  double result = cross(v0, v1);
  EXPECT_EQ(result, 2.0);
}


TEST(Vector2DTest, DotProduct) 
{
   
  const std::array<double, 2> v0 = {
   3.0, 4.0};
  const std::array<double, 2> v1 = {
   1.0, 2.0};
  double result = dot(v0, v1);
  EXPECT_EQ(result, 11.0);
}

TEST(Vector2DTest, SquaredLength) 
{
   
  std::array<double, 2> v = {
   3.0, 4.0};
  double result = squared_length(v);
  EXPECT_EQ(result, 25.0);
}

TEST(Vector2DTest, Length) 
{
   
  const std::array<double, 2> v = {
   3.0, 4.0};
  double result = length(v);
  EXPECT_DOUBLE_EQ(result, 5.0);
}

TEST(Vector3DTest, CrossProduct)
{
   
  const std::array<double, 3> v0 = {
   1.0, 2.0, 3.0};
  const std::array<double, 3> v1 = {
   4.0, 5.0, 6.0};
  std::array<double, 3> result = cross(v0, v1);
  std::array<double, 3> expected = {
   -3.0, 6.0, -3.0};
  EXPECT_EQ(result, expected);

}

TEST(Vector3DTest, DotProduct) 
{
   
    const std::array<double, 3> v0 = {
   1.0, 2.0, 3.0};
    const std::array<double, 3> v1 = {
   4.0, 5.0, 6.0};
    double result = dot(v0, v1);
    double expected = 32.0;
    EXPECT_EQ(result, expected);
}

TEST(Vector3DTest, SquaredLength) {
   
  const std::array<double, 3> v = {
   3.0, 4.0, 12.0};
  double result = squared_length(v);
  double expected = 169.0;
  EXPECT_EQ(result, expected);
}

TEST(Vector3DTest, Length) 
{
   
  const std::array<double, 3> v = {
   3.0, 4.0, 12.0};
  double result = length(v);
  double expected = 13.0;
  EXPECT_EQ(result, expected);
}

TEST(VectorBarycenter, Barycenter) 
{
   
  const std::array<double, 3> p0 = {
   1.0, 2.0, 3.0};
  const std::array<double, 3> p1 = {
   4.0, 5.0, 6.0};
  const std::array<double, 3> p2 = {
   7.0, 8.0, 9.0};
  const std::array<double, 3> p3 = {
   10.0, 11.0, 12.0};

  std::array<double, 3> result = barycenter(p0, p1);
  std::array<double, 3> expected = {
   2.5, 3.5, 4.5};
  EXPECT_EQ(result, expected);
  
  result = barycenter(p0, p1, p2);
  expected = {
   4.0, 5.0, 6.0};
  EXPECT_EQ(result, expected);

  result = barycenter(p0, p1, p2, p3);
  expected = {
   6.5, 7.75, 9.0};
  EXPECT_EQ(result, expected);
}

   

add_executable(test_geoalg test_geoalg.cpp)
target_link_libraries(
  test_geoalg 
  GTest::gtest_main
  ${
   CSCEC_LIBRARIES}
)

 class TetMeshTest : public ::testing::Test
        {
   
            protected:
                void SetUp()
                {
      
                    mesh.from_one_tetrahedron();
                    auto data = mesh.data();
                    std::cout << mesh << std::endl;
                }

            public:
                TetMesh mesh;
        }; // end of class

        TEST_F(TetMeshTest, NumberOfEntities)
        {
   
            EXPECT_EQ(mesh.number_of_nodes(),4);
            EXPECT_EQ(mesh.number_of_cells(),1);
        }










相关推荐

  1. GoogleTest 单元测试

    2024-02-04 06:24:02       46 阅读
  2. 一个简单好用的C++语言单元测试框架-GoogleTest

    2024-02-04 06:24:02       56 阅读
  3. 软件测试——单元测试

    2024-02-04 06:24:02       57 阅读
  4. 单元测试和集成测试

    2024-02-04 06:24:02       27 阅读
  5. 软件测试单元测试

    2024-02-04 06:24:02       26 阅读
  6. spring 单元测试 Junit

    2024-02-04 06:24:02       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-04 06:24:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-04 06:24:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-04 06:24:02       82 阅读
  4. Python语言-面向对象

    2024-02-04 06:24:02       91 阅读

热门阅读

  1. 打卡C语言程序设计Day16 万年历

    2024-02-04 06:24:02       50 阅读
  2. NLP自然语言处理的基本语言任务介绍

    2024-02-04 06:24:02       52 阅读
  3. 举例说明自然语言处理(NLP)技术

    2024-02-04 06:24:02       46 阅读
  4. Golang中的HTTP请求凝聚器

    2024-02-04 06:24:02       49 阅读
  5. 【QT系列】tableView

    2024-02-04 06:24:02       43 阅读
  6. docker 搭建 Seafile 集成 onlyoffice

    2024-02-04 06:24:02       59 阅读
  7. 如何确定子网地址(范例)?

    2024-02-04 06:24:02       48 阅读