Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // ./s_pool_test.sh 
0002 
0003 /**
0004 s_pool_test.cc
0005 =================
0006 
0007 
0008 
0009 **/
0010 
0011 #include "Obj.h"
0012 
0013 
0014 struct s_pool_test
0015 {
0016     static int stack_Obj(); 
0017     static int heap_Obj(); 
0018     static int vector_0_FAILS(); 
0019     static int vector_1_FAILS(); 
0020     static int vector_2(); 
0021     static int create_delete_0(); 
0022     static int roundtrip(); 
0023     static int main(); 
0024 };
0025 
0026 
0027 int s_pool_test::stack_Obj()
0028 {
0029     Obj o(100); 
0030     return 0 ; 
0031 }
0032 int s_pool_test::heap_Obj()
0033 {
0034     Obj* o = new Obj(100); 
0035     delete o ; 
0036     return 0 ; 
0037 }
0038 
0039 /**
0040 s_pool_test::vector_0_FAILS
0041 ------------------------------
0042 
0043 Trying to use stack objects causes fail
0044 because of double dtors 
0045 
0046 Obj dtor runs doubled up because its being copied into the vector, 
0047 or due to temporaries perhaps.  
0048 
0049 **/
0050 
0051 
0052 int s_pool_test::vector_0_FAILS()
0053 {
0054     printf("//test_vector_0_FAILS\n"); 
0055     Obj a(100); 
0056     Obj b(200); 
0057     Obj c(300); 
0058 
0059     std::vector<Obj> oo = {a, b, c } ; 
0060 
0061     return 0 ; 
0062 }
0063 
0064 int s_pool_test::vector_1_FAILS()
0065 {
0066     Obj a(100); 
0067     Obj b(200); 
0068     Obj c(300); 
0069 
0070     std::vector<Obj> oo ; 
0071     oo.push_back(a); 
0072     oo.push_back(b); 
0073     oo.push_back(c); 
0074     // same as _0 
0075     return 0 ; 
0076 }
0077 
0078 
0079 int s_pool_test::vector_2()
0080 {
0081     Obj* a = new Obj(100); 
0082     Obj* b = new Obj(200); 
0083     Obj* c = new Obj(300); 
0084 
0085     std::vector<Obj*> oo ; 
0086     oo.push_back(a); 
0087     oo.push_back(b); 
0088     oo.push_back(c); 
0089 
0090     return 0 ; 
0091 }
0092 
0093 /**
0094 s_pool_test::create_delete_0
0095 ------------------------------
0096 
0097 1. create 10 then delete the first 5 
0098 2. check that Obj::Index for the remaining pointers
0099    gives the expected 0,1,2,3,4
0100 
0101 
0102 ::
0103 
0104    0.   X
0105    1.   X
0106    2.   X
0107    3.   X  
0108    4.   X
0109    5.       0
0110    6.       1
0111    7.       2
0112    8.       3
0113    9.       4
0114 
0115 
0116 **/
0117 
0118 
0119 
0120 int s_pool_test::create_delete_0()
0121 {
0122     static int N1 = 10 ; 
0123     static int D1 = 5  ; 
0124     Obj* oo[N1] ;
0125 
0126     for(int i=0 ; i < N1 ; i++) 
0127     {
0128         Obj* o = new Obj(100.) ; 
0129         oo[i] = o ;
0130     }
0131     for(int i=0 ; i < N1 ; i++) 
0132     {
0133         Obj* o0 = oo[i] ; 
0134 
0135         int idx = Obj::Index(o0) ; 
0136         assert( idx == i ) ;  
0137   
0138         Obj* o1 = Obj::GetByIdx(i); 
0139         assert( o1 == o0 ); 
0140     }
0141 
0142 
0143     for(int i=0 ; i < D1 ; i++) 
0144     {
0145         Obj* o = oo[i] ;   
0146         delete o ; 
0147         oo[i] = nullptr ; 
0148     }  
0149 
0150 
0151 
0152     for(int i=0 ; i < N1 ; i++) 
0153     {
0154         Obj* o0 = oo[i] ; 
0155         int idx = o0 == nullptr ? -1 : Obj::Index(o0) ; 
0156         int x_idx = i < D1 ? -1 : i - D1 ;  
0157         assert( idx == x_idx ) ;  
0158     }
0159 
0160 
0161 
0162     for(int i=0 ; i < N1 ; i++) 
0163     {
0164         int pid = i ; 
0165         Obj* o = Obj::Lookup(pid) ; 
0166         std::cout << "Obj::Lookup(" << std::setw(2) << pid << ") : " << Obj::Desc(o) << "\n" ; 
0167         Obj* o_x = pid < D1 ? oo[D1+pid] : nullptr ;  
0168         //assert( o == o_x ) ;  
0169     }
0170 
0171 
0172     for(int i=0 ; i < N1 ; i++) 
0173     {
0174         int idx = i ; 
0175         Obj* o = Obj::GetByIdx(idx) ; 
0176         std::cout << "Obj::GetByIdx(" << std::setw(2) << i << ") : " << Obj::Desc(o) << "\n" ; 
0177         Obj* o_x = idx < D1 ? oo[D1+idx] : nullptr ;  
0178         assert( o == o_x ) ;  
0179     }
0180 
0181 
0182 
0183 
0184     return 0 ; 
0185 }
0186 
0187 
0188 
0189 
0190 
0191 
0192 
0193 int s_pool_test::roundtrip()
0194 {
0195     std::vector<_Obj> buf ; 
0196     {
0197         Obj* a = new Obj(100) ; 
0198         Obj* b = new Obj(200) ; 
0199         Obj c(300, a, b );     
0200 
0201         // because Obj deep deletes would get double delete 
0202         // with a and b on stack 
0203         Obj::pool->serialize_(buf) ; 
0204     }
0205 
0206     std::cout << " buf.size " << buf.size() << std::endl ; 
0207 
0208     Obj::pool->import_(buf); 
0209 
0210     return 0 ; 
0211 }
0212 
0213 
0214 int s_pool_test::main()
0215 {
0216     const char* TEST = ssys::getenvvar("TEST","vector_0") ; 
0217 
0218     int rc = 0 ; 
0219     if(      strcmp(TEST, "vector_0_FAILS") == 0 ) rc = vector_0_FAILS() ;
0220     else if( strcmp(TEST, "vector_1_FAILS") == 0 ) rc = vector_1_FAILS() ;
0221     else if( strcmp(TEST, "vector_2") == 0 ) rc = vector_2() ;
0222     else if( strcmp(TEST, "vector_2") == 0 ) rc = vector_2() ;
0223     else if( strcmp(TEST, "create_delete_0") == 0 ) rc = create_delete_0() ;
0224     else if( strcmp(TEST, "heap_Obj") == 0 ) rc = heap_Obj();
0225     else if( strcmp(TEST, "stack_Obj") == 0 ) rc = stack_Obj();
0226     else if( strcmp(TEST, "roundtrip") == 0 ) rc = roundtrip();
0227  
0228     return rc ; 
0229 }
0230 
0231 
0232 int main(int argc, char** argv)
0233 {
0234     Obj::pool = new Obj::POOL("Obj") ;  
0235 
0236     return s_pool_test::main() ; 
0237 }
0238 
0239 
0240