Testbinstream.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "Testbinstream.h"
  8. #include <string>
  9. #include <vector>
  10. #include <exception>
  11. #include <core/basics/binstream.h>
  12. #include <core/basics/gzbinstream.h>
  13. using namespace NICE;
  14. CPPUNIT_TEST_SUITE_REGISTRATION( Testbinstream );
  15. void Testbinstream::setUp() {
  16. }
  17. void Testbinstream::tearDown() {
  18. remove("tmp.bin");
  19. }
  20. void Testbinstream::testOperators() {
  21. int i= rand();
  22. char c= rand();
  23. short s = rand();
  24. long l = rand();
  25. unsigned int ui = rand();
  26. unsigned short us = rand();
  27. unsigned char uc = rand();
  28. unsigned long ul = rand();
  29. float f = rand();
  30. double d = rand();
  31. long long ll = rand();
  32. unsigned long long ull = rand();
  33. long double ld = rand();
  34. std::string str="string";
  35. ofbinstream file("tmp.bin");
  36. char array[]="array";
  37. std::vector<int> v(3);
  38. v[0] = 10;
  39. v[1] = 20;
  40. v[2] = 30;
  41. (NICE::obinstream&)file
  42. << 'x' << i << 'x' << c << 'x' << s << 'x' << l << 'x' << ll
  43. << 'x' << f << 'x' << d << 'x' << ld
  44. << 'x' << ui << 'x' << uc << 'x' << us << 'x' << ul << 'x' << ull
  45. << "array" << 'x' << str << v;
  46. file.close();
  47. int i_new= 0;
  48. char c_new= 0;
  49. short s_new= 0;
  50. long l_new= 0;
  51. unsigned int ui_new= 0;
  52. unsigned short us_new= 0;
  53. unsigned char uc_new= 0;
  54. unsigned long ul_new= 0;
  55. float f_new= 0;
  56. double d_new= 0;
  57. long long ll_new= 0;
  58. unsigned long long ull_new= 0;
  59. long double ld_new= 0;
  60. std::string str_new;
  61. std::vector<int> v_new;
  62. char t[14];
  63. char buf[10];
  64. ifbinstream in("tmp.bin");
  65. (ibinstream&)in
  66. >> t[0] >> i_new >> t[1] >> c_new >> t[2] >> s_new >> t[3] >> l_new >> t[4] >> ll_new
  67. >> t[5] >> f_new >> t[6] >> d_new >> t[7] >> ld_new
  68. >> t[8] >> ui_new >> t[9] >> uc_new >> t[10] >> us_new >> t[11] >> ul_new >> t[12] >> ull_new
  69. >> buf >> t[13] >> str_new >> v_new;
  70. in.close();
  71. CPPUNIT_ASSERT_EQUAL(i, i_new);
  72. CPPUNIT_ASSERT_EQUAL(c, c_new);
  73. CPPUNIT_ASSERT_EQUAL(s, s_new);
  74. CPPUNIT_ASSERT_EQUAL(l, l_new);
  75. CPPUNIT_ASSERT_EQUAL(ll, ll_new);
  76. CPPUNIT_ASSERT_EQUAL(ui, ui_new);
  77. CPPUNIT_ASSERT_EQUAL(uc, uc_new);
  78. CPPUNIT_ASSERT_EQUAL(us, us_new);
  79. CPPUNIT_ASSERT_EQUAL(ul, ul_new);
  80. CPPUNIT_ASSERT_EQUAL(ull, ull_new);
  81. CPPUNIT_ASSERT_EQUAL(f, f_new);
  82. CPPUNIT_ASSERT_EQUAL(d, d_new);
  83. CPPUNIT_ASSERT_EQUAL(ld, ld_new);
  84. CPPUNIT_ASSERT_EQUAL(str, str_new);
  85. for(uint i=0;i<strlen(array);i++) {
  86. CPPUNIT_ASSERT_EQUAL(buf[i], array[i]);
  87. }
  88. for(uint i=0;i<v.size();i++) {
  89. CPPUNIT_ASSERT_EQUAL(v[i], v_new[i]);
  90. }
  91. for(int i=0;i<14;i++) {
  92. CPPUNIT_ASSERT_EQUAL(t[i], 'x');
  93. }
  94. }
  95. void Testbinstream::testOperatorsGz() {
  96. int i= rand();
  97. char c= rand();
  98. short s = rand();
  99. long l = rand();
  100. unsigned int ui = rand();
  101. unsigned short us = rand();
  102. unsigned char uc = rand();
  103. unsigned long ul = rand();
  104. float f = rand();
  105. double d = rand();
  106. long long ll = rand();
  107. unsigned long long ull = rand();
  108. long double ld = rand();
  109. std::string str="string";
  110. ogzbinstream file("tmp.bin");
  111. char array[]="array";
  112. std::vector<int> v(3);
  113. v[0] = 10;
  114. v[1] = 20;
  115. v[2] = 30;
  116. (NICE::obinstream&)file
  117. << 'x' << i << 'x' << c << 'x' << s << 'x' << l << 'x' << ll
  118. << 'x' << f << 'x' << d << 'x' << ld
  119. << 'x' << ui << 'x' << uc << 'x' << us << 'x' << ul << 'x' << ull
  120. << "array" << 'x' << str << v;
  121. file.close();
  122. int i_new= 0;
  123. char c_new= 0;
  124. short s_new= 0;
  125. long l_new= 0;
  126. unsigned int ui_new= 0;
  127. unsigned short us_new= 0;
  128. unsigned char uc_new= 0;
  129. unsigned long ul_new= 0;
  130. float f_new= 0;
  131. double d_new= 0;
  132. long long ll_new= 0;
  133. unsigned long long ull_new= 0;
  134. long double ld_new= 0;
  135. std::string str_new;
  136. std::vector<int> v_new;
  137. char t[14];
  138. char buf[10];
  139. igzbinstream in("tmp.bin");
  140. (ibinstream&)in
  141. >> t[0] >> i_new >> t[1] >> c_new >> t[2] >> s_new >> t[3] >> l_new >> t[4] >> ll_new
  142. >> t[5] >> f_new >> t[6] >> d_new >> t[7] >> ld_new
  143. >> t[8] >> ui_new >> t[9] >> uc_new >> t[10] >> us_new >> t[11] >> ul_new >> t[12] >> ull_new
  144. >> buf >> t[13] >> str_new >> v_new;
  145. in.close();
  146. CPPUNIT_ASSERT_EQUAL(i, i_new);
  147. CPPUNIT_ASSERT_EQUAL(c, c_new);
  148. CPPUNIT_ASSERT_EQUAL(s, s_new);
  149. CPPUNIT_ASSERT_EQUAL(l, l_new);
  150. CPPUNIT_ASSERT_EQUAL(ll, ll_new);
  151. CPPUNIT_ASSERT_EQUAL(ui, ui_new);
  152. CPPUNIT_ASSERT_EQUAL(uc, uc_new);
  153. CPPUNIT_ASSERT_EQUAL(us, us_new);
  154. CPPUNIT_ASSERT_EQUAL(ul, ul_new);
  155. CPPUNIT_ASSERT_EQUAL(ull, ull_new);
  156. CPPUNIT_ASSERT_EQUAL(f, f_new);
  157. CPPUNIT_ASSERT_EQUAL(d, d_new);
  158. CPPUNIT_ASSERT_EQUAL(ld, ld_new);
  159. CPPUNIT_ASSERT_EQUAL(str, str_new);
  160. for(uint i=0;i<strlen(array);i++) {
  161. CPPUNIT_ASSERT_EQUAL(buf[i], array[i]);
  162. }
  163. for(uint i=0;i<v.size();i++) {
  164. CPPUNIT_ASSERT_EQUAL(v[i], v_new[i]);
  165. }
  166. for(int i=0;i<14;i++) {
  167. CPPUNIT_ASSERT_EQUAL(t[i], 'x');
  168. }
  169. }
  170. #endif