A simple ut test statistics program

Source: Internet
Author: User

People who have used unit testing series should be very clear, such as cunit, cppunit, and javaunit. These libraries are frequently used in unit testing, but sometimes they are not, however, we also need to write a program to perform unit tests on the module, so we can use macro judgment and case statistics similar to those in cuint.

This program uses a simple one-minute method to implement the above basic functions. It can collect statistics on test cases and test assert. the test case here is a function, generally, many functions are written during unit tests. Each function has different interfaces or functions for the module. Therefore, a function is considered as a test case, A function can contain multiple test assert instances.

The following are all the codes implemented. These codes can be used at work for convenience.

Source program: /*************************************** * ************** file: ut_test.h * discription: * Author: dremi * Create Date: *************************************** * **************/# ifdef _ cplusplus # If _ cplusplus extern "C" {# endif # ifndef __ ut_test_h __# DEFINE _ ut_test_h __# define ut_max_func_name_len 128/* the max length of function name */# define ut_assert_compare (actualval, exp Ectval, OP)/do {/ut_print (actualval) OP (expectval), (_ line _), (_ file __), (_ function _); //} while (0)/* = */# define ut_assert_equal (actualval, expectval)/ut_assert_compare (actualval, expectval, =) /*! = */# Define ut_assert_not_equal (actualval, expectval)/ut_assert_compare (actualval, expectval ,! =)/* <*/# Define ut_assert_less (actualval, expectval)/ut_assert_compare (actualval, expectval, <)/* <= */# define encode (actualval, expectval) /ut_assert_compare (actualval, expectval, <=)/*> */# define ut_assert_great (actualval, expectval)/ut_assert_compare (actualval, expectval,>) /*> = */# define ut_assert_great_equal (actualval, expectval)/ut_assert_compare (actualval, expectval, >=) # Define ut_show_reault () typedef struct tagutcounter {int icasepasscnt;/* The passed case count */INT icasefailcnt;/* The failed case count */INT handle; /* The passed assert count */INT iassertfailcnt;/* The failed assert count */INT ipretotalfailcnt;/* The Last Time total assert fail count */Char aclastfuncname [limit]; /* The last case functio N name */} ut_counter; # endif/* _ ut_test_h _ */# ifdef _ cplusplus # If _ cplusplus} # endif /************* **************************************** * file: ut_test.c * discription: * Author: dremi * Create Date: *************************************** * ***************/# include <stdio. h> # include <string. h> # include "ut_test.h" # ifdef _ cplusplus # If _ cplusplus extern "C" {# endif # endifs Tatic ut_counter m_ut_counter;/* to count the cases * // * print the error info if the opresult = 0, and record the count */void ut_print (INT opresult, const int line, const char * file, const char * func) {/* Judge curent function name is same the last one */If (strcmp (m_ut_counter.aclastfuncname, func )! = 0) {If (condition <condition) m_ut_counter.icasefailcnt ++; else condition ++; condition = condition; strcpy (m_ut_counter.aclastfuncname, func);} If (opresult) {m_ut_counter.iassertpasscnt ++;} else {printf ("\ r \ nfailed in file: % s at line: % d @ [% s] \ r \ n", file, line, func); listener ++ ;}} void ut_show_reault () {If (m_ut_counter.ipretotalfailcnt> callback) {m_ut_counter.icasefailcnt ++;} printf ("\ r \ N --- Run Summary: total passed failed "" \ r \ n case: % 8d % 8d % 8d "" \ r \ n assert: % 8d % 8d % 8d/N ", (operator + operator), operator, m_ut_counter.icasefailcnt, (operator + operator), operator, operator) ;}# ifdef _ cplusplus # If _ cplusplus} # endif test program: # include "ut_test.h" Void ut_test_1 () {ut_assert_equal (1, 1); ut_assert_equal (3, 2); ut_assert_equal (2, 2);} void ut_test_2 () {round (2, 1); Round (2, 1); ut_assert_not_equal (2, 1);} void ut_test_3 () {ut_assert_great (3, 4); ut_assert_great (3, 3, 4); ut_assert_great (5, 4); ut_assert_great (3, 4); ut_assert_great (5, 4);} void ut_test_4 () {int A, B; A = 1; B = 2; ut_assert_less (a, B); ut_assert_less (a, B); ut_assert_less (-1212,222 2); ut_assert_less (111,333);} void ut_test_5 () {int, b; A = 1; B = 2; ut_assert_less_equal (a, B); hour (-1, 0); ut_assert_less_equal (0, 0); ut_assert_less_equal (-1, -1);} void ut_test_6 () {int A, B; A = 1; B = 2; ut_assert_great_equal (2, 1); ut_assert_great_equal (B, ); values (1, 1); ut_assert_great_equal (1111111,111 11);} int main () {ut_test_1 (); ut_test_2 (); ut_test_3 (); ut_test_4 (); ut_test_5 (); ut_test_6 ();/* show the reault */ut_show_reault (); Return 0;} the running effect is as follows: failed in file: Main. c At line: 8 @ [ut_test_1] failed in file: Main. c At line: 21 @ [ut_test_3] failed in file: Main. c At line: 22 @ [ut_test_3] failed in file: Main. c At line: 24 @ [ut_test_3] --- Run Summary: Total passed failed case: 6 4 2 assert: 23 19 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.