LbmMinusExecStreamTest.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/lucidera/test/LbmMinusExecStreamTest.cpp#8 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2006-2009 LucidEra, Inc.
00005 // Copyright (C) 2006-2009 The Eigenbase Project
00006 //
00007 // This program is free software; you can redistribute it and/or modify it
00008 // under the terms of the GNU General Public License as published by the Free
00009 // Software Foundation; either version 2 of the License, or (at your option)
00010 // any later version approved by The Eigenbase Project.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 */
00021 
00022 #include "fennel/common/CommonPreamble.h"
00023 #include "fennel/lucidera/test/LbmExecStreamTestBase.h"
00024 #include "fennel/lucidera/bitmap/LbmMinusExecStream.h"
00025 #include <stdarg.h>
00026 
00027 #include <boost/test/test_tools.hpp>
00028 
00029 using namespace fennel;
00030 
00034 class LbmMinusExecStreamTest : public LbmExecStreamTestBase
00035 {
00036     void testMinus(
00037         uint nInputs, uint nRows, std::vector<InputData> const &inputData);
00038 
00043     void testRestartingMinus(
00044         uint nRows,
00045         std::vector<int> const &repeatSeqValues,
00046         uint subtrahendInterval);
00047 
00048     void newMinusStream(
00049         LbmMinusExecStreamParams &params,
00050         ExecStreamEmbryo &embryo,
00051         TupleDescriptor const &outputDesc);
00052 
00053 public:
00054     explicit LbmMinusExecStreamTest()
00055     {
00056         FENNEL_UNIT_TEST_CASE(LbmMinusExecStreamTest, test2Inputs);
00057         FENNEL_UNIT_TEST_CASE(LbmMinusExecStreamTest, test3Inputs);
00058         FENNEL_UNIT_TEST_CASE(LbmMinusExecStreamTest, testLargeOutput);
00059         FENNEL_UNIT_TEST_CASE(LbmMinusExecStreamTest, testAnchorLarger1);
00060         FENNEL_UNIT_TEST_CASE(LbmMinusExecStreamTest, testAnchorLarger2);
00061         FENNEL_UNIT_TEST_CASE(LbmMinusExecStreamTest, testChildrenLarger);
00062 
00063         // tests for restarting minus
00064         FENNEL_UNIT_TEST_CASE(LbmMinusExecStreamTest, testEvens);
00065         FENNEL_UNIT_TEST_CASE(LbmMinusExecStreamTest, testNines);
00066         FENNEL_UNIT_TEST_CASE(LbmMinusExecStreamTest, testClose);
00067         FENNEL_UNIT_TEST_CASE(
00068             LbmMinusExecStreamTest, testLowCardinalityRestart);
00069     }
00070 
00071     void test2Inputs();
00072     void test3Inputs();
00073     void testLargeOutput();
00074     void testAnchorLarger1();
00075     void testAnchorLarger2();
00076     void testChildrenLarger();
00077 
00078     void testEvens();
00079     void testNines();
00080     void testClose();
00081     void testLowCardinalityRestart();
00082 };
00083 
00093 class RestartingMinusExecStreamGenerator
00094     : public MockProducerExecStreamGenerator
00095 {
00096 protected:
00097     uint nKeys;
00098     std::vector<int> repeatSeqValues;
00099     uint subtrahendInterval;
00100     uint interval;
00101     boost::shared_array<uint> changeIndexes;
00102     uint current;
00103     uint lastRow;
00104 
00105 public:
00106     RestartingMinusExecStreamGenerator(
00107         uint nRows, std::vector<int> repeatSeqValues, uint subtrahendInterval)
00108     {
00109         this->nKeys = repeatSeqValues.size();
00110         this->repeatSeqValues = repeatSeqValues;
00111         interval = LbmExecStreamTestBase::getTupleInterval(repeatSeqValues);
00112 
00113         // find the number of times each key value repeats
00114         boost::scoped_array<uint> valueCounts;
00115         valueCounts.reset(new uint[interval]);
00116         for (uint i = 0; i < interval; i++) {
00117             valueCounts[i] = getValueCount(nRows, i);
00118         }
00119         // account for subtrahend
00120         for (uint iRow = 0; iRow < nRows; iRow += subtrahendInterval) {
00121             valueCounts[iRow % interval]--;
00122         }
00123 
00124         // find indexes where the values change
00125         changeIndexes.reset(new uint[interval]);
00126         changeIndexes[0] = valueCounts[0];
00127         for (uint i = 1; i < interval; i++) {
00128             changeIndexes[i] = changeIndexes[i - 1] + valueCounts[i];
00129         }
00130         current = 0;
00131         lastRow = 0;
00132     }
00133 
00134     virtual int64_t generateValue(uint iRow, uint iCol)
00135     {
00136         // must be generated in order
00137         assert (lastRow <= iRow);
00138         lastRow = iRow;
00139 
00140         if (iRow >= changeIndexes[current]) {
00141             current++;
00142             assert (current < interval);
00143         }
00144         assert (iCol < nKeys);
00145         return current % repeatSeqValues[iCol];
00146     }
00147 
00151     uint getValueCount(uint nRows, uint iValue)
00152     {
00153         uint nCopies = nRows / interval;
00154         if (iValue < nRows % interval) {
00155             nCopies++;
00156         }
00157         return nCopies;
00158     }
00159 
00163     uint getRowCount()
00164     {
00165         return changeIndexes[interval - 1];
00166     }
00167 };
00168 
00169 void LbmMinusExecStreamTest::test2Inputs()
00170 {
00171     uint nInputs = 2;
00172     uint nRows = 1000;
00173     std::vector<InputData> inputData;
00174     InputData input;
00175 
00176     // bitmap input 1 -- all bits set
00177     input.bitmapSize = 4;
00178     input.startRid = LcsRid(10);
00179     input.skipRows = 1;
00180     inputData.push_back(input);
00181 
00182     // bitmap input 2 -- every other bit set
00183     input.bitmapSize = 8;
00184     input.startRid = LcsRid(0);
00185     input.skipRows = 2;
00186     inputData.push_back(input);
00187 
00188     // expected result -- every other bit set, starting at 11
00189     input.bitmapSize = resultBitmapSize(11, nRows);
00190     input.startRid = LcsRid(11);
00191     input.skipRows = 2;
00192     inputData.push_back(input);
00193 
00194     testMinus(nInputs, nRows, inputData);
00195 }
00196 
00197 void LbmMinusExecStreamTest::test3Inputs()
00198 {
00199     uint nInputs = 3;
00200     uint nRows = 1000;
00201     std::vector<InputData> inputData;
00202     InputData input;
00203 
00204     // bitmap input 1 -- every rid set, starting at 22
00205     input.bitmapSize = 9;
00206     input.startRid = LcsRid(22);
00207     input.skipRows = 1;
00208     inputData.push_back(input);
00209 
00210     // bitmap input 2 -- every 3 rids set, starting at 2
00211     input.bitmapSize = 8;
00212     input.startRid = LcsRid(2);
00213     input.skipRows = 3;
00214     inputData.push_back(input);
00215 
00216     // bitmap input 3 -- every 3 set, starting at 3
00217     input.bitmapSize = 10;
00218     input.startRid = LcsRid(3);
00219     input.skipRows = 3;
00220     inputData.push_back(input);
00221 
00222     // expected result -- every 3 rids set, starting at 22
00223     input.bitmapSize = resultBitmapSize(22, nRows);
00224     input.startRid = LcsRid(22);
00225     input.skipRows = 3;
00226     inputData.push_back(input);
00227 
00228     testMinus(nInputs, nRows, inputData);
00229 }
00230 
00235 void LbmMinusExecStreamTest::testLargeOutput()
00236 {
00237     uint nInputs = 16;
00238     uint nRows = 100000;
00239     std::vector<InputData> inputData;
00240     InputData input;
00241 
00242     // bitmap input 1 -- all bits set
00243     input.bitmapSize = 1;
00244     input.startRid = LcsRid(0);
00245     input.skipRows = 1;
00246     inputData.push_back(input);
00247 
00248     // bitmap input 2 -- every 16 bits set, starting at 1
00249     input.bitmapSize = 1;
00250     input.startRid = LcsRid(1);
00251     input.skipRows = 16;
00252     inputData.push_back(input);
00253 
00254     // bitmap input 3 -- every 16 bits set, starting at 2
00255     input.bitmapSize = 1;
00256     input.startRid = LcsRid(2);
00257     input.skipRows = 16;
00258     inputData.push_back(input);
00259 
00260     // bitmap input 4 -- every 16 bits set, starting at 3
00261     input.bitmapSize = 1;
00262     input.startRid = LcsRid(3);
00263     input.skipRows = 16;
00264     inputData.push_back(input);
00265 
00266     // bitmap input 5 -- every 16 bits set, starting at 4
00267     input.bitmapSize = 1;
00268     input.startRid = LcsRid(4);
00269     input.skipRows = 16;
00270     inputData.push_back(input);
00271 
00272     // bitmap input 6 -- every 16 bits set, starting at 5
00273     input.bitmapSize = 1;
00274     input.startRid = LcsRid(5);
00275     input.skipRows = 16;
00276     inputData.push_back(input);
00277 
00278     // bitmap input 7 -- every 16 bits set, starting at 6
00279     input.bitmapSize = 1;
00280     input.startRid = LcsRid(6);
00281     input.skipRows = 16;
00282     inputData.push_back(input);
00283 
00284     // bitmap input 8 -- every 16 bits set, starting at 7
00285     input.bitmapSize = 1;
00286     input.startRid = LcsRid(7);
00287     input.skipRows = 16;
00288     inputData.push_back(input);
00289 
00290     // bitmap input 9 -- every 16 bits set, starting at 8
00291     input.bitmapSize = 1;
00292     input.startRid = LcsRid(8);
00293     input.skipRows = 16;
00294     inputData.push_back(input);
00295 
00296     // bitmap input 10 -- every 16 bits set, starting at 9
00297     input.bitmapSize = 1;
00298     input.startRid = LcsRid(9);
00299     input.skipRows = 16;
00300     inputData.push_back(input);
00301 
00302     // bitmap input 11 -- every 16 bits set, starting at 10
00303     input.bitmapSize = 1;
00304     input.startRid = LcsRid(10);
00305     input.skipRows = 16;
00306     inputData.push_back(input);
00307 
00308     // bitmap input 12 -- every 16 bits set, starting at 11
00309     input.bitmapSize = 1;
00310     input.startRid = LcsRid(11);
00311     input.skipRows = 16;
00312     inputData.push_back(input);
00313 
00314     // bitmap input 13 -- every 16 bits set, starting at 12
00315     input.bitmapSize = 1;
00316     input.startRid = LcsRid(12);
00317     input.skipRows = 16;
00318     inputData.push_back(input);
00319 
00320     // bitmap input 14 -- every 16 bits set, starting at 13
00321     input.bitmapSize = 1;
00322     input.startRid = LcsRid(13);
00323     input.skipRows = 16;
00324     inputData.push_back(input);
00325 
00326     // bitmap input 15 -- every 16 bits set, starting at 14
00327     input.bitmapSize = 1;
00328     input.startRid = LcsRid(14);
00329     input.skipRows = 16;
00330     inputData.push_back(input);
00331 
00332     // bitmap input 16 -- every 16 bits set, starting at 15
00333     input.bitmapSize = 1;
00334     input.startRid = LcsRid(15);
00335     input.skipRows = 16;
00336     inputData.push_back(input);
00337 
00338     // expected result -- every 16 bits set, starting at 0
00339     input.bitmapSize = resultBitmapSize(0, nRows);
00340     input.startRid = LcsRid(0);
00341     input.skipRows = 16;
00342     inputData.push_back(input);
00343 
00344     testMinus(nInputs, nRows, inputData);
00345 }
00346 
00351 void LbmMinusExecStreamTest::testAnchorLarger1()
00352 {
00353     uint nInputs = 3;
00354     uint nRows = 100;
00355     std::vector<InputData> inputData;
00356     InputData input;
00357 
00358     // bitmap input 1 -- every 17 bits set (0, 17, ..., 85)
00359     input.bitmapSize = 10;
00360     input.startRid = LcsRid(0);
00361     input.skipRows = 17;
00362     inputData.push_back(input);
00363 
00364     // bitmap input 2 -- every 60 bits (0, 60)
00365     input.bitmapSize = 10;
00366     input.startRid = LcsRid(0);
00367     input.skipRows = 60;
00368     inputData.push_back(input);
00369 
00370     // bitmap input 3 -- every 70 bits set (0, 70)
00371     input.bitmapSize = 10;
00372     input.startRid = LcsRid(0);
00373     input.skipRows = 70;
00374     inputData.push_back(input);
00375 
00376     // expected result -- every 17 bits set, starting at 17
00377     input.bitmapSize = resultBitmapSize(17, nRows);
00378     input.startRid = LcsRid(17);
00379     input.skipRows = 17;
00380     inputData.push_back(input);
00381 
00382     testMinus(nInputs, nRows, inputData);
00383 }
00384 
00389 void LbmMinusExecStreamTest::testAnchorLarger2()
00390 {
00391     uint nInputs = 3;
00392     uint nRows = 100;
00393     std::vector<InputData> inputData;
00394     InputData input;
00395 
00396     // bitmap input 1 -- every 17 bits set (0, 17, ..., 85)
00397     input.bitmapSize = 10;
00398     input.startRid = LcsRid(0);
00399     input.skipRows = 17;
00400     inputData.push_back(input);
00401 
00402     // bitmap input 2 -- every 60 bits (0, 60)
00403     input.bitmapSize = 10;
00404     input.startRid = LcsRid(0);
00405     input.skipRows = 60;
00406     inputData.push_back(input);
00407 
00408     // bitmap input 3 -- every 61 bits set (0, 61)
00409     input.bitmapSize = 10;
00410     input.startRid = LcsRid(0);
00411     input.skipRows = 61;
00412     inputData.push_back(input);
00413 
00414     // expected result -- every 17 bits set, starting at 17
00415     input.bitmapSize = resultBitmapSize(17, nRows);
00416     input.startRid = LcsRid(17);
00417     input.skipRows = 17;
00418     inputData.push_back(input);
00419 
00420     testMinus(nInputs, nRows, inputData);
00421 }
00422 
00427 void LbmMinusExecStreamTest::testChildrenLarger()
00428 {
00429     uint nInputs = 3;
00430     uint nRows = 100;
00431     std::vector<InputData> inputData;
00432     InputData input;
00433 
00434     // bitmap input 1 -- (0, 60)
00435     input.bitmapSize = 10;
00436     input.startRid = LcsRid(0);
00437     input.skipRows = 60;
00438     inputData.push_back(input);
00439 
00440     // bitmap input 2 -- only bit 70 set
00441     input.bitmapSize = 10;
00442     input.startRid = LcsRid(70);
00443     input.skipRows = 70;
00444     inputData.push_back(input);
00445 
00446     // bitmap input 3 -- only bit 80 set
00447     input.bitmapSize = 10;
00448     input.startRid = LcsRid(80);
00449     input.skipRows = 80;
00450     inputData.push_back(input);
00451 
00452     // expected result -- (0, 60)
00453     input.bitmapSize = resultBitmapSize(0, nRows);
00454     input.startRid = LcsRid(0);
00455     input.skipRows = 60;
00456     inputData.push_back(input);
00457 
00458     testMinus(nInputs, nRows, inputData);
00459 }
00460 
00461 void LbmMinusExecStreamTest::testEvens()
00462 {
00463     uint nRows = 100000;
00464     std::vector<int> repeatSeqValues;
00465     repeatSeqValues.push_back(1);
00466     repeatSeqValues.push_back(5);
00467     repeatSeqValues.push_back(9);
00468     uint subtrahendInterval = 2;
00469 
00470     testRestartingMinus(nRows, repeatSeqValues, subtrahendInterval);
00471 }
00472 
00473 void LbmMinusExecStreamTest::testNines()
00474 {
00475     uint nRows = 1000;
00476     std::vector<int> repeatSeqValues;
00477     repeatSeqValues.push_back(1);
00478     repeatSeqValues.push_back(5);
00479     repeatSeqValues.push_back(9);
00480     uint subtrahendInterval = 9;
00481 
00482     testRestartingMinus(nRows, repeatSeqValues, subtrahendInterval);
00483 }
00484 
00485 void LbmMinusExecStreamTest::testClose()
00486 {
00487     uint nRows = 1000;
00488     std::vector<int> repeatSeqValues;
00489     repeatSeqValues.push_back(2);
00490     repeatSeqValues.push_back(3);
00491     uint subtrahendInterval = 4;
00492 
00493     testRestartingMinus(nRows, repeatSeqValues, subtrahendInterval);
00494 }
00495 
00496 void LbmMinusExecStreamTest::testLowCardinalityRestart()
00497 {
00498     uint nRows = 10000;
00499     std::vector<int> repeatSeqValues;
00500     repeatSeqValues.push_back(2);
00501     uint subtrahendInterval = 21;
00502 
00503     testRestartingMinus(nRows, repeatSeqValues, subtrahendInterval);
00504 }
00505 
00506 void LbmMinusExecStreamTest::testMinus(
00507     uint nInputs, uint nRows, std::vector<InputData> const &inputData)
00508 {
00509     boost::scoped_array<BitmapInput> bmInputs;
00510     boost::scoped_array<ValuesExecStreamParams> valuesParams;
00511     std::vector<ExecStreamEmbryo> valuesStreamEmbryoList;
00512     ExecStreamEmbryo valuesStreamEmbryo;
00513 
00514     // +1 is for expected result
00515     assert(inputData.size() == nInputs + 1);
00516 
00517     bmInputs.reset(new BitmapInput[nInputs + 1]);
00518     valuesParams.reset(new ValuesExecStreamParams[nInputs]);
00519 
00520     // setup values exec stream for each input
00521     for (uint i = 0; i < nInputs; i++) {
00522         initBitmapInput(bmInputs[i], nRows, inputData[i]);
00523         initValuesExecStream(
00524             i, valuesParams[i], valuesStreamEmbryo, bmInputs[i]);
00525         valuesStreamEmbryoList.push_back(valuesStreamEmbryo);
00526     }
00527     // set up expected result
00528     if (inputData[nInputs].bitmapSize > 0) {
00529         initBitmapInput(bmInputs[nInputs], nRows, inputData[nInputs]);
00530     } else {
00531         bmInputs[nInputs].bufArray.reset();
00532         bmInputs[nInputs].nBitmaps = 0;
00533     }
00534 
00535     LbmMinusExecStreamParams minusParams;
00536     ExecStreamEmbryo minusEmbryo;
00537     newMinusStream(minusParams, minusEmbryo, bitmapTupleDesc);
00538 
00539     SharedExecStream pOutputStream = prepareConfluenceGraph(
00540         valuesStreamEmbryoList, minusEmbryo);
00541 
00542     verifyBufferedOutput(
00543         *pOutputStream, bitmapTupleDesc, bmInputs[nInputs].nBitmaps,
00544         bmInputs[nInputs].bufArray.get());
00545 }
00546 
00547 void LbmMinusExecStreamTest::testRestartingMinus(
00548     uint nRows,
00549     std::vector<int> const &repeatSeqValues,
00550     uint subtrahendInterval)
00551 {
00552     uint nKeys = repeatSeqValues.size();
00553 
00554     // initialize minuend [ (keys), (bitmaps) ]
00555     initKeyBitmap(nRows, repeatSeqValues);
00556     ValuesExecStreamParams valuesParams;
00557     valuesParams.outputTupleDesc = keyBitmapTupleDesc;
00558     valuesParams.pTupleBuffer = keyBitmapBuf;
00559     valuesParams.bufSize = keyBitmapBufSize;
00560 
00561     ExecStreamEmbryo minuendEmbryo;
00562     minuendEmbryo.init(new ValuesExecStream(), valuesParams);
00563     minuendEmbryo.getStream()->setName("MinuendValuesExecStream");
00564 
00565     // initialize subtrahend [ (bitmaps) ]
00566     InputData subtrahendData;
00567     subtrahendData.startRid = LcsRid(0);
00568     subtrahendData.skipRows = subtrahendInterval;
00569     subtrahendData.bitmapSize = 8;
00570 
00571     BitmapInput bmInput;
00572     initBitmapInput(bmInput, nRows, subtrahendData);
00573 
00574     ValuesExecStreamParams subtrahendParams;
00575     ExecStreamEmbryo subtrahendEmbryo;
00576     initValuesExecStream(0, subtrahendParams, subtrahendEmbryo, bmInput);
00577 
00578     // initialize the minus stream
00579     LbmMinusExecStreamParams minusParams;
00580     ExecStreamEmbryo minusEmbryo;
00581     newMinusStream(minusParams, minusEmbryo, keyBitmapTupleDesc);
00582 
00583     // initialize normalizer
00584     ExecStreamEmbryo normalizerEmbryo;
00585     LbmNormalizerExecStreamParams normalizerParams;
00586     initNormalizerExecStream(normalizerParams, normalizerEmbryo, nKeys);
00587 
00588     // build inputs -> minus stream -> normalizer transforms
00589     SharedExecStream pOutputStream = prepareConfluenceTransformGraph(
00590         minuendEmbryo, subtrahendEmbryo, minusEmbryo, normalizerEmbryo);
00591     RestartingMinusExecStreamGenerator
00592         verifier(nRows, repeatSeqValues, subtrahendInterval);
00593     verifyOutput(
00594         *pOutputStream,
00595         verifier.getRowCount(),
00596         verifier);
00597 }
00598 
00599 void LbmMinusExecStreamTest::newMinusStream(
00600     LbmMinusExecStreamParams &params,
00601     ExecStreamEmbryo &embryo,
00602     TupleDescriptor const &outputDesc)
00603 {
00604     params.rowLimitParamId = DynamicParamId(1);
00605     params.startRidParamId = DynamicParamId(2);
00606     params.outputTupleDesc = outputDesc;
00607     params.scratchAccessor =
00608         pSegmentFactory->newScratchSegment(pCache, 10);
00609 
00610     embryo.init(new LbmMinusExecStream(), params);
00611     embryo.getStream()->setName("MinusExecStream");
00612 }
00613 
00614 FENNEL_UNIT_TEST_SUITE(LbmMinusExecStreamTest);
00615 
00616 
00617 // End LbmMinusExecStreamTest.cpp

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