CalcExtDateTimeTest.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calctest/CalcExtDateTimeTest.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/common/FennelExcn.h"
00031 
00032 #include <boost/test/test_tools.hpp>
00033 #include <boost/scoped_array.hpp>
00034 #include <string>
00035 #include <limits>
00036 
00037 using namespace fennel;
00038 using namespace std;
00039 
00040 
00041 class CalcExtDateTimeTest : virtual public TestBase, public TraceSource
00042 {
00043     void testCalcExtConvertDateToString();
00044     void testCalcExtLocalTime();
00045     void testCalcExtLocalTimestamp();
00046 
00047     void checkWarnings(Calculator& calc, string expected);
00048     void printOutput(
00049         TupleData const & tup,
00050         Calculator const & calc);
00051 
00052 public:
00053     explicit CalcExtDateTimeTest()
00054         : TraceSource(shared_from_this(),"CalcExtDateTimeTest")
00055     {
00056         srand(time(NULL));
00057         CalcInit::instance();
00058         FENNEL_UNIT_TEST_CASE(
00059             CalcExtDateTimeTest, testCalcExtConvertDateToString);
00060         FENNEL_UNIT_TEST_CASE(CalcExtDateTimeTest, testCalcExtLocalTime);
00061         FENNEL_UNIT_TEST_CASE(CalcExtDateTimeTest, testCalcExtLocalTimestamp);
00062     }
00063 
00064     virtual ~CalcExtDateTimeTest()
00065     {
00066     }
00067 
00068     // helpers
00069 
00070     bool equals(TupleDatum buf, const char *expected) {
00071         char *s = reinterpret_cast<char *>(const_cast<PBuffer>(buf.pData));
00072         return !strncmp(expected, s, buf.cbData);
00073     }
00074 };
00075 
00076 // for nitty-gritty debugging. sadly, doesn't use BOOST_MESSAGE.
00077 void
00078 CalcExtDateTimeTest::printOutput(
00079     TupleData const & tup,
00080     Calculator const & calc)
00081 {
00082 #if 1
00083     TuplePrinter tuplePrinter;
00084     tuplePrinter.print(cout, calc.getOutputRegisterDescriptor(), tup);
00085     cout << endl;
00086 #endif
00087 }
00088 
00089 
00090 void
00091 CalcExtDateTimeTest::testCalcExtConvertDateToString()
00092 {
00093     ostringstream pg("");
00094 
00095     pg << "O vc,10;" << endl;
00096     pg << "L vc,10;" << endl;
00097     pg << "C s8;" << endl;
00098     pg << "V 115200000;" << endl; // (in PDT).
00099     pg << "T;" << endl;
00100     pg << "CALL 'CastDateToStrA(L0, C0);" << endl;
00101     pg << "REF O0, L0;" << endl;
00102 
00103     Calculator calc(0);
00104 
00105     try {
00106         calc.assemble(pg.str().c_str());
00107     } catch (FennelExcn& ex) {
00108         BOOST_FAIL("Assemble exception " << ex.getMessage()<< pg.str());
00109     }
00110 
00111     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00112     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00113 
00114     calc.bind(&inTuple, &outTuple);
00115     calc.exec();
00116     //    printOutput(outTuple, calc);
00117 
00118     BOOST_CHECK(equals(outTuple[0], "1970-01-02"));
00119 }
00120 
00121 void
00122 CalcExtDateTimeTest::testCalcExtLocalTime()
00123 {
00124     ostringstream pg("");
00125 
00126     pg << "O s8;" << endl;
00127     pg << "I s4;" << endl;
00128     pg << "L s8;" << endl;
00129     pg << "C bo, bo, c,23;" << endl;
00130     pg << "V 1, 0, 0x5053542D385044542C4D332E322E302C4D31312E312E30"
00131         " /* PST-8PDT,M3.2.0,M11.1.0 */;" << endl;
00132     pg << "T;" << endl;
00133     pg << "CALL 'LocalTime2(L0, C2) /* 0: LOCALTIME($t1) */;" << endl;
00134     pg << "REF O0, L0 /* 1: */;" << endl;
00135     //    pg << "RETURN /* 2: */;|" << endl;
00136 
00137     Calculator calc(0);
00138 
00139     try {
00140         calc.assemble(pg.str().c_str());
00141     } catch (FennelExcn& ex) {
00142         BOOST_FAIL("Assemble exception " << ex.getMessage()<< pg.str());
00143     }
00144 
00145     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00146     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00147 
00148     calc.bind(&inTuple, &outTuple);
00149     calc.exec();
00150     printOutput(outTuple, calc);
00151 
00152 }
00153 
00154 void
00155 CalcExtDateTimeTest::testCalcExtLocalTimestamp()
00156 {
00157     ostringstream pg("");
00158 
00159     pg << "O s8;" << endl;
00160     pg << "I s4;" << endl;
00161     pg << "L s8;" << endl;
00162     pg << "C bo, bo, c,23;" << endl;
00163     pg << "V 1, 0, 0x5053542D385044542C4D332E322E302C4D31312E312E30"
00164         " /* PST-8PDT,M3.2.0,M11.1.0 */;" << endl;
00165     pg << "T;" << endl;
00166     pg << "CALL 'LocalTimestamp2(L0, C2) /* 0: LOCALTIMESTAMP($t1) */;" << endl;
00167     pg << "REF O0, L0 /* 1: */;" << endl;
00168     //    pg << "RETURN /* 2: */;|" << endl;
00169 
00170     Calculator calc(0);
00171 
00172     try {
00173         calc.assemble(pg.str().c_str());
00174     } catch (FennelExcn& ex) {
00175         BOOST_FAIL("Assemble exception " << ex.getMessage()<< pg.str());
00176     }
00177 
00178     TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00179     TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00180 
00181     calc.bind(&inTuple, &outTuple);
00182     calc.exec();
00183     printOutput(outTuple, calc);
00184 
00185 }
00186 
00187 FENNEL_UNIT_TEST_SUITE(CalcExtDateTimeTest);
00188 
00189 // End CalcExtDateTimeTest.cpp

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