Original article, copyright belongs to Hu Tian Fa (hutianfa@163.com) All, reprint please indicate the source:
Http://blog.csdn.net/aidisheng/archive/2008/09/06/2892744.aspx
The complete source code is as follows:
1. unittest. h
- /*
- * Copyright (c) 2008, Hu Tian Fa (hutianfa@163.com)
- *
- * Simple unit test framework
- *
- */
- # Include <stdio. h>
- # Include <string. h>
- # Include <time. h>
- # Include <stdlib. h>
- /*
- * If there is no sleep function in VC, write
- * The unit of wait is millisecond.
- */
- Extern void sleep (clock_t wait );
- /*
- * Determines whether the value is true.
- */
- Void asserttrue (char * MSG, bool actual );
- /*
- * Determine whether the expected and actual results are consistent
- */
- Void assertequals (char * MSG, int expect CT, int actual );
- /*
- * Initialize the test and start timing.
- */
- Void Init ();
- /*
- * End the test, end the timing, and print the report.
- */
- Void end ();
2. unittest. cpp
- /*
- * Copyright (c) 2008, Hu Tian Fa (hutianfa@163.com)
- *
- * Simple unit test framework
- *
- */
- # Include "unittest. H"
- /*
- * If there is no sleep function in VC, write
- * The unit of wait is millisecond.
- */
- Void sleep (clock_t wait)
- {
- Clock_t goal;
- Goal = wait + clock ();
- While (Goal> clock ())
- {
- ;
- }
- }
- /*
- * Global variables that store test information and error information
- */
- Char * errors [100] = {""};
- Int errorcount = 0;
- Int testcount = 0;
- Time_t starttime, endtime;
- /*
- * Add error message
- */
- Void adderror (char * MSG)
- {
- Char error [100] = "test '";
- Strcat (error, MSG );
- Strcat (error, "'is failed! ");
- Errors [errorcount] = new char [2, 100];
- Strcpy (errors [errorcount], error );
- Errorcount ++;
- }
- /*
- * Add error messages with expected results and actual result Parameters
- */
- Void adderror (char * MSG, int expect CT, int actual)
- {
- Char error [100] = "test '";
- Char num [10];
- Strcat (error, MSG );
- Strcat (error, "'is failed! ");
- Strcat (error, "expected :");
- Strcat (error, ITOA (distinct CT, num, 10 ));
- Strcat (error, ", actual :");
- Strcat (error, ITOA (actual, num, 10 ));
- Errors [errorcount] = new char [2, 100];
- Strcpy (errors [errorcount], error );
- Errorcount ++;
- }
- /*
- * Determines whether the value is true.
- */
- Void asserttrue (char * MSG, bool actual)
- {
- Sleep (10 );
- Testcount ++;
- If (actual)
- {
- Printf (".");
- }
- Else
- {
- Printf ("F ");
- Adderror (MSG );
- }
- }
- /*
- * Determine whether the expected and actual results are consistent
- */
- Void assertequals (char * MSG, int expect CT, int actual)
- {
- Sleep (10 );
- Testcount ++;
- If (reverse Ct = actual)
- {
- Printf (".");
- }
- Else
- {
- Printf ("F ");
- Adderror (MSG, CT, actual );
- }
- }
- /*
- * Test report
- */
- Void testreport ()
- {
- Printf ("/n/ntotal run tests:"); // Test Summary
- Printf ("% d", testcount );
- Printf (", passed: % d", testcount-errorcount );
- Printf (", failed: % d/N", errorcount );
- Printf ("test escaped time: % 6.3f seconds/N", (double) (endtime-starttime)/1000.0 );
- If (errorcount> 0) // test failure details
- {
- Printf ("/n ***************** failed test's detail ************** **/n ");
- For (INT I = 0; I <errorcount; I ++)
- {
- Printf ("% d:", I + 1 );
- Printf (errors [I]);
- Printf ("/N ");
- }
- Printf ("/n **************** end of failed detail *************** */n ");
- }
- Else // All tests pass
- {
- Printf ("/n ****** all tests had passed! * *****/N ");
- }
- }
- /*
- * Initialize the test and start timing.
- */
- Void Init ()
- {
- Printf ("/n ******** Test start ******/N ");
- Starttime = clock ();
- }
- /*
- * End the test, end the timing, and print the report.
- */
- Void end ()
- {
- Endtime = clock ();
- Testreport ();
- }
3. Actual test results: