/[ascend]/trunk/ascend/general/test/test_ospath.c
ViewVC logotype

Annotation of /trunk/ascend/general/test/test_ospath.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3311 - (hide annotations) (download) (as text)
Sat Jan 6 07:03:24 2018 UTC (2 years, 9 months ago) by jpye
File MIME type: text/x-csrc
File size: 15558 byte(s)
added extra flag to env.h functions to ensure correct ASC_FREE use after calls to *getenvptr.

1 johnpye 607 /* ASCEND modelling environment
2     Copyright (C) 2006 Carnegie Mellon University
3    
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2, or (at your option)
7     any later version.
8    
9     This program 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
12     GNU General Public License for more details.
13    
14     You should have received a copy of the GNU General Public License
15 jpye 2648 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 johnpye 607 *//**
17     @file
18     CUnit tests for the ospath.c module.
19 jpye 2335 by John Pye.
20 johnpye 607 */
21    
22 jpye 2018 #include <ascend/general/ospath.h>
23 johnpye 607
24 jpye 2346 #include <test/common.h>
25    
26 jpye 3258 static FilePathTestFn ospath_searchpath_testexists;
27 johnpye 607
28 johnpye 998 #define NDEBUG
29    
30 johnpye 607 #ifndef NDEBUG
31     # include <assert.h>
32     # define M(MSG) fprintf(stderr,"%s:%d: (%s) %s\n",__FILE__,__LINE__,__FUNCTION__,MSG);fflush(stderr);fflush(stderr)
33 jpye 2532 # define MC(CLR,MSG) color_on(stderr,CLR);fprintf(stderr,"%s:%d: (%s) %s\n",__FILE__,__LINE__,__FUNCTION__,MSG);color_off(stderr);fflush(stderr)
34     # define MM(MSG) MC(ASC_FG_MAGENTA,MSG)
35 johnpye 607 # define X(VAR) fprintf(stderr,"%s:%d: (%s) %s=%s\n",__FILE__,__LINE__,__FUNCTION__,#VAR,VAR);fflush(stderr)
36     # define C(VAR) fprintf(stderr,"%s:%d: (%s) %s=%c\n",__FILE__,__LINE__,__FUNCTION__,#VAR,VAR);fflush(stderr)
37     # define V(VAR) fprintf(stderr,"%s:%d: (%s) %s=%d\n",__FILE__,__LINE__,__FUNCTION__,#VAR,(VAR));fflush(stderr)
38     # define D(VAR) fprintf(stderr,"%s:%d: (%s) %s=",__FILE__,__LINE__,__FUNCTION__,#VAR);ospath_debug(VAR);fflush(stderr)
39 jpye 2532 # define DD(VAR) color_on(stderr,ASC_FG_BRIGHTBLUE);fprintf(stderr,"%s:%d: (%s)",__FILE__, __LINE__,__FUNCTION__);color_off(stderr);fprintf(stderr,"%s=",#VAR);ospath_debug(VAR);fflush(stderr)
40 johnpye 607 #else
41     # include <assert.h>
42     # define M(MSG) ((void)0)
43     # define MC(CLR,MSG) ((void)0)
44     # define X(VAR) ((void)0)
45     # define C(VAR) ((void)0)
46     # define V(VAR) ((void)0)
47     # define D(VAR) ((void)0)
48     # define DD(VAR) ((void)0)
49     # define MM(VAR) ((void)0)
50     #endif
51    
52 johnpye 998 #ifndef MEMUSED
53     # define MEMUSED(N) CU_TEST(ascmeminuse()==(N))
54     #endif
55    
56 johnpye 607 /**
57 johnpye 998 This is a sample searchpath test function. Assumes the 'userdata' is a
58 johnpye 607 relative FilePath which is appended to path, and then if it matches
59     the path \GTK\bin\johnpye\extfn, returns true. This is of
60     course a fairly useless test function, so it's just for testing.
61     */
62 jpye 3258 static int ospath_searchpath_testexists(struct FilePath *path,void *file){
63 johnpye 607 struct FilePath *fp, *fp1, *fp2;
64     fp = (struct FilePath *)file;
65     D(fp);
66     fp1 = ospath_concat(path,fp);
67     D(fp1);
68    
69     #ifdef WINPATHS
70     fp2 = ospath_new("c:\\GTK\\bin\\johnpye\\extfn");
71     #else
72     fp2 = ospath_new("/GTK/bin/johnpye/extfn");
73     #endif
74    
75     char *t=ospath_str(fp1);
76 jpye 2532 MC(ASC_FG_BRIGHT,t);
77 jpye 3310 ASC_FREE(t);
78 johnpye 607
79     t=ospath_str(fp2);
80 jpye 2532 MC(ASC_FG_BRIGHTRED,t);
81 jpye 3310 ASC_FREE(t);
82 johnpye 607
83     if(ospath_cmp(fp1,fp2)==0){
84 jpye 2532 MC(ASC_FG_GREEN,"MATCH");
85 johnpye 998 ospath_free(fp1);
86     ospath_free(fp2);
87 johnpye 607 return 1;
88     }
89 jpye 2532 MC(ASC_FG_RED,"NO MATCH");
90 johnpye 998 ospath_free(fp1);
91     ospath_free(fp2);
92 johnpye 607 return 0;
93     }
94    
95    
96     // switch to boldface for messages in 'main'
97     #undef D
98     #define D DD
99     #undef M
100     #define M MM
101    
102 jpye 2346 static void test_getparent(void){
103 johnpye 998 struct FilePath *fp1, *fp2, *fp3;
104 johnpye 607
105     //------------------------
106    
107     fp1 = ospath_new_from_posix("/usr/local/hello/");
108     fp2 = ospath_getparent(fp1);
109     fp3 = ospath_new_from_posix("/usr/local");
110    
111     D(fp1);
112     D(fp2);
113     D(fp3);
114     CU_TEST(ospath_cmp(fp2,fp3)==0);
115 jpye 2335 if(ospath_cmp(fp2,fp3)==0)M("Passed 'getparent' test\n");
116 johnpye 607
117     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3);
118    
119 johnpye 998 MEMUSED(0);
120     }
121 johnpye 607 //------------------------
122    
123 jpye 2346 static void test_cleanup(void){
124 johnpye 998 struct FilePath *fp1, *fp2;
125    
126     fp1 = ospath_new_from_posix("/usr/include/../local");
127     fp2 = ospath_new_from_posix("/usr/local");
128    
129     D(fp1);
130     D(fp2);
131     CU_TEST(ospath_cmp(fp1,fp2)==0);
132     M("Passed 'cleanup' test\n");
133    
134     ospath_free(fp1); ospath_free(fp2);
135     MEMUSED(0);
136     }
137     //------------------------
138    
139 jpye 3258 static void test_newcopy(void){
140     struct FilePath *fp1, *fp2;
141     fp1 = ospath_new_from_posix("/usr/local/bin");
142     fp2 = ospath_new_copy(fp1);
143     CU_TEST(ospath_cmp(fp1,fp2)==0);
144     ospath_free(fp1);
145     ospath_free(fp2);
146     MEMUSED(0);
147     }
148     //------------------------
149    
150 jpye 2346 static void test_newfromposix(void){
151 johnpye 998
152     struct FilePath *fp1, *fp2;
153    
154 johnpye 607 fp1 = ospath_new_from_posix("models/johnpye/extfn/extfntest");
155     D(fp1);
156     fp2 = ospath_new("models\\johnpye\\extfn\\extfntest");
157     D(fp2);
158     D(fp1);
159     CU_TEST(ospath_cmp(fp1,fp2)==0);
160     M("Passed 'new_from_posix' test\n");
161    
162     ospath_free(fp1);
163     ospath_free(fp2);
164 johnpye 998 MEMUSED(0);
165     }
166     //------------------------
167 johnpye 607
168 jpye 2346 static void test_secondcleanup(void){
169 johnpye 998 struct FilePath *fp1, *fp2, *fp3;
170 johnpye 607 fp1 = ospath_new(".\\src/.\\images\\..\\\\movies\\");
171     fp2 = ospath_new(".\\src\\movies");
172    
173     D(fp1);
174     D(fp2);
175    
176     CU_TEST(ospath_cmp(fp1,fp2)==0);
177     M("Passed mid-path '..' cleanup test\n");
178    
179     ospath_free(fp2);
180    
181     fp2 = ospath_new("./src/movies\\kubrick");
182     fp3 = ospath_getparent(fp2);
183    
184     D(fp2);
185     D(fp3);
186    
187     CU_TEST(ospath_cmp(fp1,fp3)==0);
188     M("Passed 'second cleanup' test\n");
189    
190 johnpye 998 ospath_free(fp1);
191     ospath_free(fp2);
192     ospath_free(fp3);
193     MEMUSED(0);
194     }
195 johnpye 607 //------------------------
196    
197 jpye 2346 static void test_append(void){
198 johnpye 998
199     struct FilePath *fp2, *fp3, *fp4;
200    
201 johnpye 607 fp2 = ospath_new("\\home\\john");
202     fp3 = ospath_new("where\\mojo");
203    
204     D(fp2);
205     D(fp3);
206    
207     ospath_append(fp2,fp3);
208    
209     D(fp2);
210    
211     fp4 = ospath_new("\\home\\john\\where\\mojo\\");
212    
213     D(fp2);
214     CU_TEST(ospath_cmp(fp2,fp4)==0);
215     M("Passed 'append' test\n");
216    
217     ospath_free(fp3);
218     ospath_free(fp2);
219 johnpye 998 ospath_free(fp4);
220     MEMUSED(0);
221     }
222 johnpye 607 //---------------------------
223    
224 jpye 2346 static void test_appendupup(void){
225 johnpye 998
226     struct FilePath *fp2, *fp3, *fp4;
227    
228 johnpye 607 fp3 = ospath_new_noclean("../..");
229     D(fp3);
230    
231     // test with appending ../.. to an existing path
232     fp2 = ospath_new("\\home\\john");
233     M("ORIGINAL PATH");
234     D(fp2);
235     ospath_append(fp2,fp3);
236     M("AFTER APPENDING ../..");
237     D(fp2);
238    
239     M("GETTING ROOT");
240     fp4 = ospath_root(fp2);
241     M("ROOT FOUND:");
242     D(fp4);
243    
244     CU_TEST(ospath_cmp(fp2,fp4)==0);
245     M("Passed 'append ../..' test\n");
246    
247     ospath_free(fp2);
248     ospath_free(fp3);
249     ospath_free(fp4);
250 johnpye 998 MEMUSED(0);
251     }
252 johnpye 607 //-------------------------
253    
254 jpye 2346 static void test_up(void){
255 johnpye 998
256     struct FilePath *fp1, *fp2;
257    
258 johnpye 607 fp1 = ospath_new("~\\somewhere\\..");
259     fp2 = ospath_new("~/.");
260    
261     CU_TEST(ospath_cmp(fp1,fp2)==0);
262    
263     D(fp2);
264    
265     ospath_free(fp1);
266     ospath_free(fp2);
267    
268     fp1 = ospath_new("/usr/local/include");
269     fp2 = ospath_new("/usr/include/../local/include");
270    
271     D(fp1);
272     D(fp2);
273    
274     CU_TEST(ospath_cmp(fp1,fp2)==0);
275     M("Passed another mid-path '..' test\n");
276    
277     ospath_free(fp1);
278     ospath_free(fp2);
279 johnpye 998 MEMUSED(0);
280     }
281 johnpye 607 //---------------------------
282    
283 jpye 2346 static void test_concat(void){
284 johnpye 998
285 jpye 2699 struct FilePath *fp1, *fp2, *fp3, *fp4, *fp5;
286 johnpye 998
287 johnpye 607 fp1 = ospath_new("/home");
288     fp2 = ospath_new("john");
289     fp3 = ospath_concat(fp1, fp2);
290    
291     fp4 = ospath_new("/home/john");
292    
293     CU_TEST(ospath_cmp(fp3,fp4)==0);
294     M("Passed 'ospath_concat' test\n");
295    
296     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
297    
298     fp1 = ospath_new("c:/Program Files");
299     fp2 = ospath_new("GnuWin32\\bin");
300     fp3 = ospath_concat(fp1, fp2);
301    
302     fp4 = ospath_new("c:/Program Files/GnuWin32/bin");
303    
304     CU_TEST(ospath_cmp(fp3,fp4)==0);
305     M("Passed 'ospath_concat' test\n");
306    
307     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
308 jpye 2699
309     M("TESTING CONCAT WITH ..");
310    
311     fp1 = ospath_new_from_posix("/usr/bin/less");
312     fp2 = ospath_getdir(fp1);
313     fp3 = ospath_new_noclean("../share/icons/pkgconfig");
314     fp4 = ospath_concat(fp2,fp3);
315     ospath_cleanup(fp4);
316    
317     fp5 = ospath_new_from_posix("/usr/share/icons/pkgconfig");
318    
319     #if 0
320     M("FP4:");
321     ospath_fwrite(fp4,stderr);
322     M("\nFP5:");
323     ospath_fwrite(fp5,stderr);
324     M("");
325     #endif
326    
327     CU_TEST(ospath_cmp(fp4,fp5)==0);
328    
329     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4); ospath_free(fp5);
330    
331 johnpye 998 MEMUSED(0);
332     }
333 johnpye 607 //---------------------------
334    
335 jpye 2346 static void test_concatmixedslash(void){
336 johnpye 998
337     struct FilePath *fp1, *fp2, *fp3, *fp4;
338    
339 johnpye 607 fp1 = ospath_new("c:/Program Files/");
340     fp2 = ospath_new("GnuWin32\\bin");
341     fp3 = ospath_concat(fp1, fp2);
342    
343     fp4 = ospath_new("c:/Program Files/GnuWin32/bin");
344    
345     CU_TEST(ospath_cmp(fp3,fp4)==0);
346     M("Passed trailing-slash 'ospath_concat' test\n");
347    
348     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
349 johnpye 998 MEMUSED(0);
350     }
351 johnpye 607 //---------------------------
352    
353 jpye 2346 static void test_trailingslash(void){
354 johnpye 998
355     struct FilePath *fp1, *fp2, *fp3, *fp4;
356    
357 johnpye 607 fp1 = ospath_new("c:/Program Files/GnuWin32/bin");
358     fp2 = ospath_new("johnpye/extfn");
359     fp3 = ospath_concat(fp1, fp2);
360    
361     fp4 = ospath_new("c:/Program Files/GnuWin32/bin/johnpye/extfn");
362    
363     CU_TEST(ospath_cmp(fp3,fp4)==0);
364     M("Passed trailing-slash 'ospath_concat' test\n");
365    
366     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
367 johnpye 998 MEMUSED(0);
368     }
369 johnpye 607 //---------------------------
370    
371 jpye 2346 static void test_searchpath(void){
372 johnpye 998
373     struct FilePath *fp1, *fp2, *fp3;
374     struct FilePath **pp, **p1;// will be returned null-terminated
375     #ifdef WINPATHS
376     char pathtext[]="c:\\Program Files\\GnuWin32\\bin;c:\\GTK\\bin;e:\\ascend\\;..\\..\\pygtk";
377     #else
378     char pathtext[]="\\Program Files\\GnuWin32\\bin:\\GTK\\bin:\\ascend\\:..\\..\\pygtk";
379     #endif
380    
381 johnpye 607 pp = ospath_searchpath_new(pathtext);
382 jpye 3258 CU_TEST(ospath_searchpath_length(pp)==4);
383 johnpye 607
384     for(p1=pp; *p1!=NULL; ++p1){
385     D(*p1);
386     }
387    
388     #ifdef WINPATHS
389     fp1 = ospath_new("c:\\program files\\GnuWin32\\bin");
390     #else
391     fp1 = ospath_new("\\Program Files\\GnuWin32\\bin");
392     #endif
393    
394     CU_TEST(pp[0]!=NULL);
395     CU_TEST(fp1!=NULL);
396    
397     D(fp1);
398     D(pp[0]);
399    
400     CU_TEST(ospath_cmp(pp[0],fp1)==0);
401    
402    
403     fp2 = ospath_new_noclean("johnpye/extfn");
404     D(fp2);
405    
406     fflush(stderr);
407    
408     fp3 = ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);
409    
410     CU_TEST(fp3!=NULL);
411     D(fp3);
412     D(pp[1]);
413    
414     CU_TEST(ospath_cmp(fp3,pp[1])==0);
415     M("Passed path-search test\n");
416    
417     ospath_free(fp1);
418     ospath_free(fp2);
419     ospath_searchpath_free(pp);
420 johnpye 998 MEMUSED(0);
421     }
422 johnpye 607 //-------------------------------
423    
424 jpye 2346 static void test_searchpath2(void){
425 johnpye 998
426     struct FilePath *fp2, *fp3;
427     struct FilePath **pp, **p1;// will be returned null-terminated
428     #ifdef WINPATHS
429     char pathtext2[]="c:\\Program Files\\ASCEND\\models";
430     #else
431     char pathtext2[]="/usr/local/ascend/models";
432     #endif
433    
434 johnpye 607 M("Path-search test 2...");
435    
436     pp = ospath_searchpath_new(pathtext2);
437    
438     CU_TEST(pp!=NULL);
439    
440     for (p1=pp; *p1!=NULL; ++p1){
441     D(*p1);
442     }
443    
444     fp2 = ospath_new_noclean("johnpye/extfn/extfntest");
445     D(fp2);
446    
447     fp3 = ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);
448     CU_TEST(fp3==NULL);
449    
450     M("Passed path-search test 2\n");
451    
452     ospath_free(fp2);
453     ospath_free(fp3);
454     ospath_searchpath_free(pp);
455 johnpye 998 MEMUSED(0);
456     }
457 johnpye 607 //-------------------------------
458    
459 jpye 2346 static void test_basefilename(void){
460 johnpye 998
461     struct FilePath *fp1;
462     char *s1;
463    
464 johnpye 607 fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest.a4c");
465     D(fp1);
466     s1 = ospath_getbasefilename(fp1);
467     X(s1);
468     CU_TEST(strcmp(s1,"extfntest.a4c")==0);
469     M("Passed getbasefilename test\n");
470    
471     ospath_free(fp1);
472 jpye 3310 ASC_FREE(s1);
473 johnpye 607
474     fp1 = ospath_new("extfntest.a4c");
475     D(fp1);
476     s1 = ospath_getbasefilename(fp1);
477     X(s1);
478     CU_TEST(strcmp(s1,"extfntest.a4c")==0);
479     M("Passed getbasefilename test 2\n");
480    
481     ospath_free(fp1);
482 jpye 3310 ASC_FREE(s1);
483 johnpye 607
484     fp1 = ospath_new("/here/is/my/path.dir/");
485     D(fp1);
486     s1 = ospath_getbasefilename(fp1);
487     X(s1);
488     CU_TEST(NULL==s1);
489     M("Passed getbasefilename test 3\n");
490    
491     ospath_free(fp1);
492 jpye 3310 if(s1)ASC_FREE(s1);
493 johnpye 607
494     #ifdef WINPATHS
495     fp1 = ospath_new("c:extfntest.a4c");
496     D(fp1);
497     s1 = ospath_getbasefilename(fp1);
498     X(s1);
499     CU_TEST(strcmp(s1,"extfntest.a4c")==0);
500     M("Passed getbasefilename test WINPATHS\n");
501    
502     ospath_free(fp1);
503 jpye 3310 ASC_FREE(s1);
504 johnpye 607 #endif
505 johnpye 998 MEMUSED(0);
506     }
507 johnpye 607 //-------------------------------
508    
509 jpye 2346 static void test_getfilestem(void){
510 johnpye 998
511     struct FilePath *fp1;
512     char *s1;
513    
514 johnpye 607 fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest.a4c");
515     D(fp1);
516     s1 = ospath_getfilestem(fp1);
517     X(s1);
518     CU_TEST(strcmp(s1,"extfntest")==0);
519     M("Passed getfilestem test\n");
520    
521     ospath_free(fp1);
522 jpye 3310 ASC_FREE(s1);
523 johnpye 607
524     fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest");
525     D(fp1);
526     s1 = ospath_getfilestem(fp1);
527     X(s1);
528     CU_TEST(strcmp(s1,"extfntest")==0);
529     M("Passed getfilestem test 2\n");
530    
531     ospath_free(fp1);
532 jpye 3310 ASC_FREE(s1);
533 johnpye 607
534     fp1 = ospath_new("/usr/share/data/ascend/.ascend.ini");
535     D(fp1);
536     s1 = ospath_getfilestem(fp1);
537     X(s1);
538     CU_TEST(strcmp(s1,".ascend")==0);
539     M("Passed getfilestem test 3\n");
540    
541     ospath_free(fp1);
542 jpye 3310 ASC_FREE(s1);
543 johnpye 607
544     fp1 = ospath_new("~/.vimrc");
545     D(fp1);
546     s1 = ospath_getfilestem(fp1);
547     X(s1);
548     CU_TEST(strcmp(s1,".vimrc")==0);
549     M("Passed getfilestem test 3\n");
550    
551     ospath_free(fp1);
552 jpye 3310 ASC_FREE(s1);
553 johnpye 607
554     fp1 = ospath_new("~/src/ascend-0.9.5-1.jdpipe.src.rpm");
555     D(fp1);
556     s1 = ospath_getfilestem(fp1);
557     X(s1);
558     CU_TEST(strcmp(s1,"ascend-0.9.5-1.jdpipe.src")==0);
559     M("Passed getfilestem test 4\n");
560    
561     ospath_free(fp1);
562 jpye 3310 ASC_FREE(s1);
563 johnpye 607
564     fp1 = ospath_new("~/dir1/dir2/");
565     D(fp1);
566     s1 = ospath_getfilestem(fp1);
567     X(s1);
568     CU_TEST(NULL==s1);
569     M("Passed getfilestem test 5\n");
570    
571     ospath_free(fp1);
572 jpye 3310 if(s1)ASC_FREE(s1);
573 johnpye 998 MEMUSED(0);
574     }
575 johnpye 607 //-------------------------------
576    
577 jpye 2346 static void test_getbasefileext(void){
578 johnpye 998
579     struct FilePath *fp1;
580     char *s1;
581    
582 johnpye 607 fp1 = ospath_new("~/src/ascend-0.9.5-1.jdpipe.src.rpm");
583     D(fp1);
584     s1 = ospath_getfileext(fp1);
585     X(s1);
586     CU_TEST(strcmp(s1,".rpm")==0);
587     M("Passed getbasefileext test\n");
588    
589     ospath_free(fp1);
590 jpye 3310 ASC_FREE(s1);
591 johnpye 607
592     fp1 = ospath_new("~/.vimrc");
593     D(fp1);
594     s1 = ospath_getfileext(fp1);
595     X(s1);
596     CU_TEST(s1==NULL);
597     M("Passed getbasefileext test 2\n");
598    
599     ospath_free(fp1);
600 jpye 3310 if(s1)ASC_FREE(s1);
601 johnpye 607
602     fp1 = ospath_new("./ascend4");
603     D(fp1);
604     s1 = ospath_getfileext(fp1);
605     X(s1);
606     CU_TEST(s1==NULL);
607     M("Passed getbasefileext test 3\n");
608    
609     ospath_free(fp1);
610 jpye 3310 if(s1)ASC_FREE(s1);
611 johnpye 998 MEMUSED(0);
612     }
613 johnpye 607 //-------------------------------
614    
615 jpye 2346 static void test_getdir(void){
616 johnpye 998
617     struct FilePath *fp1, *fp2, *fp3;
618    
619 johnpye 607 fp1 = ospath_new("/home/myfile");
620     fp2 = ospath_getdir(fp1);
621     fp3 = ospath_new("/home");
622     CU_TEST(ospath_cmp(fp2,fp3)==0);
623     M("Passed ospath_getdir test\n");
624    
625     ospath_free(fp1);
626     ospath_free(fp2);
627     ospath_free(fp3);
628    
629     fp1 = ospath_new("/home/myfile.ext");
630     fp2 = ospath_getdir(fp1);
631     fp3 = ospath_new("/home");
632     CU_TEST(ospath_cmp(fp2,fp3)==0);
633     M("Passed ospath_getdir test 2\n");
634    
635     ospath_free(fp1);
636     ospath_free(fp2);
637     ospath_free(fp3);
638    
639     fp1 = ospath_new("/home/mydir/");
640     fp2 = ospath_getdir(fp1);
641     fp3 = ospath_new("/home/mydir");
642     CU_TEST(ospath_cmp(fp2,fp3)==0);
643     M("Passed ospath_getdir test 3\n");
644    
645     ospath_free(fp1);
646     ospath_free(fp2);
647     ospath_free(fp3);
648 johnpye 998 MEMUSED(0);
649 johnpye 607 }
650 jpye 3258 //-------------------------------
651 johnpye 607
652 jpye 3258 /*
653     return NULL for unfound env vars, else point to a string that must not be
654     modified by the caller, and may later be changed by a later call to getenv.
655     */
656     static char *my_ospath_getenv(const char *name){
657     if(strcmp(name,"MYHOME")==0){
658     return "/home/john";
659     }else if(strcmp(name,"MYBIN")==0){
660     return "/usr/local/bin";
661     }
662     return NULL;
663     }
664    
665     static void test_expandenv(void){
666     struct FilePath *fp1, *fp2;
667    
668     CU_TEST(strcmp(my_ospath_getenv("MYHOME"),"/home/john")==0);
669 jpye 3311 fp1 = ospath_new_expand_env("$MYHOME/myfile.ext",my_ospath_getenv,0);
670 jpye 3258 fp2 = ospath_new("/home/john/myfile.ext");
671     CU_TEST(ospath_cmp(fp1,fp2)==0);
672    
673     ospath_free(fp1);
674     ospath_free(fp2);
675     MEMUSED(0);
676     }
677    
678 johnpye 607 /*===========================================================================*/
679     /* Registration information */
680    
681 jpye 2346 #define TESTS(T) \
682     T(getparent) \
683 jpye 3258 T(newfromposix) \
684     T(newcopy) \
685 jpye 2346 T(cleanup) \
686     T(secondcleanup) \
687     T(append) \
688     T(appendupup) \
689     T(up) \
690     T(concat) \
691     T(concatmixedslash) \
692     T(trailingslash) \
693     T(searchpath) \
694     T(searchpath2) \
695     T(basefilename) \
696     T(getfilestem) \
697     T(getbasefileext) \
698 jpye 3258 T(getdir) \
699     T(expandenv)
700 johnpye 607
701 jpye 2346 REGISTER_TESTS_SIMPLE(general_ospath, TESTS);
702 johnpye 607

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22