LCOV - code coverage report
Current view: top level - src - smbunit.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 38 42 90.5 %
Date: 2016-12-21 02:12:01 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /***************************************************************************//**
       2             : 
       3             :   @file         smbunit.c
       4             : 
       5             :   @author       Stephen Brennan
       6             : 
       7             :   @date         Created Saturday, 28 September 2013
       8             : 
       9             :   @brief        A simple, lightweight unit test runner based on function
      10             :                 pointers.
      11             : 
      12             :   @copyright    Copyright (c) 2013-2016, Stephen Brennan.  Released under the
      13             :                 Revised BSD License.  See the LICENSE.txt file for details.
      14             : 
      15             : *******************************************************************************/
      16             : 
      17             : #include <stdio.h>            /* printf */
      18             : #include <string.h>           /* strncpy */
      19             : 
      20             : #include "libstephen/base.h"
      21             : #include "libstephen/ut.h"    /* functions we're defining */
      22             : 
      23         185 : smb_ut_test *su_create_test(char *description, int (*run)())
      24             : {
      25         185 :   smb_ut_test *test = smb_new(smb_ut_test, 1);
      26         185 :   strncpy(test->description, description, SMB_UNIT_DESCRIPTION_SIZE - 1);
      27         185 :   test->description[SMB_UNIT_DESCRIPTION_SIZE - 1] = 0;
      28         185 :   test->run = run;
      29         185 :   return test;
      30             : }
      31             : 
      32          18 : smb_ut_group *su_create_test_group(char *description)
      33             : {
      34          18 :   smb_ut_group *group = smb_new(smb_ut_group, 1);
      35          18 :   strncpy(group->description, description, SMB_UNIT_DESCRIPTION_SIZE - 1);
      36          18 :   group->description[SMB_UNIT_DESCRIPTION_SIZE - 1] = 0;
      37             : 
      38          18 :   group->num_tests = 0;
      39          18 :   return group;
      40             : }
      41             : 
      42         185 : void su_add_test(smb_ut_group *group, smb_ut_test *test)
      43             : {
      44         185 :   if (group->num_tests < SMB_UNIT_TESTS_PER_GROUP) {
      45         185 :     group->tests[group->num_tests++] = test;
      46             :   }
      47         185 : }
      48             : 
      49         185 : int su_run_test(smb_ut_test *test, char *file)
      50             : {
      51         185 :   int result = test->run();
      52             : 
      53         185 :   if (result) {
      54           0 :     printf ("%s:%d: assertion failed in %s\n", file, result, test->description);
      55           0 :     return 1;
      56             :   }
      57             : 
      58         185 :   printf ("TEST \"%s\" passed!\n",test->description);
      59         185 :   return 0;
      60             : }
      61             : 
      62          18 : int su_run_group(smb_ut_group *group)
      63             : {
      64          18 :   int result = 0;
      65          18 :   printf ("## GROUP \"%s\" running...\n",group->description);
      66         203 :   for (int i = 0; i < group->num_tests; i++) {
      67         185 :     result = su_run_test(group->tests[i], group->description);
      68         185 :     if (result) {
      69           0 :       printf ("## GROUP \"%s\" failed on test: %d\n\n", group->description, i);
      70           0 :       exit(result);
      71             :     }
      72             :   }
      73          18 :   printf ("## GROUP \"%s\" passed!\n\n", group->description);
      74          18 :   return 0;
      75             : }
      76             : 
      77         185 : void su_delete_test(smb_ut_test *test)
      78             : {
      79         185 :   smb_free(test);
      80         185 : }
      81             : 
      82          18 : void su_delete_group(smb_ut_group *group)
      83             : {
      84         203 :   for (int i = 0; i < group->num_tests; i++) {
      85         185 :     if (group->tests[i]) // don't delete if already deleted
      86         185 :       su_delete_test(group->tests[i]);
      87             :   }
      88          18 :   smb_free(group);
      89          18 : }

Generated by: LCOV version 1.11