File indexing completed on 2026-04-09 07:49:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <cassert>
0023 #include <csignal>
0024 #include <vector>
0025 #include <string>
0026 #include <iomanip>
0027 #include <iostream>
0028 #include <fstream>
0029
0030 #include "SPath.hh"
0031 #include "SStr.hh"
0032 #include "sdigest.h"
0033
0034 #include "OPTICKS_LOG.hh"
0035
0036
0037 void test_Stem()
0038 {
0039 LOG(info);
0040 const char* name = "hello.cu" ;
0041 const char* stem = SPath::Stem(name);
0042 const char* x_stem = "hello" ;
0043
0044 bool stem_expect = strcmp( stem, x_stem ) == 0 ;
0045 assert(stem_expect );
0046 if(!stem_expect) std::raise(SIGINT);
0047 }
0048
0049 void test_GetHomePath()
0050 {
0051 LOG(info);
0052 const char* bashrc = SPath::GetHomePath(".bashrc") ;
0053 std::cout << bashrc << std::endl ;
0054 }
0055
0056 void test_IsReadable()
0057 {
0058 LOG(info);
0059 const char* self = SPath::GetHomePath("opticks/sysrap/tests/SPathTest.cc") ;
0060 const char* non = SPath::GetHomePath("opticks/sysrap/tests/SPathTest.cc.non") ;
0061 std::cout << self << std::endl ;
0062 bool readable = SPath::IsReadable(self);
0063 if(!readable)
0064 {
0065 LOG(fatal) << "looks like opticks source not in HOME" ;
0066 }
0067
0068
0069 bool readable_non = SPath::IsReadable(non);
0070 bool readable_non_expect = readable_non == false ;
0071 if(!readable_non_expect) std::raise(SIGINT) ;
0072 assert( readable_non_expect );
0073 }
0074
0075
0076 const char* PATHS = R"LITERAL(
0077 /tmp
0078 /tmp/nonexisting
0079 )LITERAL" ;
0080
0081 void test_IsReadable_path()
0082 {
0083 std::stringstream ss(PATHS) ;
0084 std::string line ;
0085 while (std::getline(ss, line))
0086 {
0087 if(line.empty()) continue ;
0088 const char* path = line.c_str();
0089 bool readable_path = SPath::IsReadable(path);
0090 LOG(info) << " path " << path << " readable_path " << readable_path ;
0091 }
0092 }
0093
0094 void test_Dirname()
0095 {
0096 LOG(info);
0097 const char* lines = R"LIT(
0098 $HOME/hello
0099 $TMP/somewhere/over/the/rainbow.txt
0100 $NON_EXISTING_EVAR/elsewhere/sub.txt
0101 /just/some/path.txt
0102 stem.ext
0103 /
0104 $
0105 )LIT";
0106 std::stringstream ss(lines);
0107 std::string line ;
0108 while (std::getline(ss, line, '\n'))
0109 {
0110 if(line.empty()) continue ;
0111 const char* path = SPath::Dirname(line.c_str());
0112 std::cout
0113 << std::setw(60) << line
0114 << " : "
0115 << std::setw(60) << path
0116 << std::endl
0117 ;
0118 }
0119 }
0120
0121
0122 void test_Basename()
0123 {
0124 LOG(info);
0125 std::vector<std::string> paths = { "/dd/materials/Water", "Water", "" } ;
0126 for(unsigned i=0 ; i < paths.size() ; i++)
0127 {
0128 const char* path = paths[i].c_str() ;
0129 const char* base = SPath::Basename(path) ;
0130 std::cout
0131 << " path [" << path << "]"
0132 << " base [" << base << "]"
0133 << std::endl
0134 ;
0135 }
0136 }
0137
0138 void test_Basename_2()
0139 {
0140 const char* path = "/tmp/blyth/opticks/GeoChain_Darwin/XJfixtureConstruction" ;
0141 const char* base = SPath::Basename(path) ;
0142
0143 LOG(info)
0144 << std::endl
0145 << " path [" << path << "]"
0146 << std::endl
0147 << " base [" << base << "]"
0148 << std::endl
0149 ;
0150
0151 assert( strcmp( base, "XJfixtureConstruction") == 0 );
0152 }
0153
0154
0155
0156
0157
0158 void test_UserTmpDir()
0159 {
0160 LOG(info);
0161 const char* tmp = SPath::UserTmpDir();
0162 std::cout << tmp << std::endl ;
0163 }
0164
0165 void test_Resolve_With_Index()
0166 {
0167 LOG(info);
0168 const char* lines = R"LIT(
0169 $TMP
0170 $HOME/hello
0171 $NON_EXISTING_EVAR/elsewhere
0172 $DefaultOutputDir
0173 $RNGDir
0174 )LIT";
0175 std::stringstream ss(lines);
0176 std::string line ;
0177 while (std::getline(ss, line, '\n'))
0178 {
0179 if(line.empty()) continue ;
0180
0181 for(int idx=-2 ; idx <= 2 ; idx++)
0182 {
0183 const char* path = SPath::Resolve(line.c_str(), idx, NOOP);
0184 std::cout
0185 << " idx "
0186 << std::setw(3) << idx
0187 << " line "
0188 << std::setw(60) << line
0189 << " : "
0190 << std::setw(60) << path
0191 << std::endl
0192 ;
0193 }
0194
0195 }
0196 }
0197
0198 void test_Resolve_Default()
0199 {
0200 const char* dir0 = SPath::Resolve(NOOP);
0201 const char* dir1 = SPath::Resolve(DIRPATH);
0202 LOG(info) << "SPath::Resolve(NOOP) " << dir0 ;
0203 LOG(info) << "SPath::Resolve(DIRPATH) " << dir1 ;
0204 }
0205
0206 void test_Resolve()
0207 {
0208 LOG(info);
0209 const char* lines = R"LIT(
0210 $TMP
0211 $DefaultOutputDir
0212 $HOME/hello
0213 $TMP/somewhere/over/the/rainbow.txt
0214 $NON_EXISTING_EVAR/elsewhere/sub.txt
0215 /just/some/path.txt
0216 stem.ext
0217 /
0218 $
0219 $RNGDir
0220 $CFBaseFromGEOM/GGeo
0221 $UserGEOMDir/origin.gdml
0222 )LIT";
0223 std::stringstream ss(lines);
0224 std::string line ;
0225 while (std::getline(ss, line, '\n'))
0226 {
0227 if(line.empty()) continue ;
0228 int create_dirs = 0 ;
0229 const char* path = SPath::Resolve(line.c_str(), create_dirs);
0230 std::cout
0231 << std::setw(60) << line
0232 << " : "
0233 << std::setw(60) << path
0234 << std::endl
0235 ;
0236 }
0237 }
0238
0239
0240 void test_ChangeName()
0241 {
0242 const char* srcpath = "/some/long/path/ending/with/pixels.jpg" ;
0243 const char* path = SPath::ChangeName(srcpath, "posi.npy" );
0244 const char* xpath = "/some/long/path/ending/with/posi.npy" ;
0245 bool path_expect = 0 == strcmp( path, xpath ) ;
0246
0247 LOG(info) << path ;
0248 assert( path_expect ) ;
0249 if(!path_expect) std::raise(SIGINT);
0250 }
0251
0252 void test_MakeDirs()
0253 {
0254 const char* path = "/tmp/SPathTest/red/green/blue" ;
0255 int rc = SPath::MakeDirs(path);
0256 LOG(info) << " path " << path << " rc " << rc ;
0257 }
0258
0259
0260 void test_MakePath()
0261 {
0262 const char* path = SPath::MakePath<double>("/tmp/SPathTest", "test_MakePath", 1.123, "demo.npy" );
0263 LOG(info) << " path " << path ;
0264 }
0265
0266 void test_Resolve_createdirs()
0267 {
0268 const char* path = "$TMP/red/green/blue/file.txt" ;
0269 int create_dirs = 1 ;
0270 const char* p = SPath::Resolve(path, create_dirs);
0271
0272 LOG(info) << path << " " << p ;
0273
0274 std::ofstream fp(p, std::ios::out);
0275 fp << path ;
0276 fp.close();
0277 }
0278
0279 void test_getcwd()
0280 {
0281 const char* cwd = SPath::getcwd() ;
0282 LOG(info) << " after SPath::chdir SPath::getcwd " << cwd ;
0283 }
0284
0285 void test_mtime()
0286 {
0287 const char* path = "/tmp/tt.txt" ;
0288 int mtime = SPath::mtime(path);
0289 LOG(info) << " path " << path << " mtime " << mtime ;
0290 }
0291
0292 void test_MakeName()
0293 {
0294 const char* stem = "cogent_stem_" ;
0295 int index = 101 ;
0296 const char* ext = ".jpg" ;
0297 std::string name = SPath::MakeName( stem, index, ext );
0298 const char* x_name = "cogent_stem_00101.jpg" ;
0299
0300 bool name_expect = strcmp( name.c_str(), x_name) == 0 ;
0301 assert(name_expect);
0302 if(!name_expect) std::raise(SIGINT);
0303 }
0304
0305 void test_Make()
0306 {
0307 const char* base = "/tmp/SPathTest/base" ;
0308 const char* relname = "some/informative/relname" ;
0309 const char* stem = "cogent_stem_" ;
0310 int index = 101 ;
0311 const char* ext = ".jpg" ;
0312
0313 const char* path_0 = SPath::Make(base, relname, stem, index, ext, FILEPATH );
0314 const char* x_path_0 = "/tmp/SPathTest/base/some/informative/relname/cogent_stem_00101.jpg" ;
0315
0316 bool path_0_expect = strcmp( path_0, x_path_0) == 0 ;
0317 assert(path_0_expect );
0318 if(!path_0_expect) std::raise(SIGINT);
0319
0320 const char* path_1 = SPath::Make(base, nullptr, stem, index, ext, FILEPATH );
0321 const char* x_path_1 = "/tmp/SPathTest/base/cogent_stem_00101.jpg" ;
0322 bool path_1_expect = strcmp( path_1, x_path_1) == 0 ;
0323 assert(path_1_expect);
0324 if(!path_1_expect) std::raise(SIGINT);
0325
0326 const char* path_2 = SPath::Make(base, nullptr, stem, -1, ext, FILEPATH );
0327 const char* x_path_2 = "/tmp/SPathTest/base/cogent_stem_.jpg" ;
0328 bool path_2_expect = strcmp( path_2, x_path_2) == 0 ;
0329 assert(path_2_expect);
0330 if(!path_2_expect) std::raise(SIGINT);
0331 }
0332
0333
0334 void test_MakeEmpty()
0335 {
0336 const char* path = "$TMP/SPathTest/file.gdml" ;
0337 bool exists_0 = SPath::Exists(path);
0338 SPath::MakeEmpty(path);
0339 bool exists_1 = SPath::Exists(path);
0340 LOG(info)
0341 << " path " << path
0342 << " exists_0 " << exists_0
0343 << " exists_1 " << exists_1
0344 ;
0345 }
0346
0347 void test_Remove()
0348 {
0349 const char* path = "$TMP/SPathTest/file.gdml" ;
0350 bool exists_0 = SPath::Exists(path);
0351 int rc = SPath::Remove(path);
0352 bool exists_1 = SPath::Exists(path);
0353 LOG(info)
0354 << " path " << path << " rc " << rc
0355 << " exists_0 " << exists_0
0356 << " exists_1 " << exists_1
0357 ;
0358 }
0359
0360 void test_SearchDirUpTreeWithFile()
0361 {
0362 const char* startdir = "/tmp/blyth/opticks/J001/G4CXSimulateTest/ALL" ;
0363 const char* relf = "CSGFoundry/solid.npy" ;
0364
0365
0366
0367 const char* cfbase = SPath::SearchDirUpTreeWithFile(startdir, relf);
0368 LOG(info)
0369 << " startdir " << startdir
0370 << " relf " << relf
0371 << " cfbase " << cfbase
0372 ;
0373 }
0374
0375
0376 void test_Copy()
0377 {
0378 const char* src = "/tmp/src.txt" ;
0379 const char* dst = "/tmp/dst.txt" ;
0380 const char* srcn = "src.txt" ;
0381 const char* dstn = "dst2.txt" ;
0382
0383 SStr::Save(src, "SPathTest test_Copy src text\n" );
0384 SPath::Copy(dst, src ) ;
0385 SPath::Copy(dstn, srcn, "/tmp" ) ;
0386
0387 std::string ds = sdigest::Path(src) ;
0388 std::string dd = sdigest::Path(dst) ;
0389 assert( strcmp( ds.c_str(), dd.c_str()) == 0 );
0390
0391 }
0392
0393 void test_Join()
0394 {
0395 const char* p = SPath::Join("red", "green", "blue" );
0396 std::cout << " p [" << p << "]" << std::endl ;
0397 }
0398
0399
0400
0401 int main(int argc , char** argv )
0402 {
0403
0404
0405
0406 OPTICKS_LOG(argc, argv);
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417 test_Resolve();
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437 return 0 ;
0438 }
0439