第28天
寒假学习生活开始的第一天
面对着巨大的阻力,身体里的每一根神经都在告诉我不想学习。
背完了2200左右的单词书,结果发现…emmm,前面的全忘了
早上
继续背单词,之后此项不在重复
学习googletest,去B站找了个教程,虽然有一点点用,但还是被坑了,2个多小时有用的不到20分钟,还好我疯狂快进。
下午
1.先是装了googletest:
- 从下载源码git clone https://github.com/google/googletest.git
- 自己新建一个文件夹,用于编译,进入后运行 cmake {$dir} 后者为源码文件夹位置。
- make 一下,如果没有自动安装,再make install
运行测试程序
- 找到源码下的samples文件夹,里面有多个例程,我选的是2.
- 找到usr/local/lib下的 libgtest_main.a 与 libgtest.a 复制到samples中
- 执行
$g++ -I …/include/ -c sample2.cc
$g++ -I …/include/ -c sample2_unittest.cc
$g++ -I …/include/ sample2.o sample2_unittest.o libgtest_main.a libgtest.a -lpthread -o test2
$./test2
2.发呆,三小时/WX
3.写作练习 前几天打算这样,samples是英语写的,我每天读一个sample ,然后用一个训练连续写作的网站重写练练。
晚上
看了一个Gtest的sample 然后练了练速写,因为是第一个sample,就只写一下它注释的技巧:
Gtest 小技巧 - 1
在Gtest中,每个测试都会被分组到对应的测试实例中去。这要有利于我们组织和
管理测试代码。你应该把逻辑相关的测试语句放入相同的测试实例中。
测试实例名和测试名,都应该符合C++标志符规范,并且你不应该在这两个名称中
使用下划线符号。
谷歌测试框架能够保证你定义的每个测试函数被执行且只被执行一次,但是不保证
各个测试之间执行的先后顺序。当然,你的测试代码也不应该写成要依靠特定顺序才
能工作。
Gtest 小技巧 - 2
EXPECT_EQ(expected, actual) 等价于 EXPECT_TRUE((expected) == (actual))
但是有两点细微的区别:
- EXPECT_EQ会在断言失败时打印两个参数的值,因此有助于调试。
- EXPECT_TRUE 可以接受布尔表达式作为参数,更通用一些。
源代码:
// A sample program demonstrating using Google C++ testing framework.
#include "sample1.h"
// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
int Factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
// Returns true if and only if n is a prime number.
bool IsPrime(int n) {
// Trivial case 1: small numbers
if (n <= 1) return false;
// Trivial case 2: even numbers
if (n % 2 == 0) return n == 2;
// Now, we have that n is odd and n >= 3.
// Try to divide n by every odd number i, starting from 3
for (int i = 3; ; i += 2) {
// We only have to try i up to the square root of n
if (i > n/i) break;
// Now, we have i <= n/i < n.
// If n is divisible by i, n is not prime.
if (n % i == 0) return false;
}
// n has no integer factor in the range (1, n), and thus is prime.
return true;
}
// Writing a unit test using Google C++ testing framework is easy as 1-2-3:
// Step 1. Include necessary header files such that the stuff your
// test logic needs is declared.
//
// Don't forget gtest.h, which declares the testing framework.
#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"
namespace {
// Step 2. Use the TEST macro to define your tests.
//
// TEST has two parameters: the test case name and the test name.
// After using the macro, you should define your test logic between a
// pair of braces. You can use a bunch of macros to indicate the
// success or failure of a test. EXPECT_TRUE and EXPECT_EQ are
// examples of such macros. For a complete list, see gtest.h.
//
// Tests Factorial().
// Tests factorial of negative numbers.
TEST(FactorialTest, Negative) {
// This test is named "Negative", and belongs to the "FactorialTest"
// test case.
EXPECT_EQ(1, Factorial(-5));
EXPECT_EQ(1, Factorial(-1));
EXPECT_GT(Factorial(-10), 0);
}
// Tests of 0.
TEST(FactorialTest, Zero) {
EXPECT_EQ(1, Factorial(0));
}
// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}
// Tests IsPrime()
// Tests negative input.
TEST(IsPrimeTest, Negative) {
// This test belongs to the IsPrimeTest test case.
EXPECT_FALSE(IsPrime(-1));
EXPECT_FALSE(IsPrime(-2));
EXPECT_FALSE(IsPrime(INT_MIN));
}
// Tests some trivial cases.
TEST(IsPrimeTest, Trivial) {
EXPECT_FALSE(IsPrime(0));
EXPECT_FALSE(IsPrime(1));
EXPECT_TRUE(IsPrime(2));
EXPECT_TRUE(IsPrime(3));
}
// Tests positive input.
TEST(IsPrimeTest, Positive) {
EXPECT_FALSE(IsPrime(4));
EXPECT_TRUE(IsPrime(5));
EXPECT_FALSE(IsPrime(6));
EXPECT_TRUE(IsPrime(23));
}
} // namespace
// Step 3. Call RUN_ALL_TESTS() in main().
//
// We do this by linking in src/gtest_main.cc file, which consists of
// a main() function which calls RUN_ALL_TESTS() for us.
//
// This runs all the tests you've defined, prints the result, and
// returns 0 if successful, or 1 otherwise.
//
// Did you notice that we didn't register the tests? The
// RUN_ALL_TESTS() macro magically knows about all the tests we
// defined. Isn't this convenient?
每日音乐:很棒的一个国风xRa3的曲子