File: test_pathcvt.c

package info (click to toggle)
netcdf-parallel 1%3A4.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 113,164 kB
  • sloc: ansic: 267,893; sh: 12,869; cpp: 5,822; yacc: 2,613; makefile: 1,813; lex: 1,216; xml: 173; awk: 2
file content (259 lines) | stat: -rw-r--r-- 5,857 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*********************************************************************
 *   Copyright 2018, UCAR/Unidata
 *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
 *********************************************************************/

/**
Test the NCpathcvt
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "netcdf.h"
#include "ncpathmgr.h"

#undef DEBUG

#define NKINDS 4
static const int kinds[NKINDS] = {NCPD_NIX,NCPD_MSYS,NCPD_CYGWIN,NCPD_WIN};

typedef struct Test {
    char* test;
    char* expected[NKINDS];
} Test;

/* Path conversion tests */
static Test PATHTESTS[] = {
{"/xxx/a/b",{
	"/xxx/a/b",		/*NCPD_LINUX*/
	"c:\\xxx\\a\\b",	/*NCPD_MSYS*/
	"/cygdrive/c/xxx/a/b",	/*NCPD_CYGWIN*/
	"c:\\xxx\\a\\b"		/*NCPD_WIN*/
	}},
{"d:/x/y",{
	 "/d/x/y",		/*NCPD_LINUX*/
	"d:\\x\\y",		/*NCPD_MSYS*/
	 "/cygdrive/d/x/y",	/*NCPD_CYGWIN*/
	 "d:\\x\\y"		/*NCPD_WIN*/
	}},
{"d:\\x\\y",{
	 "/d/x/y",		/*NCPD_LINUX*/
	"d:\\x\\y",		/*NCPD_MSYS*/
	 "/cygdrive/d/x/y",	/*NCPD_CYGWIN*/
	 "d:\\x\\y"		/*NCPD_WIN*/
	}},
{"/cygdrive/d/x/y",{
	 "/cygdrive/d/x/y",	/*NCPD_LINUX*/
	"d:\\x\\y",		/*NCPD_MSYS*/
	"/cygdrive/d/x/y",	/*NCPD_CYGWIN*/
	 "d:\\x\\y"		/*NCPD_WIN*/
	}},
{"/d/x/y",{
	 "/d/x/y",		/*NCPD_LINUX*/
	"c:\\d\\x\\y",		/*NCPD_MSYS*/
	 "/cygdrive/c/d/x/y",	/*NCPD_CYGWIN*/
	 "c:\\d\\x\\y"		/*NCPD_WIN*/
	}},
{"/cygdrive/d",{
	 "/cygdrive/d",		/*NCPD_LINUX*/
	"d:",			/*NCPD_MSYS*/
	 "/cygdrive/d",		/*NCPD_CYGWIN*/
	 "d:"			/*NCPD_WIN*/
	}},
{"/d", {
	"/d",			/*NCPD_LINUX*/
	"c:\\d",		/*NCPD_MSYS*/
	 "/cygdrive/c/d",	/*NCPD_CYGWIN*/
	 "c:\\d"		/*NCPD_WIN*/
	}},
{"/cygdrive/d/git/netcdf-c/dap4_test/test_anon_dim.2.syn",{
    "/cygdrive/d/git/netcdf-c/dap4_test/test_anon_dim.2.syn",	/*NCPD_LINUX*/
    "d:\\git\\netcdf-c\\dap4_test\\test_anon_dim.2.syn",	/*NCPD_MSYS*/
    "/cygdrive/d/git/netcdf-c/dap4_test/test_anon_dim.2.syn",	/*NCPD_CYGWIN*/
    "d:\\git\\netcdf-c\\dap4_test\\test_anon_dim.2.syn"		/*NCPD_WIN*/
    }},
/* Test relative path */
{"x/y",{
	 "x/y",	/*NCPD_LINUX*/
	"x\\y",	/*NCPD_MSYS*/
	"x/y",	/*NCPD_CYGWIN*/
	 "x\\y"	/*NCPD_WIN*/
	}},
{"x\\y",{
	 "x/y",/*NCPD_LINUX*/
	"x\\y",	/*NCPD_MSYS*/
	"x/y",	/*NCPD_CYGWIN*/
	 "x\\y"	/*NCPD_WIN*/
	}},
#ifndef _WIN32X
/* Test utf8 path */
{"/海/海",{
	 "/海/海",		/*NCPD_LINUX*/
	"c:\\海\\海",		/*NCPD_MSYS*/
	"/cygdrive/c/海/海",	/*NCPD_CYGWIN*/
	 "c:\\海\\海"		/*NCPD_WIN*/
	}},
/* Test network path */
{"//git/netcdf-c/dap4_test",{
    NULL /*meaningless*/,		/*NCPD_LINUX*/
    "\\\\git\\netcdf-c\\dap4_test",	/*NCPD_MSYS*/
    NULL /*meaningless*/,		/*NCPD_CYGWIN*/
    "\\\\git\\netcdf-c\\dap4_test"	/*NCPD_WIN*/
    }},
#endif
{NULL, {NULL, NULL, NULL, NULL}}
};

char* macros[128];

/*Forward */
static char* expand(const char* s);
static void setmacros(void);
static void reclaimmacros(void);
#ifdef DEBUG
static const char* kind2string(int kind);
#endif

int
main(int argc, char** argv)
{
    Test* test;
    int failcount = 0;
    char* cvt = NULL;
    char* unescaped = NULL;
    char* expanded = NULL;
    int k;
    int drive = 'c';

    nc_initialize();

    setmacros();

    /* Test localkind X path-kind */
    for(test=PATHTESTS;test->test;test++) {
#ifdef DEBUG
	int inputkind = NCgetinputpathkind(test->test);
#endif
        /* Iterate over the test paths */
        for(k=0;k<NKINDS;k++) {
	    int kind = kinds[k];
	    /* Compare output for the localkind */
            if(test->expected[k] == NULL) {
#ifdef DEBUG
	        fprintf(stderr,"TEST input=%s target=%s: %s ignored\n",kind2string(inputkind),kind2string(kind),test->test);
#endif
	        continue;
	    }
	    /* ensure that NC_shellUnescape does not affect result */
	    unescaped = NC_shellUnescape(test->test);	
	    expanded = expand(test->expected[k]);
#ifdef DEBUG
	    fprintf(stderr,">>> unescaped=|%s| expanded=|%s|\n",unescaped,expanded);
#endif
#ifdef DEBUG
	    fprintf(stderr,"TEST input=%s target=%s: input: |%s|\n",
			kind2string(inputkind),kind2string(kind),test->test);
#endif
   	    cvt = NCpathcvt_test(unescaped,kind,drive);
#ifdef DEBUG
	    fprintf(stderr,"\texpected=|%s| actual=|%s|: ",expanded,cvt);
#endif
	    fflush(stderr); fflush(stdout);
	    if(cvt == NULL) {
#ifdef DEBUG
		fprintf(stderr," ILLEGAL");
#endif
		failcount++;
	    } else if(strcmp(cvt,expanded) != 0) {
#ifdef DEBUG
		fprintf(stderr," FAIL");
#endif
	        failcount++;
	    } else {
#ifdef DEBUG
		fprintf(stderr," PASS");
#endif
	    }
#ifdef DEBUG
	    fprintf(stderr,"\n");
#endif	    
	    nullfree(unescaped); unescaped = NULL;
	    nullfree(expanded); expanded = NULL;
	    nullfree(cvt); cvt = NULL;
	}
    }
    nullfree(cvt); nullfree(unescaped);
    fprintf(stderr,"%s test_pathcvt\n",failcount > 0 ? "***FAIL":"***PASS");

    reclaimmacros();

    nc_finalize();
    return (failcount > 0 ? 1 : 0);
}

#ifdef DEBUG
static const char*
kind2string(int kind)
{
    switch(kind) {
    case NCPD_NIX:
	return "Linux";
    case NCPD_MSYS:
	return "MSYS";
    case NCPD_CYGWIN:
	return "Cygwin";
    case NCPD_WIN:
	return "Windows";
    case NCPD_REL:
	return "Relative";
    default: break;
    }
    return "Unknown";
}
#endif

static char*
expand(const char* s)
{
    const char *p;
    char expanded[8192];
    char q[2];

    q[1] = '\0';
    expanded[0] = '\0';
    for(p=s;*p;p++) {
	char c = *p;
	if(c == '%') {
	    p++;
	    c = *p;
	    if(macros[(int)c] != NULL)
	        strlcat(expanded,macros[(int)c],sizeof(expanded));
	} else {
	    q[0] = c;
	    strlcat(expanded,q,sizeof(expanded));
	}
    }
    return strdup(expanded);
}

static void
setmacros(void)
{
    int i;
    const char* m;
    for(i=0;i<128;i++) macros[i] = NULL;
    if((m=getenv("MSYS2_PREFIX"))) {
	macros['m'] = strdup(m);    
    }
}

static void
reclaimmacros(void)
{
    int i;
    for(i=0;i<128;i++) {
	if(macros[i]) free(macros[i]);
	macros[i] = NULL;
    }
}