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 : #undef NDEBUG
24 : #include <config.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/utils/path.h>
37 :
38 : #ifdef _WIN32
39 : enum { is_windows = 1 };
40 : #else
41 : enum { is_windows = 0 };
42 : #endif
43 :
44 8 : int main(void)
45 : {
46 : tds_dir_char *path;
47 : size_t len;
48 : FILE *f;
49 : char c_buf[64];
50 : tds_dir_char path_buf[64];
51 :
52 : /* tds_dir_len */
53 8 : len = tds_dir_len(TDS_DIR("this is a filename"));
54 : assert(len == 18);
55 :
56 : /* tds_dir_dup, tds_dir_cmp */
57 8 : path = tds_dir_dup(TDS_DIR("filename"));
58 8 : assert(path);
59 8 : assert(tds_dir_cmp(path, TDS_DIR("filename")) == 0);
60 8 : free(path);
61 :
62 : /* tds_join_path, tds_dir_cmp */
63 8 : path = tds_join_path(TDS_DIR("a") , TDS_DIR("b"));
64 8 : assert(path);
65 8 : assert(tds_dir_cmp(path, is_windows ? TDS_DIR("a\\b") : TDS_DIR("a/b")) == 0);
66 8 : free(path);
67 :
68 : /* tds_join_path, tds_dir_cmp */
69 8 : path = tds_join_path(TDS_DIR("") , TDS_DIR("b"));
70 8 : assert(path);
71 8 : assert(tds_dir_cmp(path, TDS_DIR("b")) == 0);
72 8 : free(path);
73 :
74 : /* tds_dir_open */
75 8 : unlink("path.txt");
76 8 : f = fopen("path.txt", "r");
77 8 : assert(f == NULL);
78 8 : f = tds_dir_open(TDS_DIR("path.txt"), TDS_DIR("w"));
79 8 : assert(f != NULL);
80 8 : fclose(f);
81 8 : f = fopen("path.txt", "r");
82 8 : assert(f != NULL);
83 8 : fclose(f);
84 8 : unlink("path.txt");
85 :
86 : /* tdsPRIdir */
87 8 : snprintf(c_buf, sizeof(c_buf), "x%" tdsPRIdir "y", TDS_DIR("abc"));
88 8 : assert(strcmp(c_buf, "xabcy") == 0);
89 :
90 : /* tds_dir_snprintf */
91 8 : tds_dir_snprintf(path_buf, TDS_VECTOR_SIZE(path_buf), TDS_DIR("x%sy"), TDS_DIR("abc"));
92 8 : assert(tds_dir_cmp(path_buf, TDS_DIR("xabcy")) == 0);
93 :
94 : /* tds_dir_from_cstr */
95 8 : path = tds_dir_from_cstr("filename");
96 8 : assert(tds_dir_cmp(path, TDS_DIR("filename")) == 0);
97 8 : free(path);
98 :
99 : /* specific to Windows implementation */
100 : #ifdef _WIN32
101 : /* utf-8 */
102 : path = tds_dir_from_cstr("abc\xc2\xab");
103 : assert(tds_dir_cmp(path, L"abc\u00ab") == 0);
104 : free(path);
105 :
106 : /* no utf-8 */
107 : path = tds_dir_from_cstr("abc\xab");
108 : assert(tds_dir_cmp(path, L"abc\u00ab") == 0 || tds_dir_cmp(path, L"abc?") == 0);
109 : free(path);
110 : #endif
111 :
112 : return 0;
113 : }
114 :
|