123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- #ifndef __SIMPLETEST_H
- #define __SIMPLETEST_H
- #include <cxxtest/TestSuite.h>
- #include "inccpp_chunkfile.hpp"
- #include "gen_test_numbers.hpp"
- #include <vector>
- #include <iostream>
- #include <sstream>
- #include <algorithm>
- #include <typeinfo>
- #include <sys/stat.h>
- 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<int> IntNumbers;
- vector<double> 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<int>()),
- FloatNumbers(getFloatTestNumbers<double>())
- {
- }
- 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<int>();
- FloatNumbers = getFloatTestNumbers2<double>();
- }
- string print()
- {
- stringstream stream;
- stream << MyFileFormat.print() << " " << HeaderChunk << " " << IntChunk << " " << FloatChunk << " ";
- for (int i=0;i<IntNumbers.size();++i) {
- stream << IntNumbers[i] << " ";
- }
- for (int i=0;i<FloatNumbers.size();++i) {
- stream << FloatNumbers[i] << " ";
- }
- return stream.str();
- }
-
- };
- bool isGreater(int a, int b) {return (a>b);}
- class ChunkFileTest : public CxxTest::TestSuite
- {
- public:
- void testMakeTempDir()
- {
- makeTempDir();
- }
-
- void xtestStdSort()
- {
- vector<int> MyVec = getIntTestNumbers<int>();
- sort (MyVec.begin(), MyVec.end(), isGreater);
- cout << "\n";
- for (vector<int>::iterator it=MyVec.begin(); it!=MyVec.end();++it) {
- cout << (*it) << " ";
- }
- }
- void testVectorTypeConversion()
- {
- vector<char> MyVec = getCharTestNumbers<char>();
- vector<char> 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<short> MyVector = getIntTestNumbers<short>();
- TestReadWriteVector(MyVector);
- }
- void testWriteAndReadIntVector()
- {
- vector<int> MyVector = getIntTestNumbers<int>();
- TestReadWriteVector(MyVector);
- }
-
- void testWriteAndReadFloatVector()
- {
- vector<float> MyVector = getFloatTestNumbers<float>();
- TestReadWriteVector(MyVector);
- }
-
- void testWriteAndReadDoubleVector()
- {
- vector<double> MyVector = getFloatTestNumbers<double>();
- TestReadWriteVector(MyVector);
- }
- void testConnectionFileHeaderAsVector()
- {
- vector<ConnectionFileHeader> ConHead (1, ConnectionFileHeader(110,10,11,240,20,12));
- writeVectorToFile(ConHead);
-
- vector<ConnectionFileHeader> 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<string> ExpectedContent;
- ExpectedContent.push_back("HeadChunk");
- ExpectedContent.push_back("IntChunk");
- ExpectedContent.push_back("FloatChunk");
- ChunkFileReader MyChunkFile(WriteFile.FileName);
- vector<string> 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<int> InputData = getCharTestNumbers<int>();
- writeVectorToFile(InputData);
-
- vector<char> MyReadVector;
- readAndCastVectorFromFile(MyReadVector);
-
- AssertDiffTypeVectorsEqual(InputData, MyReadVector);
- }
-
- void testExceptionThrownOnBadVectorCast()
- {
- vector<int> InputData = getIntTestNumbers<int>();
- writeVectorToFile(InputData);
-
- vector<char> 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<int> MyVector;
- string MyFileName;
- FileFormat MyFileFormat;
- string ChunkName;
- string mTempDir;
- bool mTempDirWasCreated;
-
- FileFormat WriteFormat;
- FileFormat ReadFormatSame;
- FileFormat ReadFormatDiff;
-
-
- template<class T>
- void TestReadWriteVector(const vector<T>& InputData)
- {
- writeVectorToFile(InputData);
- vector<T> MyReadVector;
- string ReturnChunkName = readVectorFromFile(MyReadVector);
- AssertVectorsEqual(InputData, MyReadVector);
- TS_ASSERT_EQUALS(ChunkName, ReturnChunkName);
- }
- template<class T>
- void writeVectorToFile(const vector<T>& InputData)
- {
- ChunkFileWriter MyFileWriter(MyFileName, MyFileFormat);
- MyFileWriter.writeVector(ChunkName, InputData);
- MyFileWriter.close();
- }
-
- template<class T>
- string readVectorFromFile(vector<T>& MyReadVector)
- {
- ChunkFileReader MyChunkFile(MyFileName, MyFileFormat);
- string ReadChunkName = MyChunkFile.readNextVector(MyReadVector);
- MyChunkFile.close();
- return ReadChunkName;
- }
- template<class T>
- void readAndCastVectorFromFile(vector<T>& MyReadVector)
- {
- ChunkFileReader MyChunkFile(MyFileName, MyFileFormat);
- MyChunkFile.readAndCastVector(ChunkName, MyReadVector);
- MyChunkFile.close();
- }
-
-
- template<class T>
- void AssertVectorsEqual(const vector<T>& Vec1, const vector<T> Vec2)
- {
- TS_ASSERT_EQUALS(Vec1.size(), Vec2.size());
- for (int i=0; i< Vec1.size(); ++i) {
- TS_ASSERT_EQUALS(Vec1[i],Vec2[i]);
- }
- }
- template<class T1, class T2>
- void AssertDiffTypeVectorsEqual(const vector<T1>& Vec1, const vector<T2> 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
|