您的位置:首页 > 教育 > 培训 > 中国十大科技公司_广告推广投放平台_网销平台排名_最近比较火的关键词

中国十大科技公司_广告推广投放平台_网销平台排名_最近比较火的关键词

2025/2/23 9:32:57 来源:https://blog.csdn.net/qq_43920838/article/details/144029226  浏览:    关键词:中国十大科技公司_广告推广投放平台_网销平台排名_最近比较火的关键词
中国十大科技公司_广告推广投放平台_网销平台排名_最近比较火的关键词

目录

  • 环境准备
  • GoogleTest

环境准备

git clone https://github.com/google/googletest.git

说cmkae版本过低了,解决方法

进到googletest中

cmake CMakeLists.txt
make
sudo make install

在这里插入图片描述


ls /usr/local/lib

存在以下文件说明安装成功

在这里插入图片描述
中间出了个问题就是,总是出现链接不成功,导致库导入不进去
在这里插入图片描述
可以对G++命令加上-L编译的命令,这样就指定了库的搜索路径。

g++ sample1.cc sample1_unittest.cc -o sample1 -L/usr/local/lib -lgtest -lgtest_main -lpthread

在这里插入图片描述

在这里插入图片描述

GoogleTest

单元测试是用来对一个模块、一个函数或者一个类来进行正确性检测的测试工作。
比如我们测试一个岛问题的解决方法

#include <iostream>
#include <initializer_list>
#include <vector>
#include <gtest/gtest.h>using namespace std;class IslandProblem {
public:using Matrix = vector<vector<char>>;IslandProblem(const initializer_list<vector<char>> list) {_islands.assign(list);}int Do() {int num = 0;for (int row = 0; row < (int)_islands.size(); row++) {for (int col = 0; col < (int)_islands[row].size(); col++) {if (canUnion(row, col)) {num++;unionIsland(row, col);}}}return num;}protected:bool canUnion(int row, int col) {if (row < 0 || row >= (int)_islands.size())return false;if (col < 0 || col >= (int)_islands[row].size())return false;if (_islands[row][col] != 1)return false;return true;}void unionIsland(int row, int col) {_islands[row][col] = 2;// upif (canUnion(row-1, col)) unionIsland(row-1, col);// leftif (canUnion(row, col-1)) unionIsland(row, col-1);// downif (canUnion(row+1, col)) unionIsland(row+1, col);// rightif (canUnion(row, col+1)) unionIsland(row, col+1);}private:Matrix _islands;
};TEST(IslandProblem, logic) {IslandProblem ip1{{1,1,1,1},{1,0,1,1},{0,0,0,0},{1,0,1,0}};EXPECT_EQ(ip1.Do(), 3);IslandProblem ip2{{1,0,1,1},{1,0,1,1},{0,0,0,0},{1,0,1,0}};EXPECT_EQ(ip2.Do(), 4);
}TEST(IslandProblem, boundary) {IslandProblem ip1{{1,1,1,1},{1,0,0,1},{1,0,0,1},{1,1,1,1}};EXPECT_EQ(ip1.Do(), 1);IslandProblem ip2{};EXPECT_EQ(ip2.Do(), 0);
}TEST(IslandProblem, exception) {IslandProblem ip1{{-1,1,1,1},{1,0,0,1},{1,0,0,1},{1,1,1,1}};EXPECT_EQ(ip1.Do(), 1);
}

解决方法要考虑:逻辑问题(功能正确),边界问题,异常情况。TEST的测试案例中已经把这些问题考虑进去了。
然后实际去测试,说明解决方法能够考虑以上三种情况在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com