00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "fennel/common/CommonPreamble.h"
00025 #include "fennel/test/TestBase.h"
00026 #include "fennel/common/TraceSource.h"
00027
00028 #include "fennel/tuple/TupleDataWithBuffer.h"
00029 #include "fennel/tuple/TuplePrinter.h"
00030 #include "fennel/calculator/CalcCommon.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 CalcExtDynamicVariableTest : virtual public TestBase, public TraceSource
00044 {
00045 void testCalcExtDynamicVariable();
00046
00047 void printOutput(TupleData const & tup, Calculator const & calc);
00048
00049 public:
00050 explicit CalcExtDynamicVariableTest()
00051 : TraceSource(shared_from_this(),"CalcExtDynamicVariableTest")
00052 {
00053 srand(time(NULL));
00054 CalcInit::instance();
00055 FENNEL_UNIT_TEST_CASE(
00056 CalcExtDynamicVariableTest, testCalcExtDynamicVariable);
00057 }
00058
00059 virtual ~CalcExtDynamicVariableTest()
00060 {
00061 }
00062 };
00063
00064
00065 void
00066 CalcExtDynamicVariableTest::printOutput(
00067 TupleData const & tup,
00068 Calculator const & calc)
00069 {
00070 if (true) {
00071 TuplePrinter tuplePrinter;
00072 tuplePrinter.print(cout, calc.getOutputRegisterDescriptor(), tup);
00073 cout << endl;
00074 }
00075 }
00076
00077 void
00078 CalcExtDynamicVariableTest::testCalcExtDynamicVariable()
00079 {
00080 ostringstream pg("");
00081 const char* typesArray[] = {
00082 "s4", "u4", "s8", "u8", "s1", "u1", "s2",
00083 "u2", "bo", "r", "d", "c,4", "vc,4", "b,4"
00084 };
00085 const uint N = sizeof(typesArray) / sizeof(typesArray[0]);
00086 string types;
00087 for (int i = 0; i < N; i++) {
00088 if (i > 0) {
00089 types += ", ";
00090 }
00091 types += typesArray[i];
00092 }
00093 pg << "O " << types << ";" << endl;
00094 pg << "L " << types << ";" << endl;
00095 pg << "C ";
00096 for (int i = 0; i < N; i++) {
00097 if (i > 0) {
00098 pg << ", ";
00099 }
00100 pg << "s4";
00101 }
00102 pg << ";" << endl;
00103 pg << "V ";
00104 for (int i = 0; i < N; i++) {
00105 if (i > 0) {
00106 pg << ", ";
00107 }
00108 pg << i;
00109 }
00110 pg << ";" << endl;
00111 pg << "T;" << endl;
00112 for (int i = 0; i < N; i++) {
00113 pg << "CALL 'dynamicVariable(L" << i << ", C" << i << ");" << endl;
00114 }
00115 for (int i = 0; i < N; i++) {
00116 pg << "REF O" << i << ", L" << i << ";" << endl;
00117 }
00118
00119 DynamicParamManager dpm;
00120 Calculator calc(&dpm);
00121
00122 try {
00123 calc.assemble(pg.str().c_str());
00124 } catch (FennelExcn& ex) {
00125 BOOST_FAIL("Assemble exception " << ex.getMessage()<< pg.str());
00126 }
00127
00128
00129 TupleDescriptor outTupleDesc = calc.getOutputRegisterDescriptor();
00130 TupleData dynamicData(outTupleDesc);
00131
00132 int32_t data0 = -321;
00133 dynamicData[0].pData = (PConstBuffer) &data0;
00134 dpm.createParam(DynamicParamId(0), outTupleDesc[0]);
00135 dpm.writeParam(DynamicParamId(0), dynamicData[0]);
00136
00137 uint32_t data1 = 622;
00138 dynamicData[1].pData = (PConstBuffer) &data1;
00139 dpm.createParam(DynamicParamId(1), outTupleDesc[1]);
00140 dpm.writeParam(DynamicParamId(1), dynamicData[1]);
00141
00142 int64_t data2 = 0xFFFFFFFFffffffffLL;
00143 dynamicData[2].pData = (PConstBuffer) &data2;
00144 dpm.createParam(DynamicParamId(2), outTupleDesc[2]);
00145 dpm.writeParam(DynamicParamId(2), dynamicData[2]);
00146
00147 uint64_t data3 = 0x8000000000000000ULL;
00148 dynamicData[3].pData = (PConstBuffer) &data3;
00149 dpm.createParam(DynamicParamId(3), outTupleDesc[3]);
00150 dpm.writeParam(DynamicParamId(3), dynamicData[3]);
00151
00152 int8_t data4 = 0xFF;
00153 dynamicData[4].pData = (PConstBuffer) &data4;
00154 dpm.createParam(DynamicParamId(4), outTupleDesc[4]);
00155 dpm.writeParam(DynamicParamId(4), dynamicData[4]);
00156
00157 uint8_t data5 = 128;
00158 dynamicData[5].pData = (PConstBuffer) &data5;
00159 dpm.createParam(DynamicParamId(5), outTupleDesc[5]);
00160 dpm.writeParam(DynamicParamId(5), dynamicData[5]);
00161
00162 int16_t data6 = 0xFFFF;
00163 dynamicData[6].pData = (PConstBuffer) &data6;
00164 dpm.createParam(DynamicParamId(6), outTupleDesc[6]);
00165 dpm.writeParam(DynamicParamId(6), dynamicData[6]);
00166
00167 uint16_t data7 = 0x8000;
00168 dynamicData[7].pData = (PConstBuffer) &data7;
00169 dpm.createParam(DynamicParamId(7), outTupleDesc[7]);
00170 dpm.writeParam(DynamicParamId(7), dynamicData[7]);
00171
00172 bool data8 = true;
00173 dynamicData[8].pData = (PConstBuffer) &data8;
00174 dpm.createParam(DynamicParamId(8), outTupleDesc[8]);
00175 dpm.writeParam(DynamicParamId(8), dynamicData[8]);
00176
00177 float data9 = 3.14f;
00178 dynamicData[9].pData = (PConstBuffer) &data9;
00179 dpm.createParam(DynamicParamId(9), outTupleDesc[9]);
00180 dpm.writeParam(DynamicParamId(9), dynamicData[9]);
00181
00182 float data10 = 3.14e300;
00183 dynamicData[10].pData = (PConstBuffer) &data10;
00184 dpm.createParam(DynamicParamId(10), outTupleDesc[10]);
00185 dpm.writeParam(DynamicParamId(10), dynamicData[10]);
00186
00187 const char* data11 = "abc";
00188 dynamicData[11].pData = (PConstBuffer) data11;
00189 dynamicData[11].cbData = strlen(data11);
00190 dpm.createParam(DynamicParamId(11), outTupleDesc[11]);
00191 dpm.writeParam(DynamicParamId(11), dynamicData[11]);
00192
00193 const char* data12 = "def";
00194 dynamicData[12].pData = (PConstBuffer) data12;
00195 dynamicData[12].cbData = strlen(data12);
00196 dpm.createParam(DynamicParamId(12), outTupleDesc[12]);
00197 dpm.writeParam(DynamicParamId(12), dynamicData[12]);
00198
00199 const char* data13 = "ghi";
00200 dynamicData[13].pData = (PConstBuffer) data13;
00201 dynamicData[13].cbData = strlen(data13);
00202 dpm.createParam(DynamicParamId(13), outTupleDesc[13]);
00203 dpm.writeParam(DynamicParamId(13), dynamicData[13]);
00204
00205 TupleDataWithBuffer outTuple(calc.getOutputRegisterDescriptor());
00206 TupleDataWithBuffer inTuple(calc.getInputRegisterDescriptor());
00207
00208 calc.bind(&inTuple, &outTuple);
00209 calc.exec();
00210
00211
00212 BOOST_CHECK(
00213 *(reinterpret_cast<int32_t*>(const_cast<PBuffer>(outTuple[0].pData)))
00214 == -321);
00215 BOOST_CHECK(
00216 *(reinterpret_cast<uint32_t*>(const_cast<PBuffer>(outTuple[1].pData)))
00217 == 622);
00218 BOOST_CHECK(
00219 *(reinterpret_cast<int64_t*>(const_cast<PBuffer>(outTuple[2].pData)))
00220 == -1);
00221 BOOST_CHECK(
00222 *(reinterpret_cast<uint64_t*>(const_cast<PBuffer>(outTuple[3].pData)))
00223 == 0x8000000000000000ULL);
00224 BOOST_CHECK(
00225 *(reinterpret_cast<int8_t*>(const_cast<PBuffer>(outTuple[4].pData)))
00226 == -1);
00227 BOOST_CHECK(
00228 *(reinterpret_cast<uint8_t*>(const_cast<PBuffer>(outTuple[5].pData)))
00229 == 128);
00230 BOOST_CHECK(
00231 *(reinterpret_cast<int16_t*>(const_cast<PBuffer>(outTuple[6].pData)))
00232 == -1);
00233 BOOST_CHECK(
00234 *(reinterpret_cast<uint16_t*>(const_cast<PBuffer>(outTuple[7].pData)))
00235 == 32768);
00236 BOOST_CHECK(
00237 *(reinterpret_cast<bool*>(const_cast<PBuffer>(outTuple[8].pData)))
00238 == true);
00239 BOOST_CHECK(
00240 *(reinterpret_cast<float*>(const_cast<PBuffer>(outTuple[9].pData)))
00241 - 3.14
00242 < 0.0001);
00243 BOOST_CHECK(
00244 *(reinterpret_cast<double*>(const_cast<PBuffer>(outTuple[10].pData)))
00245 - 3.14e300
00246 < 0.0001);
00247
00248 BOOST_CHECK(!memcmp("abc",outTuple[11].pData,3));
00249 BOOST_CHECK(!memcmp("def",outTuple[12].pData,3));
00250 BOOST_CHECK(!memcmp("ghi",outTuple[13].pData,3));
00251 }
00252
00253 FENNEL_UNIT_TEST_SUITE(CalcExtDynamicVariableTest);
00254
00255