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/SegStorageTestBase.h"
00026 #include "fennel/segment/SegOutputStream.h"
00027 #include "fennel/segment/SegInputStream.h"
00028 #include "fennel/segment/SpillOutputStream.h"
00029
00030 #include <fstream>
00031 #include <boost/scoped_array.hpp>
00032 #include <boost/test/test_tools.hpp>
00033
00034 using namespace fennel;
00035
00036 class SegStreamTest : virtual public SegStorageTestBase
00037 {
00038 size_t maxWordLength;
00039 uint totalDictLength;
00040
00041 void testWrite(SharedByteOutputStream pOutputStream,uint nRuns)
00042 {
00043 maxWordLength = 0;
00044 totalDictLength = 0;
00045 for (uint i = 0; i < nRuns; ++i) {
00046 std::ifstream wordStream(
00047 configMap.getStringParam(paramDictionaryFileName).c_str());
00048 BOOST_CHECK(wordStream.good());
00049 std::string word;
00050 for (;;) {
00051 word.clear();
00052 wordStream >> word;
00053 if (word == "") {
00054 break;
00055 }
00056 maxWordLength = std::max(word.size(),maxWordLength);
00057 if (i == 0) {
00058 totalDictLength += word.size();
00059 }
00060 if (i < 2) {
00061 pOutputStream->writeBytes(word.c_str(),word.size());
00062 } else {
00063 uint cbActual;
00064 PBuffer pBuf = pOutputStream->getWritePointer(
00065 word.size(),&cbActual);
00066 BOOST_CHECK(cbActual >= word.size());
00067 memcpy(pBuf,word.c_str(),word.size());
00068 pOutputStream->consumeWritePointer(word.size());
00069 }
00070 }
00071 }
00072 }
00073
00074 void testRead(SharedByteInputStream pInputStream,uint nRuns)
00075 {
00076 boost::scoped_array<char> wordArray(new char[maxWordLength+1]);
00077 for (uint i = 0; i < nRuns; ++i) {
00078 if (i == 0) {
00079 pInputStream->seekForward(totalDictLength);
00080 continue;
00081 }
00082 std::ifstream wordStream(
00083 configMap.getStringParam(paramDictionaryFileName).c_str());
00084 std::string word,segWord;
00085 for (;;) {
00086 word.clear();
00087 wordStream >> word;
00088 if (word == "") {
00089 break;
00090 }
00091 segWord.clear();
00092 if (i < 2) {
00093 uint nChars = pInputStream->readBytes(
00094 wordArray.get(),word.size());
00095 BOOST_CHECK_EQUAL(nChars,word.size());
00096 segWord.assign(wordArray.get(),word.size());
00097 } else {
00098 uint cbActual;
00099 PConstBuffer pBuf = pInputStream->getReadPointer(
00100 word.size(),&cbActual);
00101 BOOST_CHECK(pBuf);
00102 BOOST_CHECK(cbActual >= word.size());
00103 segWord.assign(
00104 reinterpret_cast<char const *>(pBuf),
00105 word.size());
00106 pInputStream->consumeReadPointer(word.size());
00107 }
00108 BOOST_CHECK_EQUAL(word,segWord);
00109 }
00110 }
00111 uint nChars = pInputStream->readBytes(wordArray.get(),1);
00112 BOOST_CHECK(!nChars);
00113 BOOST_CHECK(!pInputStream->getReadPointer(1));
00114 }
00115
00116 public:
00117 explicit SegStreamTest()
00118 {
00119
00120
00121 nDiskPages = 0;
00122
00123 FENNEL_UNIT_TEST_CASE(SegStreamTest,testWriteSeg);
00124 FENNEL_UNIT_TEST_CASE(SegStreamTest,testReadSeg);
00125 FENNEL_UNIT_TEST_CASE(SegStreamTest,testMarkReset);
00126 FENNEL_UNIT_TEST_CASE(SegStreamTest,testWriteSpillAndRead);
00127 }
00128
00129 void testWriteSeg()
00130 {
00131 openStorage(DeviceMode::createNew);
00132 SegmentAccessor segmentAccessor(pLinearSegment,pCache);
00133 SharedSegOutputStream pOutputStream =
00134 SegOutputStream::newSegOutputStream(segmentAccessor);
00135 testWrite(pOutputStream,3);
00136 pOutputStream.reset();
00137 segmentAccessor.reset();
00138 closeStorage();
00139 }
00140
00141 void testReadSeg()
00142 {
00143 openStorage(DeviceMode::load);
00144 SegmentAccessor segmentAccessor(pLinearSegment,pCache);
00145 SharedSegInputStream pInputStream =
00146 SegInputStream::newSegInputStream(segmentAccessor);
00147 pInputStream->startPrefetch();
00148 testRead(pInputStream,3);
00149 pInputStream.reset();
00150 segmentAccessor.reset();
00151 closeStorage();
00152 }
00153
00154 void testMarkReset()
00155 {
00156 openStorage(DeviceMode::load);
00157 SegmentAccessor segmentAccessor(pLinearSegment,pCache);
00158 SharedSegInputStream pInputStream =
00159 SegInputStream::newSegInputStream(segmentAccessor);
00160 SharedByteStreamMarker pMarker = pInputStream->newMarker();
00161 pInputStream->mark(*pMarker);
00162 pInputStream->startPrefetch();
00163 testRead(pInputStream,3);
00164 pInputStream->reset(*pMarker);
00165 testRead(pInputStream,3);
00166 pInputStream.reset();
00167 segmentAccessor.reset();
00168 closeStorage();
00169 }
00170
00171 void testWriteSpillAndRead()
00172 {
00173 openStorage(DeviceMode::createNew);
00174 SharedSpillOutputStream pOutputStream =
00175 SpillOutputStream::newSpillOutputStream(
00176 pSegmentFactory,pCache,"spill.dat");
00177 testWrite(pOutputStream,2);
00178 SharedByteInputStream pInputStream = pOutputStream->getInputStream();
00179 pOutputStream.reset();
00180 testRead(pInputStream,2);
00181 pInputStream.reset();
00182 closeStorage();
00183 }
00184
00185 };
00186
00187 FENNEL_UNIT_TEST_SUITE(SegStreamTest);
00188
00189