/* Copyright (C) 2014 InfiniDB, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include using namespace std; #include using namespace boost; #include #include "we_dctnry.h" #include "we_dctnrystore.h" using namespace WriteEngine; class DctnryTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE( DctnryTest ); // Dctnry basic testing //CPPUNIT_TEST( testDctnryInsertLarge ); //CPPUNIT_TEST( testDctnryInsertDelete ); ///CPPUNIT_TEST( testDctnryInsertStress); //CPPUNIT_TEST( testGetBlockHdr); //CPPUNIT_TEST( testGetFreeSpace); CPPUNIT_TEST( testDctnryInsertDeleteStore); CPPUNIT_TEST_SUITE_END(); private: Dctnry m_Dctnry; DctnryStore m_DctnryStore; int m_oId; public: void setUp() { } void tearDown() { } DctnryTest() { m_oId =0; } void testDctnryInsertLarge() { int oId=2000; // int blockCount =10; FILE* dFile =NULL; int rc =0; DataBlock curBlock; // int largeSize=6144; int largeSize=2724; int smallSize=2032; uint16_t freeSpace; uint64_t nextPtr; uint16_t offSet0; uint16_t endHeader; unsigned char dctnryHeader[14]; m_Dctnry.setUseSmallSize(false); m_Dctnry.setDebugLevel( DEBUG_3 ); m_oId = oId; memset(curBlock.data,0, sizeof (curBlock.data)); memset(dctnryHeader,0, sizeof(dctnryHeader)); printf("\nRunning testDctnryInsertLarge \n"); rc = m_Dctnry.deleteFile(oId); rc = m_Dctnry.createDctnry(oId,3); CPPUNIT_ASSERT( rc == NO_ERROR ); rc = m_Dctnry.openDctnry(oId); CPPUNIT_ASSERT( rc == NO_ERROR ); dFile = m_Dctnry.getDctnryFile(); CPPUNIT_ASSERT( dFile != NULL ); rc = m_Dctnry.initDctnryHdr( dFile); CPPUNIT_ASSERT( rc == NO_ERROR ); m_Dctnry.closeDctnry(); printf("After initDctnryHdr \n"); testGetBlockHdr(); //dFile= m_Dctnry.openFile( oId); rc = m_Dctnry.openDctnry(); CPPUNIT_ASSERT( rc == NO_ERROR ); dFile = m_Dctnry.getDctnryFile(); CPPUNIT_ASSERT( dFile != NULL ); //12 for 6 bytes and 14 for 8 bytes rc =m_Dctnry.readSubBlockEntry( dFile, &curBlock, 0, 0, 0, 14, &dctnryHeader); CPPUNIT_ASSERT( rc == NO_ERROR ); memcpy(&freeSpace,dctnryHeader,2); memcpy(&nextPtr,dctnryHeader+2,8); // 8 bytes memcpy(&offSet0,dctnryHeader+10,2); memcpy(&endHeader,dctnryHeader+12,2); unsigned char sgnature_value[largeSize]; memset(sgnature_value,0, sizeof(sgnature_value)); int j=0; for (int i=0; i255) // j=0; sgnature_value[i]=119; // j++; } //insert a signature value Token token; rc = m_Dctnry.insertDctnry(dFile, largeSize, sgnature_value, token); CPPUNIT_ASSERT( rc == NO_ERROR ); testGetBlockHdr(); rc = m_Dctnry.insertDctnry(dFile, largeSize, sgnature_value, token); CPPUNIT_ASSERT( rc == NO_ERROR ); testGetBlockHdr(); rc = m_Dctnry.insertDctnry(dFile, largeSize, sgnature_value, token); CPPUNIT_ASSERT( rc == NO_ERROR ); testGetBlockHdr(); token.fbo = 0; token.op = 3; rc =m_Dctnry.deleteDctnryValue( dFile, token); testGetBlockHdr(); rc = m_Dctnry.insertDctnry(dFile, largeSize, sgnature_value, token); CPPUNIT_ASSERT( rc == NO_ERROR ); testGetBlockHdr(); return; //add the same dictionary again. it should not be added rc = m_Dctnry.insertDctnry(dFile, largeSize, sgnature_value, token); testGetBlockHdr(); //add the same dictionary again. it should not be added to the next block rc = m_Dctnry.insertDctnry(dFile, largeSize, sgnature_value, token); testGetBlockHdr(); printf("token fbo = %i",token.fbo); printf("token op = %i",token.op); token.fbo = 0; token.op = 2; // token.op++; rc =m_Dctnry.deleteDctnryValue( dFile, token); testGetBlockHdr(); return; //add a 2nd dictionary to fillup the whole file block unsigned char sgnature_value_s[smallSize]; memset(sgnature_value_s,0, sizeof(sgnature_value_s)); for (int i=0; i255) // j=0; sgnature_value_s[i]=118; // j++; } //insert a signature value rc = m_Dctnry.insertDctnry(dFile, smallSize, sgnature_value_s, token); CPPUNIT_ASSERT( rc == NO_ERROR ); testGetBlockHdr(); return; //insert a signature value Token token3; rc = m_Dctnry.insertDctnry(dFile, largeSize, sgnature_value, token3); CPPUNIT_ASSERT( rc == NO_ERROR ); m_Dctnry.closeDctnry(dFile); printf("After insert dictionary \n"); testGetBlockHdr(); rc = m_Dctnry.openDctnry(oId); CPPUNIT_ASSERT( rc == NO_ERROR ); dFile = m_Dctnry.getDctnryFile(); rc =m_Dctnry.deleteDctnryValue( dFile, token); m_Dctnry.closeDctnry(dFile); printf("After delete dictionary \n"); testGetBlockHdr(); CPPUNIT_ASSERT( rc == NO_ERROR ); j=255; for (int i=0; i255) j=0; sgnature_value[i]=j; j++; } //insert a signature value Token token; rc = m_Dctnry.insertDctnry(dFile, largeSize, sgnature_value, token); CPPUNIT_ASSERT( rc ==ERR_DICT_SIZE_GT_8000); largeSize = 6000; Token token3; for (int i=0; i255) j=0; sgnature_value[i]=j; j++; } rc = m_Dctnry.insertDctnry(dFile, largeSize, sgnature_value, token3); CPPUNIT_ASSERT( rc == NO_ERROR ); CPPUNIT_ASSERT( token3.fbo == 0 ); CPPUNIT_ASSERT( token3.op == 1 ); m_Dctnry.closeDctnry(dFile); printf("After insert token 3 into dictionary \n"); testGetBlockHdr(); rc = m_Dctnry.openDctnry(); CPPUNIT_ASSERT( rc == NO_ERROR ); dFile = m_Dctnry.getDctnryFile(); CPPUNIT_ASSERT( dFile != NULL ); j=255; for (int i=0; i15) { CPPUNIT_ASSERT( rc != NO_ERROR ); printf("294. Error code is: %i \n", rc); } else CPPUNIT_ASSERT( rc == NO_ERROR ); }//endfor k Offset prevOffset, curOffset; j=0; unsigned char* value = NULL; //This is to get the value out for (i=0; i %i i-> %i error code -> %i \n ", k, i, rc); if (ksize is %i\n",resultString, strlen(resultString) ); m_Dctnry.closeDctnry(); printf("After insert the result string, the header look like the following:\n"); testGetBlockHdr(); return; }//testDctnryInsertStress void testGetBlockHdr() { int blockCount=2; int oId =2002; int i =0, k=0; FILE* dFile; int opCount; Offset hdrOffsets[4040]; memset(hdrOffsets,0, sizeof(hdrOffsets)); m_Dctnry.setDebugLevel( DEBUG_3 ); printf("\nRunning testGetBlockHdr \n"); dFile= m_Dctnry.openFile( m_oId ); if (dFile==NULL) dFile= m_Dctnry.openFile( oId ); CPPUNIT_ASSERT( dFile != NULL ); blockCount = m_Dctnry.getFileSize( dFile )/BYTE_PER_BLOCK ; for (i=0; ifree space:%i \n", i, m_Dctnry.m_freeSpaceArray[i]); } m_Dctnry.closeDctnry(); } void testDctnryInsertDeleteStore() { int rc =0; int smallSize=1000; FID dctnryOID =2002; FID treeOID = 101; FID listOID = 102; m_DctnryStore.setDebugLevel( DEBUG_3 ); printf("\nRunning testDctnryInsertDeleteStore \n"); rc = m_DctnryStore.dropDctnryStore( treeOID, dctnryOID, listOID); //CPPUNIT_ASSERT( rc == NO_ERROR ); rc = m_DctnryStore.createDctnryStore( dctnryOID, treeOID, listOID); CPPUNIT_ASSERT( rc == NO_ERROR ); rc = m_DctnryStore.dropDctnryStore( dctnryOID, treeOID, listOID); CPPUNIT_ASSERT( rc == NO_ERROR ); rc = m_DctnryStore.createDctnryStore( dctnryOID, treeOID, listOID); CPPUNIT_ASSERT( rc == NO_ERROR ); rc = m_DctnryStore.dropDctnryStore( treeOID, dctnryOID, listOID); CPPUNIT_ASSERT( rc == NO_ERROR ); rc = m_DctnryStore.createDctnryStore( dctnryOID, treeOID, listOID); CPPUNIT_ASSERT( rc == NO_ERROR ); rc = m_DctnryStore.dropDctnryStore(); CPPUNIT_ASSERT( rc == NO_ERROR ); rc = m_DctnryStore.createDctnryStore( dctnryOID, treeOID, listOID); CPPUNIT_ASSERT( rc == NO_ERROR ); rc = m_DctnryStore.openDctnryStore(dctnryOID, treeOID, listOID ); CPPUNIT_ASSERT( rc ==NO_ERROR ); m_DctnryStore.closeDctnryStore(); CPPUNIT_ASSERT( rc ==NO_ERROR ); rc = m_DctnryStore.openDctnryStore(); CPPUNIT_ASSERT( rc ==NO_ERROR ); unsigned char sgnature_value[smallSize]; Token token[100]; memset(token, 0, sizeof(token)); int i,j,k; for (int i=0; i255) // j=0; sgnature_value[i]=119; // j++; } //insert a signature value for (k=1;k<12;k++) { rc = m_DctnryStore.updateDctnryStore(sgnature_value, smallSize,token[k]); printf("token fbo = %i",token[k].fbo); printf("token op = %i",token[k].op); } testGetBlockHdr(); return; rc = m_DctnryStore.updateDctnryStore(sgnature_value, smallSize,token[1]); testGetBlockHdr(); //Recheck if the tokens are all inserted for (k=1; k<99; k++) { for (i=0; i10) sgnature_value[i]=k; else { if (i<8) sgnature_value[i] =1; else sgnature_value[i]=k; } j--; }//endfor i ; second time rc = m_DctnryStore.updateDctnryStore(sgnature_value, smallSize,token[k]); if (rc!= NO_ERROR) { printf("443. Attentione!!! ERROR CODE : %i \n", rc); } CPPUNIT_ASSERT( rc == NO_ERROR ); } //endof for k; second time int tempSize = 7; rc = m_DctnryStore.updateDctnryStore(sgnature_value, tempSize,token[k]); if (rc!= NO_ERROR) { printf("452. Predicted Error Code should be 1363: The result ERROR CODE : %i \n", rc); } CPPUNIT_ASSERT( rc ==1363 ); tempSize = 8; rc = m_DctnryStore.updateDctnryStore(sgnature_value, tempSize,token[k]); if (rc!= NO_ERROR) { printf("461. Attention!!! ERROR CODE : %i \n", rc); } CPPUNIT_ASSERT( rc == NO_ERROR ); for (i=1; i<99; i++) { if( m_DctnryStore.isDebug( DEBUG_3 )) { printf("i : %i token.fbo %i token.op %i \n", i, (int)token[i].fbo, (int)token[i].op); } } for (i=1; i<99; i++) { rc = m_DctnryStore.deleteDctnryToken(token[i]); if (rc!= NO_ERROR) { printf("475 . Attention!!! ERROR CODE : %i \n", rc); } CPPUNIT_ASSERT( rc == NO_ERROR ); } m_DctnryStore.closeDctnryStore(); //rc = m_DctnryStore.dropDctnryStore(); //CPPUNIT_ASSERT( rc == NO_ERROR ); return; }//testDctnryInsertDeleteStore }; CPPUNIT_TEST_SUITE_REGISTRATION( DctnryTest ); #include #include int main( int argc, char **argv) { CppUnit::TextUi::TestRunner runner; CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry(); runner.addTest( registry.makeTest() ); bool wasSuccessful = runner.run( "", false ); return (wasSuccessful ? 0 : 1); }