#ifndef __SIMPLETEST_H #define __SIMPLETEST_H #include #include "inccpp_chunkfile.hpp" #include "gen_test_numbers.hpp" #include #include #include #include #include #include using namespace std; // // A simple test suite: Just inherit CxxTest::TestSuite and write tests! // using namespace CxxTest; struct ConnectionFileHeader { ConnectionFileHeader() : NSource(0), NSx(0), NSy(0), NTarget(0), NTx(0), NTy(0) {} ConnectionFileHeader(int _NSource, int _NSx, int _NSy, int _NTarget, int _NTx, int _NTy) : NSource(_NSource), NSx(_NSx), NSy(_NSy), NTarget(_NTarget), NTx(_NTx), NTy(_NTy) {} string stringify() { stringstream o; o << "SourceTargetDim=" << NSource << " " << NSx << " " << NSy << " " << NTarget << " " << NTx << " " << NTy; return o.str(); } int NSource; int NSx; int NSy; int NTarget; int NTx; int NTy; }; class DataFile { public: ConnectionFileHeader ConHead; vector IntNumbers; vector FloatNumbers; string FileName; string HeaderChunk; string IntChunk; string FloatChunk; FileFormat MyFileFormat; DataFile() : ConHead(110,10,11,240,20,12), FileName("weightfilename.weights"), HeaderChunk("HeadChunk"), IntChunk("IntChunk"), FloatChunk("FloatChunk"), MyFileFormat("VecCon", 222222, 333333), IntNumbers(getIntTestNumbers()), FloatNumbers(getFloatTestNumbers()) { } void write() { ChunkFileWriter MyFileWriter(FileName, MyFileFormat); MyFileWriter.write(HeaderChunk, ConHead); MyFileWriter.writeVector(IntChunk, IntNumbers); MyFileWriter.writeVector(FloatChunk, FloatNumbers); MyFileWriter.close(); } void read() { ChunkFileReader MyChunkFile(FileName, MyFileFormat); HeaderChunk = MyChunkFile.read(ConHead); IntChunk = MyChunkFile.readNextVector(IntNumbers); FloatChunk = MyChunkFile.readNextVector(FloatNumbers); MyChunkFile.close(); } void readByChunkNames() { ChunkFileReader MyChunkFile(FileName, MyFileFormat); MyChunkFile.read(HeaderChunk, ConHead); MyChunkFile.readVector(FloatChunk, FloatNumbers); MyChunkFile.readVector(IntChunk, IntNumbers); MyChunkFile.close(); } void setOtherNumbers() { IntNumbers = getIntTestNumbers2(); FloatNumbers = getFloatTestNumbers2(); } string print() { stringstream stream; stream << MyFileFormat.print() << " " << HeaderChunk << " " << IntChunk << " " << FloatChunk << " "; for (int i=0;ib);} class ChunkFileTest : public CxxTest::TestSuite { public: void testMakeTempDir() { makeTempDir(); } void xtestStdSort() { vector MyVec = getIntTestNumbers(); sort (MyVec.begin(), MyVec.end(), isGreater); cout << "\n"; for (vector::iterator it=MyVec.begin(); it!=MyVec.end();++it) { cout << (*it) << " "; } } void testVectorTypeConversion() { vector MyVec = getCharTestNumbers(); vector MyCopyVec = MyVec; AssertVectorsEqual(MyCopyVec, MyVec); } void testFileFormatEqual() { FileFormat MyFormat("lala", 47,11); FileFormat DifferentFormat1("lala", 47,12); FileFormat DifferentFormat2("laba", 47,11); FileFormat DifferentFormat3("lala", 48,11); FileFormat SameFormat("lala", 47,11); TS_ASSERT(!MyFormat.isEqual(DifferentFormat1)); TS_ASSERT(!MyFormat.isEqual(DifferentFormat2)); TS_ASSERT(!MyFormat.isEqual(DifferentFormat3)); TS_ASSERT(MyFormat.isEqual(SameFormat)); } void testWrongFileFormat() { string FileName(mTempDir+"/WrongFileFormat.ChunkFile"); ChunkFileWriter MyFileWriter(FileName, WriteFormat); MyFileWriter.close(); ChunkFileReader MyFileReader(FileName); MyFileReader.close(); TS_ASSERT(ReadFormatSame.isEqual(MyFileReader.Version())); TS_ASSERT(!ReadFormatDiff.isEqual(MyFileReader.Version())); } void testWrongFormatThrowsException() { ChunkFileWriter MyFileWriter(MyFileName, WriteFormat); MyFileWriter.close(); TS_ASSERT_THROWS_NOTHING ( ChunkFileReader MyFileReaderSame(MyFileName, ReadFormatSame); MyFileReaderSame.close() ); TS_ASSERT_THROWS_ANYTHING( ChunkFileReader MyFileReaderDiff(MyFileName, ReadFormatDiff) ); } void testWrongFormatException() { ChunkFileWriter MyFileWriter(MyFileName, WriteFormat); MyFileWriter.close(); string ExceptionText ="Wrong FileFormat="+WriteFormat.print()+". Expected was:"+ReadFormatDiff.print(); try { ChunkFileReader MyFileReaderDiff(MyFileName, ReadFormatDiff); } catch (WrongFileVersion e) { TS_ASSERT_EQUALS(ExceptionText, e.what()); } } void testLongFormatString() { string FormatString("VecConnection"); for (int i=0;i<150;++i) FormatString.push_back('K'); for (int i=0;i<260;++i) FormatString.push_back('U'); FileFormat _FileFormat(FormatString.c_str(), 222222, 333333); ChunkFileWriter MyFileWriter(MyFileName, _FileFormat); MyFileWriter.close(); ChunkFileReader MyReadChunkFile(MyFileName); // if no format string is supplied, file version is not checked string TrunkatedFormatString = FormatString.substr(0, 255); TS_ASSERT_EQUALS(MyReadChunkFile.FormatString(), TrunkatedFormatString); } void testWriteAndReadFormatInfo() { ChunkFileWriter MyFileWriter(MyFileName, MyFileFormat); MyFileWriter.close(); ChunkFileReader MyReadChunkFile(MyFileName); TS_ASSERT_EQUALS(MyReadChunkFile.MajorVersion(), MyFileFormat.MajorVersion()); TS_ASSERT_EQUALS(MyReadChunkFile.MinorVersion(), MyFileFormat.MinorVersion()); } void testConnectionFileHeader() { ConnectionFileHeader ConHead(110,10,11,240,20,12); string ChunkName("ConHeader"); ChunkFileWriter MyFileWriter(MyFileName, MyFileFormat); MyFileWriter.write(ChunkName, ConHead); MyFileWriter.close(); ConnectionFileHeader ReadConHead(1,2,3,4,5,6); ChunkFileReader MyChunkFile(MyFileName); MyChunkFile.read(ReadConHead); MyChunkFile.close(); TS_ASSERT_EQUALS(ReadConHead.NSource, ConHead.NSource); TS_ASSERT_SAME_DATA(&(ReadConHead), &(ConHead), sizeof(ConnectionFileHeader)); } void testWriteAndReadShortVector() { vector MyVector = getIntTestNumbers(); TestReadWriteVector(MyVector); } void testWriteAndReadIntVector() { vector MyVector = getIntTestNumbers(); TestReadWriteVector(MyVector); } void testWriteAndReadFloatVector() { vector MyVector = getFloatTestNumbers(); TestReadWriteVector(MyVector); } void testWriteAndReadDoubleVector() { vector MyVector = getFloatTestNumbers(); TestReadWriteVector(MyVector); } void testConnectionFileHeaderAsVector() { vector ConHead (1, ConnectionFileHeader(110,10,11,240,20,12)); writeVectorToFile(ConHead); vector ReadConHead (1, ConnectionFileHeader(1,2,3,4,5,6)); readVectorFromFile(ReadConHead); TS_ASSERT_EQUALS(ReadConHead[0].NSource, ConHead[0].NSource); TS_ASSERT_SAME_DATA(&(ReadConHead[0]), &(ConHead[0]), sizeof(ConnectionFileHeader)); } void testWriteAndReadMultipleChunks() { DataFile WriteFile; WriteFile.write(); DataFile ReadFile; ReadFile.setOtherNumbers(); TS_ASSERT_DIFFERS(WriteFile.print(), ReadFile.print()); ReadFile.read(); TS_ASSERT_EQUALS(WriteFile.print(), ReadFile.print()); } void test_CxxTest_StringifiedStructAssert() { ConnectionFileHeader a(1,2,3,4,5,6); ConnectionFileHeader b(1,2,3,4,5,6); TS_ASSERT_EQUALS(a.stringify(),b.stringify()); } void testGetFileContent() { DataFile WriteFile; WriteFile.write(); vector ExpectedContent; ExpectedContent.push_back("HeadChunk"); ExpectedContent.push_back("IntChunk"); ExpectedContent.push_back("FloatChunk"); ChunkFileReader MyChunkFile(WriteFile.FileName); vector FileContent = MyChunkFile.getFileContent(); FileContent = MyChunkFile.getFileContent(); // to make shure the function can be called twice MyChunkFile.close(); AssertVectorsEqual(FileContent, ExpectedContent); } void testWriteAndReadMultipleChunksByChunkName() { DataFile WriteFile; WriteFile.write(); DataFile ReadFile; ReadFile.setOtherNumbers(); TS_ASSERT_DIFFERS(WriteFile.print(), ReadFile.print()); ReadFile.readByChunkNames(); TS_ASSERT_EQUALS(WriteFile.print(), ReadFile.print()); } void testWriteIndReadCharVector() { vector InputData = getCharTestNumbers(); writeVectorToFile(InputData); vector MyReadVector; readAndCastVectorFromFile(MyReadVector); AssertDiffTypeVectorsEqual(InputData, MyReadVector); } void testExceptionThrownOnBadVectorCast() { vector InputData = getIntTestNumbers(); writeVectorToFile(InputData); vector MyReadVector; TS_ASSERT_THROWS_ANYTHING(readAndCastVectorFromFile(MyReadVector)); } void testCleanUp() { if (mTempDirWasCreated) { system((string("rm -rf ") + mTempDir).c_str()); } } void setUp() { MyFileName = string(mTempDir + "weightfilename.weights"); string FormatString("VecConnection"); MyFileFormat = FileFormat(FormatString.c_str(), 222222, 333333); ChunkName = string("VectorDataTest"); WriteFormat = FileFormat ("lala", 47,11); ReadFormatSame = FileFormat ("lala", 47,11); ReadFormatDiff = FileFormat ("lala", 47,12); } private: vector MyVector; string MyFileName; FileFormat MyFileFormat; string ChunkName; string mTempDir; bool mTempDirWasCreated; FileFormat WriteFormat; FileFormat ReadFormatSame; FileFormat ReadFormatDiff; template void TestReadWriteVector(const vector& InputData) { writeVectorToFile(InputData); vector MyReadVector; string ReturnChunkName = readVectorFromFile(MyReadVector); AssertVectorsEqual(InputData, MyReadVector); TS_ASSERT_EQUALS(ChunkName, ReturnChunkName); } template void writeVectorToFile(const vector& InputData) { ChunkFileWriter MyFileWriter(MyFileName, MyFileFormat); MyFileWriter.writeVector(ChunkName, InputData); MyFileWriter.close(); } template string readVectorFromFile(vector& MyReadVector) { ChunkFileReader MyChunkFile(MyFileName, MyFileFormat); string ReadChunkName = MyChunkFile.readNextVector(MyReadVector); MyChunkFile.close(); return ReadChunkName; } template void readAndCastVectorFromFile(vector& MyReadVector) { ChunkFileReader MyChunkFile(MyFileName, MyFileFormat); MyChunkFile.readAndCastVector(ChunkName, MyReadVector); MyChunkFile.close(); } template void AssertVectorsEqual(const vector& Vec1, const vector Vec2) { TS_ASSERT_EQUALS(Vec1.size(), Vec2.size()); for (int i=0; i< Vec1.size(); ++i) { TS_ASSERT_EQUALS(Vec1[i],Vec2[i]); } } template void AssertDiffTypeVectorsEqual(const vector& Vec1, const vector Vec2) { TS_ASSERT_EQUALS(Vec1.size(), Vec2.size()); for (int i=0; i< Vec1.size(); ++i) { TS_ASSERT_EQUALS(Vec1[i],Vec2[i]); } } void makeTempDir() { mTempDir = "temp_chunkfiletest/" ; mkdir(mTempDir.c_str(),16877); mTempDirWasCreated = true; } }; #endif // __SIMPLETEST_H