1. 前言

最近突然对代码测试非常感兴趣, 学习了一些google test 框架的内容之后, 开始啃Modern C++ Programming with Test-Driven Develpment,ie, code better sleep better

工程代码地址: https://github.com/zhyh2010/DDTlearning
电子书资源: http://download.csdn.net/detail/zhyh1435589631/9672524

2. 主要内容

2.1 小结的一些东西

  1. chp 2 主要是手把手的带我们写了一个小程序, 涉及TDD, refactor等各种东西, 麻雀虽小五脏俱全
  2. 主要借助的是google mock 的测试框架, 我们这里顺手替换成了 google test, 也非常类似
  3. 每次先写测试代码, 测试不通过之后, 再考虑编写相应代码使得测试代码可以通过
  4. 每次写代码的时候, 为了让测试得以通过, 怎么方便怎么写, 根据不断的新的测试需求, 对代码不断重构 refactor
  5. TDD 先写测试再写功能代码, 测试(先写功能代码),效果一致, 不过如果是TDD 驱动的话, 可能测试用例会想的更全一些, 更加稳健
  6. 使用 DISABLED_ 前缀来使一个测试无效
  7. 除了用户需求之外, 还应该关注一些, 异常情况,非法输入的处理
  8. 单一职责SRP 的使用

2.2 经典语句

1.test list

Each test you write in TDD and get to pass represents a new, working piece of
behavior that you add to the system. Aside from getting an entire feature shipped,
your passing tests represent your best measure of progress. You name each test to

2.incrementalism
这个就非常类似软件工程中所谈及的敏捷开发的概念了, 小步伐, 快速更新迭代

We have two jobs: write a new test that describes the behavior, and change
our existing test to ensure it meets the spec.

A strength of TDD is its ability to let you move forward in the face of incomplete information and in its ability to let you correct earlier choices as new information
arises.

3.Thinking and TDD
利用测试驱动, 编写满足需求的代码

The cycle of TDD, once again in brief, is to write a small test, ensure it fails, get it to pass, review and clean up the design (including that of the tests), and ensure the tests all still pass

3. 相应的代码

SoundexTest.cpp

#include "gtest/gtest.h"
#include "Soundex.h"class SoundexEncoding :public testing::Test{
public:Soundex soundex;
};TEST_F(SoundexEncoding, RetainsSoleLetterOfOneLetterWord){auto encoded = soundex.encode("A");ASSERT_EQ(encoded, std::string("A000"));
}TEST_F(SoundexEncoding, PadsWithZerosToEnsureThreeDigits){auto encoded = soundex.encode("I");ASSERT_EQ(encoded, std::string("I000"));
}TEST_F(SoundexEncoding, ReplaceConsonantsWithAppropriateDigits){EXPECT_EQ(soundex.encode("Ab"), std::string("A100"));EXPECT_EQ(soundex.encode("Ac"), std::string("A200"));EXPECT_EQ(soundex.encode("Ad"), std::string("A300"));EXPECT_EQ(soundex.encode("Ax"), std::string("A200"));EXPECT_EQ(soundex.encode("A@"), std::string("A000"));
}TEST_F(SoundexEncoding, ReplacesMultipleConsonantsWithDigits){ASSERT_EQ(soundex.encode("Acdl"), std::string("A234"));
}TEST_F(SoundexEncoding, LimitsLengthToFourCharacters){ASSERT_EQ(soundex.encode("Dcdlb").length(), 4u);
}TEST_F(SoundexEncoding, IgnoresVowelLikeLetters){ASSERT_EQ(soundex.encode("Baeiouhycdl"), std::string("B234"));
}TEST_F(SoundexEncoding, CombinesDuplicateEncodings){ASSERT_EQ(soundex.encodedDigits('b'), soundex.encodedDigits('f'));ASSERT_EQ(soundex.encodedDigits('c'), soundex.encodedDigits('g'));ASSERT_EQ(soundex.encodedDigits('d'), soundex.encodedDigits('t'));ASSERT_EQ(soundex.encode("Abfcgdt"), std::string("A123"));
}TEST_F(SoundexEncoding, UppercasesFirstLetter){ASSERT_EQ(soundex.encode("abcd").substr(0, 1), std::string("A"));
}TEST_F(SoundexEncoding, IgnoreVowelLikeLetters){ASSERT_EQ(soundex.encode("BaAeEiIoOuUhHyYcdl"), std::string("B234"));
}TEST_F(SoundexEncoding, CombinesDuplicateCodesWhen2ndLetterDuplicateslst){ASSERT_EQ(soundex.encode("Bbcd"), std::string("B230"));
}TEST_F(SoundexEncoding, DoesNotCombineDuplicateEncodingsSeparatedByVowels){ASSERT_EQ(soundex.encode("Jbob"), std::string("J110"));
}int main(int argc, char ** argv){::testing::InitGoogleTest(&argc, argv);RUN_ALL_TESTS();system("pause");return 0;
}

Soundex.h

#ifndef _SOUNDEX_H_
#define _SOUNDEX_H_#include <string>
#include <unordered_map>
#include <cctype>class Soundex{static const size_t MaxCodeLength{ 4 };const std::string NotADigit{ "*" };public:std::string encode(const std::string & word) const{return zeroPad(upperFront(head(word)) + tail(encodedDigits(word)));}std::string encodedDigits(char letter) const{const std::unordered_map<char, std::string> encodings{{ 'b', "1" }, { 'f', "1" }, { 'p', "1" }, { 'v', "1" },{ 'c', "2" }, { 'g', "2" }, { 'j', "2" }, { 'k', "2" },{ 'q', "2" }, { 's', "2" }, { 'x', "2" }, { 'z', "2" },{ 'd', "3" }, { 't', "3" },{ 'l', "4" },{ 'm', "5" }, { 'n', "5" },{ 'r', "6" }};auto it = encodings.find(tolower(letter));return it == encodings.end() ? NotADigit : it->second;}private:std::string upperFront(const std::string & string) const {return std::string(1, std::toupper(static_cast<unsigned char>(string.front())));}char lower(char c) const{return std::tolower(static_cast<unsigned char>(c));}std::string head(const std::string & word) const{return word.substr(0, 1);}std::string tail(const std::string & word) const{return word.substr(1);}void encodeHead(std::string & encoding, const std::string & word) const{encoding += encodedDigits(word.front());}void encodeLetter(std::string & encoding, char letter, char lastLetter) const{auto digit = encodedDigits(letter);if (digit != NotADigit && (digit != lastDigit(encoding) || isVowel(lastLetter)))encoding += digit;}bool isVowel(char letter) const{return std::string("aeiouy").find(lower(letter)) != std::string::npos;}void encodeTail(std::string & encoding, const std::string & word) const{for (auto i = 1u; i < word.length(); i++){if (!isComplete(encoding))encodeLetter(encoding, word[i], word[i - 1]);           }}std::string encodedDigits(const std::string & word) const{std::string encoding;encodeHead(encoding, word);encodeTail(encoding, word);                 return encoding;}std::string lastDigit(const std::string & encoding) const{if (encoding.empty()) return NotADigit;return std::string(1, encoding.back());}bool isComplete(const std::string & encoding) const{return encoding.length() == MaxCodeLength;}std::string zeroPad(const std::string & word) const{auto zerosNeed = MaxCodeLength - word.length();return word + std::string(zerosNeed, '0');}
};#endif // _SOUNDEX_H_

[阅读笔记]Modern C++ Programming with Test-Driven Develpment chp2相关推荐

  1. Introduction to modern cryptography 第一章阅读笔记

    文章目录 前言 1.1 密码学以及现代密码学 1.2 私钥密码的设定 (1) 一些定义 (2) 密码方案的语法 (3) Kerckhoffs原理 1.3 一些密码方案 (1) Caesar's cip ...

  2. Robot fish: bio-inspired fishlike underwater robots 阅读笔记 1

    整书概览 第一部分 引言--RuXu Du 1. Propulsion in Water from Ancient to Modern 2. How Do Fish Swim 2.1 Research ...

  3. Reconstruction and Representation of 3D Objects with Radial Basis Functions 阅读笔记

    Reconstruction and Representation of 3D Objects with Radial Basis Functions 阅读笔记 紧接着上面的连篇blog,本篇学习如何 ...

  4. Towards Real-Time Multi-Object Tracking(JDE)论文阅读笔记

    JDE阅读笔记 (一)Title (二)Summary (三)Research Problem (四)Problem Statement (五)Method 5.1 将Detection和Embedd ...

  5. 技术人修炼之道阅读笔记(二)重新定义自己

    技术人修炼之道阅读笔记(二)重新定义自己 在工作中有一个非常普遍的现象:有些人根本不知道自己想要什么或者什么都想要,无法取舍,但是人的精力毕竟是有限.那么我们如何来避免浪费自己的青春年华呢? 这就需要 ...

  6. TCP’s Congestion Control Implementation in Linux Kernel 文献阅读笔记

    TCP's Congestion Control Implementation in Linux Kernel 文献阅读笔记 作者:Somaya Arianfar Aalto University S ...

  7. trainer setup_Detectron2源码阅读笔记-(一)Configamp;Trainer

    一.代码结构概览 1.核心部分 configs:储存各种网络的yaml配置文件 datasets:存放数据集的地方 detectron2:运行代码的核心组件 tools:提供了运行代码的入口以及一切可 ...

  8. VoxelNet阅读笔记

    作者:Tom Hardy Date:2020-02-11 来源:VoxelNet阅读笔记

  9. Transformers包tokenizer.encode()方法源码阅读笔记

    Transformers包tokenizer.encode()方法源码阅读笔记_天才小呵呵的博客-CSDN博客_tokenizer.encode

  10. 源码阅读笔记 BiLSTM+CRF做NER任务 流程图

    源码阅读笔记 BiLSTM+CRF做NER任务(二) 源码地址:https://github.com/ZhixiuYe/NER-pytorch 本篇正式进入源码的阅读,按照流程顺序,一一解剖. 一.流 ...

最新文章

  1. IIS+PHP+MySQL+Zend Optimizer+GD库+phpMyAdmin安装配置[完整修正实用版]
  2. WebService相关
  3. 服务器配置—开网站空间
  4. 控制器属性传值的一些小问题
  5. Pulsar集群搭建部署
  6. 自定函数获取datagrid,datalist,rpeater控件中header,footer栏中控件
  7. 一文读懂 | 进程并发与同步
  8. 屠杀机器人和无处不在的监控:AI是我们最大的生存威胁?
  9. 使用@Conditional条件注解
  10. java中中文乱码_java中中文乱码怎么解决?
  11. mysql批量导出导入数据
  12. shell 脚本实例--持续更新
  13. 解决office2007打开很慢问题
  14. java搜索页面历史记录,页面缓存的操作(搜索历史记录),页面搜索功能实现...
  15. html图片显示详情,纯CSS鼠标经过图片视差弹出层显示详情链接按钮特效代码.html...
  16. 《安富莱嵌入式周报》第269期:2022.06.06--2022.06.12
  17. 保险私有云 IaaS 资源池选型与演进之路 | SmartX 客户实践
  18. SMTP协议:使用telnet发邮件【纯纯小白】
  19. 为什么我选择并且推崇用ROS开发机器人?
  20. idea中maven导入依赖报红的解决办法(版本不一致)

热门文章

  1. 离散数学课程对应目录
  2. 使用python来刷csdn下载积分(一)
  3. vue 引入json地图_前端学习 之 Vue 引入Echarts地图
  4. notepad正则提取
  5. EB开发乱码处理总结
  6. Fiddler2用于手机抓包时的配置方法
  7. Spring源码全解析,帮你彻底学习Spring源码
  8. ofo(小黄车)项目分析
  9. python中如何生成项目帮助文档
  10. 【PLY】Lex和Yacc简单示例