PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test_streamutil.cxx

https://bitbucket.org/rdpate/kniht
C++ | 200 lines | 187 code | 13 blank | 0 comment | 24 complexity | a583efbe99b8d4b26fce941f6df78c80 MD5 | raw file
  1. #include "../kniht/streamutil.hxx"
  2. #include <cassert>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <string>
  7. void test_getline() {
  8. using namespace std;
  9. using namespace kniht;
  10. {
  11. istringstream in ("abc");
  12. string s = getline(in);
  13. assert(s == "abc");
  14. try {
  15. getline(in);
  16. assert(!"failed to throw");
  17. }
  18. catch (runtime_error&) {}
  19. }
  20. {
  21. istringstream in ("abc\n");
  22. string s = getline(in);
  23. assert(s == "abc");
  24. try {
  25. getline(in);
  26. assert(!"failed to throw");
  27. }
  28. catch (runtime_error&) {}
  29. }
  30. {
  31. istringstream in ("abc\ndef");
  32. string s = getline(in);
  33. assert(s == "abc");
  34. s = getline(in);
  35. assert(s == "def");
  36. try {
  37. getline(in);
  38. assert(!"failed to throw");
  39. }
  40. catch (runtime_error&) {}
  41. }
  42. }
  43. void test_limited_getline() {
  44. using namespace std;
  45. using kniht::getline;
  46. {
  47. istringstream in ("abc");
  48. string s = getline(in, 40, true);
  49. assert(s == "abc");
  50. assert(in);
  51. assert(in.eof());
  52. }
  53. {
  54. istringstream in ("abc");
  55. try {
  56. getline(in, 2);
  57. assert(false);
  58. }
  59. catch (runtime_error&) {}
  60. catch (...) { assert(false); }
  61. }
  62. {
  63. istringstream in ("\n");
  64. string s = getline(in, 0);
  65. assert(s == "");
  66. }
  67. {
  68. istringstream in ("");
  69. string s = getline(in, 0, true);
  70. assert(s == "");
  71. }
  72. {
  73. istringstream in ("a\n");
  74. string s = getline(in, 1);
  75. assert(s == "a");
  76. }
  77. {
  78. istringstream in ("abc\ndef\n");
  79. string s = getline(in, 40);
  80. assert(s == "abc");
  81. s = getline(in, 40);
  82. assert(s == "def");
  83. }
  84. }
  85. template<class T>
  86. std::string container_inserter_repr(T const& value) {
  87. using namespace kniht::container_inserters;
  88. std::ostringstream out;
  89. out << value;
  90. assert(out);
  91. std::string nrvo;
  92. out.str().swap(nrvo);
  93. return nrvo;
  94. }
  95. void test_container_inserters() {
  96. #define G(N) assert(container_inserter_repr(std::N<int>(3, 42)) == "[42, 42, 42]");
  97. G(vector)
  98. G(deque)
  99. G(list)
  100. #undef G
  101. {
  102. std::set<int> s;
  103. s.insert(3);
  104. s.insert(5);
  105. s.insert(5);
  106. s.insert(42);
  107. assert(container_inserter_repr(s) == "{3, 5, 42}");
  108. }
  109. {
  110. std::multiset<int> s;
  111. s.insert(3);
  112. s.insert(5);
  113. s.insert(5);
  114. s.insert(42);
  115. assert(container_inserter_repr(s) == "{3, 5, 5, 42}");
  116. }
  117. {
  118. std::map<int, int> m;
  119. m[3] = 42;
  120. m[5] = 5;
  121. assert(container_inserter_repr(m) == "{3: 42, 5: 5}");
  122. }
  123. {
  124. std::multimap<int, int> m;
  125. m.insert(std::make_pair(3, 42));
  126. m.insert(std::make_pair(5, 5));
  127. m.insert(std::make_pair(5, 42));
  128. assert(container_inserter_repr(m) == "{3: 42, 5: 5, 5: 42}");
  129. }
  130. {
  131. std::vector<std::vector<int> > v (3);
  132. v[0].push_back(1);
  133. v[1].push_back(1);
  134. v[1].push_back(2);
  135. assert(container_inserter_repr(v) == "[[1], [1, 2], []]");
  136. }
  137. }
  138. void test_write_escaped() {
  139. using namespace kniht;
  140. { std::ostringstream ss; write_escaped(ss, 0, 0); assert(ss); assert(ss.str() == "NULL"); }
  141. { std::ostringstream ss; write_escaped(ss, 0); assert(ss); assert(ss.str() == "NULL"); }
  142. #define T(in,out) { std::ostringstream ss; write_escaped(ss, (in)); assert(ss); std::cout << ss.str() << '\n'; assert(ss.str() == ("\"" out "\"")); }
  143. T("abc", "abc");
  144. T("\\", "\\\\");
  145. T("\"", "\\\"");
  146. T("\t", "\\t");
  147. T("\n", "\\n");
  148. T("\r", "\\r");
  149. T(std::string(1, '\0'), "\\x00");
  150. T(std::string(1, '\x0F'), "\\x0F");
  151. T(std::string(1, '\xF0'), "\\xF0");
  152. T(std::string(1, '\xFF'), "\\xFF");
  153. #undef T
  154. #if 0
  155. { std::wostringstream ss; write_escaped(ss, 0, 0); assert(ss); assert(ss.str() == L"NULL"); }
  156. #define T(in,out) { std::wostringstream ss; write_escaped(ss, (in)); assert(ss); std::wcout << ss.str() << '\n'; assert(ss.str() == (L"\"" out L"\"")); }
  157. T(L"abc", L"abc");
  158. T(L"\\", L"\\\\");
  159. T(L"\"", L"\\\"");
  160. T(L"\t", L"\\t");
  161. T(L"\n", L"\\n");
  162. T(L"\r", L"\\r");
  163. T(std::wstring(1, '\0'), L"\\x00");
  164. T(std::wstring(1, '\x0F'), L"\\x0F");
  165. T(std::wstring(1, '\xF0'), L"\\xF0");
  166. T(std::wstring(1, '\xFF'), L"\\xFF");
  167. #undef T
  168. #endif
  169. }
  170. void test_reopen() {
  171. using kniht::reopen;
  172. std::ifstream file(__FILE__);
  173. assert(file && file.is_open());
  174. reopen(file, __FILE__);
  175. assert(file && file.is_open());
  176. reopen(file, __FILE__, file.in);
  177. assert(file && file.is_open());
  178. std::cout << "test_reopen done.\n";
  179. }
  180. int main() {
  181. test_getline();
  182. test_limited_getline();
  183. test_container_inserters();
  184. test_write_escaped();
  185. test_reopen();
  186. return 0;
  187. }