]> git.tdb.fi Git - libs/core.git/blob - tests/fsutils.cpp
Interpret POLLHUP as end-of-file
[libs/core.git] / tests / fsutils.cpp
1 #include <msp/fs/utils.h>
2 #include <msp/test/test.h>
3
4 using namespace std;
5 using namespace Msp;
6
7 class FSUtilTests: public Test::RegisteredTest<FSUtilTests>
8 {
9 public:
10         FSUtilTests();
11
12         static const char *get_name() { return "fsutils"; }
13
14 private:
15         void pathsplit();
16         void fnsplit();
17         void hierarchy();
18         void bad_hierarchy();
19 };
20
21 FSUtilTests::FSUtilTests()
22 {
23         add(&FSUtilTests::pathsplit, "Path splitting");
24         add(&FSUtilTests::fnsplit, "Filename splitting");
25         add(&FSUtilTests::hierarchy, "Hierarchy");
26         add(&FSUtilTests::bad_hierarchy, "Bad hierarchy").expect_throw<invalid_argument>();
27 }
28
29 void FSUtilTests::pathsplit()
30 {
31         FS::Path path("/foo/bar/baz");
32         FS::Path dir = FS::dirname(path);
33         EXPECT_EQUAL(dir.str(), "/foo/bar");
34         string base = FS::basename(path);
35         EXPECT_EQUAL(base, "baz");
36 }
37
38 void FSUtilTests::fnsplit()
39 {
40         string filename = "file.png";
41         string base = FS::basepart(filename);
42         EXPECT_EQUAL(base, "file");
43         string ext = FS::extpart(filename);
44         EXPECT_EQUAL(ext, ".png");
45
46         filename = "file.tar.gz";
47         base = FS::basepart(filename);
48         EXPECT_EQUAL(base, "file.tar");
49         ext = FS::extpart(filename);
50         EXPECT_EQUAL(ext, ".gz");
51 }
52
53 void FSUtilTests::hierarchy()
54 {
55         FS::Path path1 = "/foo/bar";
56         FS::Path path2 = "/foo/bar/quux/baz";
57         int depth = FS::descendant_depth(path2, path1);
58         EXPECT_EQUAL(depth, 2);
59         FS::Path rel = FS::relative(path2, path1);
60         EXPECT_EQUAL(rel.str(), "./quux/baz");
61
62         path2 = "/foo/quux/baz";
63         depth = FS::descendant_depth(path2, path1);
64         EXPECT_EQUAL(depth, -1);
65         rel = FS::relative(path2, path1);
66         EXPECT_EQUAL(rel.str(), "../quux/baz");
67         FS::Path anc = FS::common_ancestor(path1, path2);
68         EXPECT_EQUAL(anc.str(), "/foo");
69
70         path2 = "bar/quux";
71         anc = FS::common_ancestor(path1, path2);
72         EXPECT(anc.empty());
73 }
74
75 void FSUtilTests::bad_hierarchy()
76 {
77         FS::Path path1 = "/foo";
78         FS::Path path2 = "bar";
79         FS::relative(path2, path1);
80 }