2 Copyright (c) 2004-2008, 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);
63 string Path_6 = ComponentsToAbsolutePath(PathList_3);
64 assert(Path_3 == Path_6);
65 assert(PathsAreEquivalent(Path_3, Path_6));
66 assert(!PathsAreEquivalent(Path_3, Path_4));
68 assert(!PathHasComponents(PathList_3.back()));
69 assert(PathHasComponents(Path_3));
71 assert(!PathIsAbsolute(Path_4));
72 assert(PathIsAbsolute(Path_5));
73 assert(PathMakeLocal(Path_3, "/foo") == "bar/baz.buz");
75 assert(PathsAreEquivalent("/foo/bar/baz", "/foo/bar/./baz"));
76 assert(PathsAreEquivalent("/foo/baz", "/foo/bar/../baz"));
78 assert(PathBasename(Path_3) == "baz.buz");
79 assert(PathDirname(Path_3) == "/foo/bar");
80 assert(PathDirname("/foo") == "/");
82 assert(PathGetExtension(Path_3) == "buz");
83 assert(PathGetExtension("foo") == "");
84 assert(PathSetExtension("foo.bar", "") == "foo");
85 assert(PathSetExtension(Path_3, "xml") == "baz.xml");
87 string Path_7 = "//tmp///////fooo";
89 PathCompList_t PathList_7;
90 PathToComponents(Path_7, PathList_7);
91 for ( PathCompList_t::const_iterator i = PathList_7.begin(); i != PathList_7.end(); i++ )
92 fprintf(stderr, "xx: \"%s\"\n", i->c_str());
93 assert(PathsAreEquivalent(PathMakeLocal(PathMakeCanonical(Path_7), "/tmp"), "fooo"));
95 string Path_8 = "tmp/foo/bar/ack";
96 CreateDirectoriesInPath(Path_8);
97 assert(PathExists(Path_8));
99 assert(!PathExists(Path_8));
101 PathList_t InList, OutList;
102 InList.push_back("tmp");
103 InList.push_back("Darwin");
104 InList.push_back(".");
106 cerr << "----------------------------------" << endl;
107 FindInPaths(PathMatchAny(), InList, OutList);
108 PathList_t::iterator pi;
110 for ( pi = OutList.begin(); pi != OutList.end(); pi++ )
113 cerr << "----------------------------------" << endl;
115 FindInPaths(PathMatchRegex("^[A-J].*\\.h$"), InList, OutList);
117 for ( pi = OutList.begin(); pi != OutList.end(); pi++ )
120 cerr << "----------------------------------" << endl;
122 FindInPaths(PathMatchGlob("*.h"), InList, OutList);
124 for ( pi = OutList.begin(); pi != OutList.end(); pi++ )
127 cerr << "----------------------------------" << endl;
129 fsize_t free_space, total_space;
130 FreeSpaceForPath("/", free_space, total_space);
131 cerr << "Free space: " << free_space << endl;
132 cerr << "Total space: " << total_space << endl;
134 cerr << "OK" << endl;