00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "xdrbuf/xdrbuf.h"
00035
00036 #include <iostream>
00037
00038 #include "common/wave_ex.h"
00039 #include "perf/perf.h"
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 static smart_ptr<xdrbuf::Output>
00050 getOutput
00051 (
00052 IN int size,
00053 IN int nLongs,
00054 IN int nFloats,
00055 IN const char * s
00056 )
00057 {
00058 ASSERT(size > 0, "Bad size: %d", size);
00059 ASSERT(nLongs >= 0, "Bad long count: %d", nLongs);
00060 ASSERT(nFloats >= 0, "Bad float count: %d", nFloats);
00061 ASSERT(s, "null");
00062
00063 smart_ptr<xdrbuf::Output> out = xdrbuf::Output::create(size);
00064 ASSERT(out, "null");
00065
00066 DPRINTF("Requested buffer of size: %d bytes", size);
00067 DPRINTF(" Remaining bytes: %d", out->getRemainingBytes());
00068 if (size != out->getRemainingBytes()) {
00069 WAVE_EX(wex);
00070 wex << "Should have size == remaining bytes!";
00071 }
00072
00073 DPRINTF(" Adding %d int32s...", nLongs);
00074 std::vector<int32_t> veclong;
00075 veclong.resize(nLongs);
00076 for (long l = 0; l < nLongs; ++l) {
00077 veclong[l] = l;
00078 }
00079 out->addInt32Packlet('a', &veclong[0], nLongs);
00080 DPRINTF(" Remaining bytes: %d", out->getRemainingBytes());
00081
00082 DPRINTF(" Adding %d floats...", nFloats);
00083 std::vector<float> vecfloat;
00084 vecfloat.resize(nFloats);
00085 for (long l = 0; l < nFloats; ++l) {
00086 vecfloat[l] = 1.0 * l;
00087 }
00088 out->addFloatPacklet('b', &vecfloat[0], nFloats);
00089 DPRINTF(" Remaining bytes: %d", out->getRemainingBytes());
00090
00091 int len = strlen(s);
00092 DPRINTF(" Adding string: %s", s);
00093 DPRINTF(" length = %d", len);
00094 out->addStringPacklet('c', s, len);
00095 DPRINTF(" Remaining bytes: %d", out->getRemainingBytes());
00096
00097 out->openPacklet('o');
00098 out->addStringPacklet('q', "test", 4);
00099 out->closePacklet('o');
00100
00101 return out;
00102 }
00103
00104
00105
00106 static void
00107 parse
00108 (
00109 IN xdrbuf::Input * in
00110 )
00111 {
00112 ASSERT(in, "null");
00113
00114 xdrbuf::PackletHeader p = in->getNextPackletHeader();
00115
00116 char n = p.getName();
00117 DPRINTF(" name: '%c'", n);
00118
00119 xdrbuf::ePackletType type = p.getType();
00120 DPRINTF(" type: %d", type);
00121
00122 int count = p.getDataCount();
00123 DPRINTF(" count: %d", count);
00124
00125 switch (type) {
00126 case xdrbuf::ePacklet_ParentBegin:
00127 {
00128 DPRINTF("Opening a packlet!");
00129 while (!in->endOfStream()) {
00130 parse(in);
00131 }
00132 }
00133 break;
00134
00135 case xdrbuf::ePacklet_ParentEnd:
00136 {
00137 DPRINTF("End of buffer!");
00138 return;
00139 }
00140
00141 case xdrbuf::ePacklet_String:
00142 {
00143 DPRINTF("Found a string!");
00144 DPRINTF(" %d characters", count);
00145 char buffer[2048];
00146 in->readString(buffer, count);
00147 buffer[count] = 0;
00148 DPRINTF(" Read: '%s'", buffer);
00149 }
00150 break;
00151
00152 case xdrbuf::ePacklet_Int32s:
00153 {
00154 DPRINTF("Found %d int32s", count);
00155 int32_t buffer[2048];
00156 in->readInt32s(buffer, count);
00157 }
00158 break;
00159
00160 case xdrbuf::ePacklet_Floats:
00161 {
00162 DPRINTF("Found %d floats", count);
00163 float buffer[2048];
00164 in->readFloats(buffer, count);
00165 }
00166 break;
00167
00168 default:
00169 ASSERT(false, "Unknown type?");
00170 }
00171 }
00172
00173
00174
00175 static void
00176 doTest1
00177 (
00178 IN int bytes,
00179 IN int nLongs,
00180 IN int nFloats,
00181 IN const char * s
00182 )
00183 {
00184 perf::Timer timer("doTest1");
00185 smart_ptr<xdrbuf::Output> out = getOutput(bytes, nLongs, nFloats, s);
00186 ASSERT(out, "null");
00187
00188 const byte_t * data = out->getData();
00189 ASSERT(data, "null");
00190
00191 int dbytes = out->getDataBytes();
00192 ASSERT(dbytes > 0, "Bad bytes: %d", dbytes);
00193
00194 smart_ptr<xdrbuf::Input> in = xdrbuf::Input::create();
00195 ASSERT(in, "null");
00196
00197 in->reset(data, dbytes);
00198
00199 while (!in->endOfStream()) {
00200 parse(in);
00201 }
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 int
00213 main
00214 (
00215 IN int argc,
00216 IN const char * argv[]
00217 )
00218 {
00219 argc = argc;
00220 argv = argv;
00221 int retval = 0;
00222 try {
00223 perf::Timer timer("overall timer");
00224
00225 doTest1(1200, 100, 100, "foo");
00226 doTest1(1200, 100, 100, "a");
00227 doTest1(1200, 100, 100, "foot");
00228 doTest1(1200, 100, 100, "foote");
00229
00230 bool threw = false;
00231 try {
00232 doTest1(40, 10, 10, "yessiree");
00233 } catch (std::exception&) {
00234 threw = true;
00235 }
00236 ASSERT_THROW(threw,
00237 "Should have complained about small buffer!");
00238
00239 } catch (std::exception& e) {
00240 DPRINTF("Exception: %s", e.what());
00241 retval = 1;
00242 } catch (...) {
00243 DPRINTF("Unknown exception!");
00244 retval = 2;
00245 }
00246 if (!retval) {
00247 DPRINTF("Success! All tests passed.");
00248 }
00249
00250 perf::dumpTimingSummary(std::cerr);
00251
00252 return retval;
00253 }
00254