CalcExtStringTest.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calctest/CalcExtStringTest.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/common/TraceSource.h"
00026 
00027 #include "fennel/tuple/TupleDataWithBuffer.h"
00028 #include "fennel/tuple/TuplePrinter.h"
00029 #include "fennel/calculator/CalcCommon.h"
00030 #include "fennel/calculator/StringToHex.h"
00031 #include "fennel/common/FennelExcn.h"
00032 
00033 #include <boost/test/test_tools.hpp>
00034 #include <boost/scoped_array.hpp>
00035 #include <string>
00036 #include <limits>
00037 
00038 
00039 using namespace fennel;
00040 using namespace std;
00041 
00042 
00043 class CalcExtStringTest : virtual public TestBase, public TraceSource
00044 {
00045     void testCalcExtStringCatA2();
00046     void testCalcExtStringCatA3();
00047     void testCalcExtStringCmpA();
00048     void testCalcExtStringCmpOct();
00049     void testCalcExtStringLenBitA();
00050     void testCalcExtStringLenCharA();
00051     void testCalcExtStringLenOctA();
00052     void testCalcExtStringOverlayA4();
00053     void testCalcExtStringOverlayA5();
00054     void testCalcExtStringPosA();
00055     void testCalcExtStringSubStringA3();
00056     void testCalcExtStringSubStringA4();
00057     void testCalcExtStringToANull();
00058     void testCalcExtStringToLower();
00059     void testCalcExtStringToUpper();
00060     void testCalcExtStringTrim();
00061 
00062     int cmpTupStr(
00063         TupleDatum const & tup,
00064         char const * const str);
00065     int cmpTupInt(
00066         TupleDatum const & tup,
00067         int val);
00068     int cmpTupNull(TupleDatum const & tup);
00069     void printOutput(
00070         TupleData const & tup,
00071         Calculator const & calc);
00072     void refLocalOutput(
00073         ostringstream& pg,
00074         int count);
00075     static const char* truncErr;
00076     static const char* substrErr;
00077 
00078 public:
00079     explicit CalcExtStringTest()
00080         : TraceSource(shared_from_this(),"CalcExtStringTest")
00081     {
00082         srand(time(NULL));
00083         CalcInit::instance();
00084         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringCatA2);
00085         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringCatA3);
00086         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringCmpA);
00087         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringCmpOct);
00088         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringLenBitA);
00089         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringLenCharA);
00090         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringLenOctA);
00091         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringOverlayA4);
00092         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringOverlayA5);
00093         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringPosA);
00094         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringSubStringA3);
00095         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringSubStringA4);
00096         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringToANull);
00097         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringToLower);
00098         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringToUpper);
00099         FENNEL_UNIT_TEST_CASE(CalcExtStringTest, testCalcExtStringTrim);
00100     }
00101 
00102     virtual ~CalcExtStringTest()
00103     {
00104     }
00105 };
00106 
00107 const char *
00108 CalcExtStringTest::truncErr = "22001";
00109 
00110 const char *
00111 CalcExtStringTest::substrErr = "22011";
00112 
00113 int
00114 CalcExtStringTest::cmpTupStr(
00115     TupleDatum const & tup,
00116     char const * const str)
00117 {
00118     int len = strlen(str);
00119     BOOST_CHECK_EQUAL(len, tup.cbData);
00120     return strncmp(
00121         reinterpret_cast<char *>(const_cast<PBuffer>(tup.pData)),
00122         str,
00123         len);
00124 }
00125 
00126 int
00127 CalcExtStringTest::cmpTupInt(
00128     TupleDatum const & tup,
00129     int val)
00130 {
00131     return *(reinterpret_cast<int*>(
00132         const_cast<PBuffer>(tup.pData)))
00133         - val;
00134 }
00135 
00136 int
00137 CalcExtStringTest::cmpTupNull(TupleDatum const & tup)
00138 {
00139     if ((const_cast<PBuffer>(tup.pData)) == NULL) {
00140         return 1;
00141     }
00142     return 0;
00143 }
00144 
00145 // for nitty-gritty debugging. sadly, doesn't use BOOST_MESSAGE.
00146 void
00147 CalcExtStringTest::printOutput(
00148     TupleData const & tup,
00149     Calculator const & calc)
00150 {
00151 #if 0
00152     TuplePrinter tuplePrinter;
00153     tuplePrinter.print(cout, calc.getOutputRegisterDescriptor(), tup);
00154     cout << endl;
00155 #endif
00156 }
00157 
00158 // copy-by-reference locals into identical output register
00159 void
00160 CalcExtStringTest::refLocalOutput(
00161     ostringstream& pg,
00162     int count)
00163 {
00164     int i;
00165 
00166     for (i = 0; i < count; i++) {
00167         pg << "REF O" << i << ", L" << i << ";" << endl;
00168     }
00169 }
00170 
00171 
00172 void
00173 CalcExtStringTest::testCalcExtStringCatA2()
00174 {
00175     ostringstream pg(""), outloc("");
00176     int i;
00177 
00178     for (i = 0; i <= 2; i++) {
00179         outloc << "vc,5, ";
00180     }
00181     outloc << "c,5, ";
00182     outloc << "vc,1, c,1;" << endl;    // right truncate
00183 
00184     pg << "O " << outloc.str();
00185     pg << "L " << outloc.str();
00186     pg << "C vc,5, vc,2, vc,2, ";  // varchar data[0-2]
00187     pg << "c,5, ";                 // char data[3]
00188     pg << "vc,5, c,5;" << endl;    // nulls[4-5]
00189     pg << "V 0x" << stringToHex("AB");
00190     pg << ", 0x" << stringToHex("CD");
00191     pg << ", 0x" << stringToHex("");
00192     pg << ", 0x" << stringToHex("GHIJ ");
00193     pg << ",,;" << endl;
00194     pg << "T;" << endl;
00195     // varchar common case
00196     // first, clear output string
00197     pg << "CALL 'strCpyA(L0, C2);" << endl;
00198     pg << "CALL 'strCatA2(L0, C0);" << endl;
00199     // append to first string
00200     pg << "CALL 'strCatA2(L0, C1);" << endl;
00201     // zero length case
00202     // first, clear output string
00203     pg << "CALL 'strCpyA(L1, C2);" << endl;
00204     pg << "CALL 'strCatA2(L1, C2);" << endl;
00205     // varchar null case
00206     pg << "CALL 'strCatA2(L2, C4);" << endl;
00207     // varchar right truncate
00208     pg << "CALL 'strCatA2(L4, C0);" << endl;
00209 
00210     // char common case: can't test fixed common case w/o
00211     // using strCatAF3 as well to have length set
00212     // correctly. tested elsewhere
00213     // char null case
00214     pg << "CALL 'strCatA2(L3, C5);" << endl;
00215     // char right truncate
00216     pg << "CALL 'strCatA2(L5, C3);" << endl;
00217     // make output available
00218     refLocalOutput(pg, 6);
00219 
00220     Calculator calc(0);
00221 
00222     try {
00223         calc.assemble(pg.str().c_str());
00224     } catch (FennelExcn& ex) {
00225         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00226         BOOST_MESSAGE(pg.str());
00227         BOOST_REQUIRE(0);
00228     }
00229 
00230     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00231     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00232 
00233     calc.bind(&inTuple, &outTuple);
00234     calc.exec();
00235     printOutput(outTuple, calc);
00236     deque<CalcMessage>::iterator iter = calc.mWarnings.begin();
00237 
00238     // varchar common case
00239     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[0], "ABCD"));
00240     // varchar zero length case
00241     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[1], ""));
00242     // varchar null case
00243     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[2]));
00244     // varchar right truncation
00245     BOOST_CHECK_EQUAL(iter->pc, 6);
00246     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
00247     iter++;
00248     // char null case
00249     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[3]));
00250     // char right truncation
00251     BOOST_CHECK_EQUAL(iter->pc, 8);
00252     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
00253     iter++;
00254     BOOST_CHECK(iter == calc.mWarnings.end());
00255 }
00256 void
00257 CalcExtStringTest::testCalcExtStringCatA3()
00258 {
00259     ostringstream pg(""), outloc("");
00260     int i;
00261 
00262     for (i = 0; i <= 3; i++) {
00263         outloc << "vc,5, ";
00264     }
00265     for (i = 4; i <= 6; i++) {
00266         outloc << "c,5, ";
00267     }
00268     outloc << "vc,1, c,1;" << endl;    // right truncate
00269 
00270     pg << "O " << outloc.str();
00271     pg << "L " << outloc.str();
00272     pg << "C vc,5, vc,2, vc,2, ";  // varchar data[0-2]
00273     pg << "c,3, c,2, ";            // char data[3-4]
00274     pg << "vc,5, c,5;" << endl;    // nulls[5-6]
00275     pg << "V 0x" << stringToHex("AB");
00276     pg << ", 0x" << stringToHex("CD");
00277     pg << ", 0x" << stringToHex("");
00278     pg << ", 0x" << stringToHex("GHI");
00279     pg << ", 0x" << stringToHex("JK");
00280     pg << ",,;" << endl;
00281     pg << "T;" << endl;
00282     // varchar common case
00283     pg << "CALL 'strCatA3(L0, C0, C1);" << endl;
00284     // zero length case
00285     pg << "CALL 'strCatA3(L1, C2, C2);" << endl;
00286     // varchar null cases
00287     pg << "CALL 'strCatA3(L2, C5, C1);" << endl;
00288     pg << "CALL 'strCatA3(L3, C0, C5);" << endl;
00289     // varchar right truncate
00290     pg << "CALL 'strCatA3(L7, C0, C0);" << endl;
00291 
00292     // char common case
00293     pg << "CALL 'strCatA3(L4, C3, C4);" << endl;
00294     // char null cases
00295     pg << "CALL 'strCatA3(L5, C6, C4);" << endl;
00296     pg << "CALL 'strCatA3(L6, C3, C6);" << endl;
00297     // char right truncate
00298     pg << "CALL 'strCatA3(L8, C3, C4);" << endl;
00299     // make output available
00300     refLocalOutput(pg, 9);
00301 
00302     Calculator calc(0);
00303 
00304     try {
00305         calc.assemble(pg.str().c_str());
00306     } catch (FennelExcn& ex) {
00307         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00308         BOOST_MESSAGE(pg.str());
00309         BOOST_REQUIRE(0);
00310     }
00311 
00312     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00313     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00314 
00315     calc.bind(&inTuple, &outTuple);
00316     calc.exec();
00317     printOutput(outTuple, calc);
00318     deque<CalcMessage>::iterator iter = calc.mWarnings.begin();
00319 
00320     // varchar common case
00321     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[0], "ABCD"));
00322     // varchar zero length case
00323     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[1], ""));
00324     // varchar null cases
00325     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[2]));
00326     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[3]));
00327     // varchar right truncation
00328     BOOST_CHECK_EQUAL(iter->pc, 4);
00329     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
00330     iter++;
00331     // char common case
00332     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[4], "GHIJK"));
00333     // char null cases
00334     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[5]));
00335     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[6]));
00336     // char right truncation
00337     BOOST_CHECK_EQUAL(iter->pc, 8);
00338     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
00339     iter++;
00340     BOOST_CHECK(iter == calc.mWarnings.end());
00341 }
00342 
00343 void
00344 CalcExtStringTest::testCalcExtStringCmpA()
00345 {
00346     ostringstream pg(""), outloc("");
00347     int i;
00348 
00349     for (i = 0; i <= 11; i++) {
00350         outloc << "s4, ";
00351     }
00352     outloc << "s4;" << endl;
00353 
00354     pg << "O " << outloc.str();
00355     pg << "L " << outloc.str();
00356     pg << "C vc,2, vc,2, vc,2, "; // varchar data[0-2]
00357     pg << "c,2, c,2, c,2, ";      // char data[3-5]
00358     pg << "vc,5, c,5;" << endl;  // nulls[6-7]
00359     pg << "V 0x" << stringToHex("AB");
00360     pg << ", 0x" << stringToHex("CD");
00361     pg << ", 0x" << stringToHex("EF");
00362     pg << ", 0x" << stringToHex("GH");
00363     pg << ", 0x" << stringToHex("IJ");
00364     pg << ", 0x" << stringToHex("EF");
00365     pg << ",,;" << endl;
00366     pg << "T;" << endl;
00367     // varchar common cases
00368     pg << "CALL 'strCmpA(L0, C0, C0);" << endl;
00369     pg << "CALL 'strCmpA(L1, C1, C0);" << endl;
00370     pg << "CALL 'strCmpA(L2, C0, C1);" << endl;
00371     // varchar null cases
00372     pg << "CALL 'strCmpA(L3, C6, C0);" << endl;
00373     pg << "CALL 'strCmpA(L4, C0, C6);" << endl;
00374     // char common cases
00375     pg << "CALL 'strCmpA(L5, C3, C3);" << endl;
00376     pg << "CALL 'strCmpA(L6, C4, C3);" << endl;
00377     pg << "CALL 'strCmpA(L7, C3, C4);" << endl;
00378     // char null cases
00379     pg << "CALL 'strCmpA(L8, C7, C2);" << endl;
00380     pg << "CALL 'strCmpA(L9, C2, C7);" << endl;
00381     // mixed common cases
00382     pg << "CALL 'strCmpA(L10, C2, C5);" << endl;
00383     pg << "CALL 'strCmpA(L11, C0, C3);" << endl;
00384     pg << "CALL 'strCmpA(L12, C3, C0);" << endl;
00385 
00386     // make output available
00387     refLocalOutput(pg, 13);
00388 
00389     Calculator calc(0);
00390 
00391     try {
00392         calc.assemble(pg.str().c_str());
00393     } catch (FennelExcn& ex) {
00394         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00395         BOOST_MESSAGE(pg.str());
00396         BOOST_REQUIRE(0);
00397     }
00398 
00399     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00400     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00401 
00402     calc.bind(&inTuple, &outTuple);
00403     calc.exec();
00404     printOutput(outTuple, calc);
00405 
00406     // varchar common cases
00407     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[0], 0));
00408     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[1], 1));
00409     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[2], -1));
00410     // varchar null cases
00411     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[3]));
00412     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[4]));
00413     // char common case
00414     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[5], 0));
00415     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[6], 1));
00416     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[7], -1));
00417     // char null cases
00418     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[8]));
00419     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[9]));
00420     // mixed cases
00421     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[10], 0));
00422     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[11], -1));
00423     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[12], 1));
00424 }
00425 
00426 void
00427 CalcExtStringTest::testCalcExtStringCmpOct()
00428 {
00429     ostringstream pg(""), outloc("");
00430     int i;
00431 
00432     for (i = 0; i <= 11; i++) {
00433         outloc << "s4, ";
00434     }
00435     outloc << "s4;" << endl;
00436 
00437     pg << "O " << outloc.str();
00438     pg << "L " << outloc.str();
00439     pg << "C vb,1, vb,1, vb,1, "; // varbinary data[0-2]
00440     //disabling binaries for now since they seem to be failing in the assembler
00441     pg << "b,1, b,1, b,1, ";      // binary data[3-5]
00442     pg << "vb,0, b,0; " << endl;  // nulls[6-7]
00443     pg << "V 0xAA";
00444     pg << ", 0xBB";
00445     pg << ", 0xCC";
00446     pg << ", 0xDD";
00447     pg << ", 0xEE";
00448     pg << ", 0xCC";
00449     pg << ",,;" << endl;
00450 
00451     pg << "T;" << endl;
00452     // varchar common cases
00453     pg << "CALL 'strCmpOct(L0, C0, C0);" << endl;
00454     pg << "CALL 'strCmpOct(L1, C1, C0);" << endl;
00455     pg << "CALL 'strCmpOct(L2, C0, C1);" << endl;
00456     // varchar null cases
00457     pg << "CALL 'strCmpOct(L3, C6, C0);" << endl;
00458     pg << "CALL 'strCmpOct(L4, C0, C6);" << endl;
00459     // char common cases
00460     pg << "CALL 'strCmpOct(L5, C3, C3);" << endl;
00461     pg << "CALL 'strCmpOct(L6, C4, C3);" << endl;
00462     pg << "CALL 'strCmpOct(L7, C3, C4);" << endl;
00463     // char null cases
00464     pg << "CALL 'strCmpOct(L8, C7, C3);" << endl;
00465     pg << "CALL 'strCmpOct(L9, C3, C7);" << endl;
00466     // mixed common cases
00467     pg << "CALL 'strCmpOct(L10, C2, C2);" << endl;
00468     pg << "CALL 'strCmpOct(L11, C0, C3);" << endl;
00469     pg << "CALL 'strCmpOct(L12, C3, C0);" << endl;
00470     // make output available
00471     refLocalOutput(pg, 13);
00472 
00473     Calculator calc(0);
00474 
00475     try {
00476         calc.assemble(pg.str().c_str());
00477     } catch (FennelExcn& ex) {
00478         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00479         BOOST_MESSAGE(pg.str());
00480         BOOST_REQUIRE(0);
00481     }
00482 
00483     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00484     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00485 
00486     calc.bind(&inTuple, &outTuple);
00487     calc.exec();
00488     printOutput(outTuple, calc);
00489 
00490     // varchar common cases
00491     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[0], 0));
00492     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[1], 1));
00493     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[2], -1));
00494     // varchar null cases
00495     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[3]));
00496     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[4]));
00497     // char common case
00498     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[5], 0));
00499     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[6], 1));
00500     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[7], -1));
00501     // char null cases
00502     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[8]));
00503     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[9]));
00504     // check mixed cases
00505     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[10], 0));
00506     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[11], -1));
00507     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[12], 1));
00508 }
00509 
00510 void
00511 CalcExtStringTest::testCalcExtStringLenBitA()
00512 {
00513     ostringstream pg(""), outloc("");
00514     int i;
00515 
00516     for (i = 0; i <= 8; i++) {
00517         outloc << "s4, ";
00518     }
00519     outloc << "s4;" << endl;
00520 
00521     pg << "O " << outloc.str();
00522     pg << "L " << outloc.str();
00523     pg << "C vc,2, vc,2, ";      // varchar data[0-1]
00524     pg << "c,2, c,2, ";          // char data[2-3]
00525     pg << "vc,5, c,5;" << endl;  // nulls[4-5]
00526     pg << "V 0x" << stringToHex("AB");
00527     pg << ", 0x" << stringToHex("");
00528     pg << ", 0x" << stringToHex("GH");
00529     pg << ", 0x" << stringToHex("  ");
00530     pg << ",,;" << endl;
00531     pg << "T;" << endl;
00532     // varchar common cases
00533     pg << "CALL 'strLenBitA(L0, C0);" << endl;
00534     pg << "CALL 'strLenBitA(L1, C1);" << endl;
00535     // varchar null case
00536     pg << "CALL 'strLenBitA(L2, C4);" << endl;
00537     // char common cases
00538     pg << "CALL 'strLenBitA(L3, C2);" << endl;
00539     pg << "CALL 'strLenBitA(L4, C3);" << endl;
00540     // char null case
00541     pg << "CALL 'strLenBitA(L5, C5);" << endl;
00542     // make output available
00543     refLocalOutput(pg, 6);
00544 
00545     Calculator calc(0);
00546 
00547     try {
00548         calc.assemble(pg.str().c_str());
00549     } catch (FennelExcn& ex) {
00550         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00551         BOOST_MESSAGE(pg.str());
00552         BOOST_REQUIRE(0);
00553     }
00554 
00555     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00556     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00557 
00558     calc.bind(&inTuple, &outTuple);
00559     calc.exec();
00560     printOutput(outTuple, calc);
00561 
00562     // varchar common cases
00563     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[0], 16));
00564     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[1], 0));
00565     // varchar null case
00566     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[2]));
00567     // char common case
00568     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[3], 16));
00569     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[4], 16));
00570     // char null case
00571     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[5]));
00572 }
00573 
00574 void
00575 CalcExtStringTest::testCalcExtStringLenCharA()
00576 {
00577     ostringstream pg(""), outloc("");
00578     int i;
00579 
00580     for (i = 0; i <= 8; i++) {
00581         outloc << "s4, ";
00582     }
00583     outloc << "s4;" << endl;
00584 
00585     pg << "O " << outloc.str();
00586     pg << "L " << outloc.str();
00587     pg << "C vc,2, vc,2, ";      // varchar data[0-1]
00588     pg << "c,2, c,2, ";          // char data[2-3]
00589     pg << "vc,5, c,5;" << endl;  // nulls[4-5]
00590     pg << "V 0x" << stringToHex("AB");
00591     pg << ", 0x" << stringToHex("");
00592     pg << ", 0x" << stringToHex("GH");
00593     pg << ", 0x" << stringToHex("  ");
00594     pg << ",,;" << endl;
00595     pg << "T;" << endl;
00596     // varchar common cases
00597     pg << "CALL 'strLenCharA(L0, C0);" << endl;
00598     pg << "CALL 'strLenCharA(L1, C1);" << endl;
00599     // varchar null case
00600     pg << "CALL 'strLenCharA(L2, C4);" << endl;
00601     // char common cases
00602     pg << "CALL 'strLenCharA(L3, C2);" << endl;
00603     pg << "CALL 'strLenCharA(L4, C3);" << endl;
00604     // char null case
00605     pg << "CALL 'strLenCharA(L5, C5);" << endl;
00606     // make output available
00607     refLocalOutput(pg, 10);
00608 
00609     Calculator calc(0);
00610 
00611     try {
00612         calc.assemble(pg.str().c_str());
00613     } catch (FennelExcn& ex) {
00614         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00615         BOOST_MESSAGE(pg.str());
00616         BOOST_REQUIRE(0);
00617     }
00618 
00619     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00620     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00621 
00622     calc.bind(&inTuple, &outTuple);
00623     calc.exec();
00624     printOutput(outTuple, calc);
00625 
00626     // varchar common cases
00627     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[0], 2));
00628     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[1], 0));
00629     // varchar null case
00630     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[2]));
00631     // char common case
00632     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[3], 2));
00633     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[4], 2));
00634     // char null case
00635     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[5]));
00636 }
00637 
00638 void
00639 CalcExtStringTest::testCalcExtStringLenOctA()
00640 {
00641     ostringstream pg(""), outloc("");
00642     int i;
00643 
00644     for (i = 0; i <= 8; i++) {
00645         outloc << "s4, ";
00646     }
00647     outloc << "s4;" << endl;
00648 
00649     pg << "O " << outloc.str();
00650     pg << "L " << outloc.str();
00651     pg << "C vc,2, vc,2, ";      // varchar data[0-1]
00652     pg << "c,2, c,2, ";          // char data[2-3]
00653     pg << "vc,5, c,5;" << endl;  // nulls[4-5]
00654     pg << "V 0x" << stringToHex("AB");
00655     pg << ", 0x" << stringToHex("");
00656     pg << ", 0x" << stringToHex("GH");
00657     pg << ", 0x" << stringToHex("  ");
00658     pg << ",,;" << endl;
00659     pg << "T;" << endl;
00660     // varchar common cases
00661     pg << "CALL 'strLenOctA(L0, C0);" << endl;
00662     pg << "CALL 'strLenOctA(L1, C1);" << endl;
00663     // varchar null case
00664     pg << "CALL 'strLenOctA(L2, C4);" << endl;
00665     // char common cases
00666     pg << "CALL 'strLenOctA(L3, C2);" << endl;
00667     pg << "CALL 'strLenOctA(L4, C3);" << endl;
00668     // char null case
00669     pg << "CALL 'strLenOctA(L5, C5);" << endl;
00670     // make output available
00671     refLocalOutput(pg, 6);
00672 
00673     Calculator calc(0);
00674 
00675     try {
00676         calc.assemble(pg.str().c_str());
00677     } catch (FennelExcn& ex) {
00678         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00679         BOOST_MESSAGE(pg.str());
00680         BOOST_REQUIRE(0);
00681     }
00682 
00683     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00684     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00685 
00686     calc.bind(&inTuple, &outTuple);
00687     calc.exec();
00688     printOutput(outTuple, calc);
00689 
00690     // varchar common cases
00691     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[0], 2));
00692     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[1], 0));
00693     // varchar null case
00694     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[2]));
00695     // char common case
00696     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[3], 2));
00697     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[4], 2));
00698     // char null case
00699     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[5]));
00700 }
00701 
00702 void
00703 CalcExtStringTest::testCalcExtStringOverlayA4()
00704 {
00705     ostringstream pg(""), outloc("");
00706     int i;
00707 
00708     for (i = 0; i <= 9; i++) {
00709         outloc << "vc,5, ";
00710     }
00711     outloc << "vc,1, vc,1;" << endl;          // right truncate
00712 
00713     pg << "O " << outloc.str();
00714     pg << "L " << outloc.str();
00715     pg << "C vc,5, vc,2, c,5, c,2, s4, "; // data[0-4]
00716     pg << "s4, ";                         // negative[5]
00717     pg << "vc,5, c,5, s4;" << endl;       // nulls[6-8]
00718     pg << "V 0x" << stringToHex("ABCD");
00719     pg << ", 0x" << stringToHex("EF");
00720     pg << ", 0x" << stringToHex("GHIJ ");
00721     pg << ", 0x" << stringToHex("KL");
00722     pg << ", 2,-2,,,;" << endl;
00723     pg << "T;" << endl;
00724     // varchar common case
00725     pg << "CALL 'strOverlayA4(L0, C0, C1, C4);" << endl;
00726     // varhcar null cases
00727     pg << "CALL 'strOverlayA4(L1, C6, C1, C4);" << endl;
00728     pg << "CALL 'strOverlayA4(L2, C0, C6, C4);" << endl;
00729     pg << "CALL 'strOverlayA4(L3, C0, C1, C8);" << endl;
00730     // varchar substring error
00731     pg << "CALL 'strOverlayA4(L4, C0, C1, C5);" << endl;
00732     // varchar right truncate
00733     pg << "CALL 'strOverlayA4(L10, C0, C1, C4);" << endl;
00734 
00735     // char common case
00736     pg << "CALL 'strOverlayA4(L5, C2, C3, C4);" << endl;
00737     // char null cases
00738     pg << "CALL 'strOverlayA4(L6, C7, C3, C4);" << endl;
00739     pg << "CALL 'strOverlayA4(L7, C2, C7, C4);" << endl;
00740     pg << "CALL 'strOverlayA4(L8, C2, C3, C8);" << endl;
00741     // varchar substring error
00742     pg << "CALL 'strOverlayA4(L9, C2, C3, C5);" << endl;
00743     // char right truncate
00744     pg << "CALL 'strOverlayA4(L11, C2, C3, C4);" << endl;
00745     // make output available
00746     refLocalOutput(pg, 12);
00747 
00748     Calculator calc(0);
00749 
00750     try {
00751         calc.assemble(pg.str().c_str());
00752     } catch (FennelExcn& ex) {
00753         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00754         BOOST_MESSAGE(pg.str());
00755         BOOST_REQUIRE(0);
00756     }
00757 
00758     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00759     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00760 
00761     calc.bind(&inTuple, &outTuple);
00762     calc.exec();
00763     printOutput(outTuple, calc);
00764     deque<CalcMessage>::iterator iter = calc.mWarnings.begin();
00765 
00766     // varchar common case
00767     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[0], "AEFD"));
00768     // varchar null cases
00769     for (i = 1; i <= 3; i++) {
00770         BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[i]));
00771     }
00772     // varchar substr errors
00773     BOOST_CHECK_EQUAL(iter->pc, 4);
00774     BOOST_CHECK_EQUAL(0, strcmp(iter->str, substrErr));
00775     iter++;
00776     // varchar right truncation
00777     BOOST_CHECK_EQUAL(iter->pc, 5);
00778     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
00779     iter++;
00780     // char common case
00781     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[5], "GKLJ "));
00782     // char null cases
00783     for (i = 6; i <= 8; i++) {
00784         BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[i]));
00785     }
00786     // char substr errors
00787     BOOST_CHECK_EQUAL(iter->pc, 10);
00788     BOOST_CHECK_EQUAL(0, strcmp(iter->str, substrErr));
00789     iter++;
00790     // varchar right truncation
00791     BOOST_CHECK_EQUAL(iter->pc, 11);
00792     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
00793     iter++;
00794     BOOST_CHECK(iter == calc.mWarnings.end());
00795 }
00796 
00797 void
00798 CalcExtStringTest::testCalcExtStringOverlayA5()
00799 {
00800     ostringstream pg(""), outloc("");
00801     int i;
00802 
00803     for (i = 0; i <= 13; i++) {
00804         outloc << "vc,5, ";
00805     }
00806     outloc << "vc,1, vc,1;" << endl;             // right truncate
00807 
00808     pg << "O " << outloc.str();
00809     pg << "L " << outloc.str();
00810     pg << "C vc,5, vc,2, c,5, c,2, s4, s4, ";// data [0-5]
00811     pg << "s4, ";                            // negative [6]
00812     pg << "vc,5, c,5, s4;" << endl;          // nulls [7-9]
00813     pg << "V 0x" << stringToHex("ABCD");
00814     pg << ", 0x" << stringToHex("EF");
00815     pg << ", 0x" << stringToHex("GHIJ ");
00816     pg << ", 0x" << stringToHex("KL");
00817     pg << ", 1, 2,-2,,,;" << endl;
00818     pg << "T;" << endl;
00819     // varchar common case
00820     pg << "CALL 'strOverlayA5(L0, C0, C1, C4, C5);" << endl;
00821     // varchar null cases
00822     pg << "CALL 'strOverlayA5(L1, C7, C1, C4, C5);" << endl;
00823     pg << "CALL 'strOverlayA5(L2, C0, C7, C4, C5);" << endl;
00824     pg << "CALL 'strOverlayA5(L3, C0, C1, C9, C5);" << endl;
00825     pg << "CALL 'strOverlayA5(L4, C0, C1, C4, C9);" << endl;
00826     // varchar substring error
00827     pg << "CALL 'strOverlayA5(L5, C0, C1, C6, C5);" << endl;
00828     pg << "CALL 'strOverlayA5(L6, C0, C1, C4, C6);" << endl;
00829     // varchar right truncate
00830     pg << "CALL 'strOverlayA5(L14, C0, C1, C4, C5);" << endl;
00831 
00832     // char common case
00833     pg << "CALL 'strOverlayA5(L7, C2, C3, C4, C5);" << endl;
00834     // char null cases
00835     pg << "CALL 'strOverlayA5(L8, C8, C3, C4, C5);" << endl;
00836     pg << "CALL 'strOverlayA5(L9, C2, C8, C4, C5);" << endl;
00837     pg << "CALL 'strOverlayA5(L10, C2, C3, C9, C5);" << endl;
00838     pg << "CALL 'strOverlayA5(L11, C2, C3, C4, C9);" << endl;
00839     // char substring error
00840     pg << "CALL 'strOverlayA5(L12, C2, C3, C6, C5);" << endl;
00841     pg << "CALL 'strOverlayA5(L13, C2, C3, C4, C6);" << endl;
00842     // char right truncate
00843     pg << "CALL 'strOverlayA5(L15, C2, C3, C4, C5);" << endl;
00844     // make output available
00845     refLocalOutput(pg, 16);
00846 
00847     Calculator calc(0);
00848 
00849     try {
00850         calc.assemble(pg.str().c_str());
00851     } catch (FennelExcn& ex) {
00852         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00853         BOOST_MESSAGE(pg.str());
00854         BOOST_REQUIRE(0);
00855     }
00856 
00857     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00858     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00859 
00860     calc.bind(&inTuple, &outTuple);
00861     calc.exec();
00862     printOutput(outTuple, calc);
00863     deque<CalcMessage>::iterator iter = calc.mWarnings.begin();
00864 
00865     // varchar common case
00866     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[0], "EFCD"));
00867     // varchar null cases
00868     for (i = 1; i <= 4; i++) {
00869         BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[i]));
00870     }
00871     // varchar substr errors
00872     BOOST_CHECK_EQUAL(iter->pc, 5);
00873     BOOST_CHECK_EQUAL(0, strcmp(iter->str, substrErr));
00874     iter++;
00875     BOOST_CHECK_EQUAL(iter->pc, 6);
00876     BOOST_CHECK_EQUAL(0, strcmp(iter->str, substrErr));
00877     iter++;
00878     // varchar right truncation
00879     BOOST_CHECK_EQUAL(iter->pc, 7);
00880     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
00881     iter++;
00882     // char common case
00883     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[7], "KLIJ "));
00884     // char null cases
00885     for (i = 8; i <= 11; i++) {
00886         BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[i]));
00887     }
00888     // char substr errors
00889     BOOST_CHECK_EQUAL(iter->pc, 13);
00890     BOOST_CHECK_EQUAL(0, strcmp(iter->str, substrErr));
00891     iter++;
00892     BOOST_CHECK_EQUAL(iter->pc, 14);
00893     BOOST_CHECK_EQUAL(0, strcmp(iter->str, substrErr));
00894     iter++;
00895     // char right truncation
00896     BOOST_CHECK_EQUAL(iter->pc, 15);
00897     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
00898     iter++;
00899     BOOST_CHECK(iter == calc.mWarnings.end());
00900 }
00901 
00902 void
00903 CalcExtStringTest::testCalcExtStringPosA()
00904 {
00905     ostringstream pg(""), outloc("");
00906     int i;
00907 
00908     for (i = 0; i <= 7; i++) {
00909         outloc << "s4, ";
00910     }
00911     outloc << "s4;" << endl;
00912 
00913     pg << "O " << outloc.str();
00914     pg << "L " << outloc.str();
00915     pg << "C vc,5, vc,4, vc,4, ";   // vc data[0-2]
00916     pg << "c,5, c,2, c,2, ";         // c data[3-5]
00917     pg << "vc,5, c,5";              // nulls[6-7]
00918     pg << ";" << endl;
00919     pg << "V 0x" << stringToHex("ABCD");
00920     pg << ", 0x" << stringToHex("BC");
00921     pg << ", 0x" << stringToHex("XX");
00922     pg << ", 0x" << stringToHex("GHIJ ");
00923     pg << ", 0x" << stringToHex("HI");
00924     pg << ", 0x" << stringToHex("XX");
00925     pg << ",,;" << endl;
00926     pg << "T;" << endl;
00927     // varchar common
00928     pg << "CALL 'strPosA(L0, C1, C0);" << endl;
00929     pg << "CALL 'strPosA(L1, C2, C0);" << endl;
00930     // varchar null
00931     pg << "CALL 'strPosA(L2, C1, C6);" << endl;
00932     pg << "CALL 'strPosA(L3, C6, C0);" << endl;
00933     // char common
00934     pg << "CALL 'strPosA(L4, C4, C3);" << endl;
00935     pg << "CALL 'strPosA(L5, C5, C3);" << endl;
00936     // char null
00937     pg << "CALL 'strPosA(L6, C4, C7);" << endl;
00938     pg << "CALL 'strPosA(L7, C7, C3);" << endl;
00939     // make output available
00940     refLocalOutput(pg, 8);
00941 
00942     Calculator calc(0);
00943 
00944     try {
00945         calc.assemble(pg.str().c_str());
00946     } catch (FennelExcn& ex) {
00947         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00948         BOOST_MESSAGE(pg.str());
00949         BOOST_REQUIRE(0);
00950     }
00951 
00952     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00953     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00954 
00955     calc.bind(&inTuple, &outTuple);
00956     calc.exec();
00957     printOutput(outTuple, calc);
00958 
00959     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[0], 2));
00960     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[1], 0));
00961     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[2]));
00962     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[3]));
00963     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[4], 2));
00964     BOOST_CHECK_EQUAL(0, cmpTupInt(outTuple[5], 0));
00965     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[6]));
00966     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[7]));
00967 }
00968 
00969 void
00970 CalcExtStringTest::testCalcExtStringSubStringA3()
00971 {
00972     ostringstream pg(""), outloc("");
00973     int i;
00974 
00975     for (i = 0; i <= 5; i++) {
00976         outloc << "vc,5, ";
00977     }
00978     outloc << "vc,1, vc,1;" << endl;    // right truncate
00979 
00980     pg << "O " << outloc.str();
00981     pg << "L " << outloc.str();
00982     pg << "C vc,5, c,5, s4, ";      // data[0-2]
00983     pg << "vc,5, c,5, s4;" << endl; // nulls[3-5]
00984 
00985     pg << "V 0x" << stringToHex("ABCD");
00986     pg << ", 0x" << stringToHex("GHIJ ");
00987     pg << ", 2,,,;" << endl;
00988     pg << "T;" << endl;
00989     // varchar common case
00990     pg << "CALL 'strSubStringA3(L0, C0, C2);" << endl;
00991     // varchar null cases
00992     pg << "CALL 'strSubStringA3(L1, C3, C2);" << endl;
00993     pg << "CALL 'strSubStringA3(L2, C0, C5);" << endl;
00994     // substring error not possible if len is unspecified
00995     // varchar right trunaction
00996     pg << "CALL 'strSubStringA3(L6, C0, C2);" << endl;
00997     // char common case
00998     pg << "CALL 'strSubStringA3(L3, C1, C2);" << endl;
00999     // char null cases
01000     pg << "CALL 'strSubStringA3(L4, C4, C2);" << endl;
01001     pg << "CALL 'strSubStringA3(L5, C1, C5);" << endl;
01002     // substring error not possible if len is unspecified
01003     // char right trunaction
01004     pg << "CALL 'strSubStringA3(L7, C1, C2);" << endl;
01005     // make output available
01006     refLocalOutput(pg, 8);
01007 
01008     Calculator calc(0);
01009 
01010     try {
01011         calc.assemble(pg.str().c_str());
01012     } catch (FennelExcn& ex) {
01013         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
01014         BOOST_MESSAGE(pg.str());
01015         BOOST_REQUIRE(0);
01016     }
01017 
01018     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
01019     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
01020 
01021     calc.bind(&inTuple, &outTuple);
01022     calc.exec();
01023     printOutput(outTuple, calc);
01024     deque<CalcMessage>::iterator iter = calc.mWarnings.begin();
01025 
01026     // varchar common case
01027     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[0], "BCD"));
01028     // varchar null cases
01029     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[1]));
01030     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[2]));
01031     // varchar right truncation
01032     BOOST_CHECK_EQUAL(iter->pc, 3);
01033     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
01034     iter++;
01035     // char common case
01036     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[3], "HIJ "));
01037     // char null cases
01038     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[4]));
01039     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[5]));
01040     // varchar right truncation
01041     BOOST_CHECK_EQUAL(iter->pc, 7);
01042     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
01043     iter++;
01044     BOOST_CHECK(iter == calc.mWarnings.end());
01045 }
01046 
01047 void
01048 CalcExtStringTest::testCalcExtStringSubStringA4()
01049 {
01050     ostringstream pg(""), outloc("");
01051     int i;
01052 
01053     for (i = 0; i <= 11; i++) {
01054         outloc << "vc,5, ";
01055     }
01056     outloc << "vc,1, vc,1;" << endl;    // right truncate
01057 
01058     pg << "O " << outloc.str();
01059     pg << "L " << outloc.str();
01060     pg << "C vc,5, c,5, s4, s4, ";  // data[0-3]
01061     pg << "s4, ";                   // negative[4]
01062     pg << "vc,5, c,5, s4;" << endl; // nulls[5-7]
01063     pg << "V 0x" << stringToHex("ABCD");
01064     pg << ", 0x" << stringToHex("GHIJ ");
01065     pg << ", 1, 2, -2,,,;" << endl;
01066     pg << "T;" << endl;
01067     // varchar common case
01068     pg << "CALL 'strSubStringA4(L0, C0, C3, C2);" << endl;
01069     pg << "CALL 'strSubStringA4(L1, C0, C2, C3);" << endl;
01070     // varchar null cases
01071     pg << "CALL 'strSubStringA4(L2, C5, C3, C2);" << endl;
01072     pg << "CALL 'strSubStringA4(L3, C0, C7, C2);" << endl;
01073     pg << "CALL 'strSubStringA4(L4, C0, C3, C7);" << endl;
01074     // varchar substring error
01075     pg << "CALL 'strSubStringA4(L5, C0, C3, C4);" << endl;
01076     // varchar right trunaction
01077     pg << "CALL 'strSubStringA4(L12, C0, C2, C3);" << endl;
01078     // char common case
01079     pg << "CALL 'strSubStringA4(L6, C1, C3, C2);" << endl;
01080     pg << "CALL 'strSubStringA4(L7, C1, C2, C3);" << endl;
01081     // char null cases
01082     pg << "CALL 'strSubStringA4(L8, C6, C3, C2);" << endl;
01083     pg << "CALL 'strSubStringA4(L9, C1, C7, C2);" << endl;
01084     pg << "CALL 'strSubStringA4(L10, C1, C3, C7);" << endl;
01085     // varchar substring error
01086     pg << "CALL 'strSubStringA4(L11, C1, C3, C4);" << endl;
01087     // char right trunaction
01088     pg << "CALL 'strSubStringA4(L13, C1, C2, C3);" << endl;
01089     // make output available
01090     refLocalOutput(pg, 14);
01091 
01092     Calculator calc(0);
01093 
01094     try {
01095         calc.assemble(pg.str().c_str());
01096     } catch (FennelExcn& ex) {
01097         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
01098         BOOST_MESSAGE(pg.str());
01099         BOOST_REQUIRE(0);
01100     }
01101 
01102     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
01103     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
01104 
01105     calc.bind(&inTuple, &outTuple);
01106     calc.exec();
01107     printOutput(outTuple, calc);
01108     deque<CalcMessage>::iterator iter = calc.mWarnings.begin();
01109 
01110     // varchar common case
01111     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[0], "B"));
01112     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[1], "AB"));
01113     // varchar null cases
01114     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[2]));
01115     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[3]));
01116     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[4]));
01117     // varchar substring error
01118     BOOST_CHECK_EQUAL(iter->pc, 5);
01119     BOOST_CHECK_EQUAL(0, strcmp(iter->str, substrErr));
01120     iter++;
01121     // varchar right truncation
01122     BOOST_CHECK_EQUAL(iter->pc, 6);
01123     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
01124     iter++;
01125     // char common case
01126     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[6], "H"));
01127     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[7], "GH"));
01128     // char null cases
01129     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[8]));
01130     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[9]));
01131     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[10]));
01132     // char substring error
01133     BOOST_CHECK_EQUAL(iter->pc, 12);
01134     BOOST_CHECK_EQUAL(0, strcmp(iter->str, substrErr));
01135     iter++;
01136     // char right truncation
01137     BOOST_CHECK_EQUAL(iter->pc, 13);
01138     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
01139     iter++;
01140     BOOST_CHECK(iter == calc.mWarnings.end());
01141 }
01142 
01143 
01144 // Test that string operatings attempting to write into
01145 // a null string result in a null, and not some other error/problem.
01146 void
01147 CalcExtStringTest::testCalcExtStringToANull()
01148 {
01149     ostringstream pg(""), outloc(""), outlocchar("");
01150     int i;
01151 
01152     for (i = 0; i <= 4; i++) {  // [0-4] char
01153         outlocchar << "c,5, ";
01154     }
01155 
01156     for (i = 5; i <= 13; i++) { // [5-14] varchar
01157         outloc << "vc,5, ";
01158     }
01159     outloc << "vc,5;" << endl;
01160 
01161     pg << "O " << outlocchar.str() << outloc.str();
01162     pg << "L " << outlocchar.str() << outloc.str();
01163     pg << "C vc,5, c,5, s4, vc,5, c,5;" << endl;    // data[0-2], null [3-4]
01164     pg << "V 0x" << stringToHex(" abc ");   // vc const
01165     pg << ", 0x" << stringToHex(" hij ");   // char const
01166     pg << ", 1,,;" << endl;
01167     pg << "T;" << endl;
01168 
01169     for (i = 0; i <= 14; i++) {
01170         pg << "TONULL L" << i << ";" << endl;
01171     }
01172 
01173     // char cases
01174     pg << "CALL 'strCatA2(L0, C1);" << endl;
01175     pg << "CALL 'strCatA3(L1, C1, C1);" << endl;
01176     pg << "CALL 'strCpyA(L2, C1);" << endl;
01177     pg << "CALL 'strToLowerA(L3, C1);" << endl;
01178     pg << "CALL 'strToUpperA(L4, C1);" << endl;
01179 
01180     // varchar cases
01181     pg << "CALL 'strCatA2(L5, C0);" << endl;
01182     pg << "CALL 'strCatA3(L6, C0, C0);" << endl;
01183     pg << "CALL 'strCpyA(L7, C0);" << endl;
01184     pg << "CALL 'strOverlayA4(L8, C0, C0, C2);" << endl;
01185     pg << "CALL 'strOverlayA5(L9, C0, C0, C2, C2);" << endl;
01186     pg << "CALL 'strSubStringA3(L10, C0, C2);" << endl;
01187     pg << "CALL 'strSubStringA4(L11, C0, C2, C2);" << endl;
01188     pg << "CALL 'strToLowerA(L12, C0);" << endl;
01189     pg << "CALL 'strToUpperA(L13, C0);" << endl;
01190     pg << "CALL 'strTrimA(L14, C0, C0, C2, C2);" << endl; // trim both
01191 
01192     // make output available
01193     refLocalOutput(pg, 15);
01194     Calculator calc(0);
01195 
01196     try {
01197         calc.assemble(pg.str().c_str());
01198     } catch (FennelExcn& ex) {
01199         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
01200         BOOST_MESSAGE(pg.str());
01201         BOOST_REQUIRE(0);
01202     }
01203 
01204     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
01205     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
01206 
01207     calc.bind(&inTuple, &outTuple);
01208     calc.exec();
01209     printOutput(outTuple, calc);
01210 
01211     for (i = 0; i <= 14 ;i++) {
01212         BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[i]));
01213     }
01214 
01215     deque<CalcMessage>::iterator iter = calc.mWarnings.begin();
01216     deque<CalcMessage>::iterator end = calc.mWarnings.end();
01217 
01218     BOOST_CHECK(iter == end);
01219 }
01220 
01221 
01222 void
01223 CalcExtStringTest::testCalcExtStringToLower()
01224 {
01225     ostringstream pg(""), outloc("");
01226     int i;
01227 
01228     for (i = 0; i <= 1; i++) {
01229         outloc << "vc,5, ";
01230     }
01231     for (i = 1; i <= 2; i++) {
01232         outloc << "c,5, ";
01233     }
01234     outloc << "vc,1;" << endl;      // right truncate
01235 
01236     pg << "O " << outloc.str();
01237     pg << "L " << outloc.str();
01238     pg << "C vc,5, c,5, ";      // data[0-1]
01239     pg << "vc,5, c,5;" << endl; // nulls[2-3]
01240     pg << "V 0x" << stringToHex("ABC");
01241     pg << ", 0x" << stringToHex("GHIJ ");
01242     pg << ",,;" << endl;
01243     pg << "T;" << endl;
01244     // varchar common case
01245     pg << "CALL 'strToLowerA(L0, C0);" << endl;
01246     // varchar null case
01247     pg << "CALL 'strToLowerA(L1, C2);" << endl;
01248     // varchar right truncation
01249     pg << "CALL 'strToLowerA(L4, C0);" << endl;
01250     // char common case
01251     pg << "CALL 'strToLowerA(L2, C1);" << endl;
01252     // char null case
01253     pg << "CALL 'strToLowerA(L3, C3);" << endl;
01254     // right truncation not possible in fixed width, as both
01255     // strings must be same length by definition.
01256     // make output available
01257     refLocalOutput(pg, 5);
01258 
01259     Calculator calc(0);
01260 
01261     try {
01262         calc.assemble(pg.str().c_str());
01263     } catch (FennelExcn& ex) {
01264         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
01265         BOOST_MESSAGE(pg.str());
01266         BOOST_REQUIRE(0);
01267     }
01268 
01269     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
01270     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
01271 
01272     calc.bind(&inTuple, &outTuple);
01273     calc.exec();
01274     printOutput(outTuple, calc);
01275     deque<CalcMessage>::iterator iter = calc.mWarnings.begin();
01276 
01277     // varchar common case
01278     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[0], "abc"));
01279     // varchar null case
01280     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[1]));
01281     // varchar right truncation
01282     BOOST_CHECK_EQUAL(iter->pc, 2);
01283     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
01284     iter++;
01285     BOOST_CHECK(iter == calc.mWarnings.end());
01286     // char common case
01287     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[2], "ghij "));
01288     // varchar null case
01289     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[3]));
01290 }
01291 
01292 void
01293 CalcExtStringTest::testCalcExtStringToUpper()
01294 {
01295     ostringstream pg(""), outloc("");
01296     int i;
01297 
01298     for (i = 0; i <= 1; i++) {
01299         outloc << "vc,5, ";
01300     }
01301     for (i = 1; i <= 2; i++) {
01302         outloc << "c,5, ";
01303     }
01304     outloc << "vc,1;" << endl;      // right truncate
01305 
01306     pg << "O " << outloc.str();
01307     pg << "L " << outloc.str();
01308     pg << "C vc,5, c,5, ";      // data[0-1]
01309     pg << "vc,5, c,5;" << endl; // nulls[2-3]
01310     pg << "V 0x" << stringToHex("abc");
01311     pg << ", 0x" << stringToHex("ghij ");
01312     pg << ",,;" << endl;
01313     pg << "T;" << endl;
01314     // varchar common case
01315     pg << "CALL 'strToUpperA(L0, C0);" << endl;
01316     // varchar null case
01317     pg << "CALL 'strToUpperA(L1, C2);" << endl;
01318     // varchar right truncation
01319     pg << "CALL 'strToUpperA(L4, C0);" << endl;
01320     // char common case
01321     pg << "CALL 'strToUpperA(L2, C1);" << endl;
01322     // char null case
01323     pg << "CALL 'strToUpperA(L3, C3);" << endl;
01324     // right truncation not possible in fixed width, as both
01325     // strings must be same length by definition.
01326     // make output available
01327     refLocalOutput(pg, 5);
01328 
01329     Calculator calc(0);
01330 
01331     try {
01332         calc.assemble(pg.str().c_str());
01333     } catch (FennelExcn& ex) {
01334         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
01335         BOOST_MESSAGE(pg.str());
01336         BOOST_REQUIRE(0);
01337     }
01338 
01339     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
01340     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
01341 
01342     calc.bind(&inTuple, &outTuple);
01343     calc.exec();
01344     printOutput(outTuple, calc);
01345     deque<CalcMessage>::iterator iter = calc.mWarnings.begin();
01346 
01347     // varchar common case
01348     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[0], "ABC"));
01349     // varchar null case
01350     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[1]));
01351     // varchar right truncation
01352     BOOST_CHECK_EQUAL(iter->pc, 2);
01353     BOOST_CHECK_EQUAL(0, strcmp(iter->str, truncErr));
01354     iter++;
01355     BOOST_CHECK(iter == calc.mWarnings.end());
01356     // char common case
01357     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[2], "GHIJ "));
01358     // varchar null case
01359     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[3]));
01360 }
01361 
01362 void
01363 CalcExtStringTest::testCalcExtStringTrim()
01364 {
01365     ostringstream pg(""), outloc("");
01366     int i;
01367 
01368     for (i = 0; i <= 28; i++) {
01369         outloc << "vc,10, ";
01370     }
01371     outloc << "vc,10;" << endl;
01372 
01373     pg << "O " << outloc.str();
01374     pg << "L " << outloc.str();
01375     pg << "C vc,5, c,5, s4, s4, ";         // data[0-3]
01376     pg << "vc,5, c,5, s4, ";               // nulls[4-6]
01377     pg << "c,1, vc,1, vc,1, vc,2, vc,1, "; // trimchar[7-11]
01378     pg << "vc,10;" << endl;                // data[12]
01379     pg << "V 0x" << stringToHex(" abc ");   // vc const
01380     pg << ", 0x" << stringToHex(" hij ");   // char const
01381     pg << ", 1, 0,,,";
01382     pg << ", 0x" << stringToHex(" ");  // space trimchar char
01383     pg << ", 0x" << stringToHex(" ");  // space trimchar varchar
01384     pg << ", 0x" << stringToHex("");   // invalid zero length trimchar
01385     pg << ", 0x" << stringToHex(" a"); // invalid two char length trimchar
01386     pg << ", 0x" << stringToHex("x");  // x as trimchar
01387     pg << ", 0x" << stringToHex("xx pqr xx"); // data[12]
01388     pg << ";" << endl;
01389     pg << "T;" << endl;
01390 
01391     // all varchar common cases
01392     pg << "CALL 'strTrimA(L0, C0, C8, C2, C2);" << endl; // trim both
01393     pg << "CALL 'strTrimA(L1, C0, C8, C2, C3);" << endl; // trim left
01394     pg << "CALL 'strTrimA(L2, C0, C8, C3, C2);" << endl; // trim right
01395     pg << "CALL 'strTrimA(L3, C0, C8, C3, C3);" << endl; // trim none
01396     // all varchar null cases
01397     pg << "CALL 'strTrimA(L4, C0, C4, C2, C2);" << endl;
01398     pg << "CALL 'strTrimA(L5, C4, C8, C2, C2);" << endl;
01399     pg << "CALL 'strTrimA(L6, C0, C8, C6, C2);" << endl;
01400     pg << "CALL 'strTrimA(L7, C0, C8, C2, C6);" << endl;
01401     // all char common cases
01402     pg << "CALL 'strTrimA(L8, C1, C7, C2, C2);" << endl; // trim both
01403     pg << "CALL 'strTrimA(L9, C1, C7, C2, C3);" << endl; // trim left
01404     pg << "CALL 'strTrimA(L10, C1, C7, C3, C2);" << endl; // trim right
01405     pg << "CALL 'strTrimA(L11, C1, C7, C3, C3);" << endl; // trim none
01406     // all char null cases
01407     pg << "CALL 'strTrimA(L12, C5, C7, C2, C2);" << endl;
01408     pg << "CALL 'strTrimA(L13, C1, C5, C2, C2);" << endl;
01409     pg << "CALL 'strTrimA(L14, C1, C7, C6, C2);" << endl;
01410     pg << "CALL 'strTrimA(L15, C1, C7, C2, C6);" << endl;
01411     // mixed varchar/char common cases
01412     pg << "CALL 'strTrimA(L16, C0, C7, C2, C2);" << endl; // trim both
01413     pg << "CALL 'strTrimA(L17, C1, C7, C2, C2);" << endl;
01414     pg << "CALL 'strTrimA(L18, C0, C8, C2, C2);" << endl;
01415     pg << "CALL 'strTrimA(L19, C1, C8, C2, C2);" << endl;
01416     // mixed varchar/char null cases
01417     pg << "CALL 'strTrimA(L20, C4, C7, C2, C2);" << endl; // vc,vcN,c
01418     pg << "CALL 'strTrimA(L21, C0, C5, C2, C2);" << endl; // vc,vc,cN
01419     pg << "CALL 'strTrimA(L22, C1, C4, C6, C2);" << endl; // vc,c,vcN
01420     pg << "CALL 'strTrimA(L23, C5, C8, C2, C6);" << endl; // vc,cN,vc
01421 
01422     // An error is thrown in the extended instruction (as opposed to
01423     // in the string library), so it needs to be tested
01424     // here. (Exceptions thrown in the string library are tested in
01425     // the string library unit test>)
01426 
01427     // invalid trim characters
01428     pg << "CALL 'strTrimA(L24, C0, C9, C2, C2);"
01429        << endl; // zero char length trimchar
01430     pg << "CALL 'strTrimA(L25, C0, C10, C2, C2);"
01431        << endl; // two char length trimchar
01432 
01433     // all varchar common cases with other trim char
01434     pg << "CALL 'strTrimA(L26, C12, C11, C2, C2);" << endl; // trim both
01435     pg << "CALL 'strTrimA(L27, C12, C11, C2, C3);" << endl; // trim left
01436     pg << "CALL 'strTrimA(L28, C12, C11, C3, C2);" << endl; // trim right
01437     pg << "CALL 'strTrimA(L29, C12, C11, C3, C3);" << endl; // trim none
01438 
01439     // make output available
01440     refLocalOutput(pg, 30);
01441 
01442     Calculator calc(0);
01443 
01444     try {
01445         calc.assemble(pg.str().c_str());
01446     } catch (FennelExcn& ex) {
01447         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
01448         BOOST_MESSAGE(pg.str());
01449         BOOST_REQUIRE(0);
01450     }
01451 
01452     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
01453     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
01454 
01455     calc.bind(&inTuple, &outTuple);
01456     calc.exec();
01457     printOutput(outTuple, calc);
01458     BOOST_MESSAGE("Calculator Warnings: |" << calc.warnings() << "|");
01459 
01460     // varchar common cases
01461     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[0], "abc"));
01462     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[1], "abc "));
01463     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[2], " abc"));
01464     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[3], " abc "));
01465     // varchar null cases
01466     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[4]));
01467     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[5]));
01468     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[6]));
01469     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[7]));
01470 
01471     // char common cases
01472     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[8], "hij"));
01473     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[9], "hij "));
01474     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[10], " hij"));
01475     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[11], " hij "));
01476     // char null cases
01477     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[12]));
01478     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[13]));
01479     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[14]));
01480     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[15]));
01481 
01482     // mixed varchar/char common cases
01483     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[16], "abc"));
01484     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[17], "hij"));
01485     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[18], "abc"));
01486     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[19], "hij"));
01487     // mixed varchar/char null cases
01488     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[20]));
01489     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[21]));
01490     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[22]));
01491     BOOST_CHECK_EQUAL(1, cmpTupNull(outTuple[23]));
01492 
01493     // check warning from invalid trim character
01494     deque<CalcMessage>::iterator iter = calc.mWarnings.begin();
01495     deque<CalcMessage>::iterator end = calc.mWarnings.end();
01496 
01497     BOOST_CHECK(iter != end);
01498     BOOST_CHECK_EQUAL(iter->pc, 24);
01499     BOOST_CHECK_EQUAL(0, strcmp(iter->str, "22027"));
01500     iter++;
01501     BOOST_CHECK_EQUAL(iter->pc, 25);
01502     BOOST_CHECK_EQUAL(0, strcmp(iter->str, "22027"));
01503     iter++;
01504     BOOST_CHECK(iter == end);
01505 
01506     // varchar common cases with other trim char
01507     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[26], " pqr "));
01508     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[27], " pqr xx"));
01509     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[28], "xx pqr "));
01510     BOOST_CHECK_EQUAL(0, cmpTupStr(outTuple[29], "xx pqr xx"));
01511 
01512 }
01513 
01514 
01515 FENNEL_UNIT_TEST_SUITE(CalcExtStringTest);
01516 
01517 // End CalcExtStringTest.cpp

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