]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/path_test.cxx
Change location used for path tests
[simgear.git] / simgear / misc / path_test.cxx
index 459f064cdd78a3464851863b5badecfda75db4c6..fb9c4dae0903596639d962eb0583189ce031bd20 100644 (file)
@@ -9,20 +9,10 @@ using std::cout;
 using std::cerr;
 using std::endl;
 
-#define COMPARE(a, b) \
-    if ((a) != (b))  { \
-        cerr << "failed:" << #a << " != " << #b << endl; \
-        exit(1); \
-    }
-
-#define VERIFY(a) \
-    if (!(a))  { \
-        cerr << "failed:" << #a << endl; \
-        exit(1); \
-    }
-    
+#include <simgear/misc/test_macros.hxx>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/misc/sg_dir.hxx>
+#include <simgear/misc/sgstream.hxx>
 
 void test_dir()
 {
@@ -41,6 +31,107 @@ void test_dir()
     }
     
     cout << temp.path().modTime() << endl;
+
+    std::cout << "Standard Locations:"
+              << "\n - Home:      " << SGPath::standardLocation(SGPath::HOME)
+              << "\n - Desktop:   " << SGPath::standardLocation(SGPath::DESKTOP)
+              << "\n - Downloads: " << SGPath::standardLocation(SGPath::DOWNLOADS)
+              << "\n - Documents: " << SGPath::standardLocation(SGPath::DOCUMENTS)
+              << "\n - Pictures:  " << SGPath::standardLocation(SGPath::PICTURES)
+              << std::endl;
+
+    VERIFY( !SGPath::standardLocation(SGPath::HOME     ).isNull() );
+    VERIFY( !SGPath::standardLocation(SGPath::DESKTOP  ).isNull() );
+    VERIFY( !SGPath::standardLocation(SGPath::DOWNLOADS).isNull() );
+    VERIFY( !SGPath::standardLocation(SGPath::DOCUMENTS).isNull() );
+    VERIFY( !SGPath::standardLocation(SGPath::PICTURES ).isNull() );
+}
+
+SGPath::Permissions validateNone(const SGPath&)
+{
+  SGPath::Permissions p;
+  p.read = false;
+  p.write = false;
+  return p;
+}
+
+SGPath::Permissions validateRead(const SGPath&)
+{
+  SGPath::Permissions p;
+  p.read = true;
+  p.write = false;
+  return p;
+}
+
+SGPath::Permissions validateWrite(const SGPath&)
+{
+  SGPath::Permissions p;
+  p.read = false;
+  p.write = true;
+  return p;
+}
+
+void test_path_dir()
+{
+       simgear::Dir temp = simgear::Dir::tempDir("path_dir");
+       temp.remove(true);
+    SGPath p = temp.path();
+
+       VERIFY(p.isAbsolute());
+       COMPARE(p.create_dir(0755), 0);
+
+       SGPath sub = p / "subA" / "subB";
+       VERIFY(!sub.exists());
+
+       SGPath subFile = sub / "fileABC.txt";
+       COMPARE(subFile.create_dir(0755), 0);
+       VERIFY(!subFile.exists());
+
+       sub.set_cached(false);
+       VERIFY(sub.exists());
+       VERIFY(sub.isDir());
+
+       SGPath sub2 = p / "subA" / "fileA";
+       {
+               sg_ofstream os(sub2);
+        VERIFY(os.is_open());
+               for (int i = 0; i < 50; ++i) {
+                       os << "ABCD" << endl;
+               }
+       }
+       VERIFY(sub2.isFile());
+       COMPARE(sub2.sizeInBytes(), 250);
+
+    SGPath sub3 = p / "subß" / "file𝕽";
+    sub3.create_dir(0755);
+
+    {
+        sg_ofstream os(sub3);
+        VERIFY(os.is_open());
+        for (int i = 0; i < 20; ++i) {
+            os << "EFGH" << endl;
+        }
+    }
+
+    sub3.set_cached(false);
+    VERIFY(sub3.exists());
+    COMPARE(sub3.sizeInBytes(), 100);
+    COMPARE(sub3.file(), "file𝕽");
+
+       simgear::Dir subD(p / "subA");
+       simgear::PathList dirChildren = subD.children(simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT);
+       COMPARE(dirChildren.size(), 1);
+       COMPARE(dirChildren[0], subD.path() / "subB");
+
+       simgear::PathList fileChildren = subD.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT);
+       COMPARE(fileChildren.size(), 1);
+       COMPARE(fileChildren[0], subD.path() / "fileA");
+
+    simgear::Dir subS(sub3.dirPath());
+    fileChildren = subS.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT);
+    COMPARE(fileChildren.size(), 1);
+    COMPARE(fileChildren[0], subS.path() / "file𝕽");
+
 }
 
 int main(int argc, char* argv[])
@@ -51,8 +142,8 @@ int main(int argc, char* argv[])
     
 // test basic parsing
     SGPath pb("/Foo/bar/something.png");
-    COMPARE(pb.str(), std::string("/Foo/bar/something.png"));
-    COMPARE(strcmp(pb.c_str(), "/Foo/bar/something.png"), 0);
+    COMPARE(pb.utf8Str(), std::string("/Foo/bar/something.png"));
+    COMPARE(pb.local8BitStr(), std::string("/Foo/bar/something.png"));
     COMPARE(pb.dir(), std::string("/Foo/bar"));
     COMPARE(pb.file(), std::string("something.png"));
     COMPARE(pb.base(), std::string("/Foo/bar/something"));
@@ -63,7 +154,8 @@ int main(int argc, char* argv[])
     
 // relative paths
     SGPath ra("where/to/begin.txt");
-    COMPARE(ra.str(), std::string("where/to/begin.txt"));
+    COMPARE(ra.utf8Str(), std::string("where/to/begin.txt"));
+    COMPARE(ra.local8BitStr(), std::string("where/to/begin.txt"));
     COMPARE(ra.dir(), std::string("where/to"));
     COMPARE(ra.file(), std::string("begin.txt"));
     COMPARE(ra.file_base(), std::string("begin"));
@@ -89,30 +181,26 @@ int main(int argc, char* argv[])
     
 // path fixing
     SGPath rd("where\\to\\begin.txt");
-    COMPARE(rd.str(), std::string("where/to/begin.txt"));
+    COMPARE(rd.utf8Str(), std::string("where/to/begin.txt"));
     
 // test modification
 // append
     SGPath d1("/usr/local");
     SGPath pc = d1;
-    COMPARE(pc.str(), std::string("/usr/local"));
+    COMPARE(pc.utf8Str(), std::string("/usr/local"));
     pc.append("include");
     
-    COMPARE(pc.str(), std::string("/usr/local/include"));
+    COMPARE(pc.utf8Str(), std::string("/usr/local/include"));
     COMPARE(pc.file(), std::string("include"));
-    
-// add
-    pc.add("/opt/local");
-    COMPARE(pc.str(), std::string("/usr/local/include/:/opt/local"));
-    
+
 // concat
     SGPath pd = pb;
     pd.concat("-1");
-    COMPARE(pd.str(), std::string("/Foo/bar/something.png-1"));
+    COMPARE(pd.utf8Str(), std::string("/Foo/bar/something.png-1"));
     
 // create with relative path
     SGPath rb(d1, "include/foo");
-    COMPARE(rb.str(), std::string("/usr/local/include/foo"));
+    COMPARE(rb.utf8Str(), std::string("/usr/local/include/foo"));
     VERIFY(rb.isAbsolute());
     
 // lower-casing of file extensions
@@ -128,12 +216,9 @@ int main(int argc, char* argv[])
     COMPARE(extB.lower_extension(), "gz");
     COMPARE(extB.complete_lower_extension(), "html.gz");
 #ifdef _WIN32
-    COMPARE(d1.str_native(), std::string("\\usr\\local"));
-    
     SGPath winAbs("C:\\Windows\\System32");
-    COMPARE(winAbs.str(), std::string("C:/Windows/System32"));
-#else
-    COMPARE(d1.str_native(), std::string("/usr/local"));
+    COMPARE(winAbs.local8BitStr(), std::string("C:/Windows/System32"));
+
 #endif
   
 // paths with only the file components
@@ -143,9 +228,30 @@ int main(int argc, char* argv[])
     COMPARE(pf.dir(), "");
     COMPARE(pf.lower_extension(), "gz");
     COMPARE(pf.complete_lower_extension(), "txt.gz");
-    
+
+    COMPARE(pf.canRead(), true);
+    COMPARE(pf.canWrite(), true);
+
+    SGPath pp(&validateNone);
+    COMPARE(pp.canRead(), false);
+    COMPARE(pp.canWrite(), false);
+
+    pp.append("./test-dir/file.txt");
+    COMPARE(pp.create_dir(0700), -3);
+
+    pp.setPermissionChecker(&validateRead);
+    COMPARE(pp.canRead(), true);
+    COMPARE(pp.canWrite(), false);
+    COMPARE(pp.create_dir(0700), -3);
+
+    pp.setPermissionChecker(&validateWrite);
+    COMPARE(pp.canRead(), false);
+    COMPARE(pp.canWrite(), true);
+
     test_dir();
     
+       test_path_dir();
+
     cout << "all tests passed OK" << endl;
     return 0; // passed
 }