sbuffer-test.cpp

Go to the documentation of this file.
00001 /*
00002  * sbuffer-test.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Quick test of string buffer object
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include <iostream>
00012 #include <sstream>
00013 
00014 #include "perf/perf.h"
00015 #include "util/string-buffer.h"
00016 
00017 
00018 ////////////////////////////////////////////////////////////////////////////////
00019 //
00020 //      static helper methods
00021 //
00022 ////////////////////////////////////////////////////////////////////////////////
00023 
00024 static void
00025 doTest
00026 (
00027 IN int nStrings
00028 )
00029 {
00030         ASSERT(nStrings > 0, "Bad string count: %d", nStrings);
00031 
00032         smart_ptr<StringBuffer> sbuf = StringBuffer::create();
00033         ASSERT(sbuf, "failed to create string buffer?");
00034 
00035         const int minLen = 0;
00036         const int maxLen = 100;
00037         const int modulo = maxLen - minLen + 1;
00038         char buffer[maxLen + 1];
00039         for (int i = 0; i < nStrings; ++i) {
00040                 int len = minLen + (rand() % modulo);
00041                 for (int j = 0; j < len; ++j) {
00042                         buffer[j] = 'a' + (rand() % 26);
00043                 }
00044                 buffer[len] = 0;        // null-terminated
00045 
00046 //              DPRINTF("String[%3d] = %s", i + 1, buffer);
00047                 int offset = sbuf->appendString(buffer);
00048 //              DPRINTF("  offset = %d", offset);
00049 
00050                 ASSERT_THROW(!strcmp(buffer, sbuf->getString(offset)),
00051                     "Strings should match!");
00052         }
00053 
00054         if (nStrings > 20) {
00055                 DPRINTF("More than 20 strings, not writing them all.");
00056         } else {
00057                 DPRINTF("all strings:");
00058                 sbuf->dumpStrings(std::cout);
00059         }
00060 }
00061 
00062 
00063 
00064 ////////////////////////////////////////////////////////////////////////////////
00065 //
00066 //      entry point
00067 //
00068 ////////////////////////////////////////////////////////////////////////////////
00069 
00070 int
00071 main
00072 (
00073 IN int argc,
00074 IN const char * argv[]
00075 )
00076 {
00077         int retval = 0;
00078         DPRINTF("Usage: util-sbuffer-test [nStrings]");
00079         int nStrings = 5000;
00080         if (argc > 1) {
00081                 nStrings = atoi(argv[1]);
00082                 DPRINTF("Using %d strings", nStrings);
00083         } else {
00084                 DPRINTF("Using default of %d strings", nStrings);
00085         }
00086 
00087         try {
00088                 perf::Timer timer("overall timer");
00089 
00090                 doTest(nStrings);
00091 
00092         } catch (std::exception& e) {
00093                 DPRINTF("Exception: %s", e.what());
00094                 retval = 1;
00095         }
00096 
00097         perf::dumpTimingSummary(std::cerr);
00098 
00099         return retval;
00100 }
00101