LCOV - code coverage report
Current view: top level - src/replacements/unittests - strings.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 37 37 100.0 %
Date: 2025-02-21 09:36:06 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2014  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             : #undef NDEBUG
      21             : #include <config.h>
      22             : 
      23             : #include <stdio.h>
      24             : 
      25             : #if HAVE_STDLIB_H
      26             : #include <stdlib.h>
      27             : #endif /* HAVE_STDLIB_H */
      28             : 
      29             : #include <assert.h>
      30             : 
      31             : #include <freetds/replacements.h>
      32             : 
      33             : /* If the system supplies these, we're going to simulate the situation
      34             :  * where it doesn't so we're always testing our own versions.
      35             :  */
      36             : #if HAVE_STRLCPY
      37             : size_t tds_strlcpy(char *dest, const char *src, size_t len);
      38             : #include "../strlcpy.c"
      39             : #endif
      40             : 
      41             : #if HAVE_STRLCAT
      42             : size_t tds_strlcat(char *dest, const char *src, size_t len);
      43             : #include "../strlcat.c"
      44             : #endif
      45             : 
      46          10 : int main(void)
      47             : {
      48          10 :         char *buf = (char *) malloc(10);
      49             : 
      50             :         /* test tds_strlcpy */
      51          10 :         memset(buf, 0xff, 10);
      52          10 :         assert(tds_strlcpy(buf, "test", 10) == 4);
      53          10 :         assert(strcmp(buf, "test") == 0);
      54             : 
      55          10 :         memset(buf, 0xff, 10);
      56          10 :         assert(tds_strlcpy(buf, "TESTTEST", 10) == 8);
      57          10 :         assert(strcmp(buf, "TESTTEST") == 0);
      58             : 
      59          10 :         memset(buf, 0xff, 10);
      60          10 :         assert(tds_strlcpy(buf, "abcdefghi", 10) == 9);
      61          10 :         assert(strcmp(buf, "abcdefghi") == 0);
      62             : 
      63          10 :         memset(buf, 0xff, 10);
      64          10 :         assert(tds_strlcpy(buf, "1234567890", 10) == 10);
      65          10 :         assert(strcmp(buf, "123456789") == 0);
      66             : 
      67          10 :         memset(buf, 0xff, 10);
      68          10 :         assert(tds_strlcpy(buf, "xyzabc1234567890", 10) == 16);
      69          10 :         assert(strcmp(buf, "xyzabc123") == 0);
      70             : 
      71             :         /* test tds_strlcat */
      72          10 :         strcpy(buf, "xyz");
      73          10 :         assert(tds_strlcat(buf, "test", 10) == 7);
      74          10 :         assert(strcmp(buf, "xyztest") == 0);
      75             : 
      76          10 :         strcpy(buf, "xyz");
      77          10 :         assert(tds_strlcat(buf, "TESTAB", 10) == 9);
      78          10 :         assert(strcmp(buf, "xyzTESTAB") == 0);
      79             : 
      80          10 :         strcpy(buf, "xyz");
      81          10 :         assert(tds_strlcat(buf, "TESTabc", 10) == 10);
      82          10 :         assert(strcmp(buf, "xyzTESTab") == 0);
      83             : 
      84          10 :         strcpy(buf, "xyz");
      85          10 :         assert(tds_strlcat(buf, "123456789012345", 10) == 18);
      86          10 :         assert(strcmp(buf, "xyz123456") == 0);
      87             : 
      88          10 :         strcpy(buf, "123456789");
      89          10 :         assert(tds_strlcat(buf, "test", 4) == 13);
      90          10 :         assert(strcmp(buf, "123456789") == 0);
      91             : 
      92             :         /* test length == 0 */
      93          10 :         assert(tds_strlcpy(buf + 10, "test", 0) == 4);
      94             : 
      95          10 :         strcpy(buf, "123");
      96          10 :         assert(tds_strlcat(buf, "456", 0) == 6);
      97          10 :         assert(strcmp(buf, "123") == 0);
      98             : 
      99          10 :         free(buf);
     100             :         return 0;
     101             : }
     102             : 

Generated by: LCOV version 1.13