Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // ~/opticks/sysrap/tests/ssys_test.sh
0002 
0003 #include <string>
0004 #include <iostream>
0005 #include <iomanip>
0006 #include "ssys.h"
0007 
0008 
0009 struct ssys_test
0010 {
0011     static constexpr const char* getenviron_SIGINT = "ssys_test__getenviron_SIGINT" ;
0012     static int getenviron();
0013     static int getenvuint64spec();
0014 };
0015 
0016 
0017 /**
0018 ssys_test::getenviron
0019 -----------------------
0020 
0021 Use this to check for envvars that identify running under ctest.
0022 
0023 **/
0024 
0025 int ssys_test::getenviron()
0026 {
0027     bool _SIGINT = ssys::hasenv_(getenviron_SIGINT);
0028     std::cout
0029         << "[ssys_test::getenviron"
0030         << " [" << getenviron_SIGINT << "] " << ( _SIGINT ? "YES" : "NO " ) << "\n"
0031         << "-ssys_test::getenviron.CTEST\n"
0032         << ssys::getenviron("CTEST")
0033         << "-ssys_test::getenviron.DART\n"
0034         << ssys::getenviron("DART")
0035         << "-ssys_test::getenviron.ALL\n"
0036         << ssys::getenviron()
0037         << "]ssys_test::getenviron\n"
0038         ;
0039 
0040     if(_SIGINT) std::raise(SIGINT);
0041     return 0 ;
0042 }
0043 
0044 
0045 int ssys_test::getenvuint64spec()
0046 {
0047     uint64_t pidx = ssys::getenvuint64spec("PIDX", "X40" );
0048     std::cout
0049          << "ssys_test::getenvuint64spec\n"
0050          << " pidx (hex) " << std::setw(15) << std::hex << pidx << std::dec
0051          << "\n"
0052          << " pidx (dec) " << std::setw(15) << pidx
0053          << "\n"
0054          ;
0055 
0056     return 0 ;
0057 }
0058 
0059 
0060 
0061 
0062 
0063 int test_popen_0()
0064 {
0065     const char* cmd = "md5 -q -s hello" ;
0066     bool chomp = false ;
0067     std::string ret = ssys::popen(cmd, chomp);
0068 
0069     std::cout
0070         << " cmd [" << cmd << "]"
0071         << std::endl
0072         << " ret [" << ret << "]"
0073         << std::endl
0074         ;
0075     return 0 ;
0076 }
0077 
0078 int test_popen_1()
0079 {
0080     std::cout << ssys::popen("md5 -q -s hello") << std::endl ;
0081     return 0 ;
0082 }
0083 
0084 
0085 int  test_is_remote_session()
0086 {
0087     std::cout << "ssys::is_remote_session [" << ( ssys::is_remote_session() ? "YES" : "NO " ) << "]" << std::endl ;
0088     return 0 ;
0089 }
0090 
0091 int test_username()
0092 {
0093     std::cout << "ssys::username [" << ssys::username() << "]" << std::endl ;
0094     return 0 ;
0095 }
0096 void test_which ()
0097 {
0098     std::cout << "ssys::which(\"md5\") [" << ssys::which("md5") << "]" << std::endl ;
0099 }
0100 
0101 
0102 
0103 void test_getenv_()
0104 {
0105     int i = ssys::getenv_<int>("i",0) ;
0106     unsigned u = ssys::getenv_<unsigned>("u",0) ;
0107     float f = ssys::getenv_<float>("f",0.f) ;
0108     double d = ssys::getenv_<double>("d",0.) ;
0109     std::string s = ssys::getenv_<std::string>("s","string") ;
0110 
0111     std::cout
0112         << "i " << i << std::endl
0113         << "u " << u << std::endl
0114         << "f " << f << std::endl
0115         << "d " << d << std::endl
0116         << "s " << s << std::endl
0117         ;
0118 }
0119 
0120 void test_getenv_vec()
0121 {
0122     std::vector<int>* ivec = ssys::getenv_vec<int>("IVEC", "1,2,3,4" );
0123     assert( ivec->size() == 4 );
0124     std::cout << "IVEC " << ssys::desc_vec<int>(ivec) << std::endl ;
0125 }
0126 
0127 void test_getenvvec_string_1()
0128 {
0129     std::vector<std::string>* svec = ssys::getenv_vec<std::string>("SVEC", "A1,B2,C3,D4" );
0130     assert( svec->size() == 4 );
0131     std::cout << "SVEC " << ssys::desc_vec<std::string>(svec) << std::endl ;
0132 }
0133 
0134 
0135 
0136 void test_getenvvec_string_2()
0137 {
0138     std::vector<std::string>* svec = ssys::getenv_vec<std::string>("SVEC", "A1,B2,C3,D4" );
0139     std::cout << "SVEC " << ssys::desc_vec<std::string>(svec) << std::endl ;
0140 
0141     for(int i=0 ; i < int(svec->size()) ; i++)
0142     {
0143         const std::string& s = (*svec)[i] ;
0144         std::cout << "[" << s << "]" << std::endl ;
0145     }
0146 }
0147 
0148 
0149 
0150 void test_getenv_kv()
0151 {
0152     typedef std::pair<std::string, std::string> KV ;
0153     std::vector<KV> kvs ;
0154 
0155     const char* kk = R"LITERAL(
0156 
0157     HOME
0158     CHECK
0159     OPTICKS_HOME
0160     COMMANDLINE
0161     ${GEOM}_GEOMList
0162 
0163 )LITERAL" ;
0164 
0165     //std::cout << "test_getenv_kv" << kk << std::endl ;
0166 
0167     ssys::getenv_(kvs, kk);
0168 
0169     for(int i=0 ; i < int(kvs.size()); i++)
0170     {
0171         const KV& kv = kvs[i];
0172         std::cout << std::setw(20) << kv.first << " : " << kv.second << std::endl ;
0173     }
0174 }
0175 
0176 void test__getenv()
0177 {
0178     const char* k = "${GEOM}_GEOMList" ;
0179     char* v = ssys::_getenv(k) ;
0180 
0181     std::cout
0182         << " k " << k
0183         << " v " << ( v ? v : "-" )
0184         << std::endl
0185         ;
0186 
0187 }
0188 
0189 void test_replace_envvar_token()
0190 {
0191     const char* kk = R"LITERAL(
0192     HOME
0193     ${HOME}_HELLO
0194     WORLD_${HOME}_HELLO
0195 
0196     ALL${VERSION}HELLO
0197     ALL${VERSIONX}HELLO
0198 
0199 )LITERAL" ;
0200 
0201     std::stringstream ss(kk) ;
0202     std::string str ;
0203     while (std::getline(ss, str))  // newlines are swallowed by getline
0204     {
0205         if(str.empty()) continue ;
0206 
0207         std::string rep = ssys::replace_envvar_token(str.c_str());
0208 
0209         std::cout
0210             << "[" << str << "]"
0211             << "[" << rep << "]"
0212             << std::endl
0213             ;
0214     }
0215 
0216 
0217 }
0218 
0219 void test_getenvfloat()
0220 {
0221     float f = ssys::getenvfloat("f",0.314f) ;
0222     std::cout << "f:" << std::scientific << f << std::endl ;
0223 }
0224 
0225 void test_getenvdouble()
0226 {
0227     double d = ssys::getenvdouble("d",1e-5 ) ;
0228     std::cout << "d:" << std::scientific << d << std::endl ;
0229 }
0230 
0231 void test_getenvvar()
0232 {
0233     const char* ekey = "OPTICKS_ELV_SELECTION,ELV" ;
0234     const char* val = ssys::getenvvar(ekey) ;
0235     std::cout
0236         << "test_getenvvar"
0237         << " ekey " << ( ekey ? ekey : "-" )
0238         << " val " << ( val ? val : "-" )
0239         << std::endl
0240         ;
0241 }
0242 
0243 void test_getenvvar_path_replacement()
0244 {
0245     const char* ekey = "stree__force_triangulate_solid" ;
0246     const char* _force_triangulate_solid = ssys::getenvvar(ekey, nullptr) ;
0247     char delim = ',' ;
0248 
0249     std::vector<std::string> force ;
0250     if( _force_triangulate_solid )
0251     {
0252         sstr::SplitTrimSuppress( _force_triangulate_solid, delim, force );
0253     }
0254 
0255     unsigned num_force = force.size();
0256 
0257     std::cout
0258         << "test_getenvvar_path_replacement "
0259         << " ekey " << ( ekey ? ekey : "-" )
0260         << " _force_triangulate_solid [" << ( _force_triangulate_solid ? _force_triangulate_solid : "-" ) << "]"
0261         << std::endl
0262         ;
0263 
0264     for(unsigned i=0 ; i < num_force ; i++)
0265     {
0266         const char* f = force[i].c_str() ;
0267         std::cout << std::setw(5) << i << ":[" << f << "]\n" ;
0268     }
0269 
0270 }
0271 
0272 
0273 void test_getenvfloatvec_fallback()
0274 {
0275     const char* ekey = "UKEY" ;
0276     std::vector<float>* fvec = ssys::getenvfloatvec(ekey, "0,0,0");
0277     int num_values = fvec ? fvec->size() : -1 ;
0278     std::cout
0279          << __FUNCTION__
0280          << " ekey " << ekey
0281          << " num_values " << num_values
0282          << std::endl
0283          ;
0284 }
0285 
0286 
0287 void test_is_listed()
0288 {
0289     std::vector<std::string> names = {"red", "green", "blue" } ;
0290     const std::vector<std::string>* nn = &names ;
0291     std::cout << __FUNCTION__ << " nn->size " << nn->size() << std::endl ;
0292 
0293     assert( ssys::is_listed(nn, "red")   == true );
0294     assert( ssys::is_listed(nn, "green") == true );
0295     assert( ssys::is_listed(nn, "blue")  == true );
0296     assert( ssys::is_listed(nn, "cyan")  == false );
0297 
0298     assert( ssys::is_listed(nullptr, "blue") == false );
0299 }
0300 
0301 
0302 void test_listed_count()
0303 {
0304 
0305     std::vector<int> ncount ;
0306     std::vector<std::string> names = {"red", "green", "blue" } ;
0307 
0308     std::vector<int>* nc = &ncount ;
0309     const std::vector<std::string>* nn = &names ;
0310 
0311     assert( ssys::listed_count(nc, nn, "pink") == -1 ) ;
0312     assert( ssys::listed_count(nc, nn, "red") == 0 ) ;
0313     assert( ssys::listed_count(nc, nn, "red") == 1 ) ;
0314     assert( ssys::listed_count(nc, nn, "green") == 0 ) ;
0315     assert( ssys::listed_count(nc, nn, "red") == 2 ) ;
0316     assert( ssys::listed_count(nullptr, nn, "red") == -1 ) ;
0317     assert( ssys::listed_count(nullptr, nullptr, "red") == -1 ) ;
0318     assert( ssys::listed_count(nc, nullptr, "red") == -1 ) ;
0319     assert( ssys::listed_count(nc, nn, "blue") == 0 ) ;
0320     assert( ssys::listed_count(nc, nn, "blue") == 1 ) ;
0321     assert( ssys::listed_count(nc, nn, "green") == 1 ) ;
0322     assert( ssys::listed_count(nc, nn, "green") == 2 ) ;
0323     assert( ssys::listed_count(nc, nn, "green") == 3 ) ;
0324     assert( ssys::listed_count(nc, nn, "blue") == 2 ) ;
0325     assert( ssys::listed_count(nc, nn, "green") == 4 ) ;
0326     assert( ssys::listed_count(nc, nn, "green") == 5 ) ;
0327     assert( ssys::listed_count(nc, nn, "green") == 6 ) ;
0328     assert( ssys::listed_count(nc, nn, "purple") == -1 ) ;
0329     assert( ssys::listed_count(nc, nn, "blue") == 3 ) ;
0330 
0331     std::cout << ssys::desc_listed_count(nc, nn) << std::endl ;
0332 }
0333 
0334 
0335 
0336 void test_make_vec()
0337 {
0338     const char* line = R"LITERAL(
0339     red
0340     green
0341     blue
0342     )LITERAL" ;
0343 
0344     std::vector<std::string>* v = ssys::make_vec<std::string>(line, '\n') ;
0345     int num_elem = v ? v->size() : 0 ;
0346 
0347     std::cout
0348         << "test_make_vec"
0349         << " num_elem " << num_elem
0350         << " line["
0351         << std::endl
0352         << line
0353         << std::endl
0354         ;
0355 
0356     for(int i=0 ; i < num_elem ; i++)
0357         std::cout << std::setw(3) << i << " : [" << (*v)[i] << "]" << std::endl ;
0358 }
0359 
0360 void test_getenv_vec_multiline()
0361 {
0362     const char* fallback = R"LITERAL(
0363     red
0364     green
0365     blue
0366     )LITERAL" ;
0367 
0368     const char* ekey = "MULTILINE" ;
0369 
0370     //char delim = ',' ;
0371     char delim = '\n' ;
0372 
0373     std::vector<std::string>* v = ssys::getenv_vec<std::string>(ekey, fallback, delim) ;
0374     int num_elem = v ? v->size() : 0 ;
0375 
0376     const char* eval = getenv(ekey) ;
0377 
0378     std::cout
0379         << "test_getenv_vec_multiline"
0380         << std::endl
0381         << " ekey " << ekey
0382         << std::endl
0383         << " eval [" << ( eval ? eval : "-" ) << "]"
0384         << std::endl
0385         ;
0386 
0387     for(int i=0 ; i < num_elem ; i++)
0388         std::cout << std::setw(3) << i << " : [" << (*v)[i] << "]" << std::endl ;
0389 }
0390 
0391 void test_getenv_multiline()
0392 {
0393     const char* ekey = "MULTILINE" ;
0394     const char* eval = getenv(ekey) ;
0395 
0396     std::cout << "test_getenv_multiline[" << std::endl ;
0397     std::cout << "[" << ( eval ? eval : "-" ) << "]" << std::endl ;
0398 }
0399 
0400 void test_fillvec()
0401 {
0402     std::vector<int> vec ;
0403     ssys::fill_vec<int>(vec, "10,20,-30,40,50", ',' );
0404     assert( vec.size() == 5 && vec[0] == 10 && vec[4] == 50 );
0405 }
0406 
0407 void test_fill_vec()
0408 {
0409     std::vector<int> vec ;
0410     ssys::fill_vec<int>(vec, "10,20,-30,40,50", ',' );
0411     assert( vec.size() == 5 && vec[0] == 10 && vec[4] == 50 );
0412 }
0413 
0414 
0415 template<typename T>
0416 void test_fill_evec()
0417 {
0418     std::vector<T> vec ;
0419     ssys::fill_evec<T>(vec, "EVEC", "10,20,-30,40,50", ',' );
0420 
0421     int num = vec.size() ;
0422 
0423     std::cout << "EVEC [" ;
0424     for(int i=0 ; i < num ; i++ ) std::cout << vec[i] << " " ;
0425     std::cout << " ] " << num << std::endl ;
0426 }
0427 
0428 void test_uname()
0429 {
0430     std::vector<std::string> args = {"-n", "-a",""} ;
0431     int num = args.size() ;
0432     for(int i=0 ; i < num ; i++) std::cout
0433         << std::setw(4) << args[i]
0434         << " : "
0435         << ssys::uname(args[i].c_str())
0436         << std::endl
0437         ;
0438 }
0439 
0440 
0441 void test_fill_evec_string()
0442 {
0443     std::vector<std::string> mat ;
0444     ssys::fill_evec(mat, "MAT", "Rock,Air,Water", ',' ) ;
0445     std::cout << ssys::desc_vec(&mat) << std::endl ;
0446 }
0447 
0448 void test_Dump()
0449 {
0450     ssys::Dump("test_Dump");
0451     ssys::Dump("test_Dump");
0452     ssys::Dump("test_Dump");
0453     ssys::Dump("test_Dump");
0454 }
0455 
0456 void test_Desc()
0457 {
0458     std::cout << ssys::Desc();
0459 }
0460 void test_PWD()
0461 {
0462     std::cout << ssys::PWD();
0463 }
0464 
0465 
0466 void test_getenv_ParseInt64()
0467 {
0468     std::vector<std::string> _fallback = { "1", "10", "20", "M1" , "K1", "M2", "M3" } ;
0469 
0470     int num_fallback = _fallback.size();
0471     const int64_t K = 1000 ;
0472     const int64_t M = 1000000 ;
0473     const char* ekey = "HELLO" ;
0474     std::cout << "test_getenv_ParseInt ekey " << ekey << std::endl ;
0475 
0476     for(int i=0 ; i < num_fallback ; i++)
0477     {
0478         const char* fallback = _fallback[i].c_str();
0479         int64_t num = ssys::getenv_ParseInt64(ekey, fallback );
0480 
0481         std::cout
0482             << std::setw(20) << fallback
0483             << " num:   " << std::setw(10) << std::scientific << num
0484             << " num/M: " << std::setw(10) << num/M
0485             << " num/K: " << std::setw(10) << num/K
0486             << std::endl
0487             ;
0488     }
0489 }
0490 
0491 
0492 
0493 void test_getenv_with_prefix()
0494 {
0495     typedef std::pair<std::string,std::string> KV ;
0496 
0497     std::vector<KV> kvs ;
0498     ssys::getenv_with_prefix(kvs, "OPTICKS_");
0499 
0500     int num_kvs = kvs.size() ;
0501     for(int i=0 ; i < num_kvs ; i++)
0502     {
0503         const KV& kv = kvs[i];
0504         std::cout
0505             << std::setw(3) << i << " "
0506             << "[" << kv.first << "]"
0507             << "[" << kv.second << "]"
0508             << std::endl
0509             ;
0510     }
0511 
0512 }
0513 
0514 void test_getenvintvec()
0515 {
0516     const char* ekey = "CEGS" ;
0517     char delim = ':' ;
0518     const char* fallback = "16:0:9:1000" ;
0519 
0520     std::vector<int> cegs ;
0521     ssys::getenvintvec(ekey, cegs, delim, fallback );
0522 
0523     std::cout << "cegs[" ;
0524     for(int i=0 ; i < int(cegs.size()) ; i++) std::cout << cegs[i] << " " ;
0525     std::cout << "]" << std::endl ;
0526 
0527     assert( cegs.size() == 4 );
0528 }
0529 
0530 int test_getenvint64spec()
0531 {
0532     std::vector<std::string> specs = {{"0", "1", "2", "M1", "k1", "K1", "H1", "H10", "G1", "yellow", "magenta", "" }} ;
0533     const char* ekey = "test_getenvintspec_dummy" ;
0534 
0535     for(int i=0 ; i < int(specs.size()) ; i++)
0536     {
0537        const char* fallback = specs[i].c_str();
0538        int64_t ival = ssys::getenvint64spec(ekey, fallback ) ;
0539        std::cout
0540            << std::setw(4) << i
0541            << " "
0542            << " ssys::getenvint64spec(\"" << ekey << "\",\"" << std::setw(10) << fallback << "\") "
0543            << " : "
0544            << ival
0545            << "\n"
0546            ;
0547     }
0548     return 0 ;
0549 }
0550 
0551 
0552 
0553 
0554 
0555 
0556 
0557 
0558 
0559 
0560 
0561 void test_getenvintpick()
0562 {
0563     std::vector<std::string> colors = {{"red", "green", "blue", "cyan", "yellow", "magenta" }} ;
0564     int idx = ssys::getenvintpick("COLOR", colors, -1 );
0565 
0566     std::cout
0567        << "test_getenvintpick\n"
0568        << " idx " << idx << "\n"
0569        << " pick " << (( idx < int(colors.size()) && idx > -1 ) ? colors[idx] : "-" ) << "\n"
0570        ;
0571 }
0572 
0573 
0574 
0575 
0576 int main(int argc, char** argv)
0577 {
0578     //const char* DEFT = "getenvintspec" ;
0579     const char* DEFT = "getenviron" ;
0580     const char* TEST = ssys::getenvvar("TEST", DEFT );
0581     bool ALL = strcmp("ALL", TEST) == 0 ;
0582 
0583     int rc = 0 ;
0584     if(ALL||0==strcmp(TEST,"popen_0")) rc += test_popen_0() ;
0585     if(ALL||0==strcmp(TEST,"popen_1")) rc += test_popen_1() ;
0586     if(ALL||0==strcmp(TEST,"username")) rc += test_username() ;
0587     if(ALL||0==strcmp(TEST,"is_remote_session")) rc += test_is_remote_session() ;
0588     if(ALL||0==strcmp(TEST,"getenvint64spec"))  rc += test_getenvint64spec() ;
0589     if(ALL||0==strcmp(TEST,"getenviron"))  rc += ssys_test::getenviron() ;
0590     if(ALL||0==strcmp(TEST,"getenvuint64spec"))  rc += ssys_test::getenvuint64spec() ;
0591 
0592 
0593 
0594     /*
0595     test_getenv_vec();
0596     test_getenv_();
0597     test_getenvvec_string_1();
0598     test_getenvvec_string_2();
0599     test__getenv();
0600     test_getenv_kv();
0601     test_replace_envvar_token();
0602     test_getenv_kv();
0603     test_getenvfloat();
0604     test_getenvdouble();
0605     test_which();
0606     test_getenvvar();
0607     test_is_listed();
0608     test_make_vec();
0609     test_getenv_multiline();
0610     test_getenv_vec_multiline();
0611     test_fill_vec();
0612 
0613     test_fill_evec<int>();
0614     test_fill_evec<float>();
0615     test_fill_evec<double>();
0616     test_fill_evec<std::string>();
0617     test_uname();
0618     test_listed_count();
0619     test_fill_evec_string();
0620     test_Dump();
0621     test_Desc();
0622     test_PWD();
0623     test_getenv_ParseInt64();
0624     test_getenv_with_prefix();
0625     test_getenvintvec();
0626     test_getenvfloatvec_fallback();
0627     test_getenvintpick();
0628     test_getenvvar_path_replacement();
0629     */
0630 
0631     return rc ;
0632 }
0633 
0634 // ~/opticks/sysrap/tests/ssys_test.sh
0635