xdrbuf/test/test1.cpp

Go to the documentation of this file.
00001 /*
00002  * test1.cpp
00003  *
00004  * Copyright (C) 2008  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * Quick tests for the XDROutputBuffer object.
00031  */
00032 
00033 // includes --------------------------------------------------------------------
00034 #include "xdrbuf/xdrbuf.h"              // include library header first
00035 
00036 #include <iostream>
00037 
00038 #include "common/wave_ex.h"
00039 #include "perf/perf.h"
00040 
00041 
00042 
00043 ////////////////////////////////////////////////////////////////////////////////
00044 //
00045 //      static helper methods
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.data(), 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.data(), 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 //      entry point
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& e) {
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