2021-01-19(含Gtest第八个例子)

第三十五天

早上

今天这第八个例子原理上挺简单的,但是他写的代码实在吓人,可以说后面这些代码越来越吓人,但是说实话我又不是做软件的,八成也用不到。

Gtest 小技巧 - 9:"联合"参数化
    前面两节为了让程序方便,我们在参数化的时候分别以类、和指针变量为参数以不同的方法避免了重复的代码。
    而“联合”的意思,是指把讲那些具有相同测试需求的工具类合并,然后在参数化测试的时候,传入的是“设置参数”(就是具体要用那种方法,测多少怎么测)。而这个“传入参数”一般会有多种类型,所以需要使用Combine(),具体用法见示例代码。(但是一旦用了,你的工具类的接口全得改,感觉就我们的测试需求来说远远用不上)

-----------------------------------------------------------------------------------------------------------

// This sample shows how to test code relying on some global flag variables.
// Combine() helps with generating all possible combinations of such flags,
// and each test is given one combination as a parameter.

// Use class definitions to test from this header.
#include "prime_tables.h"

#include "gtest/gtest.h"
namespace {
     

// Suppose we want to introduce a new, improved implementation of PrimeTable
// which combines speed of PrecalcPrimeTable and versatility of
// OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both
// PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more
// appropriate under the circumstances. But in low memory conditions, it can be
// told to instantiate without PrecalcPrimeTable instance at all and use only
// OnTheFlyPrimeTable.
class HybridPrimeTable : public PrimeTable {
     
 public:
  HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
      : on_the_fly_impl_(new OnTheFlyPrimeTable),
        precalc_impl_(force_on_the_fly
                          ? nullptr
                          : new PreCalculatedPrimeTable(max_precalculated)),
        max_precalculated_(max_precalculated) {
     }
  ~HybridPrimeTable() override {
     
    delete on_the_fly_impl_;
    delete precalc_impl_;
  }

  bool IsPrime(int n) const override {
     
    if (precalc_impl_ != nullptr && n < max_precalculated_)
      return precalc_impl_->IsPrime(n);
    else
      return on_the_fly_impl_->IsPrime(n);
  }

  int GetNextPrime(int p) const override {
     
    int next_prime = -1;
    if (precalc_impl_ != nullptr && p < max_precalculated_)
      next_prime = precalc_impl_->GetNextPrime(p);

    return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);
  }

 private:
  OnTheFlyPrimeTable* on_the_fly_impl_;
  PreCalculatedPrimeTable* precalc_impl_;
  int max_precalculated_;
};

using ::testing::TestWithParam;
using ::testing::Bool;
using ::testing::Values;
using ::testing::Combine;

// To test all code paths for HybridPrimeTable we must test it with numbers
// both within and outside PreCalculatedPrimeTable's capacity and also with
// PreCalculatedPrimeTable disabled. We do this by defining fixture which will
// accept different combinations of parameters for instantiating a
// HybridPrimeTable instance.
class PrimeTableTest : public TestWithParam< ::std::tuple<bool, int> > {
     
 protected:
  void SetUp() override {
     
    bool force_on_the_fly;
    int max_precalculated;
    std::tie(force_on_the_fly, max_precalculated) = GetParam();
    table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
  }
  void TearDown() override {
     
    delete table_;
    table_ = nullptr;
  }
  HybridPrimeTable* table_;
};

TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
     
  // Inside the test body, you can refer to the test parameter by GetParam().
  // In this case, the test parameter is a PrimeTable interface pointer which
  // we can use directly.
  // Please note that you can also save it in the fixture's SetUp() method
  // or constructor and use saved copy in the tests.

  EXPECT_FALSE(table_->IsPrime(-5));
  EXPECT_FALSE(table_->IsPrime(0));
  EXPECT_FALSE(table_->IsPrime(1));
  EXPECT_FALSE(table_->IsPrime(4));
  EXPECT_FALSE(table_->IsPrime(6));
  EXPECT_FALSE(table_->IsPrime(100));
}

TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
     
  EXPECT_TRUE(table_->IsPrime(2));
  EXPECT_TRUE(table_->IsPrime(3));
  EXPECT_TRUE(table_->IsPrime(5));
  EXPECT_TRUE(table_->IsPrime(7));
  EXPECT_TRUE(table_->IsPrime(11));
  EXPECT_TRUE(table_->IsPrime(131));
}

TEST_P(PrimeTableTest, CanGetNextPrime) {
     
  EXPECT_EQ(2, table_->GetNextPrime(0));
  EXPECT_EQ(3, table_->GetNextPrime(2));
  EXPECT_EQ(5, table_->GetNextPrime(3));
  EXPECT_EQ(7, table_->GetNextPrime(5));
  EXPECT_EQ(11, table_->GetNextPrime(7));
  EXPECT_EQ(131, table_->GetNextPrime(128));
}

// In order to run value-parameterized tests, you need to instantiate them,
// or bind them to a list of values which will be used as test parameters.
// You can instantiate them in a different translation module, or even
// instantiate them several times.
//
// Here, we instantiate our tests with a list of parameters. We must combine
// all variations of the boolean flag suppressing PrecalcPrimeTable and some
// meaningful values for tests. We choose a small value (1), and a value that
// will put some of the tested numbers beyond the capability of the
// PrecalcPrimeTable instance and some inside it (10). Combine will produce all
// possible combinations.
INSTANTIATE_TEST_SUITE_P(MeaningfulTestParameters, PrimeTableTest,
                         Combine(Bool(), Values(1, 10)));

}  // namespace

然后去练练写作吧。
整体的意思能记住,细节有遗忘。

下午

    按照老师的话,改变了一下写代码的思路:从测试开始设计,以“系统”的整体思想去考虑了一下,确实收获很大。
    整个思路都不一样了,尤其是当你以接口和适用性为最终目的的时候,而不是为了单单“实现功能”。原来的时候确实蠢,想到啥写啥,完全没有规划,重构也不过是让代码更冗余罢了,不敢相信当时改的时候还感觉良好。
    最后的计划是按照上个月在黑河调试的时候使用别人的api的时候的感觉,进行代码设计,最明显的特点就是

  • 不再分Client和Server端
  • 按功能进行库文件归类,而不是类型。
  • 代码、函数要精简,记住我们现在是提供的框架,是api,不是让你写一个程序出来。
  • 当然,最后我会写几个样例和test、readme之类的文件

   这样一看的话就把我之前混沌复杂的工作明晰化了,从此来说似乎确实用不了太久就能搭完,然后用自己的API进行程序设计体验一下,这样在写代码是就能全神贯注的去思考实现思路,而不是过一会就要去调整底层,因为之前的代码过于specific了。然后根据使用到的不足再做修改,最后给老师同学体验!

你可能感兴趣的:(Gtest学习,学习日志)