From b6f166c4ed98f94bdd2cc82885d61173a101abfd Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 26 Jul 2016 15:12:54 +0200 Subject: Redesign path to store trailing slash for directories --- tests/link/driver.cxx | 12 +- tests/path/driver.cxx | 375 ++++++++++++++++++++++++++++++++++------------- tests/process/driver.cxx | 2 +- 3 files changed, 282 insertions(+), 107 deletions(-) (limited to 'tests') diff --git a/tests/link/driver.cxx b/tests/link/driver.cxx index a32a8dc..4a4a00f 100644 --- a/tests/link/driver.cxx +++ b/tests/link/driver.cxx @@ -43,8 +43,10 @@ link_file (const path& target, const path& link, bool hard, bool check_content) #ifndef _WIN32 static bool -link_dir ( - const dir_path& target, const dir_path& link, bool hard, bool check_content) +link_dir (const dir_path& target, + const dir_path& link, + bool hard, + bool check_content) { try { @@ -53,8 +55,9 @@ link_dir ( else mksymlink (target, link); } - catch (const system_error&) + catch (const system_error& e) { + //cerr << e.what () << endl; return false; } @@ -62,6 +65,7 @@ link_dir ( return true; dir_path tp (target.absolute () ? target : link.directory () / target); + set> te; for (const dir_entry& de: dir_iterator (tp)) te.emplace (de.ltype (), de.path ()); @@ -112,7 +116,7 @@ main () // Create the file symlink using an unexistent file path. // - assert (link_file (fp / path ("a"), td / path ("sa"), false, false)); + assert (link_file (fp + "-a", td / path ("sa"), false, false)); // Prepare the target directory. // diff --git a/tests/path/driver.cxx b/tests/path/driver.cxx index ad76940..7ca36b7 100644 --- a/tests/path/driver.cxx +++ b/tests/path/driver.cxx @@ -30,14 +30,43 @@ main () static_assert (is_nothrow_move_constructible::value, ""); #endif - assert (path ("/").string () == "/"); - assert (path ("//").string () == "/"); - assert (path ("/tmp/foo/").string () == "/tmp/foo"); + auto test = [] (const char* p, const char* s, const char* r) + { + path x (p); + return x.string () == s && x.representation () == r; + }; + + auto dir_test = [] (const char* p, const char* s, const char* r) + { + dir_path x (p); + return x.string () == s && x.representation () == r; + }; + + assert (test ("/", "/", "/")); + assert (test ("//", "/", "/")); + assert (test ("/tmp/foo", "/tmp/foo", "/tmp/foo")); + assert (test ("/tmp/foo/", "/tmp/foo", "/tmp/foo/")); + assert (test ("/tmp/foo//", "/tmp/foo", "/tmp/foo/")); + #ifdef _WIN32 - assert (path ("/\\").string () == "/"); - assert (path ("C:").string () == "C:"); - assert (path ("C:\\").string () == "C:"); - assert (path ("C:\\tmp\\foo\\").string () == "C:\\tmp\\foo"); + assert (test ("/\\", "/", "/")); + assert (test ("C:", "C:", "C:")); + assert (test ("C:\\", "C:", "C:\\")); + assert (test ("c:/", "c:", "c:/")); + assert (test ("C:\\tmp\\foo\\", "C:\\tmp\\foo", "C:\\tmp\\foo\\")); + assert (test ("C:\\tmp\\foo\\/\\", "C:\\tmp\\foo", "C:\\tmp\\foo\\")); +#endif + + assert (dir_test ("/", "/", "/")); + assert (dir_test ("/tmp/foo/", "/tmp/foo", "/tmp/foo/")); +#ifndef _WIN32 + assert (dir_test ("tmp/foo", "tmp/foo", "tmp/foo/")); +#else + assert (dir_test ("tmp\\foo", "tmp\\foo", "tmp\\foo\\")); + + assert (dir_test ("C:\\", "C:", "C:\\")); + assert (dir_test ("C:\\tmp/foo\\", "C:\\tmp/foo", "C:\\tmp/foo\\")); + assert (dir_test ("c:/tmp\\foo", "c:/tmp\\foo", "c:/tmp\\foo\\")); #endif // absolute/relative/root @@ -45,51 +74,73 @@ main () #ifndef _WIN32 assert (path ("/").root ()); assert (path ("//").root ()); + assert (!path ("/foo").root ()); assert (path ("/").absolute ()); assert (path ("/foo/bar").absolute ()); assert (path ("bar/baz").relative ()); + + assert (path ("/").root_directory ().representation () == "/"); + assert (path ("/bar/baz").root_directory ().representation () == "/"); #else assert (path ("C:").root ()); assert (path ("C:\\").root ()); + assert (!path ("C:\\foo").root ()); + assert (path ("C:").absolute ()); assert (path ("C:\\").absolute ()); assert (path ("C:\\foo\\bar").absolute ()); assert (path ("bar\\baz").relative ()); + + assert (path ("C:").root_directory ().representation () == "C:\\"); + assert (path ("c:/").root_directory ().representation () == "c:/"); + assert (path ("C:\\bar\\baz").root_directory ().representation () == "C:\\"); #endif // leaf // + assert (path ().leaf ().empty ()); #ifndef _WIN32 - assert (path ("/").leaf ().string () == ""); - assert (path ("/tmp").leaf ().string () == "tmp"); - assert (path ("//tmp").leaf ().string () == "tmp"); + assert (path ("/").leaf ().representation () == "/"); + assert (path ("/tmp").leaf ().representation () == "tmp"); + assert (path ("/tmp/").leaf ().representation () == "tmp/"); + assert (path ("//tmp").leaf ().representation () == "tmp"); #else - assert (path ("C:").leaf ().string () == "C:"); - assert (path ("C:\\tmp").leaf ().string () == "tmp"); - assert (path ("C:\\\\tmp").leaf ().string () == "tmp"); + assert (path ("C:\\").leaf ().representation () == "C:\\"); + assert (path ("C:\\tmp").leaf ().representation () == "tmp"); + assert (path ("C:\\tmp\\").leaf ().representation () == "tmp\\"); + assert (path ("C:\\tmp/").leaf ().representation () == "tmp/"); + assert (path ("C:\\\\tmp").leaf ().representation () == "tmp"); #endif // directory // + assert (path ().directory ().empty ()); #ifndef _WIN32 - assert (path ("/").directory ().string () == ""); - assert (path ("/tmp").directory ().string () == "/"); - assert (path ("//tmp").directory ().string () == "/"); + assert (path ("/").directory ().representation () == ""); + assert (path ("/tmp").directory ().representation () == "/"); + assert (path ("/tmp/").directory ().representation () == "/"); + assert (path ("//tmp").directory ().representation () == "//"); + assert (path ("/tmp/foo").directory ().representation () == "/tmp/"); + assert (path ("/tmp/foo/").directory ().representation () == "/tmp/"); #else - assert (path ("C:").directory ().string () == ""); - assert (path ("C:\\tmp").directory ().string () == "C:"); - assert (path ("C:\\\\tmp").directory ().string () == "C:"); + assert (path ("C:").directory ().representation () == ""); + assert (path ("C:\\tmp").directory ().representation () == "C:\\"); + assert (path ("C:\\\\tmp").directory ().representation () == "C:\\\\"); + assert (path ("C:\\tmp\\foo").directory ().representation () == "C:\\tmp\\"); + assert (path ("C:\\tmp/foo\\").directory ().representation () == "C:\\tmp/"); #endif // base // - assert (path ("/").base ().string () == "/"); - assert (path ("/foo.txt").base ().string () == "/foo"); - assert (path (".txt").base ().string () == ".txt"); - assert (path ("/.txt").base ().string () == "/.txt"); - assert (path ("foo.txt.orig").base ().string () == "foo.txt"); + assert (path ("/").base ().representation () == "/"); + assert (path ("/foo.txt").base ().representation () == "/foo"); + assert (path ("/foo.txt/").base ().representation () == "/foo/"); + assert (path (".txt").base ().representation () == ".txt"); + assert (path ("/.txt").base ().representation () == "/.txt"); + assert (path ("foo.txt.orig").base ().representation () == "foo.txt"); #ifdef _WIN32 - assert (path ("C:").base ().string () == "C:"); - assert (path ("C:\\foo.txt").base ().string () == "C:\\foo"); + assert (path ("C:").base ().representation () == "C:"); + assert (path ("C:\\foo.txt").base ().representation () == "C:\\foo"); + assert (path ("C:\\foo.txt\\").base ().representation () == "C:\\foo\\"); #endif // iteration @@ -117,8 +168,15 @@ main () { path p ("foo/bar"); path::iterator i (p.begin ()); - assert (i != p.end () && *i == "foo"); - assert (++i != p.end () && *i == "bar"); + assert (i != p.end () && *i == "foo" && i.separator () == '/'); + assert (++i != p.end () && *i == "bar" && i.separator () == '\0'); + assert (++i == p.end ()); + } + { + path p ("foo/bar/"); + path::iterator i (p.begin ()); + assert (i != p.end () && *i == "foo" && i.separator () == '/'); + assert (++i != p.end () && *i == "bar" && i.separator () == '/'); assert (++i == p.end ()); } { @@ -148,7 +206,7 @@ main () { path p ("/"); path::iterator i (p.begin ()); - assert (i != p.end () && *i == ""); + assert (i != p.end () && *i == "" && i.separator () == '/'); assert (++i == p.end ()); } { @@ -162,87 +220,128 @@ main () // iterator range construction // { - path p; - assert (path (p.begin (), p.end ()) == p); - } - { - path p ("foo"); - assert (path (p.begin (), p.end ()) == p); - assert (path (++p.begin (), p.end ()) == path ()); - } - { - path p ("foo/bar"); - assert (path (p.begin (), p.end ()) == p); - assert (path (++p.begin (), p.end ()) == path ("bar")); - assert (path (p.begin (), ++p.begin ()) == path ("foo")); - } - { - path p ("/foo/bar"); - assert (path (p.begin (), p.end ()) == p); - assert (path (++p.begin (), p.end ()) == path ("foo/bar")); - assert (path (++(++p.begin ()), p.end ()) == path ("bar")); + auto test = [] (const path::iterator& b, const path::iterator& e) + { + return path (b, e).representation (); + }; + + { + path p; + assert (test (p.begin (), p.end ()) == ""); + } + { + path p ("foo"); + assert (test (p.begin (), p.end ()) == "foo"); + assert (test (++p.begin (), p.end ()) == ""); + } + { + path p ("foo/"); + assert (test (p.begin (), p.end ()) == "foo/"); + } + { + path p ("foo/bar"); + assert (test (p.begin (), p.end ()) == "foo/bar"); + assert (test (++p.begin (), p.end ()) == "bar"); + assert (test (p.begin (), ++p.begin ()) == "foo/"); + } + { + path p ("/foo/bar"); + assert (test (p.begin (), p.end ()) == "/foo/bar"); + assert (test (++p.begin (), p.end ()) == "foo/bar"); + assert (test (++(++p.begin ()), p.end ()) == "bar"); #ifndef _WIN32 - assert (path (p.begin (), ++p.begin ()) == path ("/")); + assert (test (p.begin (), ++p.begin ()) == "/"); #endif - assert (path (++p.begin (), ++(++p.begin ())) == path ("foo")); - assert (path (++(++p.begin ()), ++(++(++p.begin ()))) == path ("bar")); - } + assert (test (++p.begin (), ++(++p.begin ())) == "foo/"); + assert (test (++(++p.begin ()), ++(++(++p.begin ()))) == "bar"); + } + { + path p ("/foo/bar/"); + assert (test (p.begin (), p.end ()) == "/foo/bar/"); + assert (test (++p.begin (), p.end ()) == "foo/bar/"); + assert (test (++(++p.begin ()), p.end ()) == "bar/"); + #ifndef _WIN32 - { - path p ("/"); - assert (path (p.begin (), p.end ()) == p); - assert (path (++p.begin (), p.end ()) == path ()); - } + assert (test (p.begin (), ++p.begin ()) == "/"); #endif + assert (test (++p.begin (), ++(++p.begin ())) == "foo/"); + assert (test (++(++p.begin ()), ++(++(++p.begin ()))) == "bar/"); + } +#ifndef _WIN32 + { + path p ("/"); + assert (test (p.begin (), p.end ()) == "/"); + assert (test (++p.begin (), p.end ()) == ""); + } +#endif + } + // operator/ // #ifndef _WIN32 - assert ((path ("/") / path ("tmp")).string () == "/tmp"); - assert ((path ("foo") / path ("bar")).string () == "foo/bar"); + assert ((path ("/") / path ("tmp")).representation () == "/tmp"); + assert ((path ("foo/") / path ("bar")).representation () == "foo/bar"); + assert ((path ("foo/") / path ("bar/")).representation () == "foo/bar/"); + assert ((path ("foo/") / path ()).representation () == "foo/"); #else - assert ((path ("\\") / path ("tmp")).string () == "\\tmp"); - assert ((path ("C:\\") / path ("tmp")).string () == "C:\\tmp"); - assert ((path ("foo") / path ("bar")).string () == "foo\\bar"); + assert ((path ("\\") / path ("tmp")).representation () == "\\tmp"); + assert ((path ("C:\\") / path ("tmp")).representation () == "C:\\tmp"); + assert ((path ("foo\\") / path ("bar")).representation () == "foo\\bar"); + assert ((path ("foo\\") / path ("bar\\")).representation () == "foo\\bar\\"); + assert ((path ("foo\\") / path ("bar/")).representation () == "foo\\bar/"); + assert ((path ("foo/") / path ("bar")).representation () == "foo/bar"); + assert ((path ("foo\\") / path ()).representation () == "foo\\"); #endif // normalize // #ifndef _WIN32 - assert (path ("../foo").normalize ().string () == "../foo"); - assert (path ("..///foo").normalize ().string () == "../foo"); - assert (path ("../../foo").normalize ().string () == "../../foo"); - assert (path (".././foo").normalize ().string () == "../foo"); - assert (path (".").normalize ().string () == ""); - assert (path ("./..").normalize ().string () == ".."); - assert (path ("../.").normalize ().string () == ".."); - assert (path ("foo/./..").normalize ().string () == ""); - assert (path ("/foo/./..").normalize ().string () == "/"); - assert (path ("./foo").normalize ().string () == "foo"); + assert (path ("../foo").normalize ().representation () == "../foo"); + assert (path ("..///foo").normalize ().representation () == "../foo"); + assert (path ("../../foo").normalize ().representation () == "../../foo"); + assert (path (".././foo").normalize ().representation () == "../foo"); + assert (path (".").normalize ().representation () == ""); + assert (path ("././").normalize ().representation () == ""); + assert (path ("./..").normalize ().representation () == "../"); + assert (path ("./../").normalize ().representation () == "../"); + assert (path ("../.").normalize ().representation () == "../"); + assert (path (".././").normalize ().representation () == "../"); + assert (path ("foo/./..").normalize ().representation () == ""); + assert (path ("/foo/./..").normalize ().representation () == "/"); + assert (path ("/foo/./../").normalize ().representation () == "/"); + assert (path ("./foo").normalize ().representation () == "foo"); + assert (path ("./foo/").normalize ().representation () == "foo/"); #else - assert (path ("../foo").normalize ().string () == "..\\foo"); - assert (path ("..///foo").normalize ().string () == "..\\foo"); - assert (path ("..\\../foo").normalize ().string () == "..\\..\\foo"); - assert (path (".././foo").normalize ().string () == "..\\foo"); - assert (path (".").normalize ().string () == ""); - assert (path ("./..").normalize ().string () == ".."); - assert (path ("../.").normalize ().string () == ".."); - assert (path ("foo/./..").normalize ().string () == ""); - assert (path ("C:/foo/./..").normalize ().string () == "C:"); - assert (path ("./foo").normalize ().string () == "foo"); - - assert (path ("C:").normalize ().string () == "C:"); - assert (path ("C:\\Foo12//Bar").normalize ().string () == "C:\\Foo12\\Bar"); + assert (path ("../foo").normalize ().representation () == "..\\foo"); + assert (path ("..///foo").normalize ().representation () == "..\\foo"); + assert (path ("..\\../foo").normalize ().representation () == "..\\..\\foo"); + assert (path (".././foo").normalize ().representation () == "..\\foo"); + assert (path (".").normalize ().representation () == ""); + assert (path (".\\.\\").normalize ().representation () == ""); + assert (path ("./..").normalize ().representation () == "..\\"); + assert (path ("../.").normalize ().representation () == "..\\"); + assert (path ("foo/./..").normalize ().representation () == ""); + assert (path ("C:/foo/./..").normalize ().representation () == "C:\\"); + assert (path ("C:/foo/./../").normalize ().representation () == "C:\\"); + assert (path ("./foo").normalize ().representation () == "foo"); + assert (path ("./foo\\").normalize ().representation () == "foo\\"); + + assert (path ("C:\\").normalize ().representation () == "C:\\"); + assert (path ("C:\\Foo12//Bar").normalize ().representation () == "C:\\Foo12\\Bar"); #endif // comparison // + assert (path ("/") == path ("/")); assert (path ("./foo") == path ("./foo")); + assert (path ("./foo/") == path ("./foo")); assert (path ("./boo") < path ("./foo")); #ifdef _WIN32 assert (path (".\\foo") == path ("./FoO")); + assert (path (".\\foo") == path ("./foo\\")); assert (path (".\\boo") < path (".\\Foo")); #endif @@ -261,29 +360,101 @@ main () // sub // - assert (path ("foo").sub (path ("foo"))); - assert (path ("foo/bar").sub (path ("foo/bar"))); - assert (path ("foo/bar").sub (path ("foo"))); - assert (!path ("foo/bar").sub (path ("bar"))); - assert (path ("/foo/bar").sub (path ("/foo"))); - assert (path ("/foo/bar/baz").sub (path ("/foo/bar"))); - assert (!path ("/foo/bar/baz").sub (path ("/foo/baz"))); + { + auto test = [] (const char* p, const char* pfx) + { + return path (p).sub (path (pfx)); + }; + + assert (test ("foo", "foo")); + assert (test ("foo/bar", "foo/bar")); + assert (test ("foo/bar", "foo")); + assert (test ("foo/bar", "foo/")); + assert (!test ("foo/bar", "bar")); + assert (!test ("/foo-bar", "/foo")); + assert (test ("/foo/bar", "/foo")); + assert (test ("/foo/bar/baz", "/foo/bar")); + assert (!test ("/foo/bar/baz", "/foo/baz")); #ifdef _WIN32 - assert (path ("c:").sub (path ("c:"))); - assert (!path ("c:").sub (path ("d:"))); - assert (path ("c:\\foo").sub (path ("c:"))); + assert (test ("c:", "c:")); + assert (test ("c:", "c:\\")); + assert (!test ("c:", "d:")); + assert (test ("c:\\foo", "c:")); + assert (test ("c:\\foo", "c:\\")); #else - assert (path ("/foo/bar/baz").sub (path ("/"))); + assert (test ("/", "/")); + assert (test ("/foo/bar/baz", "/")); #endif + } + + // sup + // + { + auto test = [] (const char* p, const char* sfx) + { + return path (p).sup (path (sfx)); + }; + + assert (test ("foo", "foo")); + assert (test ("foo/bar", "foo/bar")); + assert (test ("foo/bar", "bar")); + assert (test ("foo/bar/", "bar/")); + assert (!test ("foo/bar", "foo")); + assert (!test ("/foo-bar", "bar")); + assert (test ("/foo/bar", "bar")); + assert (test ("/foo/bar/baz", "bar/baz")); + assert (!test ("/foo/bar/baz", "bar")); + +#ifdef _WIN32 + assert (test ("c:", "c:")); + assert (test ("c:\\", "c:")); + assert (!test ("d:", "c:")); + assert (test ("c:\\foo", "foo")); + assert (test ("c:\\foo\\", "foo\\")); +#else + assert (test ("/", "/")); +#endif + } + + // leaf(path) + // + { + auto test = [] (const char* p, const char* d) + { + return path (p).leaf (path (d)).representation (); + }; + + assert (test ("/foo", "/") == "foo"); + //assert (test ("foo/bar", "foo") == "bar"); + assert (test ("foo/bar", "foo/") == "bar"); + assert (test ("foo/bar/", "foo/") == "bar/"); + assert (test ("/foo/bar", "/foo/") == "bar"); + } + + // directory(path) + // + { + auto test = [] (const char* p, const char* l) + { + return path (p).directory (path (l)).representation (); + }; + + assert (test ("/foo", "foo") == "/"); + assert (test ("foo/bar", "bar") == "foo/"); + assert (test ("foo/bar/", "bar/") == "foo/"); + assert (test ("foo/bar/", "bar") == "foo/"); + assert (test ("foo/bar/baz", "bar/baz") == "foo/"); + assert (test ("/foo/bar/baz", "bar/baz") == "/foo/"); + } // relative // - assert (path ("foo").relative (path ("foo")) == path ()); - assert (path ("foo/bar").relative (path ("foo/bar")) == path ()); - assert (path ("foo/bar/baz").relative (path ("foo/bar")) == path ("baz")); + assert (path ("foo/").relative (path ("foo/")) == path ()); + assert (path ("foo/bar/").relative (path ("foo/bar/")) == path ()); + assert (path ("foo/bar/baz").relative (path ("foo/bar/")) == path ("baz")); assert (path ("foo/bar/baz").relative (path ("foo/bar/buz")). posix_string () == "../baz"); - assert (path ("foo/bar/baz").relative (path ("foo/biz/baz")). + assert (path ("foo/bar/baz").relative (path ("foo/biz/baz/")). posix_string () == "../../bar/baz"); assert (path ("foo/bar/baz").relative (path ("fox/bar/baz")). posix_string () == "../../../foo/bar/baz"); @@ -302,10 +473,10 @@ main () #endif assert (path::temp_directory ().absolute ()); - assert (wpath::temp_directory ().absolute ()); + //assert (wpath::temp_directory ().absolute ()); assert (path::home ().absolute ()); - assert (wpath::home ().absolute ()); + //assert (wpath::home ().absolute ()); /* path p ("../foo"); diff --git a/tests/process/driver.cxx b/tests/process/driver.cxx index 5c43fb5..18b4fd8 100644 --- a/tests/process/driver.cxx +++ b/tests/process/driver.cxx @@ -287,7 +287,7 @@ main (int argc, const char* argv[]) // dir_path::current (fp.directory ()); - assert (exec (path (".") / fp.leaf ())); + assert (exec (dir_path (".") / fp.leaf ())); // Fail for unexistent file path. // -- cgit v1.1