LCOV - code coverage report
Current view: top level - src/utils/unittests - path.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 32 32 100.0 %
Date: 2025-04-26 20:22:58 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2023  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 path utilities.
      22             :  */
      23             : 
      24             : #include <freetds/utils/test_base.h>
      25             : 
      26             : #include <assert.h>
      27             : #include <stdio.h>
      28             : #include <stdlib.h>
      29             : #include <string.h>
      30             : 
      31             : #if HAVE_UNISTD_H
      32             : #include <unistd.h>
      33             : #endif /* HAVE_UNISTD_H */
      34             : 
      35             : #include <freetds/macros.h>
      36             : #include <freetds/sysdep_private.h>
      37             : #include <freetds/utils/path.h>
      38             : 
      39             : #ifdef _WIN32
      40             : enum { is_windows = 1 };
      41             : #else
      42             : enum { is_windows = 0 };
      43             : #endif
      44             : 
      45          10 : TEST_MAIN()
      46             : {
      47             :         tds_dir_char *path;
      48             :         size_t len;
      49             :         FILE *f;
      50             :         char c_buf[64];
      51             :         tds_dir_char path_buf[64];
      52             : 
      53             :         /* tds_dir_len */
      54          10 :         len = tds_dir_len(TDS_DIR("this is a filename"));
      55             :         assert(len == 18);
      56             : 
      57             :         /* tds_dir_dup, tds_dir_cmp */
      58          10 :         path = tds_dir_dup(TDS_DIR("filename"));
      59          10 :         assert(path);
      60          10 :         assert(tds_dir_cmp(path, TDS_DIR("filename")) == 0);
      61          10 :         free(path);
      62             : 
      63             :         /* tds_join_path, tds_dir_cmp */
      64          10 :         path = tds_join_path(TDS_DIR("a") , TDS_DIR("b"));
      65          10 :         assert(path);
      66          10 :         assert(tds_dir_cmp(path, is_windows ? TDS_DIR("a\\b") : TDS_DIR("a/b")) == 0);
      67          10 :         free(path);
      68             : 
      69             :         /* tds_join_path, tds_dir_cmp */
      70          10 :         path = tds_join_path(TDS_DIR("") , TDS_DIR("b"));
      71          10 :         assert(path);
      72          10 :         assert(tds_dir_cmp(path, TDS_DIR("b")) == 0);
      73          10 :         free(path);
      74             : 
      75             :         /* tds_dir_open */
      76          10 :         unlink("path.txt");
      77          10 :         f = fopen("path.txt", "r");
      78          10 :         assert(f == NULL);
      79          10 :         f = tds_dir_open(TDS_DIR("path.txt"), TDS_DIR("w"));
      80          10 :         assert(f != NULL);
      81          10 :         fclose(f);
      82          10 :         f = fopen("path.txt", "r");
      83          10 :         assert(f != NULL);
      84          10 :         fclose(f);
      85          10 :         unlink("path.txt");
      86             : 
      87             :         /* tdsPRIdir */
      88          10 :         snprintf(c_buf, sizeof(c_buf), "x%" tdsPRIdir "y", TDS_DIR("abc"));
      89          10 :         assert(strcmp(c_buf, "xabcy") == 0);
      90             : 
      91             :         /* tds_dir_snprintf */
      92          10 :         tds_dir_snprintf(path_buf, TDS_VECTOR_SIZE(path_buf), TDS_DIR("x%sy"), TDS_DIR("abc"));
      93          10 :         assert(tds_dir_cmp(path_buf, TDS_DIR("xabcy")) == 0);
      94             : 
      95             :         /* tds_dir_from_cstr */
      96          10 :         path = tds_dir_from_cstr("filename");
      97          10 :         assert(tds_dir_cmp(path, TDS_DIR("filename")) == 0);
      98          10 :         free(path);
      99             : 
     100             :         /* specific to Windows implementation */
     101             : #ifdef _WIN32
     102             :         /* utf-8 */
     103             :         path = tds_dir_from_cstr("abc\xc2\xab");
     104             :         assert(tds_dir_cmp(path, L"abc\u00ab") == 0);
     105             :         free(path);
     106             : 
     107             :         /* no utf-8 */
     108             :         path = tds_dir_from_cstr("abc\xab");
     109             :         assert(tds_dir_cmp(path, L"abc\u00ab") == 0 || tds_dir_cmp(path, L"abc?") == 0);
     110             :         free(path);
     111             : #endif
     112             : 
     113          10 :         return 0;
     114             : }
     115             : 

Generated by: LCOV version 1.13