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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2335 - (show annotations) (download) (as text)
Sun Dec 26 03:24:37 2010 UTC (12 years, 11 months ago) by jpye
File MIME type: text/x-csrc
File size: 14295 byte(s)
Fixed bug with ospath_isroot.
1 /* 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 by John Pye.
22 */
23
24 #include <ascend/general/ospath.h>
25 #include <CUnit/CUnit.h>
26
27 FilePathTestFn ospath_searchpath_testexists;
28
29 #define NDEBUG
30
31 #ifndef NDEBUG
32 # include <assert.h>
33 # define M(MSG) fprintf(stderr,"%s:%d: (%s) %s\n",__FILE__,__LINE__,__FUNCTION__,MSG);fflush(stderr);fflush(stderr)
34 # define MC(CLR,MSG) fprintf(stderr,"%c[%sm%s:%d: (%s) %s%c[0m\n",27,CLR,__FILE__,__LINE__,__FUNCTION__,MSG,27);fflush(stderr)
35 # define MM(MSG) MC("34",MSG)
36 # define X(VAR) fprintf(stderr,"%s:%d: (%s) %s=%s\n",__FILE__,__LINE__,__FUNCTION__,#VAR,VAR);fflush(stderr)
37 # define C(VAR) fprintf(stderr,"%s:%d: (%s) %s=%c\n",__FILE__,__LINE__,__FUNCTION__,#VAR,VAR);fflush(stderr)
38 # define V(VAR) fprintf(stderr,"%s:%d: (%s) %s=%d\n",__FILE__,__LINE__,__FUNCTION__,#VAR,(VAR));fflush(stderr)
39 # define D(VAR) fprintf(stderr,"%s:%d: (%s) %s=",__FILE__,__LINE__,__FUNCTION__,#VAR);ospath_debug(VAR);fflush(stderr)
40 # define DD(VAR) fprintf(stderr,"\033[34;1m%s:%d: (%s)\033[0m %s=",__FILE__, __LINE__,__FUNCTION__, #VAR);ospath_debug(VAR);fflush(stderr)
41 #else
42 # include <assert.h>
43 # define M(MSG) ((void)0)
44 # define MC(CLR,MSG) ((void)0)
45 # define X(VAR) ((void)0)
46 # define C(VAR) ((void)0)
47 # define V(VAR) ((void)0)
48 # define D(VAR) ((void)0)
49 # define DD(VAR) ((void)0)
50 # define MM(VAR) ((void)0)
51 #endif
52
53 #ifndef MEMUSED
54 # define MEMUSED(N) CU_TEST(ascmeminuse()==(N))
55 #endif
56
57 /**
58 This is a sample searchpath test function. Assumes the 'userdata' is a
59 relative FilePath which is appended to path, and then if it matches
60 the path \GTK\bin\johnpye\extfn, returns true. This is of
61 course a fairly useless test function, so it's just for testing.
62 */
63 int ospath_searchpath_testexists(struct FilePath *path,void *file){
64 struct FilePath *fp, *fp1, *fp2;
65 fp = (struct FilePath *)file;
66 D(fp);
67 fp1 = ospath_concat(path,fp);
68 D(fp1);
69
70 #ifdef WINPATHS
71 fp2 = ospath_new("c:\\GTK\\bin\\johnpye\\extfn");
72 #else
73 fp2 = ospath_new("/GTK/bin/johnpye/extfn");
74 #endif
75
76 char *t=ospath_str(fp1);
77 MC("1",t);
78 FREE(t);
79
80 t=ospath_str(fp2);
81 MC("31;1",t);
82 FREE(t);
83
84 if(ospath_cmp(fp1,fp2)==0){
85 MC("32","MATCH");
86 ospath_free(fp1);
87 ospath_free(fp2);
88 return 1;
89 }
90 MC("31","NO MATCH");
91 ospath_free(fp1);
92 ospath_free(fp2);
93 return 0;
94 }
95
96
97 // switch to boldface for messages in 'main'
98 #undef D
99 #define D DD
100 #undef M
101 #define M MM
102
103 static void test_ospath_getparent(void){
104 struct FilePath *fp1, *fp2, *fp3;
105
106 //------------------------
107
108 fp1 = ospath_new_from_posix("/usr/local/hello/");
109 fp2 = ospath_getparent(fp1);
110 fp3 = ospath_new_from_posix("/usr/local");
111
112 D(fp1);
113 D(fp2);
114 D(fp3);
115 CU_TEST(ospath_cmp(fp2,fp3)==0);
116 if(ospath_cmp(fp2,fp3)==0)M("Passed 'getparent' test\n");
117
118 ospath_free(fp1); ospath_free(fp2); ospath_free(fp3);
119
120 MEMUSED(0);
121 }
122 //------------------------
123
124 static void test_ospath_cleanup(void){
125 struct FilePath *fp1, *fp2;
126
127 fp1 = ospath_new_from_posix("/usr/include/../local");
128 fp2 = ospath_new_from_posix("/usr/local");
129
130 D(fp1);
131 D(fp2);
132 CU_TEST(ospath_cmp(fp1,fp2)==0);
133 M("Passed 'cleanup' test\n");
134
135 ospath_free(fp1); ospath_free(fp2);
136 MEMUSED(0);
137 }
138 //------------------------
139
140 static void test_ospath_newfromposix(void){
141
142 struct FilePath *fp1, *fp2;
143
144 fp1 = ospath_new_from_posix("models/johnpye/extfn/extfntest");
145 D(fp1);
146 fp2 = ospath_new("models\\johnpye\\extfn\\extfntest");
147 D(fp2);
148 D(fp1);
149 CU_TEST(ospath_cmp(fp1,fp2)==0);
150 M("Passed 'new_from_posix' test\n");
151
152 ospath_free(fp1);
153 ospath_free(fp2);
154 MEMUSED(0);
155 }
156 //------------------------
157
158 static void test_ospath_secondcleanup(void){
159 struct FilePath *fp1, *fp2, *fp3;
160 fp1 = ospath_new(".\\src/.\\images\\..\\\\movies\\");
161 fp2 = ospath_new(".\\src\\movies");
162
163 D(fp1);
164 D(fp2);
165
166 CU_TEST(ospath_cmp(fp1,fp2)==0);
167 M("Passed mid-path '..' cleanup test\n");
168
169 ospath_free(fp2);
170
171 fp2 = ospath_new("./src/movies\\kubrick");
172 fp3 = ospath_getparent(fp2);
173
174 D(fp2);
175 D(fp3);
176
177 CU_TEST(ospath_cmp(fp1,fp3)==0);
178 M("Passed 'second cleanup' test\n");
179
180 ospath_free(fp1);
181 ospath_free(fp2);
182 ospath_free(fp3);
183 MEMUSED(0);
184 }
185 //------------------------
186
187 static void test_ospath_append(void){
188
189 struct FilePath *fp2, *fp3, *fp4;
190
191 fp2 = ospath_new("\\home\\john");
192 fp3 = ospath_new("where\\mojo");
193
194 D(fp2);
195 D(fp3);
196
197 ospath_append(fp2,fp3);
198
199 D(fp2);
200
201 fp4 = ospath_new("\\home\\john\\where\\mojo\\");
202
203 D(fp2);
204 CU_TEST(ospath_cmp(fp2,fp4)==0);
205 M("Passed 'append' test\n");
206
207 ospath_free(fp3);
208 ospath_free(fp2);
209 ospath_free(fp4);
210 MEMUSED(0);
211 }
212 //---------------------------
213
214 static void test_ospath_appendupup(void){
215
216 struct FilePath *fp2, *fp3, *fp4;
217
218 fp3 = ospath_new_noclean("../..");
219 D(fp3);
220
221 // test with appending ../.. to an existing path
222 fp2 = ospath_new("\\home\\john");
223 M("ORIGINAL PATH");
224 D(fp2);
225 ospath_append(fp2,fp3);
226 M("AFTER APPENDING ../..");
227 D(fp2);
228
229 M("GETTING ROOT");
230 fp4 = ospath_root(fp2);
231 M("ROOT FOUND:");
232 D(fp4);
233
234 CU_TEST(ospath_cmp(fp2,fp4)==0);
235 M("Passed 'append ../..' test\n");
236
237 ospath_free(fp2);
238 ospath_free(fp3);
239 ospath_free(fp4);
240 MEMUSED(0);
241 }
242 //-------------------------
243
244 static void test_ospath_up(void){
245
246 struct FilePath *fp1, *fp2;
247
248 fp1 = ospath_new("~\\somewhere\\..");
249 fp2 = ospath_new("~/.");
250
251 CU_TEST(ospath_cmp(fp1,fp2)==0);
252
253 D(fp2);
254
255 ospath_free(fp1);
256 ospath_free(fp2);
257
258 fp1 = ospath_new("/usr/local/include");
259 fp2 = ospath_new("/usr/include/../local/include");
260
261 D(fp1);
262 D(fp2);
263
264 CU_TEST(ospath_cmp(fp1,fp2)==0);
265 M("Passed another mid-path '..' test\n");
266
267 ospath_free(fp1);
268 ospath_free(fp2);
269 MEMUSED(0);
270 }
271 //---------------------------
272
273 static void test_ospath_concat(void){
274
275 struct FilePath *fp1, *fp2, *fp3, *fp4;
276
277 fp1 = ospath_new("/home");
278 fp2 = ospath_new("john");
279 fp3 = ospath_concat(fp1, fp2);
280
281 fp4 = ospath_new("/home/john");
282
283 CU_TEST(ospath_cmp(fp3,fp4)==0);
284 M("Passed 'ospath_concat' test\n");
285
286 ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
287
288 fp1 = ospath_new("c:/Program Files");
289 fp2 = ospath_new("GnuWin32\\bin");
290 fp3 = ospath_concat(fp1, fp2);
291
292 fp4 = ospath_new("c:/Program Files/GnuWin32/bin");
293
294 CU_TEST(ospath_cmp(fp3,fp4)==0);
295 M("Passed 'ospath_concat' test\n");
296
297 ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
298 MEMUSED(0);
299 }
300 //---------------------------
301
302 static void test_ospath_concatmixedslash(void){
303
304 struct FilePath *fp1, *fp2, *fp3, *fp4;
305
306 fp1 = ospath_new("c:/Program Files/");
307 fp2 = ospath_new("GnuWin32\\bin");
308 fp3 = ospath_concat(fp1, fp2);
309
310 fp4 = ospath_new("c:/Program Files/GnuWin32/bin");
311
312 CU_TEST(ospath_cmp(fp3,fp4)==0);
313 M("Passed trailing-slash 'ospath_concat' test\n");
314
315 ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
316 MEMUSED(0);
317 }
318 //---------------------------
319
320 static void test_ospath_trailingslash(void){
321
322 struct FilePath *fp1, *fp2, *fp3, *fp4;
323
324 fp1 = ospath_new("c:/Program Files/GnuWin32/bin");
325 fp2 = ospath_new("johnpye/extfn");
326 fp3 = ospath_concat(fp1, fp2);
327
328 fp4 = ospath_new("c:/Program Files/GnuWin32/bin/johnpye/extfn");
329
330 CU_TEST(ospath_cmp(fp3,fp4)==0);
331 M("Passed trailing-slash 'ospath_concat' test\n");
332
333 ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);
334 MEMUSED(0);
335 }
336 //---------------------------
337
338 static void test_ospath_searchpath(void){
339
340 struct FilePath *fp1, *fp2, *fp3;
341 struct FilePath **pp, **p1;// will be returned null-terminated
342 #ifdef WINPATHS
343 char pathtext[]="c:\\Program Files\\GnuWin32\\bin;c:\\GTK\\bin;e:\\ascend\\;..\\..\\pygtk";
344 #else
345 char pathtext[]="\\Program Files\\GnuWin32\\bin:\\GTK\\bin:\\ascend\\:..\\..\\pygtk";
346 #endif
347
348 pp = ospath_searchpath_new(pathtext);
349
350 for(p1=pp; *p1!=NULL; ++p1){
351 D(*p1);
352 }
353
354 #ifdef WINPATHS
355 fp1 = ospath_new("c:\\program files\\GnuWin32\\bin");
356 #else
357 fp1 = ospath_new("\\Program Files\\GnuWin32\\bin");
358 #endif
359
360 CU_TEST(pp[0]!=NULL);
361 CU_TEST(fp1!=NULL);
362
363 D(fp1);
364 D(pp[0]);
365
366 CU_TEST(ospath_cmp(pp[0],fp1)==0);
367
368
369 fp2 = ospath_new_noclean("johnpye/extfn");
370 D(fp2);
371
372 fflush(stderr);
373
374 fp3 = ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);
375
376 CU_TEST(fp3!=NULL);
377 D(fp3);
378 D(pp[1]);
379
380 CU_TEST(ospath_cmp(fp3,pp[1])==0);
381 M("Passed path-search test\n");
382
383 ospath_free(fp1);
384 ospath_free(fp2);
385 ospath_searchpath_free(pp);
386 MEMUSED(0);
387 }
388 //-------------------------------
389
390 static void test_ospath_searchpath2(void){
391
392 struct FilePath *fp2, *fp3;
393 struct FilePath **pp, **p1;// will be returned null-terminated
394 #ifdef WINPATHS
395 char pathtext2[]="c:\\Program Files\\ASCEND\\models";
396 #else
397 char pathtext2[]="/usr/local/ascend/models";
398 #endif
399
400 M("Path-search test 2...");
401
402 pp = ospath_searchpath_new(pathtext2);
403
404 CU_TEST(pp!=NULL);
405
406 for (p1=pp; *p1!=NULL; ++p1){
407 D(*p1);
408 }
409
410 fp2 = ospath_new_noclean("johnpye/extfn/extfntest");
411 D(fp2);
412
413 fp3 = ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);
414 CU_TEST(fp3==NULL);
415
416 M("Passed path-search test 2\n");
417
418 ospath_free(fp2);
419 ospath_free(fp3);
420 ospath_searchpath_free(pp);
421 MEMUSED(0);
422 }
423 //-------------------------------
424
425 static void test_ospath_basefilename(void){
426
427 struct FilePath *fp1;
428 char *s1;
429
430 fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest.a4c");
431 D(fp1);
432 s1 = ospath_getbasefilename(fp1);
433 X(s1);
434 CU_TEST(strcmp(s1,"extfntest.a4c")==0);
435 M("Passed getbasefilename test\n");
436
437 ospath_free(fp1);
438 FREE(s1);
439
440 fp1 = ospath_new("extfntest.a4c");
441 D(fp1);
442 s1 = ospath_getbasefilename(fp1);
443 X(s1);
444 CU_TEST(strcmp(s1,"extfntest.a4c")==0);
445 M("Passed getbasefilename test 2\n");
446
447 ospath_free(fp1);
448 FREE(s1);
449
450 fp1 = ospath_new("/here/is/my/path.dir/");
451 D(fp1);
452 s1 = ospath_getbasefilename(fp1);
453 X(s1);
454 CU_TEST(NULL==s1);
455 M("Passed getbasefilename test 3\n");
456
457 ospath_free(fp1);
458 if(s1)FREE(s1);
459
460 #ifdef WINPATHS
461 fp1 = ospath_new("c:extfntest.a4c");
462 D(fp1);
463 s1 = ospath_getbasefilename(fp1);
464 X(s1);
465 CU_TEST(strcmp(s1,"extfntest.a4c")==0);
466 M("Passed getbasefilename test WINPATHS\n");
467
468 ospath_free(fp1);
469 FREE(s1);
470 #endif
471 MEMUSED(0);
472 }
473 //-------------------------------
474
475 static void test_ospath_getfilestem(void){
476
477 struct FilePath *fp1;
478 char *s1;
479
480 fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest.a4c");
481 D(fp1);
482 s1 = ospath_getfilestem(fp1);
483 X(s1);
484 CU_TEST(strcmp(s1,"extfntest")==0);
485 M("Passed getfilestem test\n");
486
487 ospath_free(fp1);
488 FREE(s1);
489
490 fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest");
491 D(fp1);
492 s1 = ospath_getfilestem(fp1);
493 X(s1);
494 CU_TEST(strcmp(s1,"extfntest")==0);
495 M("Passed getfilestem test 2\n");
496
497 ospath_free(fp1);
498 FREE(s1);
499
500 fp1 = ospath_new("/usr/share/data/ascend/.ascend.ini");
501 D(fp1);
502 s1 = ospath_getfilestem(fp1);
503 X(s1);
504 CU_TEST(strcmp(s1,".ascend")==0);
505 M("Passed getfilestem test 3\n");
506
507 ospath_free(fp1);
508 FREE(s1);
509
510 fp1 = ospath_new("~/.vimrc");
511 D(fp1);
512 s1 = ospath_getfilestem(fp1);
513 X(s1);
514 CU_TEST(strcmp(s1,".vimrc")==0);
515 M("Passed getfilestem test 3\n");
516
517 ospath_free(fp1);
518 FREE(s1);
519
520 fp1 = ospath_new("~/src/ascend-0.9.5-1.jdpipe.src.rpm");
521 D(fp1);
522 s1 = ospath_getfilestem(fp1);
523 X(s1);
524 CU_TEST(strcmp(s1,"ascend-0.9.5-1.jdpipe.src")==0);
525 M("Passed getfilestem test 4\n");
526
527 ospath_free(fp1);
528 FREE(s1);
529
530 fp1 = ospath_new("~/dir1/dir2/");
531 D(fp1);
532 s1 = ospath_getfilestem(fp1);
533 X(s1);
534 CU_TEST(NULL==s1);
535 M("Passed getfilestem test 5\n");
536
537 ospath_free(fp1);
538 if(s1)FREE(s1);
539 MEMUSED(0);
540 }
541 //-------------------------------
542
543 static void test_ospath_getbasefileext(void){
544
545 struct FilePath *fp1;
546 char *s1;
547
548 fp1 = ospath_new("~/src/ascend-0.9.5-1.jdpipe.src.rpm");
549 D(fp1);
550 s1 = ospath_getfileext(fp1);
551 X(s1);
552 CU_TEST(strcmp(s1,".rpm")==0);
553 M("Passed getbasefileext test\n");
554
555 ospath_free(fp1);
556 FREE(s1);
557
558 fp1 = ospath_new("~/.vimrc");
559 D(fp1);
560 s1 = ospath_getfileext(fp1);
561 X(s1);
562 CU_TEST(s1==NULL);
563 M("Passed getbasefileext test 2\n");
564
565 ospath_free(fp1);
566 if(s1)FREE(s1);
567
568 fp1 = ospath_new("./ascend4");
569 D(fp1);
570 s1 = ospath_getfileext(fp1);
571 X(s1);
572 CU_TEST(s1==NULL);
573 M("Passed getbasefileext test 3\n");
574
575 ospath_free(fp1);
576 if(s1)FREE(s1);
577 MEMUSED(0);
578 }
579 //-------------------------------
580
581 static void test_ospath_getdir(void){
582
583 struct FilePath *fp1, *fp2, *fp3;
584
585 fp1 = ospath_new("/home/myfile");
586 fp2 = ospath_getdir(fp1);
587 fp3 = ospath_new("/home");
588 CU_TEST(ospath_cmp(fp2,fp3)==0);
589 M("Passed ospath_getdir test\n");
590
591 ospath_free(fp1);
592 ospath_free(fp2);
593 ospath_free(fp3);
594
595 fp1 = ospath_new("/home/myfile.ext");
596 fp2 = ospath_getdir(fp1);
597 fp3 = ospath_new("/home");
598 CU_TEST(ospath_cmp(fp2,fp3)==0);
599 M("Passed ospath_getdir test 2\n");
600
601 ospath_free(fp1);
602 ospath_free(fp2);
603 ospath_free(fp3);
604
605 fp1 = ospath_new("/home/mydir/");
606 fp2 = ospath_getdir(fp1);
607 fp3 = ospath_new("/home/mydir");
608 CU_TEST(ospath_cmp(fp2,fp3)==0);
609 M("Passed ospath_getdir test 3\n");
610
611 ospath_free(fp1);
612 ospath_free(fp2);
613 ospath_free(fp3);
614 MEMUSED(0);
615 }
616
617 /*===========================================================================*/
618 /* Registration information */
619
620 #define T(N) {#N, test_ospath_##N }
621 static CU_TestInfo ospath_test_list[] = {
622 T(getparent)
623 ,T(cleanup)
624 ,T(newfromposix)
625 ,T(secondcleanup)
626 ,T(append)
627 ,T(appendupup)
628 ,T(up)
629 ,T(concat)
630 ,T(concatmixedslash)
631 ,T(trailingslash)
632 ,T(searchpath)
633 ,T(searchpath2)
634 ,T(basefilename)
635 ,T(getfilestem)
636 ,T(getbasefileext)
637 ,T(getdir)
638 ,CU_TEST_INFO_NULL
639 };
640 #undef T
641
642 static CU_SuiteInfo suites[] = {
643 {"general_ospath", NULL, NULL, ospath_test_list},
644 CU_SUITE_INFO_NULL
645 };
646
647 /*-------------------------------------------------------------------*/
648 CU_ErrorCode test_register_general_ospath(void)
649 {
650 return CU_register_suites(suites);
651 }

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