WordExcludeMask.cc

Go to the documentation of this file.
00001 //
00002 // Part of the ht://Dig package   <http://www.htdig.org/>
00003 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group
00004 // For copyright details, see the file COPYING in your distribution
00005 // or the GNU General Public License version 2 or later
00006 // <http://www.gnu.org/copyleft/gpl.html>
00007 //
00008 // $Id: WordExcludeMask_8cc-source.html,v 1.1 2008/06/08 10:13:10 sebdiaz Exp $
00009 //
00010 #ifdef HAVE_CONFIG_H
00011 #include <config.h>
00012 #endif /* HAVE_CONFIG_H */
00013 
00014 #ifdef HAVE_UNISTD_H
00015 #include <unistd.h>
00016 #endif /* HAVE_UNISTD_H */
00017 
00018 #include <htString.h>
00019 
00020 #include <WordExcludeMask.h>
00021 
00022 void WordExcludeMask::Get(String& buffer) const
00023 {
00024   buffer.trunc();
00025   unsigned int i;
00026   for(i = 0; i < ignore_maxi; i++) {
00027     if(bit2bit[i] == WORD_EXCLUDE_IGNORED)
00028       buffer << ((ignore_mask & (1 << i)) ? '3' : '2');
00029     else
00030       buffer << ((WordExclude::Mask() & (1 << bit2bit[i])) ? '1' : '0');
00031   }
00032 }
00033 
00034 int WordExcludeMask::Set(const String& buffer)
00035 {
00036   WordExclude::Initialize(0, 0, 0, 0);
00037 
00038   ignore_mask = 0;
00039   ignore_bits = 0;
00040   ignore_maxi = buffer.length();
00041 
00042   unsigned int i;
00043   for(i = 0; i < ignore_maxi; i++) {
00044     if(buffer[i] == '1' || buffer[i] == '0') {
00045       if(buffer[i] == '1') {
00046         WordExclude::mask |= (1 << WordExclude::maxi);
00047         WordExclude::bits++;
00048       }
00049       bit2bit[i] = WordExclude::maxi;
00050       WordExclude::maxi++;
00051     } else if(buffer[i] == '3' || buffer[i] == '2') {
00052       if(buffer[i] == '3') {
00053         ignore_mask |= (1 << i);
00054         ignore_bits++;
00055       }
00056       bit2bit[i] = WORD_EXCLUDE_IGNORED;
00057     }
00058   }
00059 
00060   return OK;
00061 }
00062 
00063 #ifdef MAIN
00064 
00065 static void exclude_test()
00066 {
00067   static unsigned int expected[] = {
00068     0x00000001,
00069     0x00000002,
00070     0x00000004,
00071     0x00000008,
00072     0x00000010,
00073     0x00000003,
00074     0x00000005,
00075     0x00000006,
00076     0x00000009,
00077     0x0000000a,
00078     0x0000000c,
00079     0x00000011,
00080     0x00000012,
00081     0x00000014,
00082     0x00000018,
00083     0x00000007,
00084     0x0000000b,
00085     0x0000000d,
00086     0x0000000e,
00087     0x00000013,
00088     0x00000015,
00089     0x00000016,
00090     0x00000019,
00091     0x0000001a,
00092     0x0000001c,
00093     0x0000000f,
00094     0x00000017,
00095     0x0000001b,
00096     0x0000001d,
00097     0x0000001e,
00098     0x0000001f
00099   };
00100 
00101   //
00102   // WordExclude
00103   //
00104   if(verbose) fprintf(stderr, "exclude_test: testing WordExclude\n");
00105   {
00106     WordExclude exclude;
00107     exclude.Initialize(5);
00108     int count = 0;
00109     while(exclude.Next() == WORD_EXCLUDE_OK) {
00110       if(expected[count] != exclude.Mask()) {
00111         fprintf(stderr, "exclude_test: WordExclude iteration %d expected 0x%08x but got 0x%08x\n", count, expected[count], exclude.Mask());
00112         exit(1);
00113       }
00114       count++;
00115     }
00116     if(count != sizeof(expected)/sizeof(unsigned int)) {
00117       fprintf(stderr, "exclude_test: WordExclude expected %d iterations but got %d\n", sizeof(expected)/sizeof(unsigned int), count);
00118       exit(1);
00119     }
00120   }
00121 
00122   //
00123   // WordExcludeMask without ignore bits behaves exactly the same
00124   // as WordExclude.
00125   //
00126   if(verbose) fprintf(stderr, "exclude_test: testing WordExcludeMask behaving like WordExclude\n");
00127   {
00128     WordExcludeMask exclude;
00129     exclude.Initialize(5, 0, 0);
00130     int count = 0;
00131     while(exclude.Next() == WORD_EXCLUDE_OK) {
00132       if(expected[count] != exclude.Mask()) {
00133         fprintf(stderr, "exclude_test: WordExcludeMask 1 iteration %d expected 0x%08x but got 0x%08x\n", count, expected[count], exclude.Mask());
00134         exit(1);
00135       }
00136       count++;
00137     }
00138     if(count != sizeof(expected)/sizeof(unsigned int)) {
00139       fprintf(stderr, "exclude_test: WordExcludeMask 1 expected %d iterations but got %d\n", sizeof(expected)/sizeof(unsigned int), count);
00140       exit(1);
00141     }
00142   }
00143 
00144   //
00145   // WordExcludeMask 
00146   //
00147   if(verbose) fprintf(stderr, "exclude_test: testing WordExcludeMask\n");
00148   {
00149     static unsigned int expected[] = {
00150       0x00000102,
00151       0x00000108,
00152       0x00000120,
00153       0x00000180,
00154       0x0000010a,
00155       0x00000122,
00156       0x00000128,
00157       0x00000182,
00158       0x00000188,
00159       0x000001a0,
00160       0x0000012a,
00161       0x0000018a,
00162       0x000001a2,
00163       0x000001a8,
00164       0x000001aa
00165     };
00166     static unsigned int excluded[] = {
00167       1,
00168       0,
00169       0,
00170       0,
00171       1,
00172       1,
00173       0,
00174       1,
00175       0,
00176       0,
00177       1,
00178       1,
00179       1,
00180       0,
00181       1
00182     };
00183 
00184     WordExcludeMask exclude;
00185     unsigned int ignore = 0x155;
00186     unsigned int ignore_mask = 0x100;
00187     exclude.Initialize(9, ignore, ignore_mask);
00188     if(verbose) {
00189       fprintf(stderr, "exclude_test: ignore\n");
00190       show_bits(ignore);
00191       fprintf(stderr, "exclude_test: ignore_mask\n");
00192       show_bits(ignore_mask);
00193     }
00194     if(exclude.NotExcludedCount() != 8) {
00195         fprintf(stderr, "exclude_test: WordExcludeMask 2 expected NoExcludedCount = 8 but got %d\n", exclude.NotExcludedCount());
00196         exit(1);
00197     }
00198     int count = 0;
00199     while(exclude.Next() == WORD_EXCLUDE_OK) {
00200       if(expected[count] != exclude.Mask()) {
00201         fprintf(stderr, "exclude_test: WordExcludeMask 2 iteration %d expected 0x%08x but got 0x%08x\n", count, expected[count], exclude.Mask());
00202         exit(1);
00203       }
00204       //
00205       // Test Excluded() method on ignored bit
00206       // Is bit 5 set ? (9 - 4) = 5 (counting from 1)
00207       //
00208       if(exclude.Excluded(4)) {
00209         fprintf(stderr, "exclude_test: WordExcludeMask 2 iteration %d bit 5 was set 0x%08x\n", count, exclude.Mask());
00210         exit(1);
00211       }
00212       //
00213       // Test Excluded() method on variable bit
00214       // Is bit 2 set ? (9 - 2) = 7 (counting from 1)
00215       //
00216       if((exclude.Excluded(7) && !excluded[count]) ||
00217          (!exclude.Excluded(7) && excluded[count])) {
00218         fprintf(stderr, "exclude_test: WordExcludeMask 2 iteration %d expected bit 2 %s but was %s in 0x%08x\n", count, (excluded[count] ? "set" : "not set"), (excluded[count] ? "not set" : "set"), expected[count]);
00219         exit(1);
00220       }
00221       count++;
00222     }
00223     if(count != sizeof(expected)/sizeof(unsigned int)) {
00224       fprintf(stderr, "exclude_test: WordExcludeMask 2 expected %d iterations but got %d\n", sizeof(expected)/sizeof(unsigned int), count);
00225       exit(1);
00226     }
00227   }
00228 
00229   {
00230     WordExclude exclude;
00231     String ascii("110101");
00232     String tmp;
00233     exclude.Set(ascii);
00234     exclude.Get(tmp);
00235     if(tmp != ascii) {
00236       fprintf(stderr, "exclude_test: WordExclude::Get/Set expected %s but got %s\n", (char*)ascii, (char*)tmp);
00237       exit(1);
00238     }
00239     if(exclude.Mask() != 0x2b) {
00240       fprintf(stderr, "exclude_test: WordExclude::Mask expected 0x2b but got 0x%02x\n", exclude.Mask());
00241       exit(1);
00242     }
00243   }
00244   {
00245     WordExcludeMask exclude;
00246     String ascii("12031");
00247     String tmp;
00248     exclude.Set(ascii);
00249     exclude.Get(tmp);
00250     if(tmp != ascii) {
00251       fprintf(stderr, "exclude_test: WordExcludeMask::Get/Set expected %s but got %s\n", (char*)ascii, (char*)tmp);
00252       exit(1);
00253     }
00254     if(exclude.Mask() != 0x19) {
00255       fprintf(stderr, "exclude_test: WordExcludeMask::Mask expected 0x19 but got 0x%02x\n", exclude.Mask());
00256       exit(1);
00257     }
00258   }
00259 }
00260 
00261 main() {
00262   exclude_test();
00263   exit(0);
00264 }
00265 
00266 #endif /* MAIN */

Generated on Sun Jun 8 10:56:40 2008 for GNUmifluz by  doxygen 1.5.5