SqlStringAsciiTest.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calctest/SqlStringAsciiTest.cpp#2 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2004-2009 SQLstream, Inc.
00006 // Copyright (C) 2009-2009 LucidEra, Inc.
00007 //
00008 // This program is free software; you can redistribute it and/or modify it
00009 // under the terms of the GNU General Public License as published by the Free
00010 // Software Foundation; either version 2 of the License, or (at your option)
00011 // any later version approved by The Eigenbase Project.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #include "fennel/common/CommonPreamble.h"
00024 #include "fennel/test/TestBase.h"
00025 #include "fennel/calculator/SqlStringAscii.h"
00026 #include "fennel/common/TraceSource.h"
00027 #include "fennel/calctest/SqlStringBuffer.h"
00028 
00029 #include <boost/test/test_tools.hpp>
00030 #include <boost/scoped_array.hpp>
00031 #include <string>
00032 #include <limits>
00033 
00034 using namespace fennel;
00035 using namespace std;
00036 
00037 
00038 #ifdef COMPLETE_REGRESSION_TEST_SETTINGS
00039 // full regression settings
00040 const int MAXLEN = 8;   // Must not be less than 5. Best >=7.
00041 const int MAXRANDOM = 5;
00042 const int MAXCMPRANDOM = 65536; // Must be nearly 2^16 char set coverage
00043 const int MAXCMPLEN = 8;  // Must not be less than 3.
00044 #else
00045 // faster check-in acceptance testing settings
00046 const int MAXLEN = 5;   // Must not be less than 5. Best >=7.
00047 const int MAXRANDOM = 1;
00048 const int MAXCMPRANDOM = 256; // Must be nearly 2^16 char set coverage
00049 const int MAXCMPLEN = 3;  // Must not be less than 3.
00050 #endif
00051 
00052 class SqlStringAsciiTest : virtual public TestBase, public TraceSource
00053 {
00054     void testSqlStringBuffer_Ascii();
00055 
00056     void testSqlStringCat_Ascii_Fix();
00057     void testSqlStringCat_Ascii_Var();
00058     void testSqlStringCat_Ascii_Var2();
00059     void testSqlStringCmp_Ascii_Fix_DiffLen();
00060     void testSqlStringCmp_Ascii_Fix_EqLen();
00061     void testSqlStringCmp_Ascii_Var_DiffLen();
00062     void testSqlStringCmp_Ascii_Var_EqLen();
00063     void testSqlStringLenBit_Ascii();
00064     void testSqlStringLenChar_Ascii();
00065     void testSqlStringLenOct_Ascii();
00066     void testSqlStringOverlay_Ascii();
00067     void testSqlStringPos_Ascii();
00068     void testSqlStringSubStr_Ascii();
00069     void testSqlStringToLower_Ascii();
00070     void testSqlStringToUpper_Ascii();
00071     void testSqlStringTrim_Ascii();
00072 
00073     void testSqlStringCmp_Ascii_Var_Helper(
00074         SqlStringBuffer &src1,
00075         int src1_len,
00076         SqlStringBuffer &src2,
00077         int src2_len);
00078     void testSqlStringCmp_Ascii_Fix_Helper(
00079         SqlStringBuffer &src1,
00080         int src1_storage,
00081         int src1_len,
00082         SqlStringBuffer &src2,
00083         int src2_storage,
00084         int src2_len);
00085     int testSqlStringNormalizeLexicalCmp(int v);
00086 
00087 public:
00088     explicit SqlStringAsciiTest()
00089         : TraceSource(shared_from_this(),"SqlStringAsciiTest")
00090     {
00091         srand(time(NULL));
00092         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringBuffer_Ascii);
00093         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringCat_Ascii_Fix);
00094         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringCat_Ascii_Var2);
00095         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringCat_Ascii_Var);
00096         FENNEL_UNIT_TEST_CASE(
00097             SqlStringAsciiTest, testSqlStringCmp_Ascii_Fix_DiffLen);
00098         FENNEL_UNIT_TEST_CASE(
00099             SqlStringAsciiTest, testSqlStringCmp_Ascii_Fix_EqLen);
00100         FENNEL_UNIT_TEST_CASE(
00101             SqlStringAsciiTest, testSqlStringCmp_Ascii_Var_DiffLen);
00102         FENNEL_UNIT_TEST_CASE(
00103             SqlStringAsciiTest, testSqlStringCmp_Ascii_Var_EqLen);
00104         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringLenBit_Ascii);
00105         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringLenChar_Ascii);
00106         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringLenOct_Ascii);
00107         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringOverlay_Ascii);
00108         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringPos_Ascii);
00109         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringSubStr_Ascii);
00110         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringToLower_Ascii);
00111         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringToUpper_Ascii);
00112         FENNEL_UNIT_TEST_CASE(SqlStringAsciiTest, testSqlStringTrim_Ascii);
00113     }
00114 
00115     virtual ~SqlStringAsciiTest()
00116     {
00117     }
00118 };
00119 
00120 void
00121 SqlStringAsciiTest::testSqlStringBuffer_Ascii()
00122 {
00123     int storage, size, leftpad, rightpad;
00124     int leftbump = 2;
00125     int rightbump = 2;
00126     int k;
00127 
00128     for (storage = 0; storage <= 5; storage++) {
00129         for (size = 0; size <= storage; size++) {
00130             for (leftpad = 0; leftpad <= storage - size; leftpad++) {
00131                 rightpad = (storage - size) - leftpad;
00132 
00133                 SqlStringBuffer t(
00134                     storage, size,
00135                     leftpad, rightpad,
00136                     'x', ' ',
00137                     leftbump, rightbump);
00138 
00139                 BOOST_CHECK_EQUAL(t.mStorage, storage);
00140                 BOOST_CHECK_EQUAL(t.mSize, size);
00141                 BOOST_CHECK_EQUAL(t.mLeftPad, leftpad);
00142                 BOOST_CHECK_EQUAL(t.mRightPad, rightpad);
00143                 BOOST_CHECK_EQUAL(
00144                     static_cast<int>(t.mS.size()),
00145                     storage + leftbump + rightbump);
00146 
00147                 BOOST_CHECK(t.verify());
00148 
00149                 char *p = t.mLeftP;
00150                 // left bumper
00151                 for (k = 0; k < leftbump; k++) {
00152                     BOOST_CHECK_EQUAL(*(p++), SqlStringBuffer::mBumperChar);
00153                 }
00154                 BOOST_CHECK(p == t.mStr);
00155                 // left padding
00156                 for (k = 0; k < leftpad; k++) {
00157                     BOOST_CHECK_EQUAL(*(p++), ' ');
00158                 }
00159                 // text
00160                 for (k = 0; k < size; k++) {
00161                     BOOST_CHECK_EQUAL(*(p++), 'x');
00162                 }
00163                 // right padding
00164                 for (k = 0; k < rightpad; k++) {
00165                     BOOST_CHECK_EQUAL(*(p++), ' ');
00166                 }
00167                 BOOST_CHECK(p == t.mRightP);
00168                 // right bumper
00169                 for (k = 0; k < rightbump; k++) {
00170                     BOOST_CHECK_EQUAL(*(p++), SqlStringBuffer::mBumperChar);
00171                 }
00172                 BOOST_CHECK_EQUAL(
00173                     static_cast<int>(p - t.mLeftP),
00174                     storage + leftbump + rightbump);
00175 
00176                 BOOST_CHECK(t.verify());
00177 
00178                 for (k = 0; k < size; k++) {
00179                     *(t.mStr + k) = '0' + (k % 10);
00180                 }
00181                 BOOST_CHECK(t.verify());
00182 
00183                 *(t.mLeftP) = 'X';
00184                 BOOST_CHECK(!t.verify());
00185                 *(t.mLeftP) = SqlStringBuffer::mBumperChar;
00186                 BOOST_CHECK(t.verify());
00187 
00188                 *(t.mStr - 1) = 'X';
00189                 BOOST_CHECK(!t.verify());
00190                 *(t.mStr - 1) = SqlStringBuffer::mBumperChar;
00191                 BOOST_CHECK(t.verify());
00192 
00193                 *(t.mRightP) = 'X';
00194                 BOOST_CHECK(!t.verify());
00195                 *(t.mRightP) = SqlStringBuffer::mBumperChar;
00196                 BOOST_CHECK(t.verify());
00197 
00198                 *(t.mRightP + t.mRightBump - 1) = 'X';
00199                 BOOST_CHECK(!t.verify());
00200                 *(t.mRightP + t.mRightBump - 1) = SqlStringBuffer::mBumperChar;
00201                 BOOST_CHECK(t.verify());
00202 
00203                 t.randomize();
00204                 BOOST_CHECK(t.verify());
00205             }
00206         }
00207     }
00208 }
00209 
00210 
00211 // Test catting 3 fixed width strings together as proof-of-concept
00212 void
00213 SqlStringAsciiTest::testSqlStringCat_Ascii_Fix()
00214 {
00215     int src1_storage, src2_storage, src3_storage, dst_storage;
00216     int src1_len, src2_len, src3_len;
00217     bool caught;
00218     int newlen = 0;
00219 
00220     for (dst_storage = 0; dst_storage < MAXLEN; dst_storage++) {
00221         for (src1_storage = 0; src1_storage < MAXLEN; src1_storage++) {
00222             for (src1_len = 0; src1_len <= src1_storage; src1_len++) {
00223                 for (src2_storage = 0; src2_storage < MAXLEN; src2_storage++) {
00224                     for (src2_len = 0; src2_len <= src2_storage; src2_len++) {
00225                         for (src3_storage = 0; src3_storage < MAXLEN;
00226                              src3_storage++)
00227                         {
00228                             for (src3_len = 0; src3_len <= src3_storage;
00229                                  src3_len++)
00230                             {
00231                                 SqlStringBuffer dst(
00232                                     dst_storage, 0,
00233                                     0, dst_storage,
00234                                     'd', ' ');
00235                                 SqlStringBuffer src1(
00236                                     src1_storage, src1_len,
00237                                     0, src1_storage - src1_len,
00238                                     '1', ' ');
00239                                 SqlStringBuffer src2(
00240                                     src2_storage, src2_len,
00241                                     0, src2_storage - src2_len,
00242                                     '2', ' ');
00243                                 SqlStringBuffer src3(
00244                                     src3_storage, src3_len,
00245                                     0, src3_storage - src3_len,
00246                                     '3', ' ');
00247 
00248                                 caught = false;
00249                                 try {
00250                                     newlen = SqlStrCat_Ascii(
00251                                         dst.mStr, dst_storage,
00252                                         src1.mStr, src1_storage,
00253                                         src2.mStr, src2_storage);
00254                                 } catch (const char *str) {
00255                                     caught = true;
00256                                     BOOST_CHECK_EQUAL(strcmp(str, "22001"), 0);
00257                                     BOOST_CHECK(
00258                                         src1_storage + src2_storage
00259                                         > dst_storage);
00260                                     BOOST_CHECK(dst.verify());
00261                                     BOOST_CHECK(src1.verify());
00262                                     BOOST_CHECK(src2.verify());
00263                                 } catch (...) {
00264                                     BOOST_CHECK(false);
00265                                 }
00266                                 if (!caught) {
00267                                     BOOST_CHECK(
00268                                         src1_storage + src2_storage
00269                                         <= dst_storage);
00270                                     BOOST_CHECK(dst.verify());
00271                                     BOOST_CHECK(src1.verify());
00272                                     BOOST_CHECK(src2.verify());
00273 
00274                                     caught = false;
00275                                     try {
00276                                         newlen = SqlStrCat_Ascii(
00277                                             dst.mStr,
00278                                             dst_storage,
00279                                             newlen,
00280                                             src3.mStr,
00281                                             src3_storage);
00282                                     } catch (const char *str) {
00283                                         caught = true;
00284                                         BOOST_CHECK_EQUAL(
00285                                             strcmp(str, "22001"),
00286                                             0);
00287                                         BOOST_CHECK(
00288                                             (src1_storage +
00289                                              src2_storage +
00290                                              src3_storage)
00291                                             > dst_storage);
00292                                         BOOST_CHECK(dst.verify());
00293                                         BOOST_CHECK(src1.verify());
00294                                         BOOST_CHECK(src2.verify());
00295                                         BOOST_CHECK(src3.verify());
00296                                     } catch (...) {
00297                                         BOOST_CHECK(false);
00298                                     }
00299                                     if (!caught) {
00300                                         BOOST_CHECK(dst.verify());
00301                                         BOOST_CHECK(src1.verify());
00302                                         BOOST_CHECK(src2.verify());
00303                                         BOOST_CHECK(src3.verify());
00304                                         BOOST_CHECK_EQUAL(
00305                                             newlen,
00306                                             (src1_storage +
00307                                              src2_storage +
00308                                              src3_storage));
00309 
00310                                         string result(dst.mStr, newlen);
00311                                         string expect(src1.mStr, src1_storage);
00312                                         expect.append(src2.mStr, src2_storage);
00313                                         expect.append(src3.mStr, src3_storage);
00314 
00315                                         BOOST_CHECK(!result.compare(expect));
00316                                     }
00317                                 }
00318                             }
00319                         }
00320                     }
00321                 }
00322             }
00323         }
00324     }
00325 
00326 }
00327 
00328 
00329 void
00330 SqlStringAsciiTest::testSqlStringCat_Ascii_Var2()
00331 {
00332     int src1_storage, src2_storage, dst_storage, src1_len, src2_len;
00333     int newlen = 0;
00334     bool caught;
00335 
00336     for (dst_storage = 0; dst_storage < MAXLEN; dst_storage++) {
00337         for (src1_storage = 0; src1_storage < MAXLEN; src1_storage++) {
00338             for (src1_len = 0; src1_len <= src1_storage; src1_len++) {
00339                 for (src2_storage = 0; src2_storage < MAXLEN; src2_storage++) {
00340                     for (src2_len = 0; src2_len <= src2_storage; src2_len++) {
00341                         SqlStringBuffer dst(
00342                             dst_storage, 0,
00343                             0, dst_storage,
00344                             'd', ' ');
00345                         SqlStringBuffer src1(
00346                             src1_storage, src1_len,
00347                             0, src1_storage - src1_len,
00348                             's', ' ');
00349                         SqlStringBuffer src2(
00350                             src2_storage, src2_len,
00351                             0, src2_storage - src2_len,
00352                             'S', ' ');
00353 
00354                         caught = false;
00355                         try {
00356                             newlen = SqlStrCat_Ascii(
00357                                 dst.mStr,
00358                                 dst_storage,
00359                                 src1.mStr,
00360                                 src1_len,
00361                                 src2.mStr,
00362                                 src2_len);
00363                         } catch (const char *str) {
00364                             caught = true;
00365                             BOOST_CHECK_EQUAL(strcmp(str, "22001"), 0);
00366                             BOOST_CHECK(src1_len + src2_len > dst_storage);
00367                         } catch (...) {
00368                             BOOST_CHECK(false);
00369                         }
00370                         if (!caught) {
00371                             BOOST_CHECK(src1_len + src2_len <= dst_storage);
00372                             BOOST_CHECK_EQUAL(newlen, src1_len + src2_len);
00373 
00374                             string expect;
00375                             expect.append(src1_len, 's');
00376                             expect.append(src2_len, 'S');
00377 
00378                             string result(dst.mStr, newlen);
00379 
00380                             BOOST_CHECK(!result.compare(expect));
00381                             BOOST_CHECK(!expect.compare(result));
00382                         }
00383                         BOOST_CHECK(dst.verify());
00384                         BOOST_CHECK(src1.verify());
00385                         BOOST_CHECK(src2.verify());
00386                     }
00387                 }
00388             }
00389         }
00390     }
00391 }
00392 
00393 
00394 void
00395 SqlStringAsciiTest::testSqlStringCat_Ascii_Var()
00396 {
00397     int src_storage, dst_storage, src_len, dst_len;
00398     int newlen = 0;
00399     bool caught;
00400 
00401     for (dst_storage = 0; dst_storage < MAXLEN; dst_storage++) {
00402         for (dst_len = 0; dst_len <= dst_storage; dst_len++) {
00403             for (src_storage = 0; src_storage < MAXLEN; src_storage++) {
00404                 for (src_len = 0; src_len <= src_storage; src_len++) {
00405                     SqlStringBuffer dst(
00406                         dst_storage, dst_len,
00407                         0, dst_storage - dst_len,
00408                         'd', ' ');
00409                     SqlStringBuffer src(
00410                         src_storage, src_len,
00411                         0, src_storage - src_len,
00412                         's', ' ');
00413                     caught = false;
00414                     try {
00415                         newlen = SqlStrCat_Ascii(
00416                             dst.mStr,
00417                             dst_storage,
00418                             dst_len,
00419                             src.mStr,
00420                             src_len);
00421                     } catch (const char *str) {
00422                         caught = true;
00423                         BOOST_CHECK_EQUAL(strcmp(str, "22001"), 0);
00424                         BOOST_CHECK(src_len + dst_len > dst_storage);
00425                     } catch (...) {
00426                         BOOST_CHECK(false);
00427                     }
00428                     if (!caught) {
00429                         BOOST_CHECK(src_len + dst_len <= dst_storage);
00430                         BOOST_CHECK_EQUAL(newlen, src_len + dst_len);
00431 
00432                         string expect;
00433                         expect.append(dst_len, 'd');
00434                         expect.append(src_len, 's');
00435 
00436                         string result(dst.mStr, newlen);
00437 
00438                         BOOST_CHECK(!result.compare(expect));
00439                         BOOST_CHECK(!expect.compare(result));
00440                     }
00441                     BOOST_CHECK(dst.verify());
00442                     BOOST_CHECK(src.verify());
00443                 }
00444             }
00445         }
00446     }
00447 }
00448 
00449 int
00450 SqlStringAsciiTest::testSqlStringNormalizeLexicalCmp(int v)
00451 {
00452     if (v < 0) {
00453         return -1;
00454     }
00455     if (v > 0) {
00456         return 1;
00457     }
00458     return 0;
00459 }
00460 
00461 
00462 
00463 void
00464 SqlStringAsciiTest::testSqlStringCmp_Ascii_Fix_Helper(
00465     SqlStringBuffer &src1,
00466     int src1_storage,
00467     int src1_len,
00468     SqlStringBuffer &src2,
00469     int src2_storage,
00470     int src2_len)
00471 {
00472     int result;
00473 
00474     string s1(src1.mStr, src1_len);
00475     string s2(src2.mStr, src2_len);
00476 
00477     // It is possible that test string ends with a space. Remove it.
00478     s1.erase(s1.find_last_not_of(" ") + 1);
00479     s2.erase(s2.find_last_not_of(" ") + 1);
00480 
00481     int expected = testSqlStringNormalizeLexicalCmp(s1.compare(s2));
00482     char const * const s1p = s1.c_str();
00483     char const * const s2p = s2.c_str();
00484     int expected2 = testSqlStringNormalizeLexicalCmp(strcmp(s1p, s2p));
00485     BOOST_CHECK_EQUAL(expected, expected2);
00486 
00487     result = SqlStrCmp_Ascii_Fix(
00488         src1.mStr, src1_storage,
00489         src2.mStr, src2_storage);
00490     BOOST_CHECK(src1.verify());
00491     BOOST_CHECK(src2.verify());
00492 
00493 #if 0
00494     BOOST_MESSAGE(" src1=|" << s1 << "|" <<
00495                   " src2=|" << s2 << "|" <<
00496                   " expect=" << expected <<
00497                   " expect2=" << expected2 <<
00498                   " result=" << result);
00499 #endif
00500     BOOST_CHECK_EQUAL(result, expected);
00501 
00502     // check the exact opposite, even if equal
00503     int result2 = SqlStrCmp_Ascii_Fix(
00504         src2.mStr, src2_storage,
00505         src1.mStr, src1_storage);
00506     BOOST_CHECK(src1.verify());
00507     BOOST_CHECK(src2.verify());
00508     BOOST_CHECK_EQUAL(result2 * -1, result);
00509 
00510     // force check of equal strings
00511     result = SqlStrCmp_Ascii_Fix(
00512         src1.mStr, src1_storage,
00513         src1.mStr, src1_storage);
00514     BOOST_CHECK(src1.verify());
00515     BOOST_CHECK_EQUAL(result, 0);
00516 
00517 }
00518 
00519 
00520 void
00521 SqlStringAsciiTest::testSqlStringCmp_Ascii_Fix_DiffLen()
00522 {
00523     int src1_storage, src2_storage, src1_len, src2_len;
00524     unsigned char startchar;
00525 
00526     for (src1_storage = 0; src1_storage <= MAXLEN; src1_storage++) {
00527         for (src1_len = 0; src1_len < src1_storage; src1_len++) {
00528             for (src2_storage = 0; src2_storage <= MAXLEN; src2_storage++) {
00529                 for (src2_len = 0; src2_len < src2_storage; src2_len++) {
00530                     // can't test w/ 0, confuses strcmp and/or std:string
00531                     for (startchar = 1; startchar < 255; startchar++) {
00532                         SqlStringBuffer src1(
00533                             src1_storage, src1_len,
00534                             0, src1_storage - src1_len,
00535                             'd', ' ');
00536                         SqlStringBuffer src2(
00537                             src2_storage, src2_len,
00538                             0, src2_storage - src2_len,
00539                             's', ' ');
00540 
00541                         src1.patternfill(startchar, 1, 255);
00542                         src2.patternfill(startchar, 1, 255);
00543 
00544                         testSqlStringCmp_Ascii_Fix_Helper(
00545                             src1, src1_storage, src1_len,
00546                             src2, src2_storage, src2_len);
00547                     }
00548                 }
00549             }
00550         }
00551     }
00552 }
00553 
00554 
00555 void
00556 SqlStringAsciiTest::testSqlStringCmp_Ascii_Fix_EqLen()
00557 {
00558     int src1_storage, src2_storage, src1_len, src2_len, randX;
00559 
00560     // not much point large length, chances of 2 random strings being equal
00561     // are very low. test forces an equality check anyway.
00562     src1_storage = MAXCMPLEN;
00563     src2_storage = MAXCMPLEN;
00564     for (src1_len = 0; src1_len < src1_storage; src1_len++) {
00565         src2_len = src1_len;
00566         for (randX = 0; randX <= MAXCMPRANDOM; randX++) {
00567             SqlStringBuffer src1(
00568                 src1_storage, src1_len,
00569                 0, src1_storage - src1_len,
00570                 'd', ' ');
00571             SqlStringBuffer src2(
00572                 src2_storage, src2_len,
00573                 0, src2_storage - src2_len,
00574                 's', ' ');
00575 
00576             // can't test w/ 0, confuses strcmp and/or std:string
00577             src1.randomize(1, 1, 255);
00578             src2.randomize(1, 1, 255);
00579 
00580             testSqlStringCmp_Ascii_Fix_Helper(
00581                 src1, src1_storage, src1_len,
00582                 src2, src2_storage, src2_len);
00583         }
00584     }
00585 }
00586 
00587 
00588 void
00589 SqlStringAsciiTest::testSqlStringCmp_Ascii_Var_Helper(
00590     SqlStringBuffer &src1,
00591     int src1_len,
00592     SqlStringBuffer &src2,
00593     int src2_len)
00594 {
00595     int result;
00596 
00597     string s1(src1.mStr, src1_len);
00598     string s2(src2.mStr, src2_len);
00599 
00600     int expected = testSqlStringNormalizeLexicalCmp(s1.compare(s2));
00601     char const * const s1p = s1.c_str();
00602     char const * const s2p = s2.c_str();
00603     int expected2 = testSqlStringNormalizeLexicalCmp(strcmp(s1p, s2p));
00604     BOOST_CHECK_EQUAL(expected, expected2);
00605 
00606 
00607     result = SqlStrCmp_Ascii_Var(
00608         src1.mStr, src1_len,
00609         src2.mStr, src2_len);
00610     BOOST_CHECK(src1.verify());
00611     BOOST_CHECK(src2.verify());
00612 
00613 #if 0
00614     BOOST_MESSAGE(" src1=|" << s1 << "|" <<
00615                   " src2=|" << s2 << "|" <<
00616                   " expect=" << expected <<
00617                   " expect2=" << expected2 <<
00618                   " result=" << result);
00619 #endif
00620     BOOST_CHECK_EQUAL(result, expected);
00621 
00622     // check the exact opposite, even if equal
00623     int result2 = SqlStrCmp_Ascii_Var(
00624         src2.mStr, src2_len,
00625         src1.mStr, src1_len);
00626     BOOST_CHECK(src1.verify());
00627     BOOST_CHECK(src2.verify());
00628     BOOST_CHECK_EQUAL(result2 * -1, result);
00629 
00630     // force check of equal strings
00631     result = SqlStrCmp_Ascii_Var(
00632         src1.mStr, src1_len,
00633         src1.mStr, src1_len);
00634     BOOST_CHECK(src1.verify());
00635     BOOST_CHECK_EQUAL(result, 0);
00636 
00637 }
00638 
00639 
00640 void
00641 SqlStringAsciiTest::testSqlStringCmp_Ascii_Var_DiffLen()
00642 {
00643     int src1_storage, src2_storage, src1_len, src2_len;
00644     unsigned char startchar;
00645 
00646     for (src1_storage = 0; src1_storage <= MAXLEN; src1_storage++) {
00647         src1_len = src1_storage;
00648         for (src2_storage = 0; src2_storage <= MAXLEN; src2_storage++) {
00649             src2_len = src2_storage;
00650             // can't test w/ 0, confuses strcmp and/or std:string
00651             for (startchar = 1; startchar < 255; startchar++) {
00652                 SqlStringBuffer src1(
00653                     src1_storage, src1_len,
00654                     0, src1_storage - src1_len,
00655                     'd', ' ');
00656                 SqlStringBuffer src2(
00657                     src2_storage, src2_len,
00658                     0, src2_storage - src2_len,
00659                     's', ' ');
00660 
00661                 src1.patternfill(startchar, 1, 255);
00662                 src2.patternfill(startchar, 1, 255);
00663 
00664                 testSqlStringCmp_Ascii_Var_Helper(
00665                     src1, src1_len,
00666                     src2, src2_len);
00667             }
00668         }
00669     }
00670 }
00671 
00672 
00673 void
00674 SqlStringAsciiTest::testSqlStringCmp_Ascii_Var_EqLen()
00675 {
00676     int src1_storage, src2_storage, src1_len, src2_len, randX;
00677 
00678     // not much point large length, chances of 2 random strings being equal
00679     // are very low. test forces an equality check anyway.
00680     src1_storage = MAXCMPLEN;
00681     src1_len = src1_storage;
00682     src2_storage = src1_storage;
00683     src2_len = src1_storage;
00684     for (randX = 0; randX <= MAXCMPRANDOM; randX++) {
00685         SqlStringBuffer src1(
00686             src1_storage, src1_len,
00687             0, src1_storage - src1_len,
00688             'd', ' ');
00689         SqlStringBuffer src2(
00690             src2_storage, src2_len,
00691             0, src2_storage - src2_len,
00692             's', ' ');
00693 
00694         // can't test w/ 0, confuses strcmp and/or std:string
00695         src1.randomize(1, 1, 255);
00696         src2.randomize(1, 1, 255);
00697 
00698         testSqlStringCmp_Ascii_Var_Helper(
00699             src1, src1_len,
00700             src2, src2_len);
00701     }
00702 }
00703 
00704 
00705 void
00706 SqlStringAsciiTest::testSqlStringLenBit_Ascii()
00707 {
00708     int src_storage, src_len;
00709     int newlen = 0;
00710 
00711     src_storage = MAXLEN;
00712     for (src_storage = 0; src_storage <= MAXLEN; src_storage++) {
00713         for (src_len = 0; src_len <= src_storage; src_len++) {
00714             SqlStringBuffer src(
00715                 src_storage, src_len,
00716                 0, src_storage - src_len,
00717                 's', ' ');
00718 
00719             newlen = SqlStrLenBit_Ascii(
00720                 src.mStr,
00721                 src_len);
00722             BOOST_CHECK_EQUAL(newlen, src_len * 8);
00723             BOOST_CHECK(src.verify());
00724 
00725             newlen = SqlStrLenBit_Ascii(
00726                 src.mStr,
00727                 src_storage);
00728             BOOST_CHECK_EQUAL(newlen, src_storage * 8);
00729             BOOST_CHECK(src.verify());
00730         }
00731     }
00732 }
00733 
00734 
00735 void
00736 SqlStringAsciiTest::testSqlStringLenChar_Ascii()
00737 {
00738     int src_storage, src_len;
00739     int newlen = 0;
00740 
00741     src_storage = MAXLEN;
00742     for (src_storage = 0; src_storage <= MAXLEN; src_storage++) {
00743         for (src_len = 0; src_len <= src_storage; src_len++) {
00744             SqlStringBuffer src(
00745                 src_storage, src_len,
00746                 0, src_storage - src_len,
00747                 's', ' ');
00748 
00749             newlen = SqlStrLenChar_Ascii(
00750                 src.mStr,
00751                 src_len);
00752             BOOST_CHECK_EQUAL(newlen, src_len);
00753             BOOST_CHECK(src.verify());
00754 
00755             newlen = SqlStrLenChar_Ascii(
00756                 src.mStr,
00757                 src_storage);
00758             BOOST_CHECK_EQUAL(newlen, src_storage);
00759             BOOST_CHECK(src.verify());
00760         }
00761     }
00762 }
00763 
00764 
00765 void
00766 SqlStringAsciiTest::testSqlStringLenOct_Ascii()
00767 {
00768     int src_storage, src_len;
00769     int newlen = 0;
00770 
00771     src_storage = MAXLEN;
00772     for (src_storage = 0; src_storage <= MAXLEN; src_storage++) {
00773         for (src_len = 0; src_len <= src_storage; src_len++) {
00774             SqlStringBuffer src(
00775                 src_storage, src_len,
00776                 0, src_storage - src_len,
00777                 's', ' ');
00778 
00779             newlen = SqlStrLenOct_Ascii(
00780                 src.mStr,
00781                 src_len);
00782             BOOST_CHECK_EQUAL(newlen, src_len);
00783             BOOST_CHECK(src.verify());
00784 
00785             newlen = SqlStrLenOct_Ascii(
00786                 src.mStr,
00787                 src_storage);
00788             BOOST_CHECK_EQUAL(newlen, src_storage);
00789             BOOST_CHECK(src.verify());
00790         }
00791     }
00792 }
00793 
00794 
00795 void
00796 SqlStringAsciiTest::testSqlStringOverlay_Ascii()
00797 {
00798     int dst_storage, src_storage, src_len, over_storage, over_len;
00799     int position, length, lengthI;
00800     int exLeftLen, exMidLen, exRightLen;
00801     char *exLeftP, *exMidP, *exRightP;
00802     bool lenSpecified;
00803     bool caught;
00804     int newlen = 0;
00805 
00806     for (dst_storage = 0; dst_storage < MAXLEN; dst_storage++) {
00807         for (src_storage = 0; src_storage < MAXLEN; src_storage++) {
00808             src_len = src_storage;
00809             for (over_storage = 0; over_storage < MAXLEN; over_storage++) {
00810                 over_len = over_storage;
00811                 for (position = 0; position < MAXLEN; position++) {
00812                     for (lengthI = -1; lengthI < MAXLEN; lengthI++) {
00813                         if (lengthI == -1) {
00814                             lenSpecified = false;
00815                             length = over_len;
00816                         } else {
00817                             lenSpecified = true;
00818                             length = lengthI;
00819                         }
00820 #if 0
00821                         BOOST_MESSAGE(
00822                             " dst_storage=" << dst_storage <<
00823                             " src_storage=" << src_storage <<
00824                             " over_storage=" << over_storage <<
00825                             " pos=" << position <<
00826                             " length=" << length <<
00827                             " spec=" << lenSpecified);
00828 #endif
00829                         SqlStringBuffer dst(
00830                             dst_storage, dst_storage,
00831                             0, 0,
00832                             'd', ' ');
00833                         SqlStringBuffer src(
00834                             src_storage, src_len,
00835                             0, src_storage - src_len,
00836                             's', ' ');
00837                         SqlStringBuffer over(
00838                             over_storage, over_len,
00839                             0, over_storage - over_len,
00840                             'o', ' ');
00841 
00842                         src.patternfill('a', 'a', 'z');
00843                         over.patternfill('A', 'A', 'Z');
00844 
00845                         // ex* vars are 0-indexed. for loops are 1-indexed
00846                         exLeftP = src.mStr;
00847                         if (position >= 1 && src_len >= 1) {
00848                             exLeftLen = position - 1;  // 1-idx -> 0-idx
00849                             if (exLeftLen > src_len) {
00850                                 exLeftLen = src_len;
00851                             }
00852                         } else {
00853                             exLeftLen = 0;
00854                         }
00855 
00856                         exMidP = over.mStr;
00857                         exMidLen = over_len;
00858 
00859                         exRightLen = src_len - (exLeftLen + length);
00860                         if (exRightLen < 0) {
00861                             exRightLen = 0;
00862                         }
00863                         exRightP = exLeftP + (src_len - exRightLen);
00864 
00865                         string expect(exLeftP, exLeftLen);
00866                         expect.append(exMidP, exMidLen);
00867                         expect.append(exRightP, exRightLen);
00868 
00869                         caught = false;
00870                         try {
00871                             newlen = SqlStrOverlay_Ascii(
00872                                 dst.mStr,
00873                                 dst_storage,
00874                                 src.mStr,
00875                                 src_len,
00876                                 over.mStr,
00877                                 over_len,
00878                                 position,
00879                                 length,
00880                                 lenSpecified);
00881                         } catch (const char *str) {
00882                             caught = true;
00883                             if (!strcmp(str, "22011")) {
00884                                 BOOST_CHECK(
00885                                     position < 1
00886                                     || (lenSpecified && length < 1));
00887                             } else if (!strcmp(str, "22001")) {
00888                                 BOOST_CHECK(src_len + over_len > dst_storage);
00889                             } else {
00890                                 BOOST_CHECK(false);
00891                             }
00892                         }
00893                         if (!caught) {
00894                             BOOST_CHECK(position > 0);
00895                             if (lenSpecified) {
00896                                 BOOST_CHECK(length >= 0);
00897                             }
00898 
00899                             BOOST_CHECK(dst.verify());
00900                             BOOST_CHECK(src.verify());
00901                             BOOST_CHECK(over.verify());
00902 
00903                             string result(dst.mStr, newlen);
00904 
00905                             BOOST_CHECK(!result.compare(expect));
00906                             BOOST_CHECK(!expect.compare(result));
00907                         }
00908                     }
00909                 }
00910             }
00911         }
00912     }
00913 }
00914 
00915 void
00916 SqlStringAsciiTest::testSqlStringPos_Ascii()
00917 {
00918     int src_storage, find_start, find_len, randX;
00919     int alter_char;
00920 
00921     int foundpos;
00922 
00923     for (src_storage = 0; src_storage < MAXLEN; src_storage++) {
00924         for (randX = 0; randX < MAXRANDOM; randX++) {
00925             SqlStringBuffer src(
00926                 src_storage, src_storage,
00927                 0, 0,
00928                 's', ' ');
00929             src.randomize('a', 'a', 'z');
00930 
00931             // find all possible valid substrings
00932             for (find_start = 0; find_start <= src_storage; find_start++) {
00933                 for (find_len = 0; find_len <= src_storage - find_start;
00934                      find_len++)
00935                 {
00936                     string validsubstr(src.mStr + find_start, find_len);
00937                     SqlStringBuffer find(
00938                         find_len, find_len,
00939                         0, 0,
00940                         'X', ' ');
00941                     memcpy(find.mStr, validsubstr.c_str(), find_len);
00942 
00943                     foundpos = SqlStrPos_Ascii(
00944                         src.mStr,
00945                         src_storage,
00946                         find.mStr,
00947                         find_len);
00948                     BOOST_CHECK(src.verify());
00949                     BOOST_CHECK(find.verify());
00950 
00951                     if (find_len) {
00952                         // foundpos is 1-indexed. find_start is 0-indexed.
00953                         BOOST_CHECK_EQUAL(foundpos, find_start + 1);
00954                     } else {
00955                         BOOST_CHECK_EQUAL(
00956                             foundpos, static_cast<int>(1));  // Case A.
00957                     }
00958 
00959                     // alter valid substring to prevent match
00960                     for (alter_char = 0; alter_char < find_len; alter_char++) {
00961                         char save = *(find.mStr + alter_char);
00962                         // 'X' not between 'a' and 'z'
00963                         *(find.mStr + alter_char) = 'X';
00964 
00965                         foundpos  = SqlStrPos_Ascii(
00966                             src.mStr,
00967                             src_storage,
00968                             find.mStr,
00969                             find_len);
00970                         BOOST_CHECK(src.verify());
00971                         BOOST_CHECK(find.verify());
00972 
00973                         BOOST_CHECK_EQUAL(foundpos, static_cast<int>(0));
00974 
00975                         *(find.mStr + alter_char) = save;
00976                     }
00977                 }
00978             }
00979         }
00980     }
00981 }
00982 
00983 void
00984 SqlStringAsciiTest::testSqlStringSubStr_Ascii()
00985 {
00986     int src_storage, src_len, dst_storage, newlen = 0;
00987     int sub_start, sub_len;
00988     bool caught;
00989     char const * resultP;
00990 
00991     // must test where substart and/or sublen larger than src_storage and
00992     // less than 0
00993 
00994     for (dst_storage = 0; dst_storage < MAXLEN; dst_storage++) {
00995         for (src_storage = 0; src_storage <= dst_storage; src_storage++) {
00996             for (src_len = 0; src_len <= src_storage; src_len++) {
00997                 for (sub_start = -3; sub_start <= 3 + src_storage;
00998                      sub_start++)
00999                 {
01000                     for (sub_len = -3; sub_len <= 3 + src_storage; sub_len++) {
01001                         SqlStringBuffer dst(
01002                             dst_storage, dst_storage,
01003                             0, 0,
01004                             'd', ' ');
01005                         SqlStringBuffer src(
01006                             src_storage, src_len,
01007                             0, src_storage - src_len,
01008                             's', ' ');
01009                         src.randomize();
01010 #if 0
01011                         BOOST_MESSAGE(
01012                             "src =|" << src.mLeftP <<
01013                             "| dest_storage=" << dst_storage <<
01014                             " src_storage=" << src_storage <<
01015                             " src_len=" << src_len <<
01016                             " sub_start=" << sub_start <<
01017                             " sub_len=" << sub_len);
01018 #endif
01019                         int exsubstart = sub_start;
01020                         int exlen = sub_len;
01021                         if (exsubstart < 1) {
01022                             // will grab fewer characters
01023                             exlen += (exsubstart - 1);
01024                         }
01025                         exsubstart--;                       // convert index
01026                         if (exsubstart < 0) {
01027                             exsubstart = 0; // clean up for std::string
01028                         }
01029                         if (exlen < 0) {
01030                             exlen = 0;           // clean up for std::string
01031                         }
01032 
01033                         if (exsubstart + exlen > src_storage) {
01034                             if (exsubstart > src_storage) {
01035                                 exlen = 0;
01036                             } else {
01037                                 exlen = src_storage - exsubstart;
01038                             }
01039                         }
01040                         if (exsubstart < 0) {
01041                             exsubstart = 0; // clean up for std::string
01042                         }
01043                         if (exlen < 0) {
01044                             exlen = 0;           // clean up for std::string
01045                         }
01046 
01047                         string expect(src.mStr + exsubstart, exlen);
01048 
01049                         caught = false;
01050                         try {
01051                             newlen = SqlStrSubStr_Ascii(
01052                                 &resultP, dst_storage,
01053                                 src.mStr, src_storage,
01054                                 sub_start, sub_len, true);
01055                         } catch (const char *str) {
01056                             caught = true;
01057                             if (!strcmp(str, "22011")) {
01058                                 BOOST_CHECK(sub_len < 0);
01059                             } else if (!strcmp(str, "22001")) {
01060                                 BOOST_CHECK(sub_len > dst_storage);
01061                                 BOOST_CHECK(sub_len >= 0);
01062                             } else {
01063                                 BOOST_CHECK(false);
01064                             }
01065                         }
01066                         if (!caught) {
01067                             BOOST_CHECK(sub_len >= 0);
01068                             string result(resultP, newlen);
01069 #if 0
01070                             BOOST_MESSAGE("expect |" << expect << "|");
01071                             BOOST_MESSAGE("result |" << result << "|");
01072 #endif
01073                             BOOST_CHECK(!result.compare(expect));
01074                             BOOST_CHECK(!expect.compare(result));
01075                         }
01076                         BOOST_CHECK(dst.verify());
01077                         BOOST_CHECK(src.verify());
01078 
01079                         // length unspecified mode
01080                         // test when length is at or past the storage
01081                         if (sub_start > 0 && sub_len > 0 &&
01082                             sub_start + sub_len - 1 > src_storage) {
01083                             caught = false;
01084                             try {
01085                                 newlen = SqlStrSubStr_Ascii(
01086                                     &resultP, dst_storage,
01087                                     src.mStr, src_storage,
01088                                     sub_start, 0, false);
01089                             } catch (const char *str) {
01090                                 caught = true;
01091                                 if (!strcmp(str, "22011")) {
01092                                     BOOST_CHECK(sub_len < 0);
01093                                 } else if (!strcmp(str, "22001")) {
01094                                     BOOST_CHECK(sub_len > dst_storage);
01095                                 } else {
01096                                     BOOST_CHECK(false);
01097                                 }
01098                             }
01099                             if (!caught) {
01100                                 // BOOST_MESSAGE(
01101                                 //     " len=" << sub_len
01102                                 //     << " start=" << sub_start);
01103                                 string result(resultP, newlen);
01104 #if 0
01105                                 BOOST_MESSAGE("expect |" << expect << "|");
01106                                 BOOST_MESSAGE("result |" << result << "|");
01107 #endif
01108                                 BOOST_CHECK(!result.compare(expect));
01109                                 BOOST_CHECK(!expect.compare(result));
01110                             }
01111                             BOOST_CHECK(dst.verify());
01112                             BOOST_CHECK(src.verify());
01113                         }
01114                     }
01115                 }
01116             }
01117         }
01118     }
01119 }
01120 
01121 void
01122 SqlStringAsciiTest::testSqlStringToLower_Ascii()
01123 {
01124     int dest_storage, dest_len, src_storage, src_len, randX;
01125     int newlen = 0;
01126     bool caught;
01127 
01128     for (dest_storage = 0; dest_storage < MAXLEN; dest_storage++) {
01129         dest_len = dest_storage;
01130         for (src_storage = 0; src_storage < MAXLEN; src_storage++) {
01131             src_len = src_storage;
01132             for (randX = 0; randX < MAXRANDOM; randX++) {
01133                 SqlStringBuffer dest(
01134                     dest_storage, dest_len,
01135                     0, 0,
01136                     'd', ' ');
01137                 SqlStringBuffer src(
01138                     src_storage, src_len,
01139                     0, src_storage - src_len,
01140                     's', ' ');
01141                 src.randomize('A');
01142 
01143                 string save(src.mStr, src_len);
01144                 string::iterator itr;
01145                 char const *s;
01146                 int count;
01147 
01148                 // copy
01149                 caught = false;
01150                 try {
01151                     newlen = SqlStrToLower_Ascii(
01152                         dest.mStr, dest_storage, src.mStr, src_len);
01153                 } catch (const char *str) {
01154                     caught = true;
01155                     BOOST_CHECK_EQUAL(strcmp(str, "22001"), 0);
01156                     BOOST_CHECK(src_len > dest_storage);
01157                 } catch (...) {
01158                     BOOST_CHECK(false);
01159                 }
01160                 if (!caught) {
01161                     BOOST_CHECK(src_len <= dest_storage);
01162                     BOOST_CHECK(src.verify());
01163                     BOOST_CHECK(dest.verify());
01164                     BOOST_CHECK_EQUAL(newlen, src_len);
01165 
01166                     itr = save.begin();
01167                     s = dest.mStr;
01168                     count = 0;
01169                     while (itr != save.end()) {
01170                         if (*itr >= 'A' && *itr <= 'Z') {
01171                             BOOST_CHECK_EQUAL(*s, (*itr + ('a' - 'A')));
01172                         } else {
01173                             BOOST_CHECK_EQUAL(*itr, *s);
01174                         }
01175                         s++;
01176                         itr++;
01177                         count++;
01178                     }
01179                     BOOST_CHECK_EQUAL(count, src_len);
01180                 }
01181             }
01182         }
01183     }
01184 }
01185 
01186 
01187 void
01188 SqlStringAsciiTest::testSqlStringToUpper_Ascii()
01189 {
01190     int dest_storage, dest_len, src_storage, src_len, randX;
01191     int newlen = 0;
01192     bool caught;
01193 
01194 
01195     for (dest_storage = 0; dest_storage < MAXLEN; dest_storage++) {
01196         dest_len = dest_storage;
01197         for (src_storage = 0; src_storage < MAXLEN; src_storage++) {
01198             src_len = src_storage;
01199             for (randX = 0; randX < MAXRANDOM; randX++) {
01200                 SqlStringBuffer dest(
01201                     dest_storage, dest_len,
01202                     0, 0,
01203                     'd', ' ');
01204                 SqlStringBuffer src(
01205                     src_storage, src_len,
01206                     0, src_storage - src_len,
01207                     's', ' ');
01208                 src.randomize('a');
01209 
01210                 string save(src.mStr, src_len);
01211                 string::iterator itr;
01212                 char const *s;
01213                 int count;
01214 
01215                 // copy
01216                 caught = false;
01217                 try {
01218                     newlen = SqlStrToUpper_Ascii(
01219                         dest.mStr, dest_storage, src.mStr, src_len);
01220                 } catch (const char *str) {
01221                     caught = true;
01222                     BOOST_CHECK_EQUAL(strcmp(str, "22001"), 0);
01223                     BOOST_CHECK(src_len > dest_storage);
01224                 } catch (...) {
01225                     BOOST_CHECK(false);
01226                 }
01227                 if (!caught) {
01228                     BOOST_CHECK(src_len <= dest_storage);
01229                     BOOST_CHECK(src.verify());
01230                     BOOST_CHECK(dest.verify());
01231                     BOOST_CHECK_EQUAL(newlen, src_len);
01232 
01233                     itr = save.begin();
01234                     s = dest.mStr;
01235                     count = 0;
01236 
01237                     while (itr != save.end()) {
01238                         if (*itr >= 'a' && *itr <= 'z') {
01239                             BOOST_CHECK_EQUAL(*s, (*itr - ('a' - 'A')));
01240                         } else {
01241                             BOOST_CHECK_EQUAL(*itr, *s);
01242                         }
01243                         s++;
01244                         itr++;
01245                         count++;
01246                     }
01247                     BOOST_CHECK_EQUAL(count, src_len);
01248                 }
01249             }
01250         }
01251     }
01252 }
01253 
01254 void
01255 SqlStringAsciiTest::testSqlStringTrim_Ascii()
01256 {
01257     int dst_storage, src_storage, src_len, leftpad, rightpad, randX, i;
01258     char padchar = ' ';
01259     char textchar = 's';
01260     bool caught;
01261 
01262     BOOST_REQUIRE(MAXLEN >= 5); // 2 pad + 1 text + 2 pad
01263 
01264     for (dst_storage = 0; dst_storage < MAXLEN; dst_storage++) {
01265         for (src_storage = 0; src_storage < dst_storage + 5; src_storage++) {
01266             for (src_len = 0; src_len <= src_storage; src_len++) {
01267                 for (leftpad = 0; leftpad <= src_storage - src_len; leftpad++) {
01268                     rightpad = src_storage - (src_len + leftpad);
01269                     for (randX = 0; randX < MAXRANDOM; randX++) {
01270                         BOOST_REQUIRE(
01271                            leftpad + rightpad + src_len == src_storage);
01272                         for (i = 0; i < 4; i++) {
01273                             int newsize;
01274                             int expectsize;
01275                             string resultByReference;
01276                             string resultCopy;
01277                             string expect;
01278                             SqlStringBuffer src(
01279                                 src_storage, src_len,
01280                                 leftpad, rightpad,
01281                                 textchar, padchar);
01282                             SqlStringBuffer dst(
01283                                 dst_storage, dst_storage,
01284                                 0, 0,
01285                                 textchar, padchar);
01286 
01287                             int lefttrim = false, righttrim = false;
01288                             switch (i) {
01289                             case 0:
01290                                 lefttrim = true;
01291                                 righttrim = true;
01292                                 expect.append(src_len, textchar);
01293                                 expectsize = src_len;
01294                                 break;
01295                             case 1:
01296                                 lefttrim = true;
01297                                 righttrim = false;
01298                                 expect.append(src_len, textchar);
01299                                 // if no text, everything is trimmed
01300                                 if (src_len) {
01301                                     expect.append(rightpad, padchar);
01302                                 }
01303                                 expectsize = src_len + (src_len ? rightpad : 0);
01304                                 break;
01305                             case 2:
01306                                 lefttrim = false;
01307                                 righttrim = true;
01308                                 // if no text, everything is trimmed
01309                                 if (src_len) {
01310                                     expect.append(leftpad, padchar);
01311                                 }
01312                                 expect.append(src_len, textchar);
01313                                 expectsize = src_len + (src_len ? leftpad : 0);
01314                                 break;
01315                             case 3:
01316                                 lefttrim = false;
01317                                 righttrim = false;
01318                                 expect.append(leftpad, padchar);
01319                                 expect.append(src_len, textchar);
01320                                 expect.append(rightpad, padchar);
01321                                 expectsize = src_len + leftpad + rightpad;
01322                                 break;
01323                             }
01324 
01325                             // test copy implementation
01326                             caught = false;
01327                             try {
01328                                 newsize =
01329                                     SqlStrTrim_Ascii(
01330                                         dst.mStr,
01331                                         dst_storage,
01332                                         src.mStr,
01333                                         src_len + leftpad + rightpad,
01334                                         lefttrim,
01335                                         righttrim);
01336                             } catch (const char *str) {
01337                                 caught = true;
01338                                 BOOST_CHECK_EQUAL(strcmp(str, "22001"), 0);
01339                                 BOOST_CHECK(expectsize > dst_storage);
01340                             } catch (...) {
01341                                 BOOST_CHECK(false);
01342                             }
01343 
01344                             if (!caught) {
01345                                 BOOST_CHECK(expectsize <= dst_storage);
01346 
01347                                 BOOST_CHECK(src.verify());
01348                                 BOOST_CHECK(dst.verify());
01349                                 BOOST_CHECK_EQUAL(newsize, expectsize);
01350                                 resultCopy = string(dst.mStr, newsize);
01351 
01352                                 BOOST_CHECK(!resultCopy.compare(expect));
01353                                 BOOST_CHECK(!expect.compare(resultCopy));
01354                             }
01355 
01356 
01357                             // test by reference
01358                             char const * start;
01359                             newsize = SqlStrTrim_Ascii(
01360                                 &start,
01361                                 src.mStr,
01362                                 src_len + leftpad + rightpad,
01363                                 lefttrim,
01364                                 righttrim);
01365 
01366                             BOOST_CHECK(start >= src.mStr);
01367                             BOOST_CHECK(start <= src.mStr + src_storage);
01368                             BOOST_CHECK(src.verify());
01369                             BOOST_CHECK(dst.verify());
01370                             BOOST_CHECK_EQUAL(newsize, expectsize);
01371                             resultByReference = string(start, newsize);
01372 
01373                             BOOST_CHECK(!resultByReference.compare(expect));
01374                             BOOST_CHECK(!expect.compare(resultByReference));
01375                         }
01376                     }
01377                 }
01378             }
01379         }
01380     }
01381 }
01382 
01383 
01384 FENNEL_UNIT_TEST_SUITE(SqlStringAsciiTest);
01385 
01386 // End SqlStringAsciiTest.cpp

Generated on Mon Jun 22 04:00:13 2009 for Fennel by  doxygen 1.5.1