// Path_t.cxx #include "dataset_util/Path.h" #include #include #include "dataset_util/FileStatus.h" using std::string; using std::cout; using std::endl; void msg(const char* msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } int Path_t() { { msg("Empty path"); Path pth; cout << pth << endl; assert( pth.size() == 0 ); } { msg("Path with value"); Path pth("A:BB:CCC"); cout << pth << endl; assert( pth.size() == 3 ); assert( pth.front() == "A"); assert( pth.back() == "CCC"); msg("to string"); string spath = pth.to_string(); cout << spath << endl; assert( Path(spath) == pth ); } { msg("Path from environment."); Path pth; pth.getenv("PATH"); cout << pth << endl; assert( pth.size() > 0 ); } { msg("Set test path"); Path pth; assert( pth.size() == 0 ); pth.push_back("BB"); pth.push_front("A"); pth.push_back("CCC"); cout << pth << endl; assert( pth.size() == 3 ); pth.setenv("JUNKPATH"); msg("Check test path"); const char* pcenv = getenv("JUNKPATH"); assert( pcenv != 0 ); string penv = pcenv; cout << penv << endl; assert( penv == "A:BB:CCC" ); } { msg("Find ls"); Path pth; pth.getenv("PATH"); Path findpath = pth.find("ls"); cout << findpath << endl; assert( findpath.size() > 0 ); string pathfile = findpath.front() + "/" + "ls"; assert( FileStatus(pathfile).exists() ); string com = "ls -ls " + pathfile; assert( system( com.c_str() ) == 0 ); } msg("All tests passed."); return 0; } #ifdef CTEST_MAIN int main() { return Path_t(); } #endif