00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <Basics.H>
00020 #include <BufStream.H>
00021
00022 #if !defined(__sun__)
00023 extern "C" {
00024 #include <getopt.h>
00025 }
00026 #endif
00027
00028 #include "FPStream.H"
00029 #include "UniqueId.H"
00030
00031 using std::cout;
00032 using std::cerr;
00033 using std::endl;
00034 using std::flush;
00035 using std::ostream;
00036 using Basics::OBufStream;
00037 using FP::FPStream;
00038
00039 bool verbose = false;
00040
00041 void write_nbytes(unsigned int len, ostream &stream1, ostream &stream2)
00042 {
00043 char *buf = NEW_PTRFREE_ARRAY(char, len);
00044 if(verbose)
00045 {
00046 cout << "Writing " << len << " bytes = {";
00047 }
00048 for(unsigned int i = 0; i < len; i++)
00049 {
00050 char c = (((unsigned int) random()) % 255);
00051 if(verbose)
00052 {
00053 if(i > 0)
00054 cout << ", ";
00055 cout << (unsigned int) ((unsigned char) c);
00056 }
00057 buf[i] = c;
00058 }
00059 if(verbose)
00060 {
00061 cout << "}" << endl;
00062 }
00063 stream1.write(buf, len);
00064 stream2.write(buf, len);
00065 delete [] buf;
00066 }
00067
00068 void write_once(ostream &stream1, ostream &stream2)
00069 {
00070 switch(((unsigned int) random()) % 4)
00071 {
00072 case 0:
00073 {
00074
00075 char c = (((unsigned int) random()) % 255);
00076 if(verbose)
00077 {
00078 cout << "Writing single byte = "
00079 << (unsigned int) ((unsigned char) c) << endl;
00080 }
00081 stream1.put(c);
00082 stream2.put(c);
00083 }
00084 break;
00085 case 1:
00086 {
00087
00088 int i = random();
00089 if(verbose)
00090 {
00091 cout << "Writing integer = \"" << i << "\"" << endl;
00092 }
00093 stream1 << i;
00094 stream2 << i;
00095 }
00096 break;
00097 case 2:
00098 {
00099
00100 unsigned int len = (((unsigned int) random()) % sizeof(Word)) + 1;
00101 write_nbytes(len, stream1, stream2);
00102 }
00103 break;
00104 case 3:
00105 {
00106
00107 unsigned int len = (((unsigned int) random()) % BUFSIZ) + sizeof(Word);
00108 write_nbytes(len, stream1, stream2);
00109 }
00110 break;
00111 }
00112 }
00113
00114 void do_one_test(unsigned int maxwrites)
00115 {
00116
00117 FP::Tag base = UniqueId();
00118
00119
00120 bool use_base = (((unsigned int) random()) % 2);
00121
00122 OBufStream buf_stream;
00123
00124 FPStream *fp_stream;
00125 if(use_base)
00126 {
00127 fp_stream = NEW_CONSTR(FPStream, (base));
00128 }
00129 else
00130 {
00131 fp_stream = NEW(FPStream);
00132 }
00133
00134 unsigned int nwrites = (((unsigned int) random()) % maxwrites) + 4;
00135
00136 for(unsigned int i = 0; i < nwrites; i++)
00137 {
00138 write_once(buf_stream, *fp_stream);
00139 }
00140
00141 unsigned int len = buf_stream.tellp();
00142
00143 FP::Tag buf_tag, fp_tag;
00144 if(use_base)
00145 {
00146 buf_tag = base;
00147 buf_tag.Extend(buf_stream.str(), len);
00148 }
00149 else
00150 {
00151 buf_tag = FP::Tag(buf_stream.str(), len);
00152 }
00153
00154 fp_tag = fp_stream->tag();
00155 unsigned int fp_count = fp_stream->tellp();
00156
00157 if(fp_count != len)
00158 {
00159 cerr << "Length mismatch:" << endl
00160 << "\t" << len << endl
00161 << "\t" << fp_count << endl;
00162 exit(1);
00163 }
00164
00165 if(buf_tag != fp_tag)
00166 {
00167 cerr << "Mismatch (total bytes " << len << "):" << endl
00168 << "\t" << buf_tag << endl
00169 << "\t" << fp_tag << endl;
00170 exit(1);
00171 }
00172
00173 delete fp_stream;
00174 }
00175
00176 Text program_name;
00177
00178 void usage()
00179 {
00180 cerr << "Usage: " << program_name << endl
00181 << "\t[-s seed-vale] Random seed" << endl
00182 << "\t[-c test-count] Number of tests to perform" << endl
00183 << "\t[-w write-count] Number of writes per test" << endl
00184 << "\t[-v] Verbose: describe each write" << endl;
00185 exit(1);
00186 }
00187
00188 int main(int argc, char* argv[])
00189 {
00190 program_name = argv[0];
00191 unsigned int seed = time(0);
00192 unsigned int count = 100;
00193 unsigned int maxwrites = 1000;
00194 for (;;)
00195 {
00196 char* slash;
00197 int c = getopt(argc, argv, "s:c:w:v");
00198 if (c == EOF) break;
00199 switch (c)
00200 {
00201 case 's':
00202 seed = strtoul(optarg, NULL, 0);
00203 break;
00204 case 'c':
00205 count = strtoul(optarg, NULL, 0);
00206 break;
00207 case 'w':
00208 maxwrites = strtoul(optarg, NULL, 0);
00209 break;
00210 case 'v':
00211 verbose = true;
00212 break;
00213 case '?':
00214 default:
00215 usage();
00216 }
00217 }
00218
00219 cout << "To reproduce: -s " << seed
00220 << " -c " << count
00221 << " -w " << maxwrites << endl;
00222 srandom(seed);
00223
00224 for(unsigned int i = 0; i < count; i++)
00225 {
00226 do_one_test(maxwrites);
00227 }
00228
00229 cout << "All tests passed!" << endl;
00230
00231 return 0;
00232 }