TestStreamable.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libiocompression - An iocompression/template for new NICE libraries
  4. * See file License for license information.
  5. */
  6. #ifdef NICE_USELIB_CPPUNIT
  7. #include <stdlib.h>
  8. #include <string>
  9. #include <exception>
  10. #include <cstddef>
  11. #include "TestStreamable.h"
  12. using namespace NICE;
  13. CPPUNIT_TEST_SUITE_REGISTRATION( TestStreamable );
  14. class StreamableClass : public NICE::Streamable, public NICE::BinStreamable {
  15. public:
  16. NICE::VectorT<double> evdouble;
  17. NICE::VectorT<long> evlong;
  18. #ifdef NICE_USELIB_LINAL
  19. LinAl::VectorCC<float> vcfloat;
  20. LinAl::MatrixCF<float> mcfloat;
  21. #endif
  22. StreamableClass(){}
  23. StreamableClass(NICE::VectorT<double> &ed, NICE::VectorT<long> &el
  24. #ifdef NICE_USELIB_LINAL
  25. ,const LinAl::VectorCC<float> &vf, const LinAl::MatrixCF<float> &mf
  26. #endif
  27. );
  28. virtual void read(std::istream& strm);
  29. virtual void write(std::ostream& strm) const;
  30. virtual void read(NICE::ibinstream& strm);
  31. virtual void write(NICE::obinstream& strm) const;
  32. };
  33. void TestStreamable::setUp() {
  34. s=NULL;
  35. static const int k=10;
  36. double darray[k];
  37. long larray[2*k];
  38. float farray[2*k];
  39. for(int i=0;i<k;i++) {
  40. larray[2*i] = i;
  41. larray[2*i+1] = rand();
  42. farray[2*i] = i;
  43. farray[2*i+1] = rand();
  44. darray[i] = rand();
  45. }
  46. VectorT<double> ed(darray,k);
  47. VectorT<long> el(larray,2*k);
  48. #ifdef NICE_USELIB_LINAL
  49. LinAl::VectorCC<float> vf(farray,2*k);
  50. LinAl::MatrixCF<float> mf=vf*vf.transpose();
  51. #endif
  52. s = new StreamableClass(ed,el
  53. #ifdef NICE_USELIB_LINAL
  54. ,vf,mf
  55. #endif
  56. );
  57. }
  58. void TestStreamable::tearDown() {
  59. if(s!=NULL)
  60. delete s;
  61. //system("rm StreamableClass.*");
  62. }
  63. void TestStreamable::testConstructor() {
  64. }
  65. void TestStreamable::testOperators() {
  66. // Test Assignment Operator
  67. //CPPUNIT_ASSERT_EQUAL(x, y);
  68. }
  69. void TestStreamable::testASCII() {
  70. s->writeToFile("StreamableClass.txt");
  71. StreamableClass s_new;
  72. s_new.readFromFile("StreamableClass.txt");
  73. VectorT<double> evdiff=s->evdouble;
  74. evdiff-=s_new.evdouble;
  75. CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, evdiff.scalarProduct(evdiff), 1E-10);
  76. #ifdef NICE_USELIB_LINAL
  77. CPPUNIT_ASSERT_EQUAL(s->vcfloat, s_new.vcfloat);
  78. CPPUNIT_ASSERT_EQUAL(s->mcfloat, s_new.mcfloat);
  79. #endif
  80. CPPUNIT_ASSERT_EQUAL(s->evlong, s_new.evlong);
  81. remove("StreamableClass.txt");
  82. }
  83. void TestStreamable::testGZ() {
  84. #ifdef NICE_USELIB_ZLIB
  85. s->writeToFile("StreamableClass.txt.gz",Streamable::ASCII_GZ);
  86. StreamableClass s_new;
  87. s_new.readFromFile("StreamableClass.txt.gz",Streamable::ASCII_GZ);
  88. VectorT<double> evdiff=s->evdouble;
  89. evdiff-=s_new.evdouble;
  90. CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, evdiff.scalarProduct(evdiff), 1E-10);
  91. #ifdef NICE_USELIB_LINAL
  92. CPPUNIT_ASSERT_EQUAL(s->vcfloat, s_new.vcfloat);
  93. CPPUNIT_ASSERT_EQUAL(s->mcfloat, s_new.mcfloat);
  94. #endif
  95. CPPUNIT_ASSERT_EQUAL(s->evlong, s_new.evlong);
  96. remove("StreamableClass.txt.gz");
  97. #endif
  98. }
  99. void TestStreamable::testBIN() {
  100. #ifdef NICE_USELIB_ZLIB
  101. s->writeToBinFile("StreamableClass.bin");
  102. StreamableClass s_new;
  103. s_new.readFromBinFile("StreamableClass.bin");
  104. VectorT<double> evdiff=s->evdouble;
  105. evdiff-=s_new.evdouble;
  106. CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, evdiff.scalarProduct(evdiff), 1E-10);
  107. #ifdef NICE_USELIB_LINAL
  108. CPPUNIT_ASSERT_EQUAL(s->vcfloat, s_new.vcfloat);
  109. CPPUNIT_ASSERT_EQUAL(s->mcfloat, s_new.mcfloat);
  110. #endif
  111. CPPUNIT_ASSERT_EQUAL(s->evlong, s_new.evlong);
  112. remove("StreamableClass.bin");
  113. #endif
  114. }
  115. void TestStreamable::testGZBIN() {
  116. #ifdef NICE_USELIB_ZLIB
  117. s->writeToBinFile("StreamableClass.bin.gz",BinStreamable::BIN_GZ);
  118. StreamableClass s_new;
  119. s_new.readFromBinFile("StreamableClass.bin.gz",BinStreamable::BIN_GZ);
  120. VectorT<double> evdiff=s->evdouble;
  121. evdiff-=s_new.evdouble;
  122. CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, evdiff.scalarProduct(evdiff), 1E-10);
  123. #ifdef NICE_USELIB_LINAL
  124. CPPUNIT_ASSERT_EQUAL(s->vcfloat, s_new.vcfloat);
  125. CPPUNIT_ASSERT_EQUAL(s->mcfloat, s_new.mcfloat);
  126. #endif
  127. CPPUNIT_ASSERT_EQUAL(s->evlong, s_new.evlong);
  128. remove("StreamableClass.bin.gz");
  129. #endif
  130. }
  131. void TestStreamable::testBZ() {
  132. #ifdef NICE_USELIB_BZLIB
  133. s->writeToFile("StreamableClass.txt.bz2",Streamable::ASCII_BZ);
  134. StreamableClass s_new;
  135. s_new.readFromFile("StreamableClass.txt.bz2",Streamable::ASCII_BZ);
  136. VectorT<double> evdiff=s->evdouble;
  137. evdiff-=s_new.evdouble;
  138. CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, evdiff.scalarProduct(evdiff), 1E-10);
  139. #ifdef NICE_USELIB_LINAL
  140. CPPUNIT_ASSERT_EQUAL(s->vcfloat, s_new.vcfloat);
  141. CPPUNIT_ASSERT_EQUAL(s->mcfloat, s_new.mcfloat);
  142. #endif
  143. CPPUNIT_ASSERT_EQUAL(s->evlong, s_new.evlong);
  144. remove("StreamableClass.txt.bz2");
  145. #endif
  146. }
  147. StreamableClass::StreamableClass(VectorT<double> &ed, VectorT<long> &el
  148. #ifdef NICE_USELIB_LINAL
  149. ,const LinAl::VectorCC<float> &vf, const LinAl::MatrixCF<float> &mf
  150. #endif
  151. )
  152. {
  153. evdouble=ed;
  154. evlong=el;
  155. #ifdef NICE_USELIB_LINAL
  156. vcfloat=vf;
  157. mcfloat=mf;
  158. #endif
  159. }
  160. void StreamableClass::read(std::istream& strm)
  161. {
  162. strm>>evdouble>>evlong;
  163. #ifdef NICE_USELIB_LINAL
  164. ReadASCII(vcfloat,strm);
  165. ReadASCII(mcfloat,strm);
  166. #endif
  167. }
  168. void StreamableClass::write(std::ostream& strm) const
  169. {
  170. strm.precision(20);
  171. strm<<evdouble<<std::endl
  172. <<evlong<<std::endl;
  173. #ifdef NICE_USELIB_LINAL
  174. WriteASCII(vcfloat,strm);
  175. WriteASCII(mcfloat,strm);
  176. #endif
  177. }
  178. void StreamableClass::read(ibinstream& strm)
  179. {
  180. strm >>evdouble >>evlong
  181. #ifdef NICE_USELIB_LINAL
  182. >>vcfloat >>mcfloat
  183. #endif
  184. ;
  185. }
  186. void StreamableClass::write(obinstream& strm) const
  187. {
  188. strm <<evdouble <<evlong
  189. #ifdef NICE_USELIB_LINAL
  190. <<vcfloat <<mcfloat
  191. #endif
  192. ;
  193. }
  194. #endif