/tests/test_streamutil.cxx
C++ | 200 lines | 187 code | 13 blank | 0 comment | 24 complexity | a583efbe99b8d4b26fce941f6df78c80 MD5 | raw file
- #include "../kniht/streamutil.hxx"
- #include <cassert>
- #include <fstream>
- #include <iostream>
- #include <sstream>
- #include <string>
- void test_getline() {
- using namespace std;
- using namespace kniht;
- {
- istringstream in ("abc");
- string s = getline(in);
- assert(s == "abc");
- try {
- getline(in);
- assert(!"failed to throw");
- }
- catch (runtime_error&) {}
- }
- {
- istringstream in ("abc\n");
- string s = getline(in);
- assert(s == "abc");
- try {
- getline(in);
- assert(!"failed to throw");
- }
- catch (runtime_error&) {}
- }
- {
- istringstream in ("abc\ndef");
- string s = getline(in);
- assert(s == "abc");
- s = getline(in);
- assert(s == "def");
- try {
- getline(in);
- assert(!"failed to throw");
- }
- catch (runtime_error&) {}
- }
- }
- void test_limited_getline() {
- using namespace std;
- using kniht::getline;
- {
- istringstream in ("abc");
- string s = getline(in, 40, true);
- assert(s == "abc");
- assert(in);
- assert(in.eof());
- }
- {
- istringstream in ("abc");
- try {
- getline(in, 2);
- assert(false);
- }
- catch (runtime_error&) {}
- catch (...) { assert(false); }
- }
- {
- istringstream in ("\n");
- string s = getline(in, 0);
- assert(s == "");
- }
- {
- istringstream in ("");
- string s = getline(in, 0, true);
- assert(s == "");
- }
- {
- istringstream in ("a\n");
- string s = getline(in, 1);
- assert(s == "a");
- }
- {
- istringstream in ("abc\ndef\n");
- string s = getline(in, 40);
- assert(s == "abc");
- s = getline(in, 40);
- assert(s == "def");
- }
- }
- template<class T>
- std::string container_inserter_repr(T const& value) {
- using namespace kniht::container_inserters;
- std::ostringstream out;
- out << value;
- assert(out);
- std::string nrvo;
- out.str().swap(nrvo);
- return nrvo;
- }
- void test_container_inserters() {
- #define G(N) assert(container_inserter_repr(std::N<int>(3, 42)) == "[42, 42, 42]");
- G(vector)
- G(deque)
- G(list)
- #undef G
- {
- std::set<int> s;
- s.insert(3);
- s.insert(5);
- s.insert(5);
- s.insert(42);
- assert(container_inserter_repr(s) == "{3, 5, 42}");
- }
- {
- std::multiset<int> s;
- s.insert(3);
- s.insert(5);
- s.insert(5);
- s.insert(42);
- assert(container_inserter_repr(s) == "{3, 5, 5, 42}");
- }
- {
- std::map<int, int> m;
- m[3] = 42;
- m[5] = 5;
- assert(container_inserter_repr(m) == "{3: 42, 5: 5}");
- }
- {
- std::multimap<int, int> m;
- m.insert(std::make_pair(3, 42));
- m.insert(std::make_pair(5, 5));
- m.insert(std::make_pair(5, 42));
- assert(container_inserter_repr(m) == "{3: 42, 5: 5, 5: 42}");
- }
- {
- std::vector<std::vector<int> > v (3);
- v[0].push_back(1);
- v[1].push_back(1);
- v[1].push_back(2);
- assert(container_inserter_repr(v) == "[[1], [1, 2], []]");
- }
- }
- void test_write_escaped() {
- using namespace kniht;
- { std::ostringstream ss; write_escaped(ss, 0, 0); assert(ss); assert(ss.str() == "NULL"); }
- { std::ostringstream ss; write_escaped(ss, 0); assert(ss); assert(ss.str() == "NULL"); }
- #define T(in,out) { std::ostringstream ss; write_escaped(ss, (in)); assert(ss); std::cout << ss.str() << '\n'; assert(ss.str() == ("\"" out "\"")); }
- T("abc", "abc");
- T("\\", "\\\\");
- T("\"", "\\\"");
- T("\t", "\\t");
- T("\n", "\\n");
- T("\r", "\\r");
- T(std::string(1, '\0'), "\\x00");
- T(std::string(1, '\x0F'), "\\x0F");
- T(std::string(1, '\xF0'), "\\xF0");
- T(std::string(1, '\xFF'), "\\xFF");
- #undef T
- #if 0
- { std::wostringstream ss; write_escaped(ss, 0, 0); assert(ss); assert(ss.str() == L"NULL"); }
- #define T(in,out) { std::wostringstream ss; write_escaped(ss, (in)); assert(ss); std::wcout << ss.str() << '\n'; assert(ss.str() == (L"\"" out L"\"")); }
- T(L"abc", L"abc");
- T(L"\\", L"\\\\");
- T(L"\"", L"\\\"");
- T(L"\t", L"\\t");
- T(L"\n", L"\\n");
- T(L"\r", L"\\r");
- T(std::wstring(1, '\0'), L"\\x00");
- T(std::wstring(1, '\x0F'), L"\\x0F");
- T(std::wstring(1, '\xF0'), L"\\xF0");
- T(std::wstring(1, '\xFF'), L"\\xFF");
- #undef T
- #endif
- }
- void test_reopen() {
- using kniht::reopen;
- std::ifstream file(__FILE__);
- assert(file && file.is_open());
- reopen(file, __FILE__);
- assert(file && file.is_open());
- reopen(file, __FILE__, file.in);
- assert(file && file.is_open());
- std::cout << "test_reopen done.\n";
- }
- int main() {
- test_getline();
- test_limited_getline();
- test_container_inserters();
- test_write_escaped();
- test_reopen();
- return 0;
- }