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

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