LCOV - code coverage report
Current view: top level - lib/replace/tests - strptime.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 32 56 57.1 %
Date: 2024-04-13 12:30:31 Functions: 1 1 100.0 %

          Line data    Source code
       1             : 
       2             : #ifdef LIBREPLACE_CONFIGURE_TEST_STRPTIME
       3             : 
       4             : #include <stdio.h>
       5             : #include <stdlib.h>
       6             : #include <time.h>
       7             : 
       8             : #define true 1
       9             : #define false 0
      10             : 
      11             : #ifndef __STRING
      12             : #define __STRING(x)    #x
      13             : #endif
      14             : 
      15             : /* make printf a no-op */
      16             : #define printf if(0) printf
      17             : 
      18             : #else /* LIBREPLACE_CONFIGURE_TEST_STRPTIME */
      19             : 
      20             : #include "replace.h"
      21             : #include "system/time.h"
      22             : #include "replace-test.h"
      23             : 
      24             : #endif /* LIBREPLACE_CONFIGURE_TEST_STRPTIME */
      25             : 
      26           4 : int libreplace_test_strptime(void)
      27             : {
      28           4 :         const char *s = "20070414101546Z";
      29           1 :         char *ret;
      30           1 :         struct tm t, t2;
      31             : 
      32           4 :         memset(&t, 0, sizeof(t));
      33           4 :         memset(&t2, 0, sizeof(t2));
      34             : 
      35           4 :         printf("test: strptime\n");
      36             : 
      37           4 :         ret = strptime(s, "%Y%m%d%H%M%S", &t);
      38           4 :         if ( ret == NULL ) {
      39           0 :                 printf("failure: strptime [\n"
      40             :                        "returned NULL\n"
      41             :                        "]\n");
      42           0 :                 return false;
      43             :         }
      44             : 
      45           4 :         if ( *ret != 'Z' ) {
      46           0 :                 printf("failure: strptime [\n"
      47             :                        "ret doesn't point to 'Z'\n"
      48             :                        "]\n");
      49           0 :                 return false;
      50             :         }
      51             : 
      52           4 :         ret = strptime(s, "%Y%m%d%H%M%SZ", &t2);
      53           4 :         if ( ret == NULL ) {
      54           0 :                 printf("failure: strptime [\n"
      55             :                        "returned NULL with Z\n"
      56             :                        "]\n");
      57           0 :                 return false;
      58             :         }
      59             : 
      60           4 :         if ( *ret != '\0' ) {
      61           0 :                 printf("failure: strptime [\n"
      62             :                        "ret doesn't point to '\\0'\n"
      63             :                        "]\n");
      64           0 :                 return false;
      65             :         }
      66             : 
      67             : #define CMP_TM_ELEMENT(t1,t2,elem) \
      68             :         if (t1.elem != t2.elem) { \
      69             :                 printf("failure: strptime [\n" \
      70             :                        "result differs if the format string has a 'Z' at the end\n" \
      71             :                        "element: %s %d != %d\n" \
      72             :                        "]\n", \
      73             :                        __STRING(elen), t1.elem, t2.elem); \
      74             :                 return false; \
      75             :         }
      76             : 
      77           4 :         CMP_TM_ELEMENT(t,t2,tm_sec);
      78           4 :         CMP_TM_ELEMENT(t,t2,tm_min);
      79           4 :         CMP_TM_ELEMENT(t,t2,tm_hour);
      80           4 :         CMP_TM_ELEMENT(t,t2,tm_mday);
      81           4 :         CMP_TM_ELEMENT(t,t2,tm_mon);
      82           4 :         CMP_TM_ELEMENT(t,t2,tm_year);
      83           4 :         CMP_TM_ELEMENT(t,t2,tm_wday);
      84           4 :         CMP_TM_ELEMENT(t,t2,tm_yday);
      85           4 :         CMP_TM_ELEMENT(t,t2,tm_isdst);
      86             : 
      87           4 :         if (t.tm_sec != 46) {
      88           0 :                 printf("failure: strptime [\n"
      89             :                        "tm_sec: expected: 46, got: %d\n"
      90             :                        "]\n",
      91             :                        t.tm_sec);
      92           0 :                 return false;
      93             :         }
      94             : 
      95           4 :         if (t.tm_min != 15) {
      96           0 :                 printf("failure: strptime [\n"
      97             :                        "tm_min: expected: 15, got: %d\n"
      98             :                        "]\n",
      99             :                        t.tm_min);
     100           0 :                 return false;
     101             :         }
     102             : 
     103           4 :         if (t.tm_hour != 10) {
     104           0 :                 printf("failure: strptime [\n"
     105             :                        "tm_hour: expected: 10, got: %d\n"
     106             :                        "]\n",
     107             :                        t.tm_hour);
     108           0 :                 return false;
     109             :         }
     110             : 
     111           4 :         if (t.tm_mday != 14) {
     112           0 :                 printf("failure: strptime [\n"
     113             :                        "tm_mday: expected: 14, got: %d\n"
     114             :                        "]\n",
     115             :                        t.tm_mday);
     116           0 :                 return false;
     117             :         }
     118             : 
     119           4 :         if (t.tm_mon != 3) {
     120           0 :                 printf("failure: strptime [\n"
     121             :                        "tm_mon: expected: 3, got: %d\n"
     122             :                        "]\n",
     123             :                        t.tm_mon);
     124           0 :                 return false;
     125             :         }
     126             : 
     127           4 :         if (t.tm_year != 107) {
     128           0 :                 printf("failure: strptime [\n"
     129             :                        "tm_year: expected: 107, got: %d\n"
     130             :                        "]\n",
     131             :                        t.tm_year);
     132           0 :                 return false;
     133             :         }
     134             : 
     135           4 :         if (t.tm_wday != 6) { /* saturday */
     136           0 :                 printf("failure: strptime [\n"
     137             :                        "tm_wday: expected: 6, got: %d\n"
     138             :                        "]\n",
     139             :                        t.tm_wday);
     140           0 :                 return false;
     141             :         }
     142             : 
     143           4 :         if (t.tm_yday != 103) {
     144           0 :                 printf("failure: strptime [\n"
     145             :                        "tm_yday: expected: 103, got: %d\n"
     146             :                        "]\n",
     147             :                        t.tm_yday);
     148           0 :                 return false;
     149             :         }
     150             : 
     151             :         /* we don't test this as it depends on the host configuration
     152             :         if (t.tm_isdst != 0) {
     153             :                 printf("failure: strptime [\n"
     154             :                        "tm_isdst: expected: 0, got: %d\n"
     155             :                        "]\n",
     156             :                        t.tm_isdst);
     157             :                 return false;
     158             :         }*/
     159             : 
     160           4 :         printf("success: strptime\n");
     161             : 
     162           4 :         return true;
     163             : }
     164             : 
     165             : #ifdef LIBREPLACE_CONFIGURE_TEST_STRPTIME
     166             : int main (void)
     167             : {
     168             :         int ret;
     169             :         ret = libreplace_test_strptime();
     170             :         if (ret == false) return 1;
     171             :         return 0;
     172             : }
     173             : #endif

Generated by: LCOV version 1.14