X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=tests%2Ffsutils.cpp;fp=tests%2Ffsutils.cpp;h=a17b5f5a2f5448f2489a891d9f708462a4520207;hp=0000000000000000000000000000000000000000;hb=844da6227b2b703a2180eae759d02809c6036cce;hpb=c4bebb877ec98d518bf02152ca81930e18eda6a7 diff --git a/tests/fsutils.cpp b/tests/fsutils.cpp new file mode 100644 index 0000000..a17b5f5 --- /dev/null +++ b/tests/fsutils.cpp @@ -0,0 +1,80 @@ +#include +#include + +using namespace std; +using namespace Msp; + +class FSUtilTests: public Test::RegisteredTest +{ +public: + FSUtilTests(); + + static const char *get_name() { return "fsutils"; } + +private: + void pathsplit(); + void fnsplit(); + void hierarchy(); + void bad_hierarchy(); +}; + +FSUtilTests::FSUtilTests() +{ + add(&FSUtilTests::pathsplit, "Path splitting"); + add(&FSUtilTests::fnsplit, "Filename splitting"); + add(&FSUtilTests::hierarchy, "Hierarchy"); + add(&FSUtilTests::bad_hierarchy, "Bad hierarchy").expect_throw(); +} + +void FSUtilTests::pathsplit() +{ + FS::Path path("/foo/bar/baz"); + FS::Path dir = FS::dirname(path); + EXPECT_EQUAL(dir.str(), "/foo/bar"); + string base = FS::basename(path); + EXPECT_EQUAL(base, "baz"); +} + +void FSUtilTests::fnsplit() +{ + string filename = "file.png"; + string base = FS::basepart(filename); + EXPECT_EQUAL(base, "file"); + string ext = FS::extpart(filename); + EXPECT_EQUAL(ext, ".png"); + + filename = "file.tar.gz"; + base = FS::basepart(filename); + EXPECT_EQUAL(base, "file.tar"); + ext = FS::extpart(filename); + EXPECT_EQUAL(ext, ".gz"); +} + +void FSUtilTests::hierarchy() +{ + FS::Path path1 = "/foo/bar"; + FS::Path path2 = "/foo/bar/quux/baz"; + int depth = FS::descendant_depth(path2, path1); + EXPECT_EQUAL(depth, 2); + FS::Path rel = FS::relative(path2, path1); + EXPECT_EQUAL(rel.str(), "./quux/baz"); + + path2 = "/foo/quux/baz"; + depth = FS::descendant_depth(path2, path1); + EXPECT_EQUAL(depth, -1); + rel = FS::relative(path2, path1); + EXPECT_EQUAL(rel.str(), "../quux/baz"); + FS::Path anc = FS::common_ancestor(path1, path2); + EXPECT_EQUAL(anc.str(), "/foo"); + + path2 = "bar/quux"; + anc = FS::common_ancestor(path1, path2); + EXPECT(anc.empty()); +} + +void FSUtilTests::bad_hierarchy() +{ + FS::Path path1 = "/foo"; + FS::Path path2 = "bar"; + FS::relative(path2, path1); +}