White box test example-9 build your own unit test framework 2 (complete code)

Source: Internet
Author: User
Tags sleep function

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

  1. /*
  2. * Copyright (c) 2008, Hu Tian Fa (hutianfa@163.com)
  3. *
  4. * Simple unit test framework
  5. *
  6. */
  7. # Include <stdio. h>
  8. # Include <string. h>
  9. # Include <time. h>
  10. # Include <stdlib. h>
  11. /*
  12. * If there is no sleep function in VC, write
  13. * The unit of wait is millisecond.
  14. */
  15. Extern void sleep (clock_t wait );
  16. /*
  17. * Determines whether the value is true.
  18. */
  19. Void asserttrue (char * MSG, bool actual );
  20. /*
  21. * Determine whether the expected and actual results are consistent
  22. */
  23. Void assertequals (char * MSG, int expect CT, int actual );
  24. /*
  25. * Initialize the test and start timing.
  26. */
  27. Void Init ();
  28. /*
  29. * End the test, end the timing, and print the report.
  30. */
  31. Void end ();

2. unittest. cpp

  1. /*
  2. * Copyright (c) 2008, Hu Tian Fa (hutianfa@163.com)
  3. *
  4. * Simple unit test framework
  5. *
  6. */
  7. # Include "unittest. H"
  8. /*
  9. * If there is no sleep function in VC, write
  10. * The unit of wait is millisecond.
  11. */
  12. Void sleep (clock_t wait)
  13. {
  14. Clock_t goal;
  15. Goal = wait + clock ();
  16. While (Goal> clock ())
  17. {
  18. ;
  19. }
  20. }
  21. /*
  22. * Global variables that store test information and error information
  23. */
  24. Char * errors [100] = {""};
  25. Int errorcount = 0;
  26. Int testcount = 0;
  27. Time_t starttime, endtime;
  28. /*
  29. * Add error message
  30. */
  31. Void adderror (char * MSG)
  32. {
  33. Char error [100] = "test '";
  34. Strcat (error, MSG );
  35. Strcat (error, "'is failed! ");
  36. Errors [errorcount] = new char [2, 100];
  37. Strcpy (errors [errorcount], error );
  38. Errorcount ++;
  39. }
  40. /*
  41. * Add error messages with expected results and actual result Parameters
  42. */
  43. Void adderror (char * MSG, int expect CT, int actual)
  44. {
  45. Char error [100] = "test '";
  46. Char num [10];
  47. Strcat (error, MSG );
  48. Strcat (error, "'is failed! ");
  49. Strcat (error, "expected :");
  50. Strcat (error, ITOA (distinct CT, num, 10 ));
  51. Strcat (error, ", actual :");
  52. Strcat (error, ITOA (actual, num, 10 ));
  53. Errors [errorcount] = new char [2, 100];
  54. Strcpy (errors [errorcount], error );
  55. Errorcount ++;
  56. }
  57. /*
  58. * Determines whether the value is true.
  59. */
  60. Void asserttrue (char * MSG, bool actual)
  61. {
  62. Sleep (10 );
  63. Testcount ++;
  64. If (actual)
  65. {
  66. Printf (".");
  67. }
  68. Else
  69. {
  70. Printf ("F ");
  71. Adderror (MSG );
  72. }
  73. }
  74. /*
  75. * Determine whether the expected and actual results are consistent
  76. */
  77. Void assertequals (char * MSG, int expect CT, int actual)
  78. {
  79. Sleep (10 );
  80. Testcount ++;
  81. If (reverse Ct = actual)
  82. {
  83. Printf (".");
  84. }
  85. Else
  86. {
  87. Printf ("F ");
  88. Adderror (MSG, CT, actual );
  89. }
  90. }
  91. /*
  92. * Test report
  93. */
  94. Void testreport ()
  95. {
  96. Printf ("/n/ntotal run tests:"); // Test Summary
  97. Printf ("% d", testcount );
  98. Printf (", passed: % d", testcount-errorcount );
  99. Printf (", failed: % d/N", errorcount );
  100. Printf ("test escaped time: % 6.3f seconds/N", (double) (endtime-starttime)/1000.0 );
  101. If (errorcount> 0) // test failure details
  102. {
  103. Printf ("/n ***************** failed test's detail ************** **/n ");
  104. For (INT I = 0; I <errorcount; I ++)
  105. {
  106. Printf ("% d:", I + 1 );
  107. Printf (errors [I]);
  108. Printf ("/N ");
  109. }
  110. Printf ("/n **************** end of failed detail *************** */n ");
  111. }
  112. Else // All tests pass
  113. {
  114. Printf ("/n ****** all tests had passed! * *****/N ");
  115. }
  116. }
  117. /*
  118. * Initialize the test and start timing.
  119. */
  120. Void Init ()
  121. {
  122. Printf ("/n ******** Test start ******/N ");
  123. Starttime = clock ();
  124. }
  125. /*
  126. * End the test, end the timing, and print the report.
  127. */
  128. Void end ()
  129. {
  130. Endtime = clock ();
  131. Testreport ();
  132. }

3. Actual test results:

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.