Add test to libpbd to check PBD::touch_file and pbd/gstdio_compat.h
authorTim Mayberry <mojofunk@gmail.com>
Thu, 17 Sep 2015 05:13:16 +0000 (15:13 +1000)
committerTim Mayberry <mojofunk@gmail.com>
Thu, 17 Sep 2015 13:26:38 +0000 (23:26 +1000)
GStatBuf is not usable on 32 bit Windows without the redefinition in
pbd/gstdio_compat.h so add a test to check for the correct behavior of
g_stat and g_utime on all platforms now that the issue is fixed.

libs/pbd/test/filesystem_test.cc
libs/pbd/test/filesystem_test.h

index adff0c409b817b8ae5358f5d924dc253cf73f441..1eaba5496873af78d3b9b97bbd5f1c7636cba7bd 100644 (file)
@@ -8,9 +8,16 @@
 
 #include <fcntl.h>
 
+#ifdef COMPILER_MSVC
+#include <sys/utime.h>
+#else
+#include <utime.h>
+#endif
+
 #include <glibmm/miscutils.h>
 #include <glibmm/fileutils.h>
 #include <glibmm/convert.h>
+#include <glibmm/timer.h>
 
 #include "pbd/file_utils.h"
 #include "pbd/pathexpand.h"
@@ -363,3 +370,72 @@ FilesystemTest::testCanonicalPath ()
        CPPUNIT_ASSERT (expanded_path == expected_path);
 #endif
 }
+
+void
+FilesystemTest::testTouchFile ()
+{
+       const string filename = "touch.me";
+
+       const string test_dir = test_output_directory ("testTouchFile");
+       const string touch_file_path = Glib::build_filename (test_dir, filename);
+
+       CPPUNIT_ASSERT (touch_file(touch_file_path));
+
+       CPPUNIT_ASSERT (Glib::file_test (touch_file_path, Glib::FILE_TEST_EXISTS));
+}
+
+void
+FilesystemTest::testStatFile ()
+{
+       const string filename1 = "touch.me";
+       const string filename2 = "touch.me.2";
+
+       const string test_dir = test_output_directory ("testStatFile");
+
+       const string path1 = Glib::build_filename (test_dir, filename1);
+       const string path2 = Glib::build_filename (test_dir, filename2);
+
+       CPPUNIT_ASSERT (touch_file(path1));
+
+       Glib::usleep (2000000);
+
+       CPPUNIT_ASSERT (touch_file(path2));
+
+       GStatBuf gsb1;
+       GStatBuf gsb2;
+
+       CPPUNIT_ASSERT (g_stat (path1.c_str(), &gsb1) == 0);
+       CPPUNIT_ASSERT (g_stat (path2.c_str(), &gsb2) == 0);
+
+       cerr << endl;
+       cerr << "StatFile: " << path1 << " access time: " << gsb1.st_atime << endl;
+       cerr << "StatFile: " << path1 << " modification time: " << gsb1.st_mtime << endl;
+       cerr << "StatFile: " << path2 << " access time: " << gsb2.st_atime << endl;
+       cerr << "StatFile: " << path2 << " modification time: " << gsb2.st_mtime << endl;
+
+       CPPUNIT_ASSERT (gsb1.st_atime == gsb1.st_mtime);
+       CPPUNIT_ASSERT (gsb2.st_atime == gsb2.st_mtime);
+
+       // at least access time works on windows(or at least on ntfs)
+       CPPUNIT_ASSERT (gsb1.st_atime < gsb2.st_atime);
+       CPPUNIT_ASSERT (gsb1.st_mtime < gsb2.st_mtime);
+
+       struct utimbuf tbuf;
+
+       tbuf.actime = gsb1.st_atime;
+       tbuf.modtime = gsb1.st_mtime;
+
+       // update the file access/modification times to be the same
+       CPPUNIT_ASSERT (g_utime (path2.c_str(), &tbuf) == 0);
+
+       CPPUNIT_ASSERT (g_stat (path2.c_str(), &gsb2) == 0);
+
+       cerr << endl;
+       cerr << "StatFile: " << path1 << " access time: " << gsb1.st_atime << endl;
+       cerr << "StatFile: " << path1 << " modification time: " << gsb1.st_mtime << endl;
+       cerr << "StatFile: " << path2 << " access time: " << gsb2.st_atime << endl;
+       cerr << "StatFile: " << path2 << " modification time: " << gsb2.st_mtime << endl;
+
+       CPPUNIT_ASSERT (gsb1.st_atime == gsb2.st_atime);
+       CPPUNIT_ASSERT (gsb1.st_mtime == gsb2.st_mtime);
+}
index c200bb2ba7d92ad2fd14b8ba7084c2733c6e1de5..0274d61139a524eb7fceeac5073f5f196119b79e 100644 (file)
@@ -12,6 +12,8 @@ class FilesystemTest : public CppUnit::TestFixture
        CPPUNIT_TEST (testClearDirectory);
        CPPUNIT_TEST (testRemoveDirectory);
        CPPUNIT_TEST (testCanonicalPath);
+       CPPUNIT_TEST (testTouchFile);
+       CPPUNIT_TEST (testStatFile);
        CPPUNIT_TEST_SUITE_END ();
 
 public:
@@ -23,5 +25,7 @@ public:
        void testClearDirectory ();
        void testRemoveDirectory ();
        void testCanonicalPath ();
+       void testTouchFile ();
+       void testStatFile ();
 };