/[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 608 - (hide annotations) (download) (as text)
Tue May 16 04:32:46 2006 UTC (14 years, 5 months ago) by johnpye
Original Path: trunk/base/generic/general/test/test_ospath.c
File MIME type: text/x-csrc
File size: 12515 byte(s)
Fixing ospath tests, hopefully.
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     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place - Suite 330,
17     Boston, MA 02111-1307, USA.
18     *//**
19     @file
20     CUnit tests for the ospath.c module.
21     */
22    
23     #include <general/ospath.h>
24     #include <CUnit/CUnit.h>
25    
26     FilePathTestFn ospath_searchpath_testexists;
27    
28     #ifndef NDEBUG
29     # include <assert.h>
30     # define M(MSG) fprintf(stderr,"%s:%d: (%s) %s\n",__FILE__,__LINE__,__FUNCTION__,MSG);fflush(stderr);fflush(stderr)
31     # define MC(CLR,MSG) fprintf(stderr,"%c[%sm%s:%d: (%s) %s%c[0m\n",27,CLR,__FILE__,__LINE__,__FUNCTION__,MSG,27);fflush(stderr)
32     # define MM(MSG) MC("34",MSG)
33     # define X(VAR) fprintf(stderr,"%s:%d: (%s) %s=%s\n",__FILE__,__LINE__,__FUNCTION__,#VAR,VAR);fflush(stderr)
34     # define C(VAR) fprintf(stderr,"%s:%d: (%s) %s=%c\n",__FILE__,__LINE__,__FUNCTION__,#VAR,VAR);fflush(stderr)
35     # define V(VAR) fprintf(stderr,"%s:%d: (%s) %s=%d\n",__FILE__,__LINE__,__FUNCTION__,#VAR,(VAR));fflush(stderr)
36     # define D(VAR) fprintf(stderr,"%s:%d: (%s) %s=",__FILE__,__LINE__,__FUNCTION__,#VAR);ospath_debug(VAR);fflush(stderr)
37     # define DD(VAR) fprintf(stderr,"\033[34;1m%s:%d: (%s)\033[0m %s=",__FILE__, __LINE__,__FUNCTION__, #VAR);ospath_debug(VAR);fflush(stderr)
38     #else
39     # include <assert.h>
40     # define M(MSG) ((void)0)
41     # define MC(CLR,MSG) ((void)0)
42     # define X(VAR) ((void)0)
43     # define C(VAR) ((void)0)
44     # define V(VAR) ((void)0)
45     # define D(VAR) ((void)0)
46     # define DD(VAR) ((void)0)
47     # define MM(VAR) ((void)0)
48     #endif
49    
50     /**
51     This is a sample searchpath test function. Assumes the 'userfdata' is a
52     relative FilePath which is appended to path, and then if it matches
53     the path \GTK\bin\johnpye\extfn, returns true. This is of
54     course a fairly useless test function, so it's just for testing.
55     */
56     int ospath_searchpath_testexists(struct FilePath *path,void *file){
57     struct FilePath *fp, *fp1, *fp2;
58     fp = (struct FilePath *)file;
59     D(fp);
60     fp1 = ospath_concat(path,fp);
61     D(fp1);
62    
63     #ifdef WINPATHS
64     fp2 = ospath_new("c:\\GTK\\bin\\johnpye\\extfn");
65     #else
66     fp2 = ospath_new("/GTK/bin/johnpye/extfn");
67     #endif
68    
69     char *t=ospath_str(fp1);
70     MC("1",t);
71     FREE(t);
72    
73     t=ospath_str(fp2);
74     MC("31;1",t);
75     FREE(t);
76    
77     if(ospath_cmp(fp1,fp2)==0){
78     MC("32","MATCH");
79     return 1;
80     }
81     MC("31","NO MATCH");
82     return 0;
83     }
84    
85    
86     // switch to boldface for messages in 'main'
87     #undef D
88     #define D DD
89     #undef M
90     #define M MM
91    
92 johnpye 608 static void test_ospath(void){
93 johnpye 607
94     struct FilePath *fp1, *fp2, *fp3, *fp4;
95     char *s1;
96     struct FilePath **pp, **p1;// will be returned null-terminated
97     #ifdef WINPATHS
98     char pathtext[]="c:\\Program Files\\GnuWin32\\bin;c:\\GTK\\bin;e:\\ascend\\;..\\..\\pygtk";
99     char pathtext2[]="c:\\Program Files\\ASCEND\\models";
100     #else
101     char pathtext[]="\\Program Files\\GnuWin32\\bin:\\GTK\\bin:\\ascend\\:..\\..\\pygtk";
102     char pathtext2[]="/usr/local/ascend/models";
103     #endif
104    
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     M("Passed 'getparent' test\n");
116    
117     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3);
118    
119     //------------------------
120    
121     fp1 = ospath_new_from_posix("models/johnpye/extfn/extfntest");
122     D(fp1);
123     fp2 = ospath_new("models\\johnpye\\extfn\\extfntest");
124     D(fp2);
125     D(fp1);
126     CU_TEST(ospath_cmp(fp1,fp2)==0);
127     M("Passed 'new_from_posix' test\n");
128    
129     ospath_free(fp1);
130     ospath_free(fp2);
131    
132     //------------------------
133     fp1 = ospath_new(".\\src/.\\images\\..\\\\movies\\");
134     fp2 = ospath_new(".\\src\\movies");
135    
136     D(fp1);
137     D(fp2);
138    
139     CU_TEST(ospath_cmp(fp1,fp2)==0);
140     M("Passed mid-path '..' cleanup test\n");
141    
142     ospath_free(fp2);
143    
144     fp2 = ospath_new("./src/movies\\kubrick");
145     fp3 = ospath_getparent(fp2);
146    
147     D(fp2);
148     D(fp3);
149    
150     CU_TEST(ospath_cmp(fp1,fp3)==0);
151     M("Passed 'second cleanup' test\n");
152    
153     //------------------------
154    
155     fp2 = ospath_new("\\home\\john");
156     fp3 = ospath_new("where\\mojo");
157    
158     D(fp2);
159     D(fp3);
160    
161     ospath_append(fp2,fp3);
162    
163     D(fp2);
164    
165     fp4 = ospath_new("\\home\\john\\where\\mojo\\");
166    
167     D(fp2);
168     CU_TEST(ospath_cmp(fp2,fp4)==0);
169     M("Passed 'append' test\n");
170    
171     ospath_free(fp3);
172     ospath_free(fp2);
173    
174     //---------------------------
175    
176     fp3 = ospath_new_noclean("../..");
177     D(fp3);
178    
179     // test with appending ../.. to an existing path
180     fp2 = ospath_new("\\home\\john");
181     M("ORIGINAL PATH");
182     D(fp2);
183     ospath_append(fp2,fp3);
184     M("AFTER APPENDING ../..");
185     D(fp2);
186    
187     M("GETTING ROOT");
188     fp4 = ospath_root(fp2);
189     M("ROOT FOUND:");
190     D(fp4);
191    
192     CU_TEST(ospath_cmp(fp2,fp4)==0);
193     M("Passed 'append ../..' test\n");
194    
195     ospath_free(fp2);
196     ospath_free(fp3);
197     ospath_free(fp4);
198    
199     //-------------------------
200    
201     fp1 = ospath_new("~\\somewhere\\..");
202     fp2 = ospath_new("~/.");
203    
204     CU_TEST(ospath_cmp(fp1,fp2)==0);
205    
206     D(fp2);
207    
208     ospath_free(fp1);
209     ospath_free(fp2);
210    
211     fp1 = ospath_new("/usr/local/include");
212     fp2 = ospath_new("/usr/include/../local/include");
213    
214     D(fp1);
215     D(fp2);
216    
217     CU_TEST(ospath_cmp(fp1,fp2)==0);
218     M("Passed another mid-path '..' test\n");
219    
220     ospath_free(fp1);
221     ospath_free(fp2);
222    
223     //---------------------------
224    
225     fp1 = ospath_new("/home");
226     fp2 = ospath_new("john");
227     fp3 = ospath_concat(fp1, fp2);
228    
229     fp4 = ospath_new("/home/john");
230    
231     CU_TEST(ospath_cmp(fp3,fp4)==0);
232     M("Passed 'ospath_concat' test\n");
233    
234     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
235    
236     //---------------------------
237    
238     fp1 = ospath_new("c:/Program Files");
239     fp2 = ospath_new("GnuWin32\\bin");
240     fp3 = ospath_concat(fp1, fp2);
241    
242     fp4 = ospath_new("c:/Program Files/GnuWin32/bin");
243    
244     CU_TEST(ospath_cmp(fp3,fp4)==0);
245     M("Passed 'ospath_concat' test\n");
246    
247     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
248    
249     //---------------------------
250    
251     fp1 = ospath_new("c:/Program Files/");
252     fp2 = ospath_new("GnuWin32\\bin");
253     fp3 = ospath_concat(fp1, fp2);
254    
255     fp4 = ospath_new("c:/Program Files/GnuWin32/bin");
256    
257     CU_TEST(ospath_cmp(fp3,fp4)==0);
258     M("Passed trailing-slash 'ospath_concat' test\n");
259    
260     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
261    
262     //---------------------------
263    
264     fp1 = ospath_new("c:/Program Files/GnuWin32/bin");
265     fp2 = ospath_new("johnpye/extfn");
266     fp3 = ospath_concat(fp1, fp2);
267    
268     fp4 = ospath_new("c:/Program Files/GnuWin32/bin/johnpye/extfn");
269    
270     CU_TEST(ospath_cmp(fp3,fp4)==0);
271     M("Passed trailing-slash 'ospath_concat' test\n");
272    
273     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
274    
275     //---------------------------
276    
277     pp = ospath_searchpath_new(pathtext);
278    
279     for(p1=pp; *p1!=NULL; ++p1){
280     D(*p1);
281     }
282    
283     #ifdef WINPATHS
284     fp1 = ospath_new("c:\\program files\\GnuWin32\\bin");
285     #else
286     fp1 = ospath_new("\\Program Files\\GnuWin32\\bin");
287     #endif
288    
289     CU_TEST(pp[0]!=NULL);
290     CU_TEST(fp1!=NULL);
291    
292     D(fp1);
293     D(pp[0]);
294    
295     CU_TEST(ospath_cmp(pp[0],fp1)==0);
296    
297    
298     fp2 = ospath_new_noclean("johnpye/extfn");
299     D(fp2);
300    
301     fflush(stderr);
302    
303     fp3 = ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);
304    
305     CU_TEST(fp3!=NULL);
306     D(fp3);
307     D(pp[1]);
308    
309     CU_TEST(ospath_cmp(fp3,pp[1])==0);
310     M("Passed path-search test\n");
311    
312     ospath_free(fp1);
313     ospath_free(fp2);
314     ospath_searchpath_free(pp);
315    
316     //-------------------------------
317    
318     M("Path-search test 2...");
319    
320     pp = ospath_searchpath_new(pathtext2);
321    
322     CU_TEST(pp!=NULL);
323    
324     for (p1=pp; *p1!=NULL; ++p1){
325     D(*p1);
326     }
327    
328     fp2 = ospath_new_noclean("johnpye/extfn/extfntest");
329     D(fp2);
330    
331     fp3 = ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);
332     CU_TEST(fp3==NULL);
333    
334     M("Passed path-search test 2\n");
335    
336     ospath_free(fp2);
337     ospath_free(fp3);
338     ospath_searchpath_free(pp);
339    
340     //-------------------------------
341    
342     fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest.a4c");
343     D(fp1);
344     s1 = ospath_getbasefilename(fp1);
345     X(s1);
346     CU_TEST(strcmp(s1,"extfntest.a4c")==0);
347     M("Passed getbasefilename test\n");
348    
349     ospath_free(fp1);
350     FREE(s1);
351    
352     //-------------------------------
353    
354     fp1 = ospath_new("extfntest.a4c");
355     D(fp1);
356     s1 = ospath_getbasefilename(fp1);
357     X(s1);
358     CU_TEST(strcmp(s1,"extfntest.a4c")==0);
359     M("Passed getbasefilename test 2\n");
360    
361     ospath_free(fp1);
362     FREE(s1);
363    
364    
365     //-------------------------------
366    
367     fp1 = ospath_new("/here/is/my/path.dir/");
368     D(fp1);
369     s1 = ospath_getbasefilename(fp1);
370     X(s1);
371     CU_TEST(NULL==s1);
372     M("Passed getbasefilename test 3\n");
373    
374     ospath_free(fp1);
375     FREE(s1);
376    
377     //-------------------------------
378    
379     #ifdef WINPATHS
380     fp1 = ospath_new("c:extfntest.a4c");
381     D(fp1);
382     s1 = ospath_getbasefilename(fp1);
383     X(s1);
384     CU_TEST(strcmp(s1,"extfntest.a4c")==0);
385     M("Passed getbasefilename test WINPATHS\n");
386    
387     ospath_free(fp1);
388     FREE(s1);
389     #endif
390    
391     //-------------------------------
392    
393     fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest.a4c");
394     D(fp1);
395     s1 = ospath_getfilestem(fp1);
396     X(s1);
397     CU_TEST(strcmp(s1,"extfntest")==0);
398     M("Passed getfilestem test\n");
399    
400     ospath_free(fp1);
401     FREE(s1);
402    
403     //-------------------------------
404    
405     fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest");
406     D(fp1);
407     s1 = ospath_getfilestem(fp1);
408     X(s1);
409     CU_TEST(strcmp(s1,"extfntest")==0);
410     M("Passed getfilestem test 2\n");
411    
412     ospath_free(fp1);
413     FREE(s1);
414    
415     //-------------------------------
416    
417     fp1 = ospath_new("/usr/share/data/ascend/.ascend.ini");
418     D(fp1);
419     s1 = ospath_getfilestem(fp1);
420     X(s1);
421     CU_TEST(strcmp(s1,".ascend")==0);
422     M("Passed getfilestem test 3\n");
423    
424     ospath_free(fp1);
425     FREE(s1);
426    
427     //-------------------------------
428    
429     fp1 = ospath_new("~/.vimrc");
430     D(fp1);
431     s1 = ospath_getfilestem(fp1);
432     X(s1);
433     CU_TEST(strcmp(s1,".vimrc")==0);
434     M("Passed getfilestem test 3\n");
435    
436     ospath_free(fp1);
437     FREE(s1);
438    
439     //-------------------------------
440    
441     fp1 = ospath_new("~/src/ascend-0.9.5-1.jdpipe.src.rpm");
442     D(fp1);
443     s1 = ospath_getfilestem(fp1);
444     X(s1);
445     CU_TEST(strcmp(s1,"ascend-0.9.5-1.jdpipe.src")==0);
446     M("Passed getfilestem test 4\n");
447    
448     ospath_free(fp1);
449     FREE(s1);
450    
451     //-------------------------------
452    
453     fp1 = ospath_new("~/dir1/dir2/");
454     D(fp1);
455     s1 = ospath_getfilestem(fp1);
456     X(s1);
457     CU_TEST(NULL==s1);
458     M("Passed getfilestem test 5\n");
459    
460     ospath_free(fp1);
461     FREE(s1);
462    
463     //-------------------------------
464    
465     fp1 = ospath_new("~/src/ascend-0.9.5-1.jdpipe.src.rpm");
466     D(fp1);
467     s1 = ospath_getfileext(fp1);
468     X(s1);
469     CU_TEST(strcmp(s1,".rpm")==0);
470     M("Passed getbasefileext test\n");
471    
472     ospath_free(fp1);
473     FREE(s1);
474    
475     //-------------------------------
476    
477     fp1 = ospath_new("~/.vimrc");
478     D(fp1);
479     s1 = ospath_getfileext(fp1);
480     X(s1);
481     CU_TEST(s1==NULL);
482     M("Passed getbasefileext test 2\n");
483    
484     ospath_free(fp1);
485     FREE(s1);
486    
487     //-------------------------------
488    
489     fp1 = ospath_new("./ascend4");
490     D(fp1);
491     s1 = ospath_getfileext(fp1);
492     X(s1);
493     CU_TEST(s1==NULL);
494     M("Passed getbasefileext test 3\n");
495    
496     ospath_free(fp1);
497     FREE(s1);
498    
499     //-------------------------------
500    
501     fp1 = ospath_new("/home/myfile");
502     fp2 = ospath_getdir(fp1);
503     fp3 = ospath_new("/home");
504     CU_TEST(ospath_cmp(fp2,fp3)==0);
505     M("Passed ospath_getdir test\n");
506    
507     ospath_free(fp1);
508     ospath_free(fp2);
509     ospath_free(fp3);
510    
511     //-------------------------------
512    
513     fp1 = ospath_new("/home/myfile.ext");
514     fp2 = ospath_getdir(fp1);
515     fp3 = ospath_new("/home");
516     CU_TEST(ospath_cmp(fp2,fp3)==0);
517     M("Passed ospath_getdir test 2\n");
518    
519     ospath_free(fp1);
520     ospath_free(fp2);
521     ospath_free(fp3);
522    
523     //-------------------------------
524    
525     fp1 = ospath_new("/home/mydir/");
526     fp2 = ospath_getdir(fp1);
527     fp3 = ospath_new("/home/mydir");
528     CU_TEST(ospath_cmp(fp2,fp3)==0);
529     M("Passed ospath_getdir test 3\n");
530    
531     ospath_free(fp1);
532     ospath_free(fp2);
533     ospath_free(fp3);
534    
535     //---------------------------------
536     M("ALL TESTS PASSED");
537     }
538    
539     /*===========================================================================*/
540     /* Registration information */
541    
542     static CU_TestInfo ospath_test_list[] = {
543     {"test_ospath", test_ospath},
544     CU_TEST_INFO_NULL
545     };
546    
547     static CU_SuiteInfo suites[] = {
548     {"test_general_ospath", NULL, NULL, ospath_test_list},
549     CU_SUITE_INFO_NULL
550     };
551    
552     /*-------------------------------------------------------------------*/
553     CU_ErrorCode test_register_general_ospath(void)
554     {
555     return CU_register_suites(suites);
556     }

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