// copy_file.cxx #include "dataset_util/copy_file.h" #include "dataset_util/FileStatus.h" #include "dataset_util/Text.h" #include #include using std::string; using std::cout; using std::endl; void msg(string msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } bool check(int val, int expect=0) { cout << "Value is " << val << endl; return val == expect; } int copy_file_t() { msg("Clean up"); system("rm -f newfile"); assert( ! FileStatus("newfile").exists() ); msg("Nonexistent input"); assert( ! check(copy_file("oldfile1", "newfile")) ); assert( ! FileStatus("newfile").exists() ); msg("Create file 1"); system("echo myfile1 > oldfile1"); assert( FileStatus("oldfile1").exists() ); Text text1("oldfile1"); msg("Create file 2"); system("echo myfile_two > oldfile2"); assert( FileStatus("oldfile2").exists() ); Text text2("oldfile2"); msg("Successful copy without overwrite"); assert( check(copy_file("oldfile1", "newfile")) ); assert( FileStatus("newfile").exists() ); assert( Text("newfile") == text1 ); assert( Text("newfile") != text2 ); msg("Fail overwrite"); assert( ! check(copy_file("oldfile2", "newfile")) ); assert( FileStatus("newfile").exists() ); assert( Text("newfile") == text1 ); assert( Text("newfile") != text2 ); msg("Successful copy with overwrite"); assert( check(copy_file("oldfile2", "newfile", true)) ); assert( FileStatus("newfile").exists() ); assert( Text("newfile") != text1 ); assert( Text("newfile") == text2 ); msg("All tests passed."); return 0; } #ifdef CTEST_MAIN int main() { return copy_file_t(); } #endif