00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
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);
00060 if (foop.on(FOOP_MAX))
00061 deadly_error(WHERE, "main", "bit_on is lit past end of vector");
00062
00063
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
00074 if (foop.bits() < 90) foop.resize(90);
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
00088 if (!foop.set(tests[i].posn, tests[i].size, tests[i].store))
00089 deadly_error(WHERE, "main", "storing int in vector failed");
00090
00091
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
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
00108 int new_size = randomizer.inclusive(0, 32000);
00109 foop.resize(new_size);
00110 break;
00111 }
00112 case 2: {
00113
00114 int new_size = randomizer.inclusive(0, 32000);
00115 foop.reset(new_size);
00116 break;
00117 }
00118 case 3: {
00119
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);
00133 }
00134 guards::alert_message("bit_vector:: works for those functions tested.");
00135 return 0;
00136 }
00137