Back to home page

EIC code displayed by LXR

 
 

    


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

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=SBitTest om-t
0021 
0022 #include "OPTICKS_LOG.hh"
0023 #include "SBit.hh"
0024 #include <cassert>
0025 #include <csignal>
0026 #include <iostream>
0027 #include <iomanip>
0028 #include <bitset>
0029 
0030 void test_ffs()
0031 {
0032     LOG(info);
0033     for(int i=0 ; i < 64 ; i++ )
0034     {
0035         int msk = 0x1 << i ; 
0036         int chk = SBit::ffs(msk) - 1;   
0037         std::cout 
0038                   << " msk ( 0x1 << " << std::setw(2) << std::dec << i << ") = "  << std::hex << std::setw(16) << msk  
0039                   << " SBit::ffs(msk) - 1 =  " << std::dec << std::setw(4) << chk
0040                   << std::endl ;   
0041 
0042          if(i < 32 ) assert(  chk == i );
0043     } 
0044 }
0045 
0046 void test_ffsll()
0047 {
0048     LOG(info);
0049     typedef long long LL ; 
0050 
0051     for(LL i=0 ; i < 64 ; i++ )
0052     {
0053         LL msk = 0x1ll << i ; 
0054         LL chk = SBit::ffsll(msk) - 1ll ;   
0055         std::cout 
0056                   << " msk ( 0x1 << " << std::setw(2) << std::dec << i << ") = "  << std::hex << std::setw(16) << msk  
0057                   << " SBit::ffsll(msk) - 1 =  " << std::dec << std::setw(4) << chk
0058                   << std::endl ;   
0059 
0060         assert(  chk == i );
0061     } 
0062 }
0063 
0064 
0065 void test_count_nibbles()
0066 {
0067     LOG(info);
0068     typedef unsigned long long ULL ; 
0069 
0070     ULL msk = 0ull ;  
0071     for(int i=-1 ; i < 64 ; i++ )
0072     {
0073         if( i > -1 )
0074         {
0075             ULL u = i ; 
0076             msk |= 0x1ull << u ;  
0077         }
0078 
0079         ULL nn = SBit::count_nibbles(msk); 
0080         std::cout 
0081              << " msk 0x " << std::hex << std::setw(16) << msk  
0082              << " nibbles " << std::dec << std::setw(4) << nn
0083              << std::endl 
0084              ;
0085     }
0086 }
0087 
0088 
0089 void test_HasOneSetBit()
0090 {
0091     LOG(info);
0092 
0093     for(int i=0 ; i < 32 ; i++ )
0094     {
0095         unsigned msk0 = 0x1 << i ; 
0096         unsigned msk1 = 0x1 << (32 - i - 1) ; 
0097         unsigned msk01 = msk0 | msk1 ; 
0098 
0099         bool onebit0 = SBit::HasOneSetBit(msk0); 
0100         bool onebit1 = SBit::HasOneSetBit(msk1); 
0101         bool onebit01 = SBit::HasOneSetBit(msk01); 
0102 
0103         std::cout 
0104              << " i " << std::setw(3) << i         
0105              << " msk0 " << std::setw(10) << std::hex << msk0         
0106              << " msk1 " << std::setw(10) << std::hex << msk1         
0107              << " msk01 " << std::setw(10) << std::hex << msk01         
0108              << " onebit0 " << std::setw(2) << std::dec << onebit0
0109              << " onebit1 " << std::setw(2) << std::dec << onebit1
0110              << " onebit01 " << std::setw(2) << std::dec << onebit01
0111              << std::endl 
0112              ;
0113 
0114         assert( onebit0 == true );
0115         assert( onebit1 == true );
0116         assert( onebit01 == false );
0117     }
0118 }
0119 
0120 
0121 #define DUMP(l, s) \
0122     { \
0123     std::string binstr = SBit::BinString((s)) ; \
0124     std::string hexstr = SBit::HexString((s)) ; \
0125     unsigned long long ull = SBit::FromBinString(binstr.c_str()) ; \
0126     unsigned long long v((s)) ; \
0127     bool match = ull == v ;  \
0128     std::cout \
0129         << std::setw(5) << (l) \
0130         << std::setw(5) << sizeof((s))   \
0131         << std::setw(5) << sizeof((s))*8 \
0132         << std::setw(30) << (s) \
0133         << " : " \
0134         << std::setw(64) \
0135         << binstr \
0136         << std::setw(32) \
0137         << hexstr \
0138         << " : " \
0139         << std::setw(32) << ull \
0140         << ( match ? " Y" : " N" ) \
0141         << std::endl \
0142         ; \
0143     } 
0144   
0145 
0146 void test_BinString()
0147 {
0148     unsigned char uc = ~0 ; 
0149     unsigned int  ui = ~0 ; 
0150     unsigned long ul = ~0 ; 
0151     unsigned long long ull = ~0 ; 
0152 
0153     char c = ~0 ; 
0154     int  i = ~0 ; 
0155     long l = ~0 ; 
0156     long long ll = ~0 ; 
0157 
0158     DUMP("uc",uc); 
0159     DUMP("ui",ui); 
0160     DUMP("ul",ul); 
0161     DUMP("ull",ull); 
0162 
0163     DUMP("c",c); 
0164     DUMP("i",i); 
0165     DUMP("l",l); 
0166     DUMP("ll",ll); 
0167 }
0168 
0169 
0170 void test_FromBinString()
0171 {
0172     {
0173         unsigned long long ull = ~0 ; 
0174         std::string binstr = SBit::BinString(ull); 
0175         std::cout 
0176              << "ull " << ull 
0177              << "0b " << binstr 
0178              << std::endl
0179              ; 
0180         unsigned long long ull2 = SBit::FromBinString(binstr.c_str()) ; 
0181         bool ull_expect =  ull == ull2 ;
0182         assert( ull_expect ); 
0183         if(!ull_expect) std::raise(SIGINT); 
0184     }
0185     {
0186         int i = ~0 ; 
0187         std::string binstr = SBit::BinString(i); 
0188         unsigned long long ull = SBit::FromBinString(binstr.c_str()) ; 
0189         std::cout 
0190              << "i " << std::setw(10) << i 
0191              << "0b " << std::setw(64) << binstr 
0192              << "ull " << std::setw(32) << ull 
0193              << std::endl
0194              ;
0195     }
0196 }
0197 
0198 void test_FromString_0()
0199 {
0200     unsigned long long ull_0 = SBit::FromString("0b11111111") ; 
0201     assert( ull_0 == 255 ); 
0202     unsigned long long ull_1 = SBit::FromString("0xff") ; 
0203     assert( ull_1 == 255 ); 
0204     unsigned long long ull_2 = SBit::FromString("255") ; 
0205     assert( ull_2 == 255 ); 
0206     unsigned long long ull_3 = SBit::FromString("0,1,2,3,4,5,6,7") ; 
0207     assert( ull_3 == 255 ); 
0208 
0209     LOG(info) 
0210         << std::endl 
0211         << " ull_0 " << ull_0 
0212         << std::endl 
0213         << " ull_1 " << ull_1 
0214         << std::endl 
0215         << " ull_2 " << ull_2
0216         << std::endl 
0217         << " ull_3 " << ull_3 
0218         << std::endl 
0219         ; 
0220 
0221 
0222 }
0223 
0224 
0225 
0226 
0227 void test_FromPosString()
0228 {
0229     const char* posstr_0 = "0,1,2,3,4,5,6,7" ; 
0230     unsigned long long ull_0 = SBit::FromPosString(posstr_0) ; 
0231     LOG(info) << " ull_0 " << ull_0 ; 
0232     assert( ull_0 == 255 ); 
0233 
0234     std::string ps = SBit::PosString(ull_0) ; 
0235     LOG(info) << " PosString ps:" << ps << " posstr_0:" << posstr_0 ; 
0236     assert( strcmp( ps.c_str(), posstr_0  ) == 0 ); 
0237 
0238     const char* posstr_1 = "63," ; 
0239     unsigned long long ull_1 = SBit::FromPosString(posstr_1) ; 
0240     unsigned long long ull_1x = 1ull << 63 ;  
0241     LOG(info) << " ull_1 " << ull_1 ; 
0242     LOG(info) << " ull_1x " << ull_1x ; 
0243     assert( ull_1 == ull_1x ); 
0244 
0245     std::string ps1 = SBit::PosString(ull_1) ; 
0246     LOG(info) << " ps1 " << ps1 ; 
0247     assert( strcmp( ps1.c_str(), posstr_1  ) == 0 ); 
0248 
0249 }
0250 
0251 void test_signbit()
0252 {
0253     int a = 0 ; 
0254     a |= 0x80000000 ; 
0255 
0256     int i = a & 0x7fffffff ; 
0257     int j = ( a & 0x80000000 ) >> 31 ; 
0258 
0259     LOG(info) 
0260          << " a " << a 
0261          << " i " << i 
0262          << " j " << j 
0263          ; 
0264 
0265 
0266 }
0267 
0268 struct TrackInfo 
0269 {
0270     TrackInfo( unsigned record_id_ , char gentype_  )
0271         :   
0272         packed((record_id_ & 0x7fffffff) | unsigned(gentype_ == 'C') << 31 )   
0273     {   
0274     }   
0275     unsigned packed  ;   
0276 
0277     char gentype() const       { return ( packed & 0x80000000 ) ? 'C' : 'S' ;  }
0278     unsigned record_id() const { return ( packed & 0x7fffffff ) ; }
0279 };
0280 
0281 void test_TrackInfo()
0282 {
0283      std::vector<std::string> checks = { "C0", "S0", "C10", "S10", "S100000", "C1000000" } ; 
0284 
0285      for(unsigned i=0 ; i < checks.size() ; i++)
0286      {
0287          const char* chk = checks[i].c_str(); 
0288          char gt = chk[0] ;
0289          unsigned ri = std::atoi(chk+1) ; 
0290 
0291          TrackInfo a(ri, gt) ;
0292 
0293          LOG(info) << " chk " << chk << " gt " << gt << " ri " << ri ; 
0294 
0295 
0296          assert( a.record_id() == ri ); 
0297          assert( a.gentype() == gt ); 
0298 
0299 
0300      }
0301 }
0302 
0303 
0304 /**
0305 
0306 
0307 As expected to_ullong() gives overflow for N > 64
0308 
0309 libc++abi.dylib: terminating with uncaught exception of type std::overflow_error: bitset to_ullong overflow error
0310 
0311 **/
0312 
0313 template <size_t N>
0314 void test_bitset_()
0315 {
0316     std::bitset<N>* bs = new std::bitset<N>(); 
0317     bs->set(0, true);  
0318     bs->set(N/2, true);  
0319     bs->set(N-1, true);  
0320     std::string s = bs->to_string() ; 
0321     
0322     std::cout << std::setw(4) << N << ":" << s << ":" << std::endl ;     
0323 
0324     if( N <= 64 )  
0325     {
0326         unsigned long long u = bs->to_ullong() ; 
0327         std::cout << " ull (hex) " << std::hex << u << " (dec) " << std::dec << u << std::endl ;
0328     }
0329 
0330 }
0331 
0332 void test_bitset()
0333 {
0334     LOG(info); 
0335     test_bitset_<1>();   
0336     test_bitset_<2>(); 
0337     test_bitset_<4>(); 
0338     test_bitset_<8>(); 
0339     test_bitset_<16>(); 
0340     test_bitset_<32>(); 
0341     test_bitset_<64>(); 
0342     test_bitset_<128>(); 
0343     test_bitset_<256>(); 
0344     test_bitset_<512>(); 
0345     test_bitset_<1024>(); 
0346 }
0347 
0348 
0349 
0350 int main(int argc, char** argv)
0351 {
0352     OPTICKS_LOG(argc, argv); 
0353 
0354 /*
0355     test_ffs(); 
0356     test_ffsll(); 
0357     test_count_nibbles(); 
0358     test_HasOneSetBit(); 
0359     test_BinString(); 
0360     test_FromBinString(); 
0361     test_FromString_0(); 
0362     test_FromString(); 
0363     test_FromPosString(); 
0364     test_signbit(); 
0365     test_TrackInfo();   
0366 */
0367 
0368     test_bitset(); 
0369 
0370     return 0 ; 
0371 }
0372 
0373 // om-;TEST=SBitTest om-t
0374