// FileStatus_t.cxx #include "dataset_util/FileStatus.h" #include #include #include using std::string; using std::cout; using std::endl; void msg(const char* msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } int FileStatus_t() { string file1 = "file1.dat"; { msg("Absent file."); FileStatus fstat(file1); cout << fstat << endl; assert( fstat.name() == file1 ); assert( ! fstat.exists() ); assert( ! fstat.is_regular() ); assert( ! fstat.is_directory() ); assert( ! fstat.is_special() ); assert( ! fstat.is_readable() ); assert( ! fstat.is_writeable() ); assert( ! fstat.is_executable() ); } { msg("Regular file."); system("echo hello > file1.dat"); FileStatus fstat(file1); cout << fstat << endl; assert( fstat.name() == file1 ); assert( fstat.exists() ); assert( fstat.is_regular() ); assert( ! fstat.is_directory() ); assert( ! fstat.is_special() ); assert( fstat.is_readable() ); assert( fstat.is_writeable() ); assert( ! fstat.is_executable() ); assert( fstat.size() > 4 ); assert( fstat.size() < 8 ); assert( fstat.modtime() > time(0) - 100 ); assert( fstat.modtime() < time(0) + 100 ); assert( fstat.acctime() > time(0) - 100 ); assert( fstat.acctime() < time(0) + 100 ); } { msg("Regular file not readable."); system("chmod -r file1.dat"); FileStatus fstat(file1); cout << fstat << endl; assert( fstat.name() == file1 ); assert( fstat.exists() ); assert( fstat.is_regular() ); assert( ! fstat.is_directory() ); assert( ! fstat.is_special() ); assert( ! fstat.is_readable() ); assert( fstat.is_writeable() ); assert( ! fstat.is_executable() ); } { msg("Regular file not writeable."); system("chmod -w file1.dat"); FileStatus fstat(file1); cout << fstat << endl; assert( fstat.name() == file1 ); assert( fstat.exists() ); assert( fstat.is_regular() ); assert( ! fstat.is_directory() ); assert( ! fstat.is_special() ); assert( ! fstat.is_readable() ); assert( ! fstat.is_writeable() ); assert( ! fstat.is_executable() ); } { msg("Regular file world writeable."); system("chmod o+w file1.dat"); FileStatus fstat(file1); cout << fstat << endl; assert( fstat.is_world_writeable() ); system("chmod o-w file1.dat"); fstat.update(); assert( ! fstat.is_world_writeable() ); } { msg("Regular file executable."); system("chmod +r file1.dat"); system("chmod +w file1.dat"); system("chmod +x file1.dat"); FileStatus fstat(file1); cout << fstat << endl; assert( fstat.name() == file1 ); assert( fstat.exists() ); assert( fstat.is_regular() ); assert( ! fstat.is_directory() ); assert( ! fstat.is_special() ); assert( fstat.is_readable() ); assert( fstat.is_writeable() ); assert( fstat.is_executable() ); } { msg("Symbolic links."); system("ln -s file1.dat softlink"); system("ln file1.dat hardlink"); FileStatus fstat(file1); FileStatus fstatsoft("softlink"); FileStatus fstathard("hardlink"); assert( fstat.exists() ); assert( fstatsoft.exists() ); assert( fstathard.exists() ); assert( ! fstat.is_symlink() ); assert( fstatsoft.is_symlink() ); assert( ! fstathard.is_symlink() ); } string dir1 = "dir1"; { msg("Regular file executable."); system("mkdir dir1"); FileStatus fstat(dir1); cout << fstat << endl; assert( fstat.name() == dir1 ); assert( fstat.exists() ); assert( ! fstat.is_regular() ); assert( fstat.is_directory() ); assert( ! fstat.is_special() ); assert( fstat.is_readable() ); assert( fstat.is_writeable() ); assert( fstat.is_executable() ); } { msg("Regular file executable."); system("mv file1.dat dir1"); FileStatus fstat(dir1,file1); cout << fstat << endl; assert( fstat.name() == dir1 + "/" + file1 ); assert( fstat.exists() ); assert( fstat.is_regular() ); assert( ! fstat.is_directory() ); assert( ! fstat.is_special() ); assert( fstat.is_readable() ); assert( fstat.is_writeable() ); assert( fstat.is_executable() ); } { msg("Assignment"); FileStatus fstat0; assert( ! fstat0.exists() ); FileStatus fstat(dir1,file1); assert( fstat.exists() ); fstat0 = fstat; assert( fstat0.exists() ); assert( fstat0.name() == fstat.name() ); } msg("All tests passed."); return 0; } #ifdef CTEST_MAIN int main() { return FileStatus_t(); } #endif