Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // om-;TEST=SVecTest om-t 
0002 /*
0003  * Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004  *
0005  * This file is part of Opticks
0006  * (see https://bitbucket.org/simoncblyth/opticks).
0007  *
0008  * Licensed under the Apache License, Version 2.0 (the "License"); 
0009  * you may not use this file except in compliance with the License.  
0010  * You may obtain a copy of the License at
0011  *
0012  *   http://www.apache.org/licenses/LICENSE-2.0
0013  *
0014  * Unless required by applicable law or agreed to in writing, software 
0015  * distributed under the License is distributed on an "AS IS" BASIS, 
0016  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017  * See the License for the specific language governing permissions and 
0018  * limitations under the License.
0019  */
0020 
0021 #include <cmath>
0022 #include <csignal>
0023 #include <algorithm>
0024 #include "SVec.hh"
0025 
0026 #include "OPTICKS_LOG.hh"
0027 
0028 
0029 void test_MaxDiff()
0030 {
0031     float epsilon = 1e-5 ; 
0032 
0033     std::vector<float> a = {1,2,3,4} ;
0034     std::vector<float> b = {1.1,2.2,3.3,4.4} ;
0035 
0036     bool dump = true ; 
0037     float md = SVec<float>::MaxDiff(a, b, dump) ; 
0038     float md_x = 0.4f ; 
0039     float md_d = std::fabs(md - md_x) ; 
0040 
0041     bool md_d_expect = md_d < epsilon ; 
0042     assert( md_d_expect );
0043     if(!md_d_expect) std::raise(SIGINT); 
0044 }
0045 
0046 void test_FindIndexOfValue()
0047 {
0048     std::vector<float> a = {1.1,2.2,3.3,4.4} ;
0049     int idx ;  
0050 
0051     idx = SVec<float>::FindIndexOfValue( a, 3.3f, 1e-6f );
0052     bool idx_expect = idx == 2 ;
0053     assert( idx_expect );
0054     if(!idx_expect) std::raise(SIGINT);
0055 
0056     idx = SVec<float>::FindIndexOfValue( a, 5.5f, 1e-6f );
0057     idx_expect = idx == -1  ;
0058     assert( idx_expect );
0059     if(!idx_expect) std::raise(SIGINT);
0060 
0061 }
0062 
0063 
0064 void test_FindIndexOfValue_NoTolerance_float()
0065 {
0066     LOG(info) ; 
0067     std::vector<float> a = {1.1,2.2,3.3,4.4} ;
0068     int idx ;  
0069 
0070     idx = SVec<float>::FindIndexOfValue( a, 3.3f);
0071     bool idx_expect = idx == 2 ;
0072     assert( idx_expect );
0073     if(!idx_expect) std::raise(SIGINT);
0074 
0075 
0076     idx = SVec<float>::FindIndexOfValue( a, 5.5f);
0077     idx_expect = idx == -1  ;
0078     assert( idx_expect );
0079     if(!idx_expect) std::raise(SIGINT);
0080 }
0081 
0082 void test_FindIndexOfValue_NoTolerance_int()
0083 {
0084     LOG(info) ;        // 0  1   2   3   4   5   6   7    8
0085     std::vector<int> a = {1, 2, 10, 20, 30, 11, 81, 42, 101 } ;
0086     int idx ;  
0087 
0088     bool idx_expect ; 
0089 
0090     idx = SVec<int>::FindIndexOfValue( a, 1);
0091     idx_expect = idx == 0 ;
0092     assert( idx_expect );
0093     if(!idx_expect) std::raise(SIGINT);
0094 
0095 
0096     idx = SVec<int>::FindIndexOfValue( a, 42);
0097     idx_expect = idx == 7 ;
0098     assert( idx_expect );
0099     if(!idx_expect) std::raise(SIGINT);
0100 
0101     idx = SVec<int>::FindIndexOfValue( a, 1000 );
0102     idx_expect = idx == -1 ;
0103     assert( idx_expect );
0104     if(!idx_expect) std::raise(SIGINT);
0105 
0106 }
0107 
0108 
0109 
0110 void test_vector_erase_pos()
0111 {
0112     LOG(info); 
0113 
0114     std::vector<int> a = {0,1,2,3,4,5,6,7,8,9} ;
0115 
0116     auto pos = std::find(std::begin(a), std::end(a), 5);
0117 
0118     a.erase(pos); 
0119 
0120     LOG(info) << " a: " << a.size(); 
0121     for(unsigned i=0 ; i < a.size() ; i++) std::cout << a[i] << " " ; 
0122     std::cout << std::endl ; 
0123 }
0124 
0125 
0126 
0127 
0128 // clang comes up with (4 * 10000 + 2 * 100 + 1)
0129 
0130 #define GCC_VERSION (__GNUC__ * 10000 \
0131                      + __GNUC_MINOR__ * 100 \
0132                      + __GNUC_PATCHLEVEL__)
0133 
0134 
0135 #define XSTR(x) STR(x)
0136 #define STR(x) #x
0137 //#pragma message "GCC_VERSION = " XSTR(GCC_VERSION) 
0138 
0139 
0140 void test_vector_erase_all()
0141 {
0142     LOG(info); 
0143 
0144     const char* gcc_version = XSTR(GCC_VERSION) ;
0145     LOG(info) << " GCC_VERSION : " << gcc_version ;  
0146 
0147     std::vector<int> a = {0,1,2,3,4,5,6,7,8,9} ;
0148 
0149     // GCC_VERSION cut is guess based on what Geant4 1062 needs
0150 #if GCC_VERSION > 40903 || __clang__  
0151 
0152     std::vector<int>::const_iterator beg = a.cbegin(); 
0153     std::vector<int>::const_iterator end = a.cend(); 
0154 
0155     a.erase(beg, end); 
0156 #else
0157     LOG(fatal) << " needs newer GCC_VERSION than : " << gcc_version ; 
0158 #endif
0159 
0160     LOG(info) << " a: " << a.size(); 
0161     for(unsigned i=0 ; i < a.size() ; i++) std::cout << a[i] << " " ; 
0162     std::cout << std::endl ; 
0163 }
0164 
0165 void test_unique_strings()
0166 {
0167     std::vector<std::string> v = { "red", "green", "blue", "cyan", "magenta", "yellow", "green" } ; 
0168     std::vector<std::string> u ; 
0169 
0170     for(unsigned i=0 ; i < v.size() ; i++)
0171     {
0172         const std::string& s = v[i] ;  
0173         if(std::find(u.begin(), u.end(), s ) == u.end()) u.push_back(s); 
0174     }
0175 
0176     for(unsigned i=0 ; i < u.size() ; i++) std::cout << u[i] << std::endl ; 
0177 }
0178 
0179 
0180 void test_MinMaxAvg()
0181 {
0182     std::vector<float> v = {1.f, 10.f, 100.f, 2.f, 1000.f } ;  
0183     float mn, mx, av ; 
0184     SVec<float>::MinMaxAvg(v,mn,mx,av);
0185 
0186     LOG(info) 
0187         << " mn " << mn 
0188         << " mx " << mx 
0189         << " av " << av 
0190         ;
0191 
0192 }
0193 
0194 
0195 void test_Desc()
0196 {
0197      std::vector<int> v = { 380, 400, 420, 440, 460 } ; 
0198      LOG(info) << SVec<int>::Desc("v", v); 
0199 }
0200 
0201 void test_Extract()
0202 {
0203      const char* str = "(-0.585,-0.805, 0.098, 0.000) (-0.809, 0.588, 0.000, 0.000) (-0.057,-0.079,-0.995, 0.000) (1022.116,1406.822,17734.953, 1.000)"  ; 
0204 
0205      std::vector<float> v ; 
0206      SVec<float>::Extract(v, str); 
0207 
0208      for(unsigned i=0 ; i < v.size() ; i++) std::cout << v[i] << std::endl ; 
0209 }
0210 
0211 
0212 
0213 
0214 int main(int argc, char** argv)
0215 {
0216     OPTICKS_LOG(argc, argv);
0217 
0218     /*
0219     test_MaxDiff();
0220     test_FindIndexOfValue();
0221     test_vector_erase_pos(); 
0222     test_vector_erase_all(); 
0223     test_unique_strings(); 
0224     test_MinMaxAvg(); 
0225     test_Desc();  
0226     test_Extract();
0227     */  
0228     test_FindIndexOfValue_NoTolerance_float();
0229     test_FindIndexOfValue_NoTolerance_int();
0230 
0231     return 0 ;
0232 }
0233 
0234 // om-;TEST=SVecTest om-t