LCOV - code coverage report
Current view: top level - tests/src - test0008.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 145 145
Test Date: 2026-03-31 13:51:38 Functions: 100.0 % 7 7
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : #include "sute.h"
       2                 :             : #include <errno.h>
       3                 :             : 
       4                 :           1 : static void report_test(void)
       5                 :             : {
       6                 :           1 :         errno = ENOMEM;
       7                 :             :         // Report an error with formatted message
       8                 :           1 :         report("Memory reallocation failed with bytes %d",10);
       9                 :           1 :         errno = EOVERFLOW;
      10                 :             :         // Report an error with multiple arguments
      11                 :           1 :         report("Buffer overflow at position %d with value %s",42,"overflow");
      12                 :           1 : }
      13                 :             : 
      14                 :           1 : static void slog_test(void)
      15                 :             : {
      16                 :           1 :         printf("All available combinations:\n");
      17                 :           1 :         printf("%s\n",rational_convert(REGULAR));
      18                 :           1 :         printf("%s\n",rational_convert(VERBOSE));
      19                 :           1 :         printf("%s\n",rational_convert(TESTING));
      20                 :           1 :         printf("%s\n",rational_convert(SILENT));
      21                 :           1 :         printf("%s\n",rational_convert(REGULAR|VERBOSE));
      22                 :           1 :         printf("%s\n",rational_convert(REGULAR|TESTING));
      23                 :           1 :         printf("%s\n",rational_convert(VERBOSE|TESTING));
      24                 :           1 :         printf("%s\n",rational_convert(REGULAR|VERBOSE|TESTING));
      25                 :           1 :         printf("%s\n",rational_convert(ERROR));
      26                 :           1 :         printf("%s\n",rational_convert(UNDECOR));
      27                 :           1 :         printf("%s\n",rational_convert(EVERY|UNDECOR));
      28                 :           1 :         printf("%s\n",rational_convert(ERROR|UNDECOR));
      29                 :           1 :         printf("%s\n",rational_convert(VISIBLE_IN_SILENT));
      30                 :             : 
      31                 :             :         /* Test REGULAR mode combinations */
      32                 :           1 :         rational_logger_mode = REGULAR;
      33                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
      34                 :           1 :         printf("1.  Must print:"); slog(REGULAR,"true"); printf("\n");
      35                 :           1 :         printf("2. Won't print:"); slog(VERBOSE,"but printed!"); printf("\n");
      36                 :           1 :         printf("3. Won't print:"); slog(TESTING,"but printed!"); printf("\n");
      37                 :           1 :         printf("4.  Must print:");   slog(ERROR,"true"); printf("\n");
      38                 :             : 
      39                 :             :         /* Test VERBOSE mode combinations */
      40                 :           1 :         rational_logger_mode = VERBOSE;
      41                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
      42                 :           1 :         printf("5. Won't print:"); slog(REGULAR,"but printed!"); printf("\n");
      43                 :           1 :         printf("6.  Must print:"); slog(VERBOSE,"true"); printf("\n");
      44                 :           1 :         printf("7. Won't print:"); slog(TESTING,"but printed!"); printf("\n");
      45                 :           1 :         printf("8.  Must print:");   slog(ERROR,"true"); printf("\n");
      46                 :             : 
      47                 :             :         /* Test TESTING mode combinations */
      48                 :           1 :         rational_logger_mode = TESTING;
      49                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
      50                 :           1 :         printf("9.  Won't print:"); slog(REGULAR,"but printed!"); printf("\n");
      51                 :           1 :         printf("10. Won't print:"); slog(VERBOSE,"but printed!"); printf("\n");
      52                 :           1 :         printf("11.  Must print:"); slog(TESTING,"true"); printf("\n");
      53                 :           1 :         printf("12.  Must print:");   slog(ERROR,"true"); printf("\n");
      54                 :             : 
      55                 :             :         /* Test SILENT mode combinations */
      56                 :           1 :         rational_logger_mode = SILENT;
      57                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
      58                 :           1 :         printf("13. Won't print:"); slog(REGULAR,"but printed!"); printf("\n");
      59                 :           1 :         printf("14. Won't print:"); slog(VERBOSE,"but printed!"); printf("\n");
      60                 :           1 :         printf("15. Won't print:"); slog(TESTING,"but printed!"); printf("\n");
      61                 :           1 :         printf("16. Won't print:");   slog(ERROR,"but printed!"); printf("\n");
      62                 :             : 
      63                 :             :         /* Test REGULAR|VERBOSE combinations */
      64                 :           1 :         rational_logger_mode = REGULAR|VERBOSE;
      65                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
      66                 :           1 :         printf("17.  Must print:"); slog(REGULAR,"true"); printf("\n");
      67                 :           1 :         printf("18.  Must print:"); slog(VERBOSE,"true"); printf("\n");
      68                 :           1 :         printf("19. Won't print:"); slog(TESTING,"but printed!"); printf("\n");
      69                 :           1 :         printf("20.  Must print:");   slog(ERROR,"true"); printf("\n");
      70                 :             : 
      71                 :             :         /* Test REGULAR|TESTING combinations */
      72                 :           1 :         rational_logger_mode = REGULAR|TESTING;
      73                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
      74                 :           1 :         printf("21.  Must print:"); slog(REGULAR,"true"); printf("\n");
      75                 :           1 :         printf("22. Won't print:"); slog(VERBOSE,"but printed!"); printf("\n");
      76                 :           1 :         printf("23.  Must print:"); slog(TESTING,"true"); printf("\n");
      77                 :           1 :         printf("24.  Must print:"); slog(ERROR,"true"); printf("\n");
      78                 :             : 
      79                 :             :         /* Test VERBOSE|TESTING combinations */
      80                 :           1 :         rational_logger_mode = VERBOSE|TESTING;
      81                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
      82                 :           1 :         printf("25. Won't print:"); slog(REGULAR,"but printed!"); printf("\n");
      83                 :           1 :         printf("26.  Must print:"); slog(VERBOSE,"true"); printf("\n");
      84                 :           1 :         printf("27.  Must print:"); slog(TESTING,"true"); printf("\n");
      85                 :           1 :         printf("28.  Must print:");   slog(ERROR,"true"); printf("\n");
      86                 :             : 
      87                 :             :         /* Test REGULAR|VERBOSE|TESTING combinations */
      88                 :           1 :         rational_logger_mode = REGULAR|VERBOSE|TESTING;
      89                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
      90                 :           1 :         printf("29. Must print:"); slog(REGULAR,"true"); printf("\n");
      91                 :           1 :         printf("30. Must print:"); slog(VERBOSE,"true"); printf("\n");
      92                 :           1 :         printf("31. Must print:"); slog(TESTING,"true"); printf("\n");
      93                 :           1 :         printf("32. Must print:");   slog(ERROR,"true"); printf("\n");
      94                 :             : 
      95                 :             :         /* Test ERROR mode combinations */
      96                 :           1 :         rational_logger_mode = ERROR;
      97                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
      98                 :           1 :         printf("33. Won't print:"); slog(REGULAR,"but printed!"); printf("\n");
      99                 :           1 :         printf("34. Won't print:"); slog(VERBOSE,"but printed!"); printf("\n");
     100                 :           1 :         printf("35. Won't print:"); slog(TESTING,"but printed!"); printf("\n");
     101                 :           1 :         printf("36.  Must print:");   slog(ERROR,"true"); printf("\n");
     102                 :             : 
     103                 :             :         /*
     104                 :             :          * Test UNDECOR flag: suppress logger prefixes (TESTING:, time/file/line/func, ERROR:)
     105                 :             :          * The output between the '|' markers should contain only the message payload
     106                 :             :          */
     107                 :           1 :         rational_logger_mode = EVERY|ERROR;
     108                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
     109                 :           1 :         printf("37. Must print no prefixes:|"); slog(EVERY|UNDECOR,"true"); printf("|\n");
     110                 :           1 :         printf("38. Must print no ERROR prefix:|"); slog(ERROR|UNDECOR,"true"); printf("|\n");
     111                 :             : 
     112                 :           1 :         rational_logger_mode = VERBOSE;
     113                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
     114                 :           1 :         printf("39. Must print no time/file/line/func:|"); slog(VERBOSE|UNDECOR,"true"); printf("|\n");
     115                 :             : 
     116                 :           1 :         rational_logger_mode = TESTING;
     117                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
     118                 :           1 :         printf("40. Must print no TESTING prefix:|"); slog(TESTING|UNDECOR,"true"); printf("|\n");
     119                 :             : 
     120                 :           1 :         rational_logger_mode = REGULAR;
     121                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
     122                 :           1 :         printf("41. Must not print (VERBOSE not enabled):|"); slog(VERBOSE|UNDECOR,"but printed!"); printf("|\n");
     123                 :             : 
     124                 :           1 :         rational_logger_mode = SILENT;
     125                 :           1 :         printf("Mode: %s\n",rational_reconvert(rational_logger_mode));
     126                 :           1 :         printf("42. Must print in SILENT without prefixes:|"); slog(EVERY|VISIBLE_IN_SILENT,"true"); printf("|\n");
     127                 :           1 :         printf("43. Must print no ERROR prefix in SILENT:|"); slog(ERROR|VISIBLE_IN_SILENT,"true"); printf("|\n");
     128                 :           1 : }
     129                 :             : 
     130                 :           1 : static void serp_case(void)
     131                 :             : {
     132                 :           1 :         errno = ENOENT;
     133                 :           1 :         serp("Failed to open file");
     134                 :           1 : }
     135                 :             : 
     136                 :           1 : static Return test0008_1(void)
     137                 :             : {
     138                 :           1 :         INITTEST;
     139                 :           1 :         create(char,captured_stdout);
     140                 :           1 :         create(char,captured_stderr);
     141                 :           1 :         create(char,pattern);
     142                 :           1 :         const char *filename = "templates/0008_001.txt";
     143                 :             : 
     144                 :           1 :         ASSERT(SUCCESS == function_capture(report_test,captured_stdout,captured_stderr));
     145                 :             : 
     146                 :             :         #if 0
     147                 :             :         printf("captured_stderr:%s",getcstring(captured_stderr));
     148                 :             :         printf("captured_stdout:%s\n",getcstring(captured_stdout));
     149                 :             :         #endif
     150                 :             : 
     151                 :           1 :         ASSERT(captured_stdout->length == 0);
     152                 :             : 
     153                 :           1 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     154                 :             : 
     155                 :           1 :         ASSERT(SUCCESS == match_pattern(captured_stderr,pattern,filename));
     156                 :             : 
     157                 :           1 :         del(pattern);
     158                 :             : 
     159                 :           1 :         del(captured_stdout);
     160                 :           1 :         del(captured_stderr);
     161                 :             : 
     162                 :           1 :         RETURN_STATUS;
     163                 :             : }
     164                 :             : 
     165                 :           1 : static Return test0008_2(void)
     166                 :             : {
     167                 :           1 :         INITTEST;
     168                 :             : 
     169                 :           1 :         create(char,captured_stdout);
     170                 :           1 :         create(char,captured_stderr);
     171                 :           1 :         create(char,pattern);
     172                 :           1 :         const char *filename = "templates/0008_002.txt";
     173                 :             : 
     174                 :           1 :         ASSERT(SUCCESS == function_capture(slog_test,captured_stdout,captured_stderr));
     175                 :             : 
     176                 :             :         #if 0
     177                 :             :         printf("captured_stderr:%s",getcstring(captured_stderr));
     178                 :             :         printf("captured_stdout:%s",getcstring(captured_stdout));
     179                 :             :         #endif
     180                 :             : 
     181                 :           1 :         ASSERT(captured_stderr->length == 0);
     182                 :             : 
     183                 :           1 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     184                 :             : 
     185                 :           1 :         ASSERT(SUCCESS == match_pattern(captured_stdout,pattern,filename));
     186                 :             : 
     187                 :           1 :         del(pattern);
     188                 :             : 
     189                 :           1 :         del(captured_stdout);
     190                 :           1 :         del(captured_stderr);
     191                 :             : 
     192                 :           1 :         RETURN_STATUS;
     193                 :             : }
     194                 :             : 
     195                 :           1 : static Return test0008_3(void)
     196                 :             : {
     197                 :           1 :         INITTEST;
     198                 :             : 
     199                 :           1 :         create(char,captured_stdout);
     200                 :           1 :         create(char,captured_stderr);
     201                 :           1 :         create(char,pattern);
     202                 :           1 :         const char *filename = "templates/0008_003.txt";
     203                 :             : 
     204                 :           1 :         ASSERT(SUCCESS == function_capture(serp_case,captured_stdout,captured_stderr));
     205                 :             : 
     206                 :           1 :         ASSERT(captured_stdout->length == 0);
     207                 :             : 
     208                 :           1 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     209                 :             : 
     210                 :           1 :         ASSERT(SUCCESS == match_pattern(captured_stderr,pattern,filename));
     211                 :             : 
     212                 :           1 :         del(pattern);
     213                 :             : 
     214                 :           1 :         del(captured_stdout);
     215                 :           1 :         del(captured_stderr);
     216                 :             : 
     217                 :           1 :         RETURN_STATUS;
     218                 :             : }
     219                 :             : 
     220                 :             : /**
     221                 :             :  * @brief librational report and logger tests
     222                 :             :  *
     223                 :             :  * @return SUCCESS when all subtests pass
     224                 :             :  */
     225                 :           1 : Return test0008(void)
     226                 :             : {
     227                 :           1 :         INITTEST;
     228                 :             : 
     229                 :           1 :         TEST(test0008_1,"librational test report messaging…");
     230                 :           1 :         TEST(test0008_2,"librational test slog messaging…");
     231                 :           1 :         TEST(test0008_3,"librational test serp messaging…");
     232                 :             : 
     233                 :           1 :         RETURN_STATUS;
     234                 :             : }
        

Generated by: LCOV version 2.0-1