Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:19

0001 /*
0002  * Copyright (c) 2019 Opticks Team. All Rights Reserved.
0003  *
0004  * This file is part of Opticks
0005  * (see https://bitbucket.org/simoncblyth/opticks).
0006  *
0007  * Licensed under the Apache License, Version 2.0 (the "License"); 
0008  * you may not use this file except in compliance with the License.  
0009  * You may obtain a copy of the License at
0010  *
0011  *   http://www.apache.org/licenses/LICENSE-2.0
0012  *
0013  * Unless required by applicable law or agreed to in writing, software 
0014  * distributed under the License is distributed on an "AS IS" BASIS, 
0015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0016  * See the License for the specific language governing permissions and 
0017  * limitations under the License.
0018  */
0019 
0020 // om-;TEST=SPathTest om-t 
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     //assert( readable == true ); 
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 ; // noop
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 ; // 1:filepath 
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     //const char* startdir = nullptr ; 
0365     //const char* relf = nullptr ; 
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     //SPath::chdir("$TMP/red/green/blue/logs");  
0404     // chdir before OPTICKS_LOG succeeds to write logfile named after executable into that directory 
0405 
0406     OPTICKS_LOG(argc, argv);
0407 
0408 /*
0409     test_Stem();  
0410     test_GetHomePath();  
0411     test_IsReadable();  
0412     test_IsReadable_path();  
0413     test_Basename_2(); 
0414     test_Resolve_With_Index();  
0415     test_Resolve_Default(); 
0416 */  
0417     test_Resolve(); 
0418 /*
0419     test_Basename(); 
0420     test_Dirname(); 
0421     test_UserTmpDir(); 
0422     test_ChangeName(); 
0423     test_MakeDirs(); 
0424     test_MakePath(); 
0425     test_Resolve_createdirs(); 
0426     test_getcwd(); 
0427     test_mtime(); 
0428     test_MakeName(); 
0429     test_Make(); 
0430     test_MakeEmpty(); 
0431     test_Remove(); 
0432     test_SearchDirUpTreeWithFile(); 
0433     test_Copy(); 
0434     test_Join();   
0435 */
0436  
0437     return 0  ; 
0438 }
0439 // om-;TEST=SPathTest om-t