testbits.cxx

Go to the documentation of this file.
00001 /*
00002  * testbits.cxx -- unit tests for the new functionality in the TBits class
00003  * by Filip Pizlo, 2004
00004  */
00005 
00006 #include "TROOT.h"
00007 #include "TBits.h"
00008 
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 
00012 static const char *current_test;
00013 static unsigned test_count;
00014 
00015 #define A(exp) do {\
00016     if (!(exp)) {\
00017         fprintf(stderr," FAILURE!\n\nIn %s:\n\n",current_test);\
00018         fprintf(stderr,"Assertion (%s) failed at %s:%d.\n\n",\
00019                        #exp,__FILE__,__LINE__);\
00020         exit(1);\
00021     }\
00022 } while (0)
00023 
00024 #define DO_TEST(exp) do {\
00025     current_test=#exp;\
00026     fprintf(stderr,".");\
00027     fflush(stderr);\
00028     exp;\
00029     ++test_count;\
00030 } while (0)
00031 
00032 static void test_set_bit() {
00033     TBits b;
00034     b.SetBitNumber(0);
00035     A(b.TestBitNumber(0));
00036 }
00037 
00038 static const Char_t char_bits[] = {
00039     0,
00040     1,
00041     3,
00042     7,
00043     15,
00044     31,
00045     63,
00046     127
00047 };
00048 
00049 static const Short_t short_bits[] = {
00050     0 + 1 * 256,
00051     3 + 7 * 256,
00052     15 + 31 * 256,
00053     63 + 127 * 256
00054 };
00055 
00056 static const Int_t int_bits[] = {
00057     0 + 1 * 256 + 3 * 65536 + 7 * 16777216,
00058     15 + 31 * 256 + 63 * 65536 + 127 * 16777216
00059 };
00060 
00061 static void set_bits_by_char(TBits &bits,
00062                              UInt_t nbits) {
00063     bits.Set(nbits, char_bits);
00064 }
00065 
00066 static void assert_bits_by_char(const TBits &bits,
00067                                 UInt_t nbits) {
00068     Char_t buf[8];
00069     memset(buf,0,sizeof(buf));
00070     bits.Get(buf);
00071     for (UInt_t i=0;i<nbits;++i) {
00072         A((buf[i>>3]&(1<<(i&7))) == (char_bits[i>>3]&(1<<(i&7))));
00073     }
00074 }
00075 
00076 static void set_bits_by_short(TBits &bits,
00077                               UInt_t nbits) {
00078     bits.Set(nbits, short_bits);
00079 }
00080 
00081 static void assert_bits_by_short(const TBits &bits,
00082                                  UInt_t nbits) {
00083     Short_t buf[4];
00084     memset(buf,0,sizeof(buf));
00085     bits.Get(buf);
00086     for (UInt_t i=0;i<nbits;++i) {
00087         A((buf[i>>4]&(1<<(i&15))) == (short_bits[i>>4]&(1<<(i&15))));
00088     }
00089 }
00090 
00091 static void set_bits_by_int(TBits &bits,
00092                             UInt_t nbits) {
00093     bits.Set(nbits, int_bits);
00094 }
00095 
00096 static void assert_bits_by_int(const TBits &bits,
00097                                UInt_t nbits) {
00098     Int_t buf[2];
00099     memset(buf,0,sizeof(buf));
00100     bits.Get(buf);
00101     for (UInt_t i=0;i<nbits;++i) {
00102         A((buf[i>>5]&(1<<(i&31))) == (int_bits[i>>5]&(1<<(i&31))));
00103     }
00104 }
00105 
00106 static void set_bits(TBits &bits,
00107                      UInt_t nbits) {
00108     UInt_t i,j;
00109     for (i=0;
00110          i<8;
00111          ++i) {
00112         for (j=i*8;
00113              j<i*8+i && j<nbits;
00114              ++j) {
00115             bits.SetBitNumber(j);
00116         }
00117         for (j=i*8+i;
00118              j<i*8+8 && j<nbits;
00119              ++j) {
00120             bits.ResetBitNumber(j);
00121         }
00122     }
00123 }
00124 
00125 static void assert_bits(const TBits &bits,
00126                         UInt_t nbits) {
00127     UInt_t i,j;
00128     for (i=0;
00129          i<8;
00130          ++i) {
00131         for (j=i*8;
00132              j<i*8+i && j<nbits;
00133              ++j) {
00134             A(bits.TestBitNumber(j));
00135         }
00136         for (j=i*8+i;
00137              j<i*8+8 && j<nbits;
00138              ++j) {
00139             A(!bits.TestBitNumber(j));
00140         }
00141     }
00142 }
00143 
00144 static void test_set_from_char(UInt_t nbits) {
00145     TBits b;
00146     set_bits_by_char(b,nbits);
00147     assert_bits(b,nbits);
00148 }
00149 
00150 static void test_set_from_short(UInt_t nbits) {
00151     TBits b;
00152     set_bits_by_short(b,nbits);
00153     assert_bits(b,nbits);
00154 }
00155 
00156 static void test_set_from_int(UInt_t nbits) {
00157     TBits b;
00158     set_bits_by_int(b,nbits);
00159     assert_bits(b,nbits);
00160 }
00161 
00162 static void test_get_to_char(UInt_t nbits) {
00163     TBits b;
00164     set_bits(b,nbits);
00165     assert_bits_by_char(b,nbits);
00166 }
00167 
00168 static void test_get_to_short(UInt_t nbits) {
00169     TBits b;
00170     set_bits(b,nbits);
00171     assert_bits_by_short(b,nbits);
00172 }
00173 
00174 static void test_get_to_int(UInt_t nbits) {
00175     TBits b;
00176     set_bits(b,nbits);
00177     assert_bits_by_int(b,nbits);
00178 }
00179 
00180 // don't have access to a 64-bit machine at the moment so long test would be
00181 // pointless...
00182 
00183 int main(int /*c*/,char **v) {
00184     TROOT app("bits_test", "Tests the TBits class's new functionality");
00185     
00186     fprintf(stderr,"%s: ",v[0]);
00187     
00188     DO_TEST(test_set_bit());
00189     
00190     DO_TEST(test_set_from_char(64));
00191     DO_TEST(test_set_from_char(64-1));
00192     DO_TEST(test_set_from_char(64-7));
00193     DO_TEST(test_set_from_char(64-8));
00194     
00195     DO_TEST(test_set_from_short(64));
00196     DO_TEST(test_set_from_short(64-1));
00197     DO_TEST(test_set_from_short(64-7));
00198     DO_TEST(test_set_from_short(64-8));
00199     DO_TEST(test_set_from_short(64-15));
00200     DO_TEST(test_set_from_short(64-16));
00201     
00202     DO_TEST(test_set_from_int(64));
00203     DO_TEST(test_set_from_int(64-1));
00204     DO_TEST(test_set_from_int(64-7));
00205     DO_TEST(test_set_from_int(64-8));
00206     DO_TEST(test_set_from_int(64-15));
00207     DO_TEST(test_set_from_int(64-16));
00208     DO_TEST(test_set_from_int(64-23));
00209     DO_TEST(test_set_from_int(64-24));
00210     DO_TEST(test_set_from_int(64-31));
00211     DO_TEST(test_set_from_int(64-32));
00212     
00213     DO_TEST(test_get_to_char(64));
00214     DO_TEST(test_get_to_char(64-1));
00215     DO_TEST(test_get_to_char(64-7));
00216     DO_TEST(test_get_to_char(64-8));
00217     
00218     DO_TEST(test_get_to_short(64));
00219     DO_TEST(test_get_to_short(64-1));
00220     DO_TEST(test_get_to_short(64-7));
00221     DO_TEST(test_get_to_short(64-8));
00222     DO_TEST(test_get_to_short(64-15));
00223     DO_TEST(test_get_to_short(64-16));
00224     
00225     DO_TEST(test_get_to_int(64));
00226     DO_TEST(test_get_to_int(64-1));
00227     DO_TEST(test_get_to_int(64-7));
00228     DO_TEST(test_get_to_int(64-8));
00229     DO_TEST(test_get_to_int(64-15));
00230     DO_TEST(test_get_to_int(64-16));
00231     DO_TEST(test_get_to_int(64-23));
00232     DO_TEST(test_get_to_int(64-24));
00233     DO_TEST(test_get_to_int(64-31));
00234     DO_TEST(test_get_to_int(64-32));
00235     
00236     fprintf(stderr," OK! (%d tests)\n",test_count);
00237     
00238     return 0;
00239 }
00240 
00241 

Generated on Tue Jul 5 15:16:18 2011 for ROOT_528-00b_version by  doxygen 1.5.1