(八)cmockery中的calculator和run_tests函数的注释代码

    所分析的calculator.c和calculator_test.c文件位于 工程中的 cmockery/src/example/ 目录下,是一个相对而言比较全面的样例程序,用到了cmockery项目中的大多数单元测试方法。基本上涵盖了之前所有的样例程序中的用法,还有两组测试是database操作的样例程序没有做分析也不准备去做了。在结束了这一个样例程序的分析之后,大概就可以将整个cmockery程序过了百分之70的样子,接下来就可以将整个项目代码过一遍了。
calculator.c
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <assert.h>
  5. #ifdef HAVE_MALLOC_H
  6. #include <malloc.h>
  7. #endif
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #define UNIT_TESTING 1
  12. // If this is being built for a unit test.
  13. #if UNIT_TESTING
  14. /* Redirect printf to a function in the test application so it's possible to
  15. * test the standard output. */
  16. #ifdef printf
  17. #undef printf
  18. #endif // printf
  19. #define printf example_test_printf
  20. extern void print_message(const char *format, ...);
  21. extern int example_test_printf(const char *format, ...);
  22. /* Redirect fprintf to a function in the test application so it's possible to
  23. * test error messages. */
  24. #ifdef fprintf
  25. #undef fprintf
  26. #endif // fprintf
  27. #define fprintf example_test_fprintf
  28. extern int example_test_fprintf(FILE * const file, const char *format, ...);
  29. // Redirect assert to mock_assert() so assertions can be caught by cmockery.
  30. #ifdef assert
  31. #undef assert
  32. #endif // assert
  33. #define assert(expression)
  34. mock_assert((int)(expression), #expression, __FILE__, __LINE__)
  35. void mock_assert(const int result, const char* expression, const char *file,
  36. const int line);
  37. /* Redirect calloc and free to test_calloc() and test_free() so cmockery can
  38. * check for memory leaks. */
  39. #ifdef calloc
  40. #undef calloc
  41. #endif // calloc
  42. #define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
  43. #ifdef free
  44. #undef free
  45. #endif // free
  46. #define free(ptr) _test_free(ptr, __FILE__, __LINE__)
  47. void* _test_calloc(const size_t number_of_elements, const size_t size,
  48. const char* file, const int line);
  49. void _test_free(void* const ptr, const char* file, const int line);
  50. /* main is defined in the unit test so redefine name of the the main function
  51. * here. */
  52. #define main example_main
  53. /* All functions in this object need to be exposed to the test application,
  54. * so redefine static to nothing. */
  55. #define static
  56. #endif // UNIT_TESTING
  57. // A binary arithmetic integer operation (add, subtract etc.)
  58. typedef int (*BinaryOperator)(int a, int b);
  59. // Structure which maps operator strings to functions.
  60. typedef struct OperatorFunction {
  61. const char* operator;
  62. BinaryOperator function;
  63. } OperatorFunction;
  64. int add(int a, int b);
  65. int subtract(int a, int b);
  66. int multiply(int a, int b);
  67. int divide(int a, int b);
  68. // Associate operator strings to functions.
  69. static OperatorFunction operator_function_map[] = {
  70. {"+", add},
  71. {"-", subtract},
  72. {"*", multiply},
  73. {"/", divide},
  74. };
  75. int add(int a, int b) {
  76. return a + b;
  77. }
  78. int subtract(int a, int b) {
  79. return a - b;
  80. }
  81. int multiply(int a, int b) {
  82. return a * b;
  83. }
  84. int divide(int a, int b) {
  85. assert(b); // Check for divide by zero.
  86. return a / b;
  87. }
  88. /* Searches the specified array of operator_functions for the function
  89. * associated with the specified operator_string. This function returns the
  90. * function associated with operator_string if successful, NULL otherwise.
  91. */
  92. BinaryOperator find_operator_function_by_string(
  93. const size_t number_of_operator_functions,
  94. const OperatorFunction * const operator_functions,
  95. const char* const operator_string) {
  96. size_t i;
  97. assert(!number_of_operator_functions || operator_functions);
  98. assert(operator_string);
  99. for (i = 0; i < number_of_operator_functions; i++) {
  100. const OperatorFunction *const operator_function =
  101. &operator_functions[i];
  102. if (strcmp(operator_function->operator, operator_string) == 0) {
  103. return operator_function->function;
  104. }
  105. }
  106. return NULL;
  107. }
  108. /* Perform a series of binary arithmetic integer operations with no operator
  109. * precedence.
  110. *
  111. * The input expression is specified by arguments which is an array of
  112. * containing number_of_arguments strings. Operators invoked by the expression
  113. * are specified by the array operator_functions containing
  114. * number_of_operator_functions, OperatorFunction structures. The value of
  115. * each binary operation is stored in a pointer returned to intermediate_values
  116. * which is allocated by malloc().
  117. *
  118. * If successful, this function returns the integer result of the operations.
  119. * If an error occurs while performing the operation error_occurred is set to
  120. * 1, the operation is aborted and 0 is returned.
  121. */
  122. int perform_operation(
  123. int number_of_arguments, char *arguments[],
  124. const size_t number_of_operator_functions,
  125. const OperatorFunction * const operator_functions,
  126. int * const number_of_intermediate_values,
  127. int ** const intermediate_values, int * const error_occurred) {
  128. char *end_of_integer;
  129. int value;
  130. unsigned int i;
  131. assert(!number_of_arguments || arguments);
  132. assert(!number_of_operator_functions || operator_functions);
  133. assert(error_occurred);
  134. assert(number_of_intermediate_values);
  135. assert(intermediate_values);
  136. *error_occurred = 0;
  137. *number_of_intermediate_values = 0;
  138. *intermediate_values = NULL;
  139. if (!number_of_arguments)
  140. return 0;
  141. // Parse the first value.
  142. value = (int)strtol(arguments[0], &end_of_integer, 10);
  143. if (end_of_integer == arguments[0]) {
  144. // If an error occurred while parsing the integer.
  145. fprintf(stderr, "Unable to parse integer from argument %s ",
  146. arguments[0]);
  147. *error_occurred = 1;
  148. return 0;
  149. }
  150. // Allocate an array for the output values.
  151. *intermediate_values = calloc(((number_of_arguments - 1) / 2),
  152. sizeof(**intermediate_values));
  153. i = 1;
  154. while (i < number_of_arguments) {
  155. int other_value;
  156. const char* const operator_string = arguments[i];
  157. const BinaryOperator function = find_operator_function_by_string(
  158. number_of_operator_functions, operator_functions, operator_string);
  159. int * const intermediate_value =
  160. &((*intermediate_values)[*number_of_intermediate_values]);
  161. (*number_of_intermediate_values) ++;
  162. if (!function) {
  163. fprintf(stderr, "Unknown operator %s, argument %d ",
  164. operator_string, i);
  165. *error_occurred = 1;
  166. break;
  167. }
  168. i ++;
  169. if (i == number_of_arguments) {
  170. fprintf(stderr, "Binary operator %s missing argument ",
  171. operator_string);
  172. *error_occurred = 1;
  173. break;
  174. }
  175. other_value = (int)strtol(arguments[i], &end_of_integer, 10);
  176. if (end_of_integer == arguments[i]) {
  177. // If an error occurred while parsing the integer.
  178. fprintf(stderr, "Unable to parse integer %s of argument %d ",
  179. arguments[i], i);
  180. *error_occurred = 1;
  181. break;
  182. }
  183. i ++;
  184. // Perform the operation and store the intermediate value.
  185. *intermediate_value = function(value, other_value);
  186. value = *intermediate_value;
  187. }
  188. if (*error_occurred) {
  189. free(*intermediate_values);
  190. *intermediate_values = NULL;
  191. *number_of_intermediate_values = 0;
  192. return 0;
  193. }
  194. return value;
  195. }
  196. int main(int argc, char *argv[]) {
  197. int return_value;
  198. int number_of_intermediate_values;
  199. int *intermediate_values;
  200. // Peform the operation.
  201. const int result = perform_operation(
  202. argc - 1, &argv[1],
  203. sizeof(operator_function_map) / sizeof(operator_function_map[0]),
  204. operator_function_map, &number_of_intermediate_values,
  205. &intermediate_values, &return_value);
  206. // If no errors occurred display the result.
  207. if (!return_value && argc > 1) {
  208. unsigned int i;
  209. unsigned int intermediate_value_index = 0;
  210. printf("%s ", argv[1]);
  211. for (i = 2; i < argc; i += 2) {
  212. assert(intermediate_value_index < number_of_intermediate_values);
  213. printf(" %s %s = %d ", argv[i], argv[i + 1],
  214. intermediate_values[intermediate_value_index++]);
  215. }
  216. printf("= %d ", result);
  217. }
  218. if (intermediate_values) {
  219. free(intermediate_values);
  220. }
  221. return return_value;
  222. }
这个是calculator.c的源码,里面自带了一个main.c程序。这个文件可以直接编译:gcc calculator.c 然后生成 a.out之后  运行就是: ./a.out 1 + 2 - 3 + 4。我在做测试的时候发现不能很好的支持*号。
  1. // A binary arithmetic integer operation (add, subtract etc.)
  2. typedef int (*BinaryOperator)(int a, int b);
  3. // Structure which maps operator strings to functions.
  4. typedef struct OperatorFunction {
  5. const char* operator;
  6. BinaryOperator function;
  7. } OperatorFunction;
  8. int add(int a, int b);
  9. int subtract(int a, int b);
  10. int multiply(int a, int b);
  11. int divide(int a, int b);
  12. // Associate operator strings to functions.
  13. static OperatorFunction operator_function_map[] = {
  14. {"+", add},
  15. {"-", subtract},
  16. {"*", multiply},
  17. {"/", divide},
  18. };
  19. int add(int a, int b) {
  20. return a + b;
  21. }
  22. int subtract(int a, int b) {
  23. return a - b;
  24. }
  25. int multiply(int a, int b) {
  26. return a * b;
  27. }
  28. int divide(int a, int b) {
  29. assert(b); // Check for divide by zero.
  30. return a / b;
  31. }
这样就构造了一个结构体数组,用来保存运算符号和运算方法的一个映射关系,后续只要在operator_function_map数组里面去根据符号找到执行方法就可以了。

find_operator_function_by_string函数就是执行了上面说的这个功能,根据传入的操作符号找到对应的函数,并进行返回函数指针。

perform_operation函数中对于传入的参数进行解析,先解析第一个放到临时变量value中,然后再循环体重进行解析操作符获取计算方法的函数指针,然后再获取另一个操作数,进行执行计算函数,然后返回值会转储到value中方便下次继续使用。肯定有一些错误告警日志信息以及错误判断。最终如果参数无误,将计算的结果进行返回。还有就是在这个函数中会分配一块内存用来存储每一次计算得到的中间值信息。

最后的main函数就是调用测试的过程,后续应该把这个main.c该为example_main.c,否则会在calculator_test.c函数编译的过程中出错。

关于不能执行乘法的解释:
    在以./a.out 2 * 3  这个命令行进行执行程序的时候,会将其中*号解析为一个通配符,即当前目录下的所有内容名字的集合。  如果想正常调用乘法的话,可以将 * 号用单引号进行包起来就可以正常的调用了。还有就是这个方法只会从左向右进行计算,不会进行优先级的区分(有兴趣可以进一步完善哟)。
运行结果如下:

calculator_test.c函数中对于calculator.c函数中的方法进行了单元测试,需要在calculator.c的最开始添加一行  #define UNIT_TESTING 1  这样使能单元测试功能。然后执行make:
  1. test : cmockery.c calculator.c calculator_test.c
  2. gcc cmockery.c calculator.c calculator_test.c -g -O0 -o test
  3. clean:
  4. rm -f test

calculator_test.c中的main函数如下:
  1. int main(int argc, char* argv[]) {
  2. UnitTest tests[] = {
  3. unit_test(test_add),
  4. unit_test(test_subtract),
  5. unit_test(test_multiply),
  6. unit_test(test_divide),
  7. unit_test(test_divide_by_zero),
  8. unit_test(test_find_operator_function_by_string_null_functions),
  9. unit_test(test_find_operator_function_by_string_null_string),
  10. unit_test(test_find_operator_function_by_string_valid_null_functions),
  11. unit_test(test_find_operator_function_by_string_not_found),
  12. unit_test(test_find_operator_function_by_string_found),
  13. unit_test(test_perform_operation_null_args),
  14. unit_test(test_perform_operation_null_operator_functions),
  15. unit_test(test_perform_operation_null_number_of_intermediate_values),
  16. unit_test(test_perform_operation_null_intermediate_values),
  17. unit_test(test_perform_operation_no_arguments),
  18. unit_test(test_perform_operation_first_arg_not_integer),
  19. unit_test(test_perform_operation_unknown_operator),
  20. unit_test(test_perform_operation_missing_argument),
  21. unit_test(test_perform_operation_no_integer_after_operator),
  22. unit_test(test_perform_operation),
  23. unit_test(test_example_main_no_args),
  24. unit_test(test_example_main),
  25. };
  26. return run_tests(tests);
  27. }
其中的前四个单元测试就是简单的一个断言测试,取test_add为例,如下:
  1. // Ensure add() adds two integers correctly.
  2. void test_add(void **state) {
  3. assert_int_equal(add(3, 3), 6);
  4. assert_int_equal(add(3, -3), 0);
  5. }
test_divide_by_zero,test_find_operator_function_by_string_null_functions,test_find_operator_function_by_string_null_string测试,使用expect_assert_failure,确认100/0是非法的。否则会无法通过测试。

最核心的两个函数如下,重要的位置都加了注释。
  1. int _run_test(
  2. const char * const function_name, const UnitTestFunction Function,
  3. void ** const state, const UnitTestFunctionType function_type,
  4. const void* const heap_check_point) {
  5. const ListNode * const check_point = heap_check_point ?
  6. heap_check_point : check_point_allocated_blocks();
  7. void *current_state = NULL;
  8. int rc = 1;
  9. int handle_exceptions = 1;
  10. #ifdef _WIN32
  11. handle_exceptions = !IsDebuggerPresent();
  12. #endif // _WIN32
  13. #if UNIT_TESTING_DEBUG
  14. handle_exceptions = 0;
  15. #endif // UNIT_TESTING_DEBUG
  16. if (handle_exceptions) {
  17. #ifndef _WIN32
  18. unsigned int i;
  19. //注册新号处理函数,出现异常的时候,会跳到exception_handler这里去处理,
  20. //然后再exception_handler调用longjmp跳回到setjmp位置继续下一次单元测试。
  21. //会返回并保存默认的信号处理方发指针
  22. for (i = 0; i < ARRAY_LENGTH(exception_signals); i++) {
  23. default_signal_functions[i] = signal(
  24. exception_signals[i], exception_handler);
  25. }
  26. #else // _WIN32
  27. previous_exception_filter = SetUnhandledExceptionFilter(
  28. exception_filter);
  29. #endif // !_WIN32
  30. }
  31. if (function_type == UNIT_TEST_FUNCTION_TYPE_TEST) {
  32. print_message("%s: Starting test ", function_name);
  33. }
  34. initialize_testing(function_name);
  35. global_running_test = 1;
  36. //在这里进行的setjmp,后续如果出现新号或者错误处理,后会返回这里继续else部分的执行。
  37. if (setjmp(global_run_test_env) == 0) {
  38. Function(state ? state : &current_state);
  39. fail_if_leftover_values(function_name);
  40. /* If this is a setup function then ignore any allocated blocks
  41. * only ensure they're deallocated on tear down. */
  42. if (function_type != UNIT_TEST_FUNCTION_TYPE_SETUP) {
  43. fail_if_blocks_allocated(check_point, function_name);
  44. }
  45. global_running_test = 0;
  46. if (function_type == UNIT_TEST_FUNCTION_TYPE_TEST) {
  47. print_message("%s: Test completed successfully. ", function_name);
  48. }
  49. rc = 0;
  50. } else {
  51. global_running_test = 0;
  52. print_message("%s: Test failed. ", function_name);
  53. }
  54. teardown_testing(function_name);
  55. if (handle_exceptions) {
  56. #ifndef _WIN32
  57. unsigned int i;
  58. //在这里回复默认的异常处理函数,每一个单元测试都要这么做一次。
  59. for (i = 0; i < ARRAY_LENGTH(exception_signals); i++) {
  60. signal(exception_signals[i], default_signal_functions[i]);
  61. }
  62. #else // _WIN32
  63. if (previous_exception_filter) {
  64. SetUnhandledExceptionFilter(previous_exception_filter);
  65. previous_exception_filter = NULL;
  66. }
  67. #endif // !_WIN32
  68. }
  69. return rc;
  70. }
  71. int _run_tests(const UnitTest * const tests, const size_t number_of_tests) {
  72. print_message("%d ",sizeof(int));
  73. // 是否执行下一个测试.
  74. int run_next_test = 1;
  75. // 是否前一个测试执行失败了.
  76. int previous_test_failed = 0;
  77. //检查堆指针的状态
  78. const ListNode * const check_point = check_point_allocated_blocks();
  79. //当前的测试索引
  80. size_t current_test = 0;
  81. //已经执行过测试的个数
  82. size_t tests_executed = 0;
  83. //测试失败的数目
  84. size_t total_failed = 0;
  85. // setup 函数的个数
  86. size_t setups = 0;//(setup主要实现测试前的初始化工作,teardown主要实现测试完成后的垃圾回收工作)
  87. // teardown 函数的个数
  88. size_t teardowns = 0;
  89. /* 一个测试状态的栈. 一个状态是当测试的setup发生时入栈
  90. * 另个是当测试的teardown发生时出栈 */
  91. TestState* test_states = malloc(number_of_tests * sizeof(*test_states));
  92. size_t number_of_test_states = 0;
  93. //失败测试的名字指针数组,最多为所有的都失败
  94. const char** failed_names = malloc(number_of_tests *
  95. sizeof(*failed_names));
  96. void **current_state = NULL;
  97. //确保最大的整数类型要不小于一个指针类型
  98. assert_true(sizeof(LargestIntegralType) >= sizeof(void*));
  99. while (current_test < number_of_tests)
  100. {
  101. const ListNode *test_check_point = NULL;
  102. TestState *current_TestState;
  103. const UnitTest * const test = &tests[current_test++];
  104. if (!test->function)
  105. {//如果结构体中的函数指针为空,那么就continue
  106. continue;
  107. }
  108. //以run_next_test控制是否为test,区分setup必须首先被执行。
  109. switch (test->function_type)
  110. {
  111. case UNIT_TEST_FUNCTION_TYPE_TEST:
  112. run_next_test = 1;
  113. break;
  114. case UNIT_TEST_FUNCTION_TYPE_SETUP:
  115. {
  116. // Checkpoint the heap before the setup.
  117. current_TestState = &test_states[number_of_test_states++];
  118. current_TestState->check_point = check_point_allocated_blocks();
  119. test_check_point = current_TestState->check_point;
  120. current_state = &current_TestState->state;
  121. *current_state = NULL;
  122. run_next_test = 1;
  123. setups ++;
  124. break;
  125. }
  126. case UNIT_TEST_FUNCTION_TYPE_TEARDOWN:
  127. // Check the heap based on the last setup checkpoint.
  128. assert_true(number_of_test_states);
  129. current_TestState = &test_states[--number_of_test_states];
  130. test_check_point = current_TestState->check_point;
  131. current_state = &current_TestState->state;
  132. teardowns ++;
  133. break;
  134. default:
  135. print_error("Invalid unit test function type %d ",
  136. test->function_type);
  137. exit_test(1);
  138. break;
  139. }
  140. if (run_next_test)
  141. {//进入测试单元进行运行函数
  142. int failed = _run_test(test->name, test->function, current_state,
  143. test->function_type, test_check_point);
  144. if (failed)
  145. {//记录错误函数的名字,后续会用来记录输出
  146. failed_names[total_failed] = test->name;
  147. }
  148. switch (test->function_type)
  149. {//统计信息
  150. case UNIT_TEST_FUNCTION_TYPE_TEST:
  151. previous_test_failed = failed;
  152. total_failed += failed;
  153. tests_executed ++;
  154. break;
  155. case UNIT_TEST_FUNCTION_TYPE_SETUP:
  156. if (failed)
  157. {
  158. total_failed ++;
  159. tests_executed ++;
  160. // Skip forward until the next test or setup function.
  161. run_next_test = 0;
  162. }
  163. previous_test_failed = 0;
  164. break;
  165. case UNIT_TEST_FUNCTION_TYPE_TEARDOWN:
  166. // If this test failed.
  167. if (failed && !previous_test_failed)
  168. {
  169. total_failed ++;
  170. }
  171. break;
  172. default:
  173. assert_false("BUG: shouldn't be here!");
  174. break;
  175. }
  176. }
  177. }
  178. //输出打印信息,并且释放申请的资源
  179. if (total_failed)
  180. {
  181. size_t i;
  182. print_error("%d out of %d tests failed! ", total_failed,
  183. tests_executed);
  184. for (i = 0; i < total_failed; i++)
  185. {
  186. print_error(" %s ", failed_names[i]);
  187. }
  188. }
  189. else
  190. {
  191. print_message("All %d tests passed ", tests_executed);
  192. }
  193. if (number_of_test_states)
  194. {
  195. print_error("Mismatched number of setup %d and teardown %d "
  196. "functions ", setups, teardowns);
  197. total_failed = -1;
  198. }
  199. free(test_states);
  200. free((void*)failed_names);
  201. //如果在这里还没有回复堆区域,那么就说明内存存在泄漏
  202. fail_if_blocks_allocated(check_point, "run_tests");
  203. return (int)total_failed;
  204. }
后续这个测试框架可以以静态库的方式存在于项目中,单独的简历一个测试文件夹,专门用于各个模块的单元测试代码。或者就编译成动态库安装到系统里,都是可以的。










原文地址:https://www.cnblogs.com/cfzhang/p/a1d8bd91d72fbe49bd015471b049f2bb.html