PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/cpp/tests/test_streamutil.cpp

https://bitbucket.org/kniht/scraps
C++ | 155 lines | 143 code | 12 blank | 0 comment | 18 complexity | 4adeca52b3dd054ca824b154a869e333 MD5 | raw file
  1. #include "../kniht/streamutil.hpp"
  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. template<class T>
  44. std::string container_inserter_repr(T const& value) {
  45. using namespace kniht::container_inserters;
  46. std::ostringstream out;
  47. out << value;
  48. assert(out);
  49. std::string nrvo;
  50. out.str().swap(nrvo);
  51. return nrvo;
  52. }
  53. void test_container_inserters() {
  54. #define G(N) assert(container_inserter_repr(std::N<int>(3, 42)) == "[42, 42, 42]");
  55. G(vector)
  56. G(deque)
  57. G(list)
  58. #undef G
  59. {
  60. std::set<int> s;
  61. s.insert(3);
  62. s.insert(5);
  63. s.insert(5);
  64. s.insert(42);
  65. assert(container_inserter_repr(s) == "{3, 5, 42}");
  66. }
  67. {
  68. std::multiset<int> s;
  69. s.insert(3);
  70. s.insert(5);
  71. s.insert(5);
  72. s.insert(42);
  73. assert(container_inserter_repr(s) == "{3, 5, 5, 42}");
  74. }
  75. {
  76. std::map<int, int> m;
  77. m[3] = 42;
  78. m[5] = 5;
  79. assert(container_inserter_repr(m) == "{3: 42, 5: 5}");
  80. }
  81. {
  82. std::multimap<int, int> m;
  83. m.insert(std::make_pair(3, 42));
  84. m.insert(std::make_pair(5, 5));
  85. m.insert(std::make_pair(5, 42));
  86. assert(container_inserter_repr(m) == "{3: 42, 5: 5, 5: 42}");
  87. }
  88. {
  89. std::vector<std::vector<int> > v (3);
  90. v[0].push_back(1);
  91. v[1].push_back(1);
  92. v[1].push_back(2);
  93. assert(container_inserter_repr(v) == "[[1], [1, 2], []]");
  94. }
  95. }
  96. void test_write_escaped() {
  97. using namespace kniht;
  98. { std::ostringstream ss; write_escaped(ss, 0, 0); assert(ss); assert(ss.str() == "NULL"); }
  99. { std::ostringstream ss; write_escaped(ss, 0); assert(ss); assert(ss.str() == "NULL"); }
  100. #define T(in,out) { std::ostringstream ss; write_escaped(ss, (in)); assert(ss); std::cout << ss.str() << '\n'; assert(ss.str() == ("\"" out "\"")); }
  101. T("abc", "abc");
  102. T("\\", "\\\\");
  103. T("\"", "\\\"");
  104. T("\t", "\\t");
  105. T("\n", "\\n");
  106. T("\r", "\\r");
  107. T(std::string(1, '\0'), "\\x00");
  108. T(std::string(1, '\x0F'), "\\x0F");
  109. T(std::string(1, '\xF0'), "\\xF0");
  110. T(std::string(1, '\xFF'), "\\xFF");
  111. #undef T
  112. #if 0
  113. { std::wostringstream ss; write_escaped(ss, 0, 0); assert(ss); assert(ss.str() == L"NULL"); }
  114. #define T(in,out) { std::wostringstream ss; write_escaped(ss, (in)); assert(ss); std::wcout << ss.str() << '\n'; assert(ss.str() == (L"\"" out L"\"")); }
  115. T(L"abc", L"abc");
  116. T(L"\\", L"\\\\");
  117. T(L"\"", L"\\\"");
  118. T(L"\t", L"\\t");
  119. T(L"\n", L"\\n");
  120. T(L"\r", L"\\r");
  121. T(std::wstring(1, '\0'), L"\\x00");
  122. T(std::wstring(1, '\x0F'), L"\\x0F");
  123. T(std::wstring(1, '\xF0'), L"\\xF0");
  124. T(std::wstring(1, '\xFF'), L"\\xFF");
  125. #undef T
  126. #endif
  127. }
  128. void test_reopen() {
  129. using kniht::reopen;
  130. std::ifstream file(__FILE__);
  131. assert(file && file.is_open());
  132. reopen(file, __FILE__);
  133. assert(file && file.is_open());
  134. reopen(file, __FILE__, file.in);
  135. assert(file && file.is_open());
  136. std::cout << "test_reopen done.\n";
  137. }
  138. int main() {
  139. test_getline();
  140. test_container_inserters();
  141. test_write_escaped();
  142. test_reopen();
  143. }