2 Copyright (c) 2004-2009, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
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.
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.
27 /*! \file path-test.cpp
29 \brief test harness for path manglers defined in KM_fileio.h
33 #include <KM_fileio.h>
42 main(int argc, const char** argv)
45 string Path_1 = "path-test.cpp";
46 assert(PathExists(Path_1));
47 assert(PathIsFile(Path_1));
48 assert(!PathIsDirectory(Path_1));
51 assert(PathExists(Path_2));
52 assert(!PathIsFile(Path_2));
53 assert(PathIsDirectory(Path_2));
55 string Path_3 = "/foo/bar/baz.buz"; // must have 3 elements
56 PathCompList_t PathList_3;
57 PathToComponents(Path_3, PathList_3);
59 assert(PathList_3.size() == 3);
61 string Path_4 = ComponentsToPath(PathList_3);
62 string Path_5 = PathMakeAbsolute(Path_4);
64 fprintf(stderr, "PathMakeAbsolute in: %s\n", Path_4.c_str());
65 fprintf(stderr, "PathMakeAbsolute out: %s\n", Path_5.c_str());
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));
72 assert(!PathHasComponents(PathList_3.back()));
73 assert(PathHasComponents(Path_3));
75 assert(!PathIsAbsolute(Path_4));
76 assert(PathIsAbsolute(Path_5));
77 assert(PathMakeLocal(Path_3, "/foo") == "bar/baz.buz");
79 assert(PathsAreEquivalent("/foo/bar/baz", "/foo/bar/./baz"));
80 assert(PathsAreEquivalent("/foo/baz", "/foo/bar/../baz"));
82 assert(PathBasename(Path_3) == "baz.buz");
83 assert(PathDirname(Path_3) == "/foo/bar");
84 assert(PathDirname("/foo") == "/");
86 assert(PathGetExtension(Path_3) == "buz");
87 assert(PathGetExtension("foo") == "");
88 assert(PathSetExtension("foo.bar", "") == "foo");
89 assert(PathSetExtension(Path_3, "xml") == "baz.xml");
91 string Path_7 = "//tmp///////fooo";
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"));
99 string Path_8 = "tmp/foo/bar/ack";
100 CreateDirectoriesInPath(Path_8);
101 assert(PathExists(Path_8));
103 assert(!PathExists(Path_8));
105 PathList_t InList, OutList;
106 InList.push_back("tmp");
107 InList.push_back("Darwin");
108 InList.push_back(".");
110 cerr << "----------------------------------" << endl;
111 FindInPaths(PathMatchAny(), InList, OutList);
112 PathList_t::iterator pi;
114 for ( pi = OutList.begin(); pi != OutList.end(); pi++ )
117 cerr << "----------------------------------" << endl;
119 FindInPaths(PathMatchRegex("^[A-J].*\\.h$"), InList, OutList);
121 for ( pi = OutList.begin(); pi != OutList.end(); pi++ )
124 cerr << "----------------------------------" << endl;
126 FindInPaths(PathMatchGlob("*.h"), InList, OutList);
128 for ( pi = OutList.begin(); pi != OutList.end(); pi++ )
131 cerr << "----------------------------------" << endl;
133 fsize_t free_space, total_space;
134 FreeSpaceForPath("/", free_space, total_space);
135 cerr << "Free space: " << free_space << endl;
136 cerr << "Total space: " << total_space << endl;
138 cerr << "OK" << endl;