Posted By

ktrout on 11/03/13


Tagged

example cunit


Versions (?)

cunit example usage


 / Published in: C
 

cunit example usage

  1. /*
  2.  * Simple example of a CUnit unit test.
  3.  *
  4.  * This program (crudely) demonstrates a very simple "black box"
  5.  * test of the standard library functions fprintf() and fread().
  6.  * It uses suite initialization and cleanup functions to open
  7.  * and close a common temporary file used by the test functions.
  8.  * The test functions then write to and read from the temporary
  9.  * file in the course of testing the library functions.
  10.  *
  11.  * The 2 test functions are added to a single CUnit suite, and
  12.  * then run using the CUnit Basic interface. The output of the
  13.  * program (on CUnit version 2.0-2) is:
  14.  *
  15.  * CUnit : A Unit testing framework for C.
  16.  * http://cunit.sourceforge.net/
  17.  *
  18.  * Suite: Suite_1
  19.  * Test: test of fprintf() ... passed
  20.  * Test: test of fread() ... passed
  21.  *
  22.  * --Run Summary: Type Total Ran Passed Failed
  23.  * suites 1 1 n/a 0
  24.  * tests 2 2 2 0
  25.  * asserts 5 5 5 0
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include "CUnit/Basic.h"
  31.  
  32. /* Pointer to the file used by the tests. */
  33. static FILE* temp_file = NULL;
  34.  
  35. /* The suite initialization function.
  36.  * Opens the temporary file used by the tests.
  37.  * Returns zero on success, non-zero otherwise.
  38.  */
  39. int init_suite1(void)
  40. {
  41. if (NULL == (temp_file = fopen("temp.txt", "w+"))) {
  42. return -1;
  43. }
  44. else {
  45. return 0;
  46. }
  47. }
  48.  
  49. /* The suite cleanup function.
  50.  * Closes the temporary file used by the tests.
  51.  * Returns zero on success, non-zero otherwise.
  52.  */
  53. int clean_suite1(void)
  54. {
  55. if (0 != fclose(temp_file)) {
  56. return -1;
  57. }
  58. else {
  59. temp_file = NULL;
  60. return 0;
  61. }
  62. }
  63.  
  64. /* Simple test of fprintf().
  65.  * Writes test data to the temporary file and checks
  66.  * whether the expected number of bytes were written.
  67.  */
  68. void testFPRINTF(void)
  69. {
  70. int i1 = 10;
  71.  
  72. if (NULL != temp_file) {
  73. CU_ASSERT(0 == fprintf(temp_file, ""));
  74. CU_ASSERT(2 == fprintf(temp_file, "Q\n"));
  75. CU_ASSERT(7 == fprintf(temp_file, "i1 = %d", i1));
  76. }
  77. }
  78.  
  79. /* Simple test of fread().
  80.  * Reads the data previously written by testFPRINTF()
  81.  * and checks whether the expected characters are present.
  82.  * Must be run after testFPRINTF().
  83.  */
  84. void testFREAD(void)
  85. {
  86. unsigned char buffer[20];
  87.  
  88. if (NULL != temp_file) {
  89. rewind(temp_file);
  90. CU_ASSERT(9 == fread(buffer, sizeof(unsigned char), 20, temp_file));
  91. CU_ASSERT(0 == strncmp(buffer, "Q\ni1 = 10", 9));
  92. }
  93. }
  94.  
  95. /* The main() function for setting up and running the tests.
  96.  * Returns a CUE_SUCCESS on successful running, another
  97.  * CUnit error code on failure.
  98.  */
  99. int main()
  100. {
  101. CU_pSuite pSuite = NULL;
  102.  
  103. /* initialize the CUnit test registry */
  104. if (CUE_SUCCESS != CU_initialize_registry())
  105. return CU_get_error();
  106.  
  107. /* add a suite to the registry */
  108. pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
  109. if (NULL == pSuite) {
  110. CU_cleanup_registry();
  111. return CU_get_error();
  112. }
  113.  
  114. /* add the tests to the suite */
  115. /* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
  116. if ((NULL == CU_add_test(pSuite, "test of fprintf()", testFPRINTF)) ||
  117. (NULL == CU_add_test(pSuite, "test of fread()", testFREAD)))
  118. {
  119. CU_cleanup_registry();
  120. return CU_get_error();
  121. }
  122.  
  123. /* Run all tests using the CUnit Basic interface */
  124. CU_basic_set_mode(CU_BRM_VERBOSE);
  125. CU_basic_run_tests();
  126. CU_cleanup_registry();
  127. return CU_get_error();
  128. }

Report this snippet  

You need to login to post a comment.