2025-02-27 20:56阅读: 2评论: 0推荐: 0

googletest:sample4分析

被测文件

simple4实现了一个简单计数器,提供增加计数、减少计数、打印功能.

simple4.h

// A simple monotonic counter.
class Counter {
 private:
  int counter_;

 public:
  // Creates a counter that starts at 0.
  Counter() : counter_(0) {}

  // Returns the current counter value, and increments it.
  int Increment();

  // Returns the current counter value, and decrements it.
  int Decrement();

  // Prints the current counter value to STDOUT.
  void Print() const;
};

simple4.cc

#include "sample4.h"

#include <stdio.h>

// Returns the current counter value, and increments it.
int Counter::Increment() { return counter_++; }

// Returns the current counter value, and decrements it.
// counter can not be less than 0, return 0 in this case
int Counter::Decrement() {
  if (counter_ == 0) {
    return counter_;
  } else {
    return counter_--;
  }
}

// Prints the current counter value to STDOUT.
void Counter::Print() const { printf("%d", counter_); }

测试文件

sample4_unittests.cc

#include "sample4.h"

#include "gtest/gtest.h"

namespace {
// Tests the Increment() method.

TEST(Counter, Increment) {
  Counter c;

  // Test that counter 0 returns 0
  EXPECT_EQ(0, c.Decrement());

  // EXPECT_EQ() evaluates its arguments exactly once, so they
  // can have side effects.

  EXPECT_EQ(0, c.Increment());
  EXPECT_EQ(1, c.Increment());
  EXPECT_EQ(2, c.Increment());

  EXPECT_EQ(3, c.Decrement());
}

}  // namespace

测试文件只对Increment()方法进行了测试,其中的Decrement()只是用来返回当前计时值.

本文作者:明明1109

本文链接:https://www.cnblogs.com/fortunely/p/18741663

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   明明1109  阅读(2)  评论(0编辑  收藏  举报
历史上的今天:
2022-02-27 muduo笔记 线程安全相关类MutexLock, MutexLockGuard
2022-02-27 muduo笔记 标记类copyable, noncopyable
2022-02-27 muduo笔记 原子类AtomicIntegerT<T>
2022-02-27 muduo笔记 时间戳类Timestamp
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.