File indexing completed on 2026-04-09 07:49:22
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <sstream>
0021 #include <string>
0022 #include <limits>
0023 #include <csignal>
0024
0025 #include "SSys.hh"
0026
0027 #include "OPTICKS_LOG.hh"
0028
0029
0030 int test_tpmt()
0031 {
0032 return SSys::run("tpmt.py");
0033 }
0034
0035 int test_RC(int irc)
0036 {
0037
0038 std::stringstream ss ;
0039 ss << "python -c 'import sys ; sys.exit(" << irc << ")'" ;
0040 std::string s = ss.str();
0041 return SSys::run(s.c_str());
0042 }
0043
0044 void test_RunPythonScript()
0045 {
0046 const char* script = "np.py" ;
0047 SSys::RunPythonScript(script);
0048 }
0049
0050
0051 void test_RC()
0052 {
0053 int rc(0);
0054 for(int irc=0 ; irc < 500 ; irc+=10 )
0055 {
0056 int xrc = irc & 0xff ;
0057 rc = test_RC(irc);
0058 bool xrc_expect = rc == xrc ;
0059 assert( xrc_expect );
0060 if(!xrc_expect) std::raise(SIGINT);
0061 }
0062 }
0063
0064
0065 void test_DumpEnv()
0066 {
0067 SSys::DumpEnv("OPTICKS");
0068 }
0069
0070 void test_IsNegativeZero()
0071 {
0072 float f = -0.f ;
0073 float z = 0.f ;
0074
0075 bool expect = SSys::IsNegativeZero(f) == true
0076 && SSys::IsNegativeZero(z) == false
0077 && SSys::IsNegativeZero(-1.f) == false
0078 ;
0079 assert( expect );
0080 if(!expect) std::raise(SIGINT);
0081 }
0082
0083 void test_hostname()
0084 {
0085 LOG(info) << SSys::hostname() ;
0086 }
0087
0088
0089 void test_POpen(bool chomp)
0090 {
0091 std::vector<std::string> cmds = {"which python", "ls -l" };
0092 int rc(0);
0093
0094 for(unsigned i=0 ; i < cmds.size() ; i++)
0095 {
0096 const char* cmd = cmds[i].c_str() ;
0097 LOG(info) << " cmd " << cmd << " chomp " << chomp ;
0098 std::string out = SSys::POpen(cmd, chomp, rc ) ;
0099
0100 LOG(info)
0101 << " out " << out
0102 << " rc " << rc
0103 ;
0104 }
0105 }
0106
0107
0108 void test_POpen2(bool chomp)
0109 {
0110 std::vector<std::string> cmds = {"which", "python", "ls", "-l" };
0111 int rc(0);
0112
0113 for(unsigned i=0 ; i < cmds.size()/2 ; i++)
0114 {
0115 const char* cmda = cmds[i*2+0].c_str() ;
0116 const char* cmdb = cmds[i*2+1].c_str() ;
0117 LOG(info)
0118 << " cmda " << cmda
0119 << " cmdb " << cmdb
0120 << " chomp " << chomp
0121 ;
0122 std::string out = SSys::POpen(cmda, cmdb, chomp, rc ) ;
0123 LOG(info)
0124 << " out " << out
0125 << " rc " << rc
0126 ;
0127
0128 }
0129 }
0130
0131 void test_Which()
0132 {
0133 std::vector<std::string> scripts = {"python", "nonexisting" };
0134
0135 for(unsigned i=0 ; i < scripts.size() ; i++)
0136 {
0137 const char* script = scripts[i].c_str();
0138 std::string path = SSys::Which( script );
0139 LOG(info)
0140 << " script " << script
0141 << " path " << path
0142 << ( path.empty() ? " EMPTY PATH " : "" )
0143 ;
0144
0145 }
0146 }
0147
0148
0149
0150 void test_hexlify()
0151 {
0152 unsigned u = 0x12abcdef ;
0153 std::cout << std::hex << u << " std::hex " << std::endl ;
0154 std::cout << SSys::hexlify(&u,4,true) << " SSys::hexlify reverse=true " << std::endl ;
0155 std::cout << SSys::hexlify(&u,4,false) << " SSys::hexlify reverse=false " << std::endl ;
0156 }
0157
0158 void test_getenvintvec()
0159 {
0160 LOG(info);
0161 std::vector<int> ivec ;
0162
0163 std::vector<int> ivals = {42,43,-44,1,2,3} ;
0164 std::stringstream ss ;
0165 for(unsigned i=0 ; i < ivals.size() ; i++)
0166 {
0167 ss << ivals[i] << ( i < ivals.size() -1 ? "," : "" ) ;
0168 }
0169 std::string s = ss.str();
0170
0171 const char* key = "SSYSTEST_GETENVINTVEC" ;
0172 bool overwrite = true ;
0173 SSys::setenvvar(key, s.c_str(), overwrite);
0174
0175 std::vector<int> ivals2 ;
0176 unsigned n = SSys::getenvintvec(key, ivals2);
0177 assert( n == ivals.size() );
0178 assert( n == ivals2.size() );
0179
0180 for(unsigned i=0 ; i < n ; i++)
0181 {
0182 std::cout << i << " " << ivals[i] << " " << ivals2[i] << std::endl ;
0183 assert( ivals[i] == ivals2[i] );
0184 }
0185 }
0186
0187 void test_getenvintvec_non()
0188 {
0189 std::vector<int> m_skip_gencode ;
0190 LOG(info) << " m_skip_gencode.size " << m_skip_gencode.size() ;
0191
0192 SSys::getenvintvec("OPTICKS_SKIP_GENCODE", m_skip_gencode, ',', "");
0193 LOG(info) << " m_skip_gencode.size " << m_skip_gencode.size() ;
0194
0195 for(unsigned i=0 ; i < m_skip_gencode.size() ; i++) std::cout << m_skip_gencode[i] << std::endl ;
0196 }
0197
0198
0199
0200 void test_getenvintvec_ptr()
0201 {
0202 LOG(info);
0203 const char* key = "SSYSTEST_IVEC" ;
0204 std::vector<int>* ivec = SSys::getenvintvec(key);
0205
0206 LOG(info)
0207 << " key " << key
0208 << " ivec " << ivec
0209 << " ivec.size " << ( ivec ? ivec->size() : 0 )
0210 ;
0211
0212 if(ivec) for(unsigned i=0 ; i < ivec->size() ; i++ ) std::cout << (*ivec)[i] << std::endl ;
0213 }
0214
0215
0216
0217
0218
0219
0220 void test_atof()
0221 {
0222 const char* s = "45.2" ;
0223 float f = SSys::atof_(s) ;
0224 LOG(info)
0225 << " s " << s
0226 << " f " << f
0227 ;
0228
0229 }
0230
0231 void test_getenvfloat()
0232 {
0233 LOG(info);
0234 const char* key = "SSYSTEST_GETENVFLOAT" ;
0235
0236 float f = 42.5f ;
0237 std::stringstream ss ;
0238 ss << std::fixed << f ;
0239 std::string s = ss.str();
0240
0241 bool overwrite = true ;
0242 SSys::setenvvar(key, s.c_str(), overwrite);
0243
0244 float f2 = SSys::getenvfloat(key);
0245 LOG(info)
0246 << " f " << f
0247 << " s " << s
0248 << " f2 " << f2
0249 ;
0250 assert( f2 == f );
0251 }
0252
0253 void test_OS()
0254 {
0255 LOG(info) << SSys::OS ;
0256
0257 }
0258
0259 void test_getenvfloatvec()
0260 {
0261 const char* ekey = "CE_OFFSET" ;
0262 const char* ceo = "0.0,-666.6,0.0" ;
0263 SSys::setenvvar(ekey, ceo );
0264
0265 std::vector<float>* fvec = SSys::getenvfloatvec(ekey);
0266 std::cout << SSys::Desc(fvec) << std::endl ;
0267 }
0268
0269
0270 void test_getenvunsigned_fallback_max()
0271 {
0272 unsigned pidx = SSys::getenvunsigned_fallback_max("PIDX");
0273 LOG(info) << " pidx " << pidx << " 0x" << std::hex << pidx << std::dec;
0274 }
0275 void test_getenvunsigned()
0276 {
0277 unsigned num = SSys::getenvunsigned("NUM", 0u);
0278 LOG(info) << " NUM " << num ;
0279 }
0280
0281
0282
0283 int main(int argc , char** argv )
0284 {
0285 OPTICKS_LOG(argc, argv);
0286
0287 int rc(0) ;
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314 test_getenvunsigned();
0315
0316
0317
0318 return rc ;
0319 }
0320