t_bitvec.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_bit_vector                                                   *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include <basis/array.cpp>
00016 #include <basis/byte_array.h>
00017 #include <basis/chaos.h>
00018 #include <basis/function.h>
00019 #include <basis/guards.h>
00020 #include <basis/istring.h>
00021 #include <data_struct/bit_vector.h>
00022 #include <data_struct/static_memory_gremlin.h>
00023 
00024 #include <stdlib.h>
00025 
00026 HOOPLE_STARTUP_CODE;
00027 
00028 #define WHERE __WHERE__.s()
00029 
00030 #define MAX_TEST 100
00031 #define FOOP_MAX 213
00032 
00033 struct test_struct { u_int store; int posn; int size; };
00034 
00035 int main(int formal(argc), char *formal(argv)[])
00036 {
00037   const array<test_struct> unused;
00038 
00039   chaos randomizer;
00040   bit_vector foop(FOOP_MAX);
00041 
00042   for (int i = 0; i < MAX_TEST; i++) {
00043     // sets a random bit and finds that one.
00044     int rando = randomizer.inclusive(0, FOOP_MAX-1);
00045     foop.light(rando);
00046     int found = foop.find_first(true);
00047     if (found != rando)
00048       deadly_error(WHERE, "main", "find first did not work");
00049     foop.clear(rando);
00050 
00051     foop.resize(FOOP_MAX);
00052     if ( (foop.find_first(0) != 0)
00053         || (foop.find_first(1) != common::NOT_FOUND) )
00054       deadly_error(WHERE, "main", "find first on cleared didn't work");
00055     for (int i = 0; i < 12; i++) foop.light(i);
00056     if (foop.find_first(0) != 12)
00057       deadly_error(WHERE, "main", "on partial set, find_first didn't work");
00058 
00059     foop.light(FOOP_MAX);  // shouldn't work, but shouldn't die.
00060     if (foop.on(FOOP_MAX))
00061       deadly_error(WHERE, "main", "bit_on is lit past end of vector");
00062 
00063     // sets a bunch of random bits.
00064     for (int j = 0; j < 40; j++) {
00065       int rando = randomizer.inclusive(0, FOOP_MAX-1);
00066       foop.light(rando);
00067     }
00068     bit_vector foop2(FOOP_MAX, ((const byte_array &)foop).observe());
00069     if (foop != foop2)
00070       deadly_error(WHERE, "main", "vectors are not identical");
00071 
00072     {
00073       // this block tests the subvector and int storage/retrieval routines.
00074       if (foop.bits() < 90) foop.resize(90);  // make sure we have room to play.
00075 
00076       array<test_struct> tests;
00077       test_struct t1 = { 27, 15, 5 };
00078       tests += t1;
00079       test_struct t2 = { 8, 25, 4 };
00080       tests += t2;
00081       test_struct t3 = { 1485, 34, 16 };
00082       tests += t3;
00083       test_struct t4 = { 872465, 50, 32 };
00084       tests += t4;
00085 
00086       for (int i = 0; i < tests.length(); i++) {
00087 // should print out which int and posn failed.
00088         if (!foop.set(tests[i].posn, tests[i].size, tests[i].store))
00089           deadly_error(WHERE, "main", "storing int in vector failed");
00090 //        bit_vector found = foop.subvector(tests[i].posn, tests[i].posn+tests[i].size-1);
00091 //        LOG(istring(istring::SPRINTF, "contents found:\n%s", found.text_form().s()));
00092         u_int to_check = foop.get(tests[i].posn, tests[i].size);
00093         if (to_check != tests[i].store)
00094           deadly_error(WHERE, "main", istring(istring::SPRINTF, "int found in "
00095               "vector (%u) is different than what was stored (%u).",
00096               to_check, tests[i].store).s());
00097       }
00098     }
00099 
00100     {
00101       // tests random resizings and resettings.
00102       int number_of_loops = randomizer.inclusive(50, 150);
00103       for (int i = 0; i < number_of_loops; i++) {
00104         int which_to_do = randomizer.inclusive(1, 3);
00105         switch (which_to_do) {
00106           case 1: {
00107             // resize.
00108             int new_size = randomizer.inclusive(0, 32000);
00109             foop.resize(new_size);
00110             break;
00111           }
00112           case 2: {
00113             // reset.
00114             int new_size = randomizer.inclusive(0, 32000);
00115             foop.reset(new_size);
00116             break;
00117           }
00118           case 3: {
00119             // random sets.
00120             int sets_to_do = randomizer.inclusive(40, 280);
00121             for (int i = 0; i < sets_to_do; i++) {
00122               int rando = randomizer.inclusive(0, foop.bits());
00123               if (randomizer.inclusive(0, 1)) foop.light(rando);
00124               else foop.clear(rando);
00125             }
00126             break;
00127           }
00128         }
00129       }
00130     }
00131 
00132     foop.reset(FOOP_MAX);  // to clear before next loop.
00133   }
00134   guards::alert_message("bit_vector:: works for those functions tested.");
00135   return 0;
00136 }
00137 

Generated on Fri Nov 21 04:30:07 2008 for HOOPLE Libraries by  doxygen 1.5.1