ChunkFileTest.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #ifndef __SIMPLETEST_H
  2. #define __SIMPLETEST_H
  3. #include <cxxtest/TestSuite.h>
  4. #include "inccpp_chunkfile.hpp"
  5. #include "gen_test_numbers.hpp"
  6. #include <vector>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <algorithm>
  10. #include <typeinfo>
  11. #include <sys/stat.h>
  12. using namespace std;
  13. //
  14. // A simple test suite: Just inherit CxxTest::TestSuite and write tests!
  15. //
  16. using namespace CxxTest;
  17. struct ConnectionFileHeader
  18. {
  19. ConnectionFileHeader()
  20. : NSource(0), NSx(0), NSy(0), NTarget(0), NTx(0), NTy(0)
  21. {}
  22. ConnectionFileHeader(int _NSource, int _NSx, int _NSy, int _NTarget, int _NTx, int _NTy)
  23. : NSource(_NSource), NSx(_NSx), NSy(_NSy), NTarget(_NTarget), NTx(_NTx), NTy(_NTy)
  24. {}
  25. string stringify()
  26. {
  27. stringstream o;
  28. o << "SourceTargetDim=" << NSource << " " << NSx << " " << NSy << " " << NTarget << " " << NTx << " " << NTy;
  29. return o.str();
  30. }
  31. int NSource;
  32. int NSx;
  33. int NSy;
  34. int NTarget;
  35. int NTx;
  36. int NTy;
  37. };
  38. class DataFile
  39. {
  40. public:
  41. ConnectionFileHeader ConHead;
  42. vector<int> IntNumbers;
  43. vector<double> FloatNumbers;
  44. string FileName;
  45. string HeaderChunk;
  46. string IntChunk;
  47. string FloatChunk;
  48. FileFormat MyFileFormat;
  49. DataFile()
  50. : ConHead(110,10,11,240,20,12),
  51. FileName("weightfilename.weights"),
  52. HeaderChunk("HeadChunk"),
  53. IntChunk("IntChunk"),
  54. FloatChunk("FloatChunk"),
  55. MyFileFormat("VecCon", 222222, 333333),
  56. IntNumbers(getIntTestNumbers<int>()),
  57. FloatNumbers(getFloatTestNumbers<double>())
  58. {
  59. }
  60. void write()
  61. {
  62. ChunkFileWriter MyFileWriter(FileName, MyFileFormat);
  63. MyFileWriter.write(HeaderChunk, ConHead);
  64. MyFileWriter.writeVector(IntChunk, IntNumbers);
  65. MyFileWriter.writeVector(FloatChunk, FloatNumbers);
  66. MyFileWriter.close();
  67. }
  68. void read()
  69. {
  70. ChunkFileReader MyChunkFile(FileName, MyFileFormat);
  71. HeaderChunk = MyChunkFile.read(ConHead);
  72. IntChunk = MyChunkFile.readNextVector(IntNumbers);
  73. FloatChunk = MyChunkFile.readNextVector(FloatNumbers);
  74. MyChunkFile.close();
  75. }
  76. void readByChunkNames()
  77. {
  78. ChunkFileReader MyChunkFile(FileName, MyFileFormat);
  79. MyChunkFile.read(HeaderChunk, ConHead);
  80. MyChunkFile.readVector(FloatChunk, FloatNumbers);
  81. MyChunkFile.readVector(IntChunk, IntNumbers);
  82. MyChunkFile.close();
  83. }
  84. void setOtherNumbers()
  85. {
  86. IntNumbers = getIntTestNumbers2<int>();
  87. FloatNumbers = getFloatTestNumbers2<double>();
  88. }
  89. string print()
  90. {
  91. stringstream stream;
  92. stream << MyFileFormat.print() << " " << HeaderChunk << " " << IntChunk << " " << FloatChunk << " ";
  93. for (int i=0;i<IntNumbers.size();++i) {
  94. stream << IntNumbers[i] << " ";
  95. }
  96. for (int i=0;i<FloatNumbers.size();++i) {
  97. stream << FloatNumbers[i] << " ";
  98. }
  99. return stream.str();
  100. }
  101. };
  102. bool isGreater(int a, int b) {return (a>b);}
  103. class ChunkFileTest : public CxxTest::TestSuite
  104. {
  105. public:
  106. void testMakeTempDir()
  107. {
  108. makeTempDir();
  109. }
  110. void xtestStdSort()
  111. {
  112. vector<int> MyVec = getIntTestNumbers<int>();
  113. sort (MyVec.begin(), MyVec.end(), isGreater);
  114. cout << "\n";
  115. for (vector<int>::iterator it=MyVec.begin(); it!=MyVec.end();++it) {
  116. cout << (*it) << " ";
  117. }
  118. }
  119. void testVectorTypeConversion()
  120. {
  121. vector<char> MyVec = getCharTestNumbers<char>();
  122. vector<char> MyCopyVec = MyVec;
  123. AssertVectorsEqual(MyCopyVec, MyVec);
  124. }
  125. void testFileFormatEqual()
  126. {
  127. FileFormat MyFormat("lala", 47,11);
  128. FileFormat DifferentFormat1("lala", 47,12);
  129. FileFormat DifferentFormat2("laba", 47,11);
  130. FileFormat DifferentFormat3("lala", 48,11);
  131. FileFormat SameFormat("lala", 47,11);
  132. TS_ASSERT(!MyFormat.isEqual(DifferentFormat1));
  133. TS_ASSERT(!MyFormat.isEqual(DifferentFormat2));
  134. TS_ASSERT(!MyFormat.isEqual(DifferentFormat3));
  135. TS_ASSERT(MyFormat.isEqual(SameFormat));
  136. }
  137. void testWrongFileFormat()
  138. {
  139. string FileName(mTempDir+"/WrongFileFormat.ChunkFile");
  140. ChunkFileWriter MyFileWriter(FileName, WriteFormat);
  141. MyFileWriter.close();
  142. ChunkFileReader MyFileReader(FileName);
  143. MyFileReader.close();
  144. TS_ASSERT(ReadFormatSame.isEqual(MyFileReader.Version()));
  145. TS_ASSERT(!ReadFormatDiff.isEqual(MyFileReader.Version()));
  146. }
  147. void testWrongFormatThrowsException()
  148. {
  149. ChunkFileWriter MyFileWriter(MyFileName, WriteFormat);
  150. MyFileWriter.close();
  151. TS_ASSERT_THROWS_NOTHING (
  152. ChunkFileReader MyFileReaderSame(MyFileName, ReadFormatSame);
  153. MyFileReaderSame.close()
  154. );
  155. TS_ASSERT_THROWS_ANYTHING( ChunkFileReader MyFileReaderDiff(MyFileName, ReadFormatDiff) );
  156. }
  157. void testWrongFormatException()
  158. {
  159. ChunkFileWriter MyFileWriter(MyFileName, WriteFormat);
  160. MyFileWriter.close();
  161. string ExceptionText ="Wrong FileFormat="+WriteFormat.print()+". Expected was:"+ReadFormatDiff.print();
  162. try {
  163. ChunkFileReader MyFileReaderDiff(MyFileName, ReadFormatDiff);
  164. }
  165. catch (WrongFileVersion e) {
  166. TS_ASSERT_EQUALS(ExceptionText, e.what());
  167. }
  168. }
  169. void testLongFormatString()
  170. {
  171. string FormatString("VecConnection");
  172. for (int i=0;i<150;++i) FormatString.push_back('K');
  173. for (int i=0;i<260;++i) FormatString.push_back('U');
  174. FileFormat _FileFormat(FormatString.c_str(), 222222, 333333);
  175. ChunkFileWriter MyFileWriter(MyFileName, _FileFormat);
  176. MyFileWriter.close();
  177. ChunkFileReader MyReadChunkFile(MyFileName); // if no format string is supplied, file version is not checked
  178. string TrunkatedFormatString = FormatString.substr(0, 255);
  179. TS_ASSERT_EQUALS(MyReadChunkFile.FormatString(), TrunkatedFormatString);
  180. }
  181. void testWriteAndReadFormatInfo()
  182. {
  183. ChunkFileWriter MyFileWriter(MyFileName, MyFileFormat);
  184. MyFileWriter.close();
  185. ChunkFileReader MyReadChunkFile(MyFileName);
  186. TS_ASSERT_EQUALS(MyReadChunkFile.MajorVersion(), MyFileFormat.MajorVersion());
  187. TS_ASSERT_EQUALS(MyReadChunkFile.MinorVersion(), MyFileFormat.MinorVersion());
  188. }
  189. void testConnectionFileHeader()
  190. {
  191. ConnectionFileHeader ConHead(110,10,11,240,20,12);
  192. string ChunkName("ConHeader");
  193. ChunkFileWriter MyFileWriter(MyFileName, MyFileFormat);
  194. MyFileWriter.write(ChunkName, ConHead);
  195. MyFileWriter.close();
  196. ConnectionFileHeader ReadConHead(1,2,3,4,5,6);
  197. ChunkFileReader MyChunkFile(MyFileName);
  198. MyChunkFile.read(ReadConHead);
  199. MyChunkFile.close();
  200. TS_ASSERT_EQUALS(ReadConHead.NSource, ConHead.NSource);
  201. TS_ASSERT_SAME_DATA(&(ReadConHead), &(ConHead), sizeof(ConnectionFileHeader));
  202. }
  203. void testWriteAndReadShortVector()
  204. {
  205. vector<short> MyVector = getIntTestNumbers<short>();
  206. TestReadWriteVector(MyVector);
  207. }
  208. void testWriteAndReadIntVector()
  209. {
  210. vector<int> MyVector = getIntTestNumbers<int>();
  211. TestReadWriteVector(MyVector);
  212. }
  213. void testWriteAndReadFloatVector()
  214. {
  215. vector<float> MyVector = getFloatTestNumbers<float>();
  216. TestReadWriteVector(MyVector);
  217. }
  218. void testWriteAndReadDoubleVector()
  219. {
  220. vector<double> MyVector = getFloatTestNumbers<double>();
  221. TestReadWriteVector(MyVector);
  222. }
  223. void testConnectionFileHeaderAsVector()
  224. {
  225. vector<ConnectionFileHeader> ConHead (1, ConnectionFileHeader(110,10,11,240,20,12));
  226. writeVectorToFile(ConHead);
  227. vector<ConnectionFileHeader> ReadConHead (1, ConnectionFileHeader(1,2,3,4,5,6));
  228. readVectorFromFile(ReadConHead);
  229. TS_ASSERT_EQUALS(ReadConHead[0].NSource, ConHead[0].NSource);
  230. TS_ASSERT_SAME_DATA(&(ReadConHead[0]), &(ConHead[0]), sizeof(ConnectionFileHeader));
  231. }
  232. void testWriteAndReadMultipleChunks()
  233. {
  234. DataFile WriteFile;
  235. WriteFile.write();
  236. DataFile ReadFile;
  237. ReadFile.setOtherNumbers();
  238. TS_ASSERT_DIFFERS(WriteFile.print(), ReadFile.print());
  239. ReadFile.read();
  240. TS_ASSERT_EQUALS(WriteFile.print(), ReadFile.print());
  241. }
  242. void test_CxxTest_StringifiedStructAssert()
  243. {
  244. ConnectionFileHeader a(1,2,3,4,5,6);
  245. ConnectionFileHeader b(1,2,3,4,5,6);
  246. TS_ASSERT_EQUALS(a.stringify(),b.stringify());
  247. }
  248. void testGetFileContent()
  249. {
  250. DataFile WriteFile;
  251. WriteFile.write();
  252. vector<string> ExpectedContent;
  253. ExpectedContent.push_back("HeadChunk");
  254. ExpectedContent.push_back("IntChunk");
  255. ExpectedContent.push_back("FloatChunk");
  256. ChunkFileReader MyChunkFile(WriteFile.FileName);
  257. vector<string> FileContent = MyChunkFile.getFileContent();
  258. FileContent = MyChunkFile.getFileContent(); // to make shure the function can be called twice
  259. MyChunkFile.close();
  260. AssertVectorsEqual(FileContent, ExpectedContent);
  261. }
  262. void testWriteAndReadMultipleChunksByChunkName()
  263. {
  264. DataFile WriteFile;
  265. WriteFile.write();
  266. DataFile ReadFile;
  267. ReadFile.setOtherNumbers();
  268. TS_ASSERT_DIFFERS(WriteFile.print(), ReadFile.print());
  269. ReadFile.readByChunkNames();
  270. TS_ASSERT_EQUALS(WriteFile.print(), ReadFile.print());
  271. }
  272. void testWriteIndReadCharVector()
  273. {
  274. vector<int> InputData = getCharTestNumbers<int>();
  275. writeVectorToFile(InputData);
  276. vector<char> MyReadVector;
  277. readAndCastVectorFromFile(MyReadVector);
  278. AssertDiffTypeVectorsEqual(InputData, MyReadVector);
  279. }
  280. void testExceptionThrownOnBadVectorCast()
  281. {
  282. vector<int> InputData = getIntTestNumbers<int>();
  283. writeVectorToFile(InputData);
  284. vector<char> MyReadVector;
  285. TS_ASSERT_THROWS_ANYTHING(readAndCastVectorFromFile(MyReadVector));
  286. }
  287. void testCleanUp()
  288. {
  289. if (mTempDirWasCreated) {
  290. system((string("rm -rf ") + mTempDir).c_str());
  291. }
  292. }
  293. void setUp()
  294. {
  295. MyFileName = string(mTempDir + "weightfilename.weights");
  296. string FormatString("VecConnection");
  297. MyFileFormat = FileFormat(FormatString.c_str(), 222222, 333333);
  298. ChunkName = string("VectorDataTest");
  299. WriteFormat = FileFormat ("lala", 47,11);
  300. ReadFormatSame = FileFormat ("lala", 47,11);
  301. ReadFormatDiff = FileFormat ("lala", 47,12);
  302. }
  303. private:
  304. vector<int> MyVector;
  305. string MyFileName;
  306. FileFormat MyFileFormat;
  307. string ChunkName;
  308. string mTempDir;
  309. bool mTempDirWasCreated;
  310. FileFormat WriteFormat;
  311. FileFormat ReadFormatSame;
  312. FileFormat ReadFormatDiff;
  313. template<class T>
  314. void TestReadWriteVector(const vector<T>& InputData)
  315. {
  316. writeVectorToFile(InputData);
  317. vector<T> MyReadVector;
  318. string ReturnChunkName = readVectorFromFile(MyReadVector);
  319. AssertVectorsEqual(InputData, MyReadVector);
  320. TS_ASSERT_EQUALS(ChunkName, ReturnChunkName);
  321. }
  322. template<class T>
  323. void writeVectorToFile(const vector<T>& InputData)
  324. {
  325. ChunkFileWriter MyFileWriter(MyFileName, MyFileFormat);
  326. MyFileWriter.writeVector(ChunkName, InputData);
  327. MyFileWriter.close();
  328. }
  329. template<class T>
  330. string readVectorFromFile(vector<T>& MyReadVector)
  331. {
  332. ChunkFileReader MyChunkFile(MyFileName, MyFileFormat);
  333. string ReadChunkName = MyChunkFile.readNextVector(MyReadVector);
  334. MyChunkFile.close();
  335. return ReadChunkName;
  336. }
  337. template<class T>
  338. void readAndCastVectorFromFile(vector<T>& MyReadVector)
  339. {
  340. ChunkFileReader MyChunkFile(MyFileName, MyFileFormat);
  341. MyChunkFile.readAndCastVector(ChunkName, MyReadVector);
  342. MyChunkFile.close();
  343. }
  344. template<class T>
  345. void AssertVectorsEqual(const vector<T>& Vec1, const vector<T> Vec2)
  346. {
  347. TS_ASSERT_EQUALS(Vec1.size(), Vec2.size());
  348. for (int i=0; i< Vec1.size(); ++i) {
  349. TS_ASSERT_EQUALS(Vec1[i],Vec2[i]);
  350. }
  351. }
  352. template<class T1, class T2>
  353. void AssertDiffTypeVectorsEqual(const vector<T1>& Vec1, const vector<T2> Vec2)
  354. {
  355. TS_ASSERT_EQUALS(Vec1.size(), Vec2.size());
  356. for (int i=0; i< Vec1.size(); ++i) {
  357. TS_ASSERT_EQUALS(Vec1[i],Vec2[i]);
  358. }
  359. }
  360. void makeTempDir()
  361. {
  362. mTempDir = "temp_chunkfiletest/" ;
  363. mkdir(mTempDir.c_str(),16877);
  364. mTempDirWasCreated = true;
  365. }
  366. };
  367. #endif // __SIMPLETEST_H