Bump patch version post tag.
[asdcplib.git] / src / path-test.cpp
1 /*
2 Copyright (c) 2004-2009, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    path-test.cpp
28   \version $Id$
29   \brief     test harness for path manglers defined in KM_fileio.h
30 */
31
32
33 #include <KM_fileio.h>
34 #include <iostream>
35
36 using namespace std;
37 using namespace Kumu;
38
39
40 //
41 int
42 main(int argc, const char** argv)
43 {
44
45   string Path_1 = "path-test.cpp";
46   assert(PathExists(Path_1));
47   assert(PathIsFile(Path_1));
48   assert(!PathIsDirectory(Path_1));
49
50   string Path_2 = ".";
51   assert(PathExists(Path_2));
52   assert(!PathIsFile(Path_2));
53   assert(PathIsDirectory(Path_2));
54   
55   string Path_3 = "/foo/bar/baz.buz"; // must have 3 elements
56   PathCompList_t PathList_3;
57   PathToComponents(Path_3, PathList_3);
58
59   assert(PathList_3.size() == 3);
60
61   string Path_4 = ComponentsToPath(PathList_3);
62   string Path_5 = PathMakeAbsolute(Path_4);
63
64   fprintf(stderr, "PathMakeAbsolute in: %s\n", Path_4.c_str());
65   fprintf(stderr, "PathMakeAbsolute out: %s\n", Path_5.c_str());
66
67   string Path_6 = ComponentsToAbsolutePath(PathList_3);
68   assert(Path_3 == Path_6);
69   assert(PathsAreEquivalent(Path_3, Path_6));
70   assert(!PathsAreEquivalent(Path_3, Path_4));
71
72   assert(!PathHasComponents(PathList_3.back()));
73   assert(PathHasComponents(Path_3));
74
75   assert(!PathIsAbsolute(Path_4));
76   assert(PathIsAbsolute(Path_5));
77   assert(PathMakeLocal(Path_3, "/foo") == "bar/baz.buz");
78
79   assert(PathsAreEquivalent("/foo/bar/baz", "/foo/bar/./baz"));
80   assert(PathsAreEquivalent("/foo/baz", "/foo/bar/../baz"));
81
82   assert(PathBasename(Path_3) == "baz.buz");
83   assert(PathDirname(Path_3) == "/foo/bar");
84   assert(PathDirname("/foo") == "/");
85
86   assert(PathGetExtension(Path_3) == "buz");
87   assert(PathGetExtension("foo") == "");
88   assert(PathSetExtension("foo.bar", "") == "foo");
89   assert(PathSetExtension(Path_3, "xml") == "baz.xml");
90
91   string Path_7 = "//tmp///////fooo";
92
93   PathCompList_t PathList_7;
94   PathToComponents(Path_7, PathList_7);
95   for ( PathCompList_t::const_iterator i = PathList_7.begin(); i != PathList_7.end(); i++ )
96     fprintf(stderr, "xx: \"%s\"\n", i->c_str());
97   assert(PathsAreEquivalent(PathMakeLocal(PathMakeCanonical(Path_7), "/tmp"), "fooo"));
98
99   string Path_8 = "tmp/foo/bar/ack";
100   CreateDirectoriesInPath(Path_8);
101   assert(PathExists(Path_8));
102   DeletePath(Path_8);
103   assert(!PathExists(Path_8));
104
105   PathList_t InList, OutList;
106   InList.push_back("tmp");
107   InList.push_back("Darwin");
108   InList.push_back(".");
109
110   cerr << "----------------------------------" << endl;
111   FindInPaths(PathMatchAny(), InList, OutList);
112   PathList_t::iterator pi;
113
114   if ( false )
115     {
116       for ( pi = OutList.begin(); pi != OutList.end(); pi++ )
117         cerr << *pi << endl;
118     }
119   else
120     {
121       cerr << OutList.size() << ( ( OutList.size() == 1 ) ? " file" : " files" ) << endl;
122     }
123
124   cerr << "----------------------------------" << endl;
125   OutList.clear();
126   FindInPaths(PathMatchRegex("^[A-J].*\\.h$"), InList, OutList);
127
128   for ( pi = OutList.begin(); pi != OutList.end(); pi++ )
129     cerr << *pi << endl;
130
131   cerr << "----------------------------------" << endl;
132   OutList.clear();
133   FindInPaths(PathMatchGlob("*.h"), InList, OutList);
134
135   for ( pi = OutList.begin(); pi != OutList.end(); pi++ )
136     cerr << *pi << endl;
137
138   cerr << "----------------------------------" << endl;
139
140   fsize_t free_space, total_space;
141   FreeSpaceForPath("/", free_space, total_space);
142   cerr << "Free space: " << free_space << endl;
143   cerr << "Total space: " << total_space << endl;
144   cerr << "Used space: " << ( (total_space - free_space ) / float(total_space) ) << endl;
145
146   cerr << "OK" << endl;
147
148   return 0;
149 }
150
151 //
152 // end path-test.cpp
153 //