LCOV - code coverage report
Current view: top level - src/tds/unittests - strftime.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 26 28 92.9 %
Date: 2025-01-18 12:13:41 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2020 Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : /*
      21             :  * Purpose: test tds_strftime.
      22             :  * This is a wrapper to strftime for portability and extension.
      23             :  */
      24             : #include "common.h"
      25             : #include <assert.h>
      26             : #include <freetds/convert.h>
      27             : #include <freetds/time.h>
      28             : 
      29             : static void
      30         104 : test(const TDSDATEREC* dr, int prec, const char *fmt, const char *expected, int line)
      31             : {
      32             :         char out[256];
      33         104 :         char *format = strdup(fmt);
      34         104 :         assert(format != NULL);
      35             : 
      36         104 :         tds_strftime(out, sizeof(out), format, dr, prec);
      37             : 
      38         104 :         if (strcmp(out, expected) != 0) {
      39           0 :                 fprintf(stderr, "%d: Wrong results got '%s' expected '%s'\n", line, out, expected);
      40           0 :                 exit(1);
      41             :         }
      42             : 
      43         104 :         free(format);
      44         104 : }
      45             : 
      46             : int
      47           8 : main(int argc, char **argv)
      48             : {
      49             :         TDSDATEREC dr;
      50             : 
      51           8 :         memset(&dr, 0, sizeof(dr));
      52             : 
      53             : #define TEST(prec, fmt, exp) test(&dr, prec, fmt, exp, __LINE__)
      54             : 
      55             :         /* %z extension, second decimals */
      56           8 :         TEST(3, "%z", "000");
      57           8 :         TEST(0, "x%z", "x");
      58           8 :         TEST(3, ".%z", ".000");
      59           8 :         dr.decimicrosecond = 1234567;
      60           8 :         TEST(5, ".%z", ".12345");
      61           8 :         TEST(4, "%z", "1234");
      62           8 :         TEST(4, "%z%%", "1234%");
      63           8 :         TEST(4, "%z%H", "123400");
      64             : 
      65             :         /* not terminated format, should not overflow */
      66           8 :         TEST(3, "%", "%");
      67             : 
      68             :         /* not portable %l, we should handle it */
      69           8 :         TEST(0, "%l", "12");
      70           8 :         TEST(0, "%%%l", "%12");
      71           8 :         dr.hour = 16;
      72           8 :         TEST(0, "%l", " 4");
      73             : 
      74             :         /* not portable %e, we should handle it */
      75           8 :         dr.day = 23;
      76           8 :         TEST(0, "%e", "23");
      77           8 :         dr.day = 5;
      78           8 :         TEST(0, "x%e", "x 5");
      79             :         return 0;
      80             : }

Generated by: LCOV version 1.13