CalcInstFactoryTest.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calctest/CalcInstFactoryTest.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 
00044 class CalcInstFactoryTest : virtual public TestBase, public TraceSource
00045 {
00046     void testBool();
00047     void testBoolNative();
00048     void testBoolPointer();
00049     void testIntegralNative();
00050     void testIntegralPointer();
00051     void testJump();
00052     void testNativeNative();
00053     void testPointerIntegral();
00054     void testPointerPointer();
00055     void testReturn();
00056 
00057     static char const * const all;
00058     static char const * const nativeNotBool;
00059     static char const * const nativeNotBoolValues;
00060     static char const * const pointerArray;
00061     static char const * const nativeIntegral;
00062     static char const * const nativeIntegralValues;
00063 
00064 public:
00065     explicit CalcInstFactoryTest()
00066         : TraceSource(shared_from_this(),"CalcInstFactoryTest")
00067     {
00068         srand(time(NULL));
00069         CalcInit::instance();
00070         FENNEL_UNIT_TEST_CASE(CalcInstFactoryTest, testBool);
00071         FENNEL_UNIT_TEST_CASE(CalcInstFactoryTest, testBoolNative);
00072         FENNEL_UNIT_TEST_CASE(CalcInstFactoryTest, testBoolPointer);
00073         FENNEL_UNIT_TEST_CASE(CalcInstFactoryTest, testIntegralNative);
00074         FENNEL_UNIT_TEST_CASE(CalcInstFactoryTest, testIntegralPointer);
00075         FENNEL_UNIT_TEST_CASE(CalcInstFactoryTest, testJump);
00076         FENNEL_UNIT_TEST_CASE(CalcInstFactoryTest, testNativeNative);
00077         FENNEL_UNIT_TEST_CASE(CalcInstFactoryTest, testPointerIntegral);
00078         FENNEL_UNIT_TEST_CASE(CalcInstFactoryTest, testPointerPointer);
00079         FENNEL_UNIT_TEST_CASE(CalcInstFactoryTest, testReturn);
00080     }
00081 
00082     virtual ~CalcInstFactoryTest()
00083     {
00084     }
00085 };
00086 
00087 char const * const
00088 CalcInstFactoryTest::all =
00089 "s1, u1, s2, u2, s4, u4, s8, u8, bo, r, d, c, vc, b, vb";
00090 // 0   1   2   3   4   5   6   7   8   9  10 11 12  13 14
00091 
00092 char const * const
00093 CalcInstFactoryTest::nativeNotBool =
00094 "s1, u1, s2, u2, s4, u4, s8, u8, r, d";
00095 // 0   1   2   3   4   5   6   7   8  9
00096 char const * const
00097 CalcInstFactoryTest::nativeNotBoolValues =
00098 "1, 2, 3, 4, 5, 6, 7, 8, 9.0, 10.0";
00099 // 0  1  2  3  4  5  6  7  8    9
00100 
00101 char const * const
00102 CalcInstFactoryTest::pointerArray =
00103 "vc,1, c,1, u4, bo, bo";
00104 // 0   1    2   3   4
00105 
00106 char const * const
00107 CalcInstFactoryTest::nativeIntegral =
00108 "s1, u1, s2, u2, s4, u4, s8, u8";
00109 // 0   1   2   3   4   5   6   7
00110 char const * const
00111 CalcInstFactoryTest::nativeIntegralValues =
00112 "1, 2, 3, 4, 5, 6, 7, 8";
00113 // 0  1  2  3  4  5  6  7
00114 
00115 
00116 
00117 void
00118 CalcInstFactoryTest::testBool()
00119 {
00120     ostringstream pg("");
00121 
00122     const char* Bool[][2] = {
00123         { "OR", "3" },
00124         { "AND", "3" },
00125         { "NOT", "2" },
00126         { "MOVE", "2" },
00127         { "REF", "2" },
00128         { "IS", "3" },
00129         { "ISNOT", "3" },
00130         { "EQ", "3" },
00131         { "NE", "3" },
00132         { "GT", "3" },
00133         { "GE", "3" },
00134         { "LT", "3" },
00135         { "LE", "3" },
00136         { "ISNULL", "2" },
00137         { "ISNOTNULL", "2" },
00138         { "TONULL", "1" },
00139         { "", "" },
00140     };
00141 
00142     pg << "O bo;" << endl;
00143     pg << "C bo,bo;" << endl;
00144     pg << "V 1,0;" << endl;
00145     pg << "T;" << endl;
00146 
00147     int inst;
00148     for (inst = 0; *(Bool[inst][0]); inst++) {
00149         BOOST_MESSAGE(Bool[inst][0]);
00150         pg << Bool[inst][0] << " O0";
00151         if (atoi(Bool[inst][1]) >= 2) {
00152             pg << ", C0";
00153         }
00154         if (atoi(Bool[inst][1]) >= 3) {
00155             pg << ", C1";
00156         }
00157         pg << ";" << endl;
00158     }
00159 
00160     BOOST_MESSAGE(pg.str());
00161 
00162     Calculator calc(0);
00163 
00164     try {
00165         calc.assemble(pg.str().c_str());
00166     } catch (FennelExcn& ex) {
00167         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00168         BOOST_REQUIRE(0);
00169     }
00170 
00171     // Don't bother executing the instructions.
00172 }
00173 
00174 void
00175 CalcInstFactoryTest::testBoolNative()
00176 {
00177     ostringstream pg("");
00178 
00179     const char* boolnative[][2] = {
00180        { "EQ", "3" },
00181        { "NE" , "3" },
00182        { "GT", "3" },
00183        { "GE", "3" },
00184        { "LT", "3" },
00185        { "LE", "3" },
00186        { "ISNULL", "2" },
00187        { "ISNOTNULL", "2" },
00188        { "", "" },
00189     };
00190 
00191     pg << "O " << nativeNotBool << ", bo;" << endl;
00192     pg << "C " << nativeNotBool << ";" << endl;
00193     pg << "V " << nativeNotBoolValues << ";" << endl;
00194     pg << "T;" << endl;
00195 
00196     int inst, type;
00197     for (inst = 0; *(boolnative[inst][0]); inst++) {
00198         BOOST_MESSAGE(boolnative[inst][0]);
00199         for (type = 0; type <= 9; type++) {
00200             pg << boolnative[inst][0] << " O10"; // always bool
00201             if (atoi(boolnative[inst][1]) >= 2) {
00202                 pg << ", C" << type;
00203             }
00204             if (atoi(boolnative[inst][1]) >= 3) {
00205                 pg << ", C" << type;
00206             }
00207             pg << ";" << endl;
00208         }
00209     }
00210 
00211     BOOST_MESSAGE(pg.str());
00212 
00213     Calculator calc(0);
00214 
00215     try {
00216         calc.assemble(pg.str().c_str());
00217     } catch (FennelExcn& ex) {
00218         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00219         BOOST_REQUIRE(0);
00220     }
00221 
00222     // Don't bother executing the instructions.
00223 }
00224 
00225 void
00226 CalcInstFactoryTest::testIntegralNative()
00227 {
00228     ostringstream pg("");
00229 
00230     const char* integralnative[][2] = {
00231         { "MOD", "3" },
00232         { "AND", "3" },
00233         { "OR", "3" },
00234         { "SHFL", "3" },
00235         { "SHFR", "3" },
00236         { "", "" },
00237     };
00238 
00239     pg << "O " << nativeIntegral << ";" << endl;
00240     pg << "C " << nativeIntegral << ";" << endl;
00241     pg << "V " << nativeIntegralValues << ";" << endl;
00242     pg << "T;" << endl;
00243 
00244     int inst, type;
00245     for (inst = 0; *(integralnative[inst][0]); inst++) {
00246         BOOST_MESSAGE(integralnative[inst][0]);
00247         for (type = 0; type <= 7; type++) {
00248             pg << integralnative[inst][0] << " O" << type;
00249             if (atoi(integralnative[inst][1]) >= 2) {
00250                 pg << ", C" << type;
00251             }
00252             if (atoi(integralnative[inst][1]) >= 3) {
00253                 pg << ", C" << type;
00254             }
00255             pg << ";" << endl;
00256         }
00257     }
00258 
00259     BOOST_MESSAGE(pg.str());
00260 
00261     Calculator calc(0);
00262 
00263     try {
00264         calc.assemble(pg.str().c_str());
00265     } catch (FennelExcn& ex) {
00266         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00267         BOOST_REQUIRE(0);
00268     }
00269 
00270     // Don't bother executing the instructions.
00271 }
00272 
00273 void
00274 CalcInstFactoryTest::testIntegralPointer()
00275 {
00276     ostringstream pg("");
00277 
00278     const char* integralpointer[][2] = {
00279        { "GETS", "2", },
00280        { "GETMS" , "2", },
00281        { "", "" },
00282     };
00283 
00284     pg << "O " << pointerArray << ";" << endl;
00285     pg << "C " << pointerArray << ";" << endl;
00286     pg << "V 0x" << stringToHex("a") << ", 0x" << stringToHex("b") <<
00287         ", 1, 0, 1;" << endl;
00288     pg << "T;" << endl;
00289 
00290     int inst, type;
00291     for (inst = 0; *(integralpointer[inst][0]); inst++) {
00292         BOOST_MESSAGE(integralpointer[inst][0]);
00293         for (type = 0; type <= 1; type++) {
00294             pg << integralpointer[inst][0] << " O2";
00295             if (atoi(integralpointer[inst][1]) >= 2) {
00296                 pg << ", C" << type;
00297             }
00298             if (atoi(integralpointer[inst][1]) >= 3) {
00299                 pg << ", C" << type;
00300             }
00301             pg << ";" << endl;
00302         }
00303     }
00304 
00305     BOOST_MESSAGE(pg.str());
00306 
00307     Calculator calc(0);
00308 
00309     try {
00310         calc.assemble(pg.str().c_str());
00311     } catch (FennelExcn& ex) {
00312         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00313         BOOST_REQUIRE(0);
00314     }
00315 
00316     // Don't bother executing the instructions.
00317 }
00318 
00319 void
00320 CalcInstFactoryTest::testPointerIntegral()
00321 {
00322     ostringstream pg("");
00323 
00324     const char* pointerintegral[][2] = {
00325        { "PUTS", "2", },
00326        { "", "" },
00327     };
00328 
00329     pg << "O " << pointerArray << ";" << endl;
00330     pg << "C " << pointerArray << ";" << endl;
00331     pg << "V 0x" << stringToHex("a") << ", 0x" << stringToHex("b") <<
00332         ", 1, 0, 1;" << endl;
00333     pg << "T;" << endl;
00334 
00335     int inst, type;
00336     for (inst = 0; *(pointerintegral[inst][0]); inst++) {
00337         BOOST_MESSAGE(pointerintegral[inst][0]);
00338         for (type = 0; type <= 1; type++) {
00339             pg << pointerintegral[inst][0] << " O" << type;
00340             pg << ", C2;" << endl;
00341         }
00342     }
00343 
00344     BOOST_MESSAGE(pg.str());
00345 
00346     Calculator calc(0);
00347 
00348     try {
00349         calc.assemble(pg.str().c_str());
00350     } catch (FennelExcn& ex) {
00351         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00352         BOOST_REQUIRE(0);
00353     }
00354 
00355     // Don't bother executing the instructions.
00356 }
00357 
00358 void
00359 CalcInstFactoryTest::testBoolPointer()
00360 {
00361     ostringstream pg("");
00362 
00363     const char* boolpointer[][2] = {
00364        { "EQ", "3", },
00365        { "NE" , "3", },
00366        { "GT", "3", },
00367        { "GE", "3", },
00368        { "LT", "3", },
00369        { "LE", "3", },
00370        { "ISNULL", "2", },
00371        { "ISNOTNULL", "2", },
00372        { "", "" },
00373     };
00374 
00375     pg << "O " << pointerArray << ";" << endl;
00376     pg << "C " << pointerArray << ";" << endl;
00377     pg << "V 0x" << stringToHex("a") << ", 0x" << stringToHex("b") <<
00378         ", 1, 0, 1;" << endl;
00379     pg << "T;" << endl;
00380 
00381     int inst, type;
00382     for (inst = 0; *(boolpointer[inst][0]); inst++) {
00383         BOOST_MESSAGE(boolpointer[inst][0]);
00384         for (type = 0; type <= 1; type++) {
00385             pg << boolpointer[inst][0] << " O3";
00386             if (atoi(boolpointer[inst][1]) >= 2) {
00387                 pg << ", C" << type;
00388             }
00389             if (atoi(boolpointer[inst][1]) >= 3) {
00390                 pg << ", C" << type;
00391             }
00392             pg << ";" << endl;
00393         }
00394     }
00395 
00396     BOOST_MESSAGE(pg.str());
00397 
00398     Calculator calc(0);
00399 
00400     try {
00401         calc.assemble(pg.str().c_str());
00402     } catch (FennelExcn& ex) {
00403         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00404         BOOST_REQUIRE(0);
00405     }
00406 
00407     // Don't bother executing the instructions.
00408 }
00409 
00410 void
00411 CalcInstFactoryTest::testJump()
00412 {
00413     ostringstream pg("");
00414 
00415     const char* Jump[][2] = {
00416         { "JMP", "0" },
00417         { "JMPT", "1" },
00418         { "JMPF", "1" },
00419         { "JMPN", "1" },
00420         { "JMPNN", "1" },
00421         { "", "" },
00422     };
00423 
00424     pg << "O bo;" << endl;
00425     pg << "C bo,bo;" << endl;
00426     pg << "V 1,0;" << endl;
00427     pg << "T;" << endl;
00428 
00429     int inst;
00430     for (inst = 0; *(Jump[inst][0]); inst++) {
00431         pg << Jump[inst][0] << " @1";
00432         if (atoi(Jump[inst][1]) >= 1) {
00433             pg << ", C0";
00434         }
00435         pg << ";" << endl;
00436     }
00437 
00438     BOOST_MESSAGE("|" << pg.str() << "|");
00439 
00440     Calculator calc(0);
00441 
00442     try {
00443         calc.assemble(pg.str().c_str());
00444     } catch (FennelExcn& ex) {
00445         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00446         BOOST_REQUIRE(0);
00447     }
00448 
00449     // Don't bother executing the instructions.
00450 }
00451 
00452 void
00453 CalcInstFactoryTest::testNativeNative()
00454 {
00455     ostringstream pg("");
00456 
00457     const char* nativenative[][2] = {
00458        { "ADD", "3" },
00459        { "SUB" , "3" },
00460        { "MUL", "3" },
00461        { "DIV", "3" },
00462        { "NEG", "2" },
00463        { "MOVE", "2" },
00464        { "REF", "2" },
00465        { "TONULL", "1" },
00466        { "", "" },
00467     };
00468 
00469     pg << "O " << nativeNotBool << ";" << endl;
00470     pg << "C " << nativeNotBool << ";" << endl;
00471     pg << "V " << nativeNotBoolValues << ";" << endl;
00472     pg << "T;" << endl;
00473 
00474     int inst, type;
00475     for (inst = 0; *(nativenative[inst][0]); inst++) {
00476         BOOST_MESSAGE(nativenative[inst][0]);
00477         for (type = 0; type <= 9; type++) {
00478             pg << nativenative[inst][0] << " O" << type;
00479             if (atoi(nativenative[inst][1]) >= 2) {
00480                 pg << ", C" << type;
00481             }
00482             if (atoi(nativenative[inst][1]) >= 3) {
00483                 pg << ", C" << type;
00484             }
00485             pg << ";" << endl;
00486         }
00487     }
00488 
00489     BOOST_MESSAGE(pg.str());
00490 
00491     Calculator calc(0);
00492 
00493     try {
00494         calc.assemble(pg.str().c_str());
00495     } catch (FennelExcn& ex) {
00496         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00497         BOOST_REQUIRE(0);
00498     }
00499 
00500     // Don't bother executing the instructions.
00501 }
00502 
00503 void
00504 CalcInstFactoryTest::testPointerPointer()
00505 {
00506     ostringstream pg("");
00507 
00508     const char* pointerpointer[][3] = {
00509        { "ADD", "2", "1" },
00510        { "SUB" , "2", "1" },
00511        { "MOVE", "2", "0" },
00512        { "REF", "2", "0" },
00513        { "TONULL", "1", "0" },
00514        { "", "" },
00515     };
00516 
00517     pg << "O " << pointerArray << ";" << endl;
00518     pg << "C " << pointerArray << ";" << endl;
00519     pg << "V 0x" << stringToHex("a") << ", 0x" << stringToHex("b") <<
00520         ", 1, 0, 1;" << endl;
00521     pg << "T;" << endl;
00522 
00523     int inst, type;
00524     for (inst = 0; *(pointerpointer[inst][0]); inst++) {
00525         BOOST_MESSAGE(pointerpointer[inst][0]);
00526         for (type = 0; type <= 1; type++) {
00527             pg << pointerpointer[inst][0] << " O" << type;
00528             if (atoi(pointerpointer[inst][1]) >= 2) {
00529                 pg << ", C" << type;
00530             }
00531             if (atoi(pointerpointer[inst][2]) >= 1) {
00532                 pg << ", C2";
00533             }
00534             pg << ";" << endl;
00535         }
00536     }
00537 
00538     BOOST_MESSAGE(pg.str());
00539 
00540     Calculator calc(0);
00541 
00542     try {
00543         calc.assemble(pg.str().c_str());
00544     } catch (FennelExcn& ex) {
00545         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00546         BOOST_REQUIRE(0);
00547     }
00548 
00549     // Don't bother executing the instructions.
00550 }
00551 
00552 void
00553 CalcInstFactoryTest::testReturn()
00554 {
00555     ostringstream pg("");
00556 
00557     pg << "O bo;" << endl;
00558     pg << "C bo,bo;" << endl;
00559     pg << "V 1,0;" << endl;
00560     pg << "T;" << endl;
00561     pg << "RETURN;" << endl;
00562 
00563     BOOST_MESSAGE(pg.str());
00564 
00565     Calculator calc(0);
00566 
00567     try {
00568         calc.assemble(pg.str().c_str());
00569     } catch (FennelExcn& ex) {
00570         BOOST_MESSAGE("Assemble exception " << ex.getMessage());
00571         BOOST_REQUIRE(0);
00572     }
00573 
00574     // Don't bother executing the instructions.
00575 }
00576 
00577 FENNEL_UNIT_TEST_SUITE(CalcInstFactoryTest);
00578 
00579 // End CalcInstFactoryTest.cpp

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