演进式设计——量纲系统

举报
用户已注销 发表于 2021/11/19 04:13:10 2021/11/19
【摘要】 一,需求 迭代1 1,实现一个长度(Length)计算系统     用户既可以使用Mile为单位来记录一个长度,也可以用Yard为单位来记录 2,用户可以对比两个用不同单位表示的长度的相等关系     1 Mile = 1760 Yard 迭代2 1,加入一个新的单位:...

一,需求

迭代1
1,实现一个长度(Length)计算系统
    用户既可以使用Mile为单位来记录一个长度,也可以用Yard为单位来记录
2,用户可以对比两个用不同单位表示的长度的相等关系
    1 Mile = 1760 Yard

迭代2
1,加入一个新的单位:Feet
    1 Yard = 3Feet
2,可以对比任意单位所表示的长度之间的相等关系
    1 Yard = 3 Feet
    1 Mile = 5280 Feet

迭代3
1,再加入一个新的单位:Inch
    1 Feet = 12 Inch
2,能够对比任意单位表示的长度之间的相等关系
    1 Feet = 12 Inch
    1 Yard = 36 Inch
    1 Mile = 63360 Inch

迭代4
1,增加一个容量(Volume)计算系统
    包含三个计量单位TSP(茶匙)、TBSP(汤匙)、OZ(盎司)
    1 TBSP = 3 TSP
    1 OZ = 2 TBSP

迭代5
1、    增加对于长度和容量的加法
    12 Inch + 2 Feet = 3 Feet
    2 TBSP + 1 OZ = 12 TSP

迭代6
1、    为了调试的需要,现需要将一个长度或者容量输出到屏幕,格式如下(以长度为例):
    长度对象的最小精度为1 Inch
    单位按照从大到小的顺序,如果一个长度在某个单位上为非0值,则应该给出其数值和单位
    Length(35, INCH)  -> 2 FEET 11 INCH
    Length(38, INCH)  -> 1 YARD 2 INCH
    Length(48, INCH)  -> 1 YARD 1 FEET
    Length(5,  FEET)  -> 1 YARD 2 FEET
    Length(1781, YARD) -> 1 MILE 21 YARD

迭代7
1、    增加一种新的打印方式(以长度为例)
    长度对象的最小精度为1 Inch
    所有的长度对象都以Inch为单位进行打印
    Length(2, INCH)   ->  2 INCH
    Length(2, FEET)     ->  24 INCH
    Length(3, YARD)   ->  108 INCH
    Length(2, MILE)     -> 126720 INCH

二,实现

头文件


  
  1. #ifndef HELLO_WORLD_H__
  2. #define HELLO_WORLD_H__
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. typedef unsigned char VOS_UINT8;
  7. typedef unsigned short VOS_UINT16;
  8. typedef unsigned int VOS_UINT32;
  9. typedef signed char VOS_INT8;
  10. typedef signed short VOS_INT16;
  11. typedef signed int VOS_INT32;
  12. typedef void VOS_VOID;
  13. #define VOS_NULL 0
  14. #define VOS_NULL_PTR 0L
  15. #define VOS_NULL_BYTE 0xFF
  16. #define VOS_NULL_WORD 0xFFFF
  17. #define VOS_NULL_DWORD 0xFFFFFFFF
  18. #define VOS_NULL_LONG VOS_NULL_DWORD
  19. #define ARF_SUCCESS 0
  20. #define ARF_FAIL (-1)
  21. typedef enum Length {
  22. MILE,
  23. YARD,
  24. FEET,
  25. INCH,
  26. LENGTH_BUF
  27. } Length;
  28. typedef enum Volume {
  29. TSP = LENGTH_BUF+1,
  30. TBSP,
  31. OZ
  32. } Volume;
  33. typedef enum Type {
  34. LENGTH,
  35. VOLUME
  36. } Type;
  37. typedef struct Node {
  38. int value;
  39. int type;
  40. } Node;
  41. #define M 100
  42. typedef struct NodeList {
  43. Node node[M];
  44. int num;
  45. } NodeList;
  46. typedef enum COM {
  47. SMALLER,
  48. EQUAL,
  49. BIGGER
  50. } COM;
  51. void init();
  52. int input(int value, int type);
  53. Node getLengthNode(int id);
  54. Node getVolumeNode(int id);
  55. int compare(Node n1, Node n2);
  56. Node added(Node a, Node b);
  57. void displayWithMax(Node a);
  58. void displayWithMin(Node a);
  59. #ifdef __cplusplus
  60. }
  61. #endif
  62. #endif /* _HELLO_WORLD_H_ */

源代码


  
  1. #include "unitest.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. NodeList lengthList, volumeList;
  6. void init()
  7. {
  8. lengthList.num = 0;
  9. volumeList.num = 0;
  10. }
  11. static int getDimenType(int type)
  12. {
  13. if (type < LENGTH_BUF) {
  14. return LENGTH;
  15. } else {
  16. return VOLUME;
  17. }
  18. }
  19. static NodeList *getList(int dimenType)
  20. {
  21. if (dimenType == LENGTH) {
  22. return &lengthList;
  23. } else if (dimenType == VOLUME) {
  24. return &volumeList;
  25. }
  26. return NULL;
  27. }
  28. // 输入并返回存储的id
  29. int input(int value, int type)
  30. {
  31. NodeList *pnode = getList(getDimenType(type));
  32. if (!pnode || pnode->num >= M) {
  33. return -1;
  34. }
  35. pnode->node[pnode->num].value = value, pnode->node[pnode->num].type = type;
  36. pnode->num++;
  37. return pnode->num - 1;
  38. }
  39. static Node getNode(int dimenType, int id)
  40. {
  41. Node defau = { 0, 0 };
  42. NodeList *pnode = getList(dimenType);
  43. if (!pnode) {
  44. return defau;
  45. }
  46. if (id < 0 || id >= pnode->num) {
  47. return defau;
  48. }
  49. return pnode->node[id];
  50. }
  51. Node getLengthNode(int id)
  52. {
  53. return getNode(LENGTH, id);
  54. }
  55. Node getVolumeNode(int id)
  56. {
  57. return getNode(VOLUME, id);
  58. }
  59. int g_type[] = {INCH, FEET, YARD, MILE, LENGTH_BUF,TSP, TBSP, OZ};
  60. int g_dimen[] = {1, 12, 36, 63360, 1, 1, 3, 6};
  61. static int getDimension(int type)
  62. {
  63. int size = sizeof(g_type) / sizeof(g_type[0]);
  64. for(int i = size-1; i>=0; i--){
  65. if(type == g_type[i]){
  66. return g_dimen[i];
  67. }
  68. }
  69. return 0;
  70. }
  71. static int getValue(Node node)
  72. {
  73. return node.value * getDimension(node.type);
  74. }
  75. int compare(Node n1, Node n2)
  76. {
  77. if (getValue(n1) > getValue(n2)) {
  78. return BIGGER;
  79. } else if (getValue(n1) < getValue(n2)) {
  80. return SMALLER;
  81. } else {
  82. return EQUAL;
  83. }
  84. }
  85. static int getBaseType(Node a)
  86. {
  87. if (getDimenType(a.type) == LENGTH) {
  88. return INCH;
  89. }
  90. if (getDimenType(a.type) == VOLUME) {
  91. return TSP;
  92. }
  93. return -1;
  94. }
  95. Node added(Node a, Node b)
  96. {
  97. int sumValue = getValue(a) + getValue(b);
  98. int type = getBaseType(a);
  99. Node ans = { sumValue, type };
  100. return ans;
  101. }
  102. static int getMaxId(int type)
  103. {
  104. if(getDimenType(type)==LENGTH){
  105. return 3;
  106. }
  107. if(getDimenType(type)==VOLUME){
  108. return 7;
  109. }
  110. }
  111. static int getMinId(int type)
  112. {
  113. if(getDimenType(type)==LENGTH){
  114. return 0;
  115. }
  116. if(getDimenType(type)==VOLUME){
  117. return 5;
  118. }
  119. }
  120. #define dispalyIfEqual(type,x) if(type==x){printf("%s",#x);return;}
  121. static void displayType(int type)
  122. {
  123. dispalyIfEqual(type,INCH);
  124. dispalyIfEqual(type,FEET);
  125. dispalyIfEqual(type,YARD);
  126. dispalyIfEqual(type,MILE);
  127. dispalyIfEqual(type,TSP);
  128. dispalyIfEqual(type,TBSP);
  129. dispalyIfEqual(type,OZ);
  130. }
  131. void displayFromId(Node a, int id)
  132. {
  133. int value = getValue(a);
  134. int maxId = id;
  135. for(int i = maxId; i>=0; i--){
  136. if(value >= g_dimen[i]){
  137. printf("%d",value/g_dimen[i]);
  138. displayType(g_type[i]);
  139. printf(" ");
  140. value%=g_dimen[i];
  141. }
  142. }
  143. printf("\n");
  144. }
  145. void displayWithMax(Node a)
  146. {
  147. displayFromId(a, getMaxId(a.type));
  148. }
  149. void displayWithMin(Node a)
  150. {
  151. displayFromId(a, getMinId(a.type));
  152. }

三,测试用例


  
  1. #include "gtest/gtest.h"
  2. #include "mockcpp/include/mockcpp/mokc.h"
  3. #include "unitest.h"
  4. /* 这个文件是个验证工程集成 mockcpp 与 gtest 的测试工程示例文件,请根据自己的需求增加测试用例文件 */
  5. class MockCppTest : public ::testing::Test {
  6. protected:
  7. void SetUp()
  8. {
  9. init();
  10. }
  11. void TearDown() {}
  12. };
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. int helloworld()
  17. {
  18. printf("hello world !\n");
  19. return 0;
  20. }
  21. int MockCppFuncTest(int a, int b)
  22. {
  23. return a + b;
  24. }
  25. #ifdef __cplusplus
  26. }
  27. #endif
  28. // 不依赖于测试套的单个函数的测试用例,可以用 TEST
  29. TEST(ArfTest, should_return_helloworld) {}
  30. // 基于带测试套初始化的测试用例,可以用 TEST_F
  31. TEST_F(MockCppTest, should_mockcpp_work)
  32. {
  33. // 函数打桩,可以用这个接口
  34. MOCKER(MockCppFuncTest).stubs().will(returnValue(100));
  35. int ret = MockCppFuncTest(1, 2);
  36. }
  37. // ----------------开始迭代1----------------
  38. TEST_F(MockCppTest, input_one_node)
  39. {
  40. printf("begin test --------------------- input_one_node \n");
  41. int ret = input(1, MILE);
  42. ASSERT_EQ(0, ret);
  43. printf("success\n");
  44. }
  45. TEST_F(MockCppTest, input_and_compare_bigger)
  46. {
  47. printf("begin test --------------------- input_and_compare_bigger \n");
  48. int ret1 = input(1, MILE);
  49. ASSERT_EQ(0, ret1);
  50. int ret2 = input(1759, YARD);
  51. ASSERT_EQ(1, ret2);
  52. Node n1 = getLengthNode(ret1);
  53. Node n2 = getLengthNode(ret2);
  54. int ret3 = compare(n1, n2);
  55. ASSERT_EQ(BIGGER, ret3);
  56. printf("success\n");
  57. }
  58. TEST_F(MockCppTest, input_and_compare_smaller)
  59. {
  60. printf("begin test --------------------- input_and_compare_smaller \n");
  61. int ret1 = input(1, MILE);
  62. ASSERT_EQ(0, ret1);
  63. int ret2 = input(1761, YARD);
  64. ASSERT_EQ(1, ret2);
  65. Node n1 = getLengthNode(ret1);
  66. Node n2 = getLengthNode(ret2);
  67. int ret3 = compare(n1, n2);
  68. ASSERT_EQ(SMALLER, ret3);
  69. printf("success\n");
  70. }
  71. TEST_F(MockCppTest, input_and_compare_equal)
  72. {
  73. printf("begin test --------------------- input_and_compare_equal \n");
  74. int ret1 = input(1, MILE);
  75. ASSERT_EQ(0, ret1);
  76. int ret2 = input(1760, YARD);
  77. ASSERT_EQ(1, ret2);
  78. Node n1 = getLengthNode(ret1);
  79. Node n2 = getLengthNode(ret2);
  80. int ret3 = compare(n1, n2);
  81. ASSERT_EQ(EQUAL, ret3);
  82. printf("success\n");
  83. }
  84. // ----------------开始迭代2----------------
  85. TEST_F(MockCppTest, input_and_compare_bigger2)
  86. {
  87. printf("begin test --------------------- input_and_compare_bigger2 \n");
  88. int ret1 = input(1, MILE);
  89. ASSERT_EQ(0, ret1);
  90. int ret2 = input(5279, FEET);
  91. ASSERT_EQ(1, ret2);
  92. Node n1 = getLengthNode(ret1);
  93. Node n2 = getLengthNode(ret2);
  94. int ret3 = compare(n1, n2);
  95. ASSERT_EQ(BIGGER, ret3);
  96. printf("success\n");
  97. }
  98. TEST_F(MockCppTest, input_and_compare_smaller2)
  99. {
  100. printf("begin test --------------------- input_and_compare_smaller2 \n");
  101. int ret1 = input(1, MILE);
  102. ASSERT_EQ(0, ret1);
  103. int ret2 = input(5281, FEET);
  104. ASSERT_EQ(1, ret2);
  105. Node n1 = getLengthNode(ret1);
  106. Node n2 = getLengthNode(ret2);
  107. int ret3 = compare(n1, n2);
  108. ASSERT_EQ(SMALLER, ret3);
  109. printf("success\n");
  110. }
  111. TEST_F(MockCppTest, input_and_compare_equal2)
  112. {
  113. printf("begin test --------------------- input_and_compare_equal2 \n");
  114. int ret1 = input(1, MILE);
  115. ASSERT_EQ(0, ret1);
  116. int ret2 = input(5280, FEET);
  117. ASSERT_EQ(1, ret2);
  118. Node n1 = getLengthNode(ret1);
  119. Node n2 = getLengthNode(ret2);
  120. int ret3 = compare(n1, n2);
  121. ASSERT_EQ(EQUAL, ret3);
  122. printf("success\n");
  123. }
  124. // ----------------开始迭代3----------------
  125. TEST_F(MockCppTest, input_and_compare_bigger3)
  126. {
  127. printf("begin test --------------------- input_and_compare_bigger3 \n");
  128. int ret1 = input(1, MILE);
  129. ASSERT_EQ(0, ret1);
  130. int ret2 = input(63359, INCH);
  131. ASSERT_EQ(1, ret2);
  132. Node n1 = getLengthNode(ret1);
  133. Node n2 = getLengthNode(ret2);
  134. int ret3 = compare(n1, n2);
  135. ASSERT_EQ(BIGGER, ret3);
  136. printf("success\n");
  137. }
  138. TEST_F(MockCppTest, input_and_compare_smaller3)
  139. {
  140. printf("begin test --------------------- input_and_compare_smaller3 \n");
  141. int ret1 = input(1, MILE);
  142. ASSERT_EQ(0, ret1);
  143. int ret2 = input(63361, INCH);
  144. ASSERT_EQ(1, ret2);
  145. Node n1 = getLengthNode(ret1);
  146. Node n2 = getLengthNode(ret2);
  147. int ret3 = compare(n1, n2);
  148. ASSERT_EQ(SMALLER, ret3);
  149. printf("success\n");
  150. }
  151. TEST_F(MockCppTest, input_and_compare_equal3)
  152. {
  153. printf("begin test --------------------- input_and_compare_equal3 \n");
  154. int ret1 = input(1, MILE);
  155. ASSERT_EQ(0, ret1);
  156. int ret2 = input(63360, INCH);
  157. ASSERT_EQ(1, ret2);
  158. Node n1 = getLengthNode(ret1);
  159. Node n2 = getLengthNode(ret2);
  160. int ret3 = compare(n1, n2);
  161. ASSERT_EQ(EQUAL, ret3);
  162. printf("success\n");
  163. }
  164. // ----------------开始迭代4----------------
  165. TEST_F(MockCppTest, input_and_compare_bigger4)
  166. {
  167. printf("begin test --------------------- input_and_compare_bigger4 \n");
  168. int ret1 = input(1, TBSP);
  169. ASSERT_EQ(0, ret1);
  170. int ret2 = input(2, TSP);
  171. ASSERT_EQ(1, ret2);
  172. Node n1 = getVolumeNode(ret1);
  173. Node n2 = getVolumeNode(ret2);
  174. int ret3 = compare(n1, n2);
  175. ASSERT_EQ(BIGGER, ret3);
  176. printf("success\n");
  177. }
  178. TEST_F(MockCppTest, input_and_compare_smaller4)
  179. {
  180. printf("begin test --------------------- input_and_compare_smaller4 \n");
  181. int ret1 = input(1, TBSP);
  182. ASSERT_EQ(0, ret1);
  183. int ret2 = input(4, TSP);
  184. ASSERT_EQ(1, ret2);
  185. Node n1 = getVolumeNode(ret1);
  186. Node n2 = getVolumeNode(ret2);
  187. int ret3 = compare(n1, n2);
  188. ASSERT_EQ(SMALLER, ret3);
  189. printf("success\n");
  190. }
  191. TEST_F(MockCppTest, input_and_compare_equal4)
  192. {
  193. printf("begin test --------------------- input_and_compare_equal4 \n");
  194. int ret1 = input(1, TBSP);
  195. ASSERT_EQ(0, ret1);
  196. int ret2 = input(3, TSP);
  197. ASSERT_EQ(1, ret2);
  198. Node n1 = getVolumeNode(ret1);
  199. Node n2 = getVolumeNode(ret2);
  200. int ret3 = compare(n1, n2);
  201. ASSERT_EQ(EQUAL, ret3);
  202. printf("success\n");
  203. }
  204. // ----------------开始迭代5----------------
  205. TEST_F(MockCppTest, length_added)
  206. {
  207. printf("begin test --------------------- length_added \n");
  208. int ret1 = input(12, INCH);
  209. ASSERT_EQ(0, ret1);
  210. int ret2 = input(2, FEET);
  211. ASSERT_EQ(1, ret2);
  212. Node n1 = getLengthNode(ret1);
  213. Node n2 = getLengthNode(ret2);
  214. Node sumNode = added(n1, n2);
  215. Node rightAns = { 3, FEET };
  216. int ret3 = compare(sumNode, rightAns);
  217. ASSERT_EQ(EQUAL, ret3);
  218. printf("success\n");
  219. }
  220. TEST_F(MockCppTest, volume_added)
  221. {
  222. printf("begin test --------------------- volume_added \n");
  223. int ret1 = input(2, TBSP);
  224. ASSERT_EQ(0, ret1);
  225. int ret2 = input(1, OZ);
  226. ASSERT_EQ(1, ret2);
  227. Node n1 = getVolumeNode(ret1);
  228. Node n2 = getVolumeNode(ret2);
  229. Node sumNode = added(n1, n2);
  230. Node rightAns = { 12, TSP };
  231. int ret3 = compare(sumNode, rightAns);
  232. ASSERT_EQ(EQUAL, ret3);
  233. printf("success\n");
  234. }
  235. // ----------------开始迭代6----------------
  236. TEST_F(MockCppTest, displaylength1)
  237. {
  238. printf("begin test --------------------- displaylength1 \n");
  239. int ret = input(35, INCH);
  240. ASSERT_EQ(0, ret);
  241. Node n = getLengthNode(ret);
  242. displayWithMax(n); // 2FEET 11INCH
  243. printf("success\n");
  244. }
  245. // ----------------开始迭代7----------------
  246. TEST_F(MockCppTest, displaylength2)
  247. {
  248. printf("begin test --------------------- displaylength2 \n");
  249. int ret = input(35, INCH);
  250. ASSERT_EQ(0, ret);
  251. Node n = getLengthNode(ret);
  252. displayWithMin(n); // 35INCH
  253. printf("success\n");
  254. }

 

文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nameofcsdn/article/details/112006170

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。