- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
#define BOOST_TEST_MODULE trololo
#include <boost/test/included/unit_test.hpp>
using namespace std;
std::string gen(size_t l, int s = 1, int r = 10/*'\253'*/)
{
std::string res;
res.reserve(l);
for (size_t i = 0; i < l; ++i)
res.push_back(/*'\1'*/'0' + (char)((i + s) % r));
return res;
}
std::string readl(std::istream& st, size_t l, bool human = false)
{
std::string res(l, '\0');
st.read((char*)res.data(), l);
res.resize((size_t)(st.gcount()));
if (human)
return "'" + res + "'";
else
return res;
}
void sp(std::iostream& st, size_t pos)
{
st.seekp(pos);
st.seekg(pos);
}
std::shared_ptr<std::iostream> makeStream(size_t bufSize = 8)
{
(void)bufSize;
return std::make_shared<std::stringstream> (std::ios::in | std::ios::out | std::ios::binary);//comment it
auto r = std::make_shared<std::fstream>();
r->open("test.txt", std::fstream::in | std::fstream::out | std::fstream::binary | std::fstream::trunc);
return r;
}
BOOST_AUTO_TEST_CASE(cross)
{
auto ss_=makeStream();
auto& ss = *ss_;
ss << gen(11);
BOOST_REQUIRE((readl(ss, 11) == gen(11)));
ss << gen(11);
sp(ss, 0);
BOOST_REQUIRE((readl(ss, 11) == gen(11)));
sp(ss, 11);
BOOST_REQUIRE((readl(ss, 11) == gen(11, 11)));
BOOST_REQUIRE((readl(ss, 1) == std::string()));
BOOST_REQUIRE(ss.eof());
ss.clear();
BOOST_REQUIRE(!ss.eof());
sp(ss, 0);
BOOST_REQUIRE((readl(ss, 0) == std::string()));
BOOST_REQUIRE(!ss.eof());
ss << std::string();
BOOST_REQUIRE(!ss.eof());
BOOST_REQUIRE((readl(ss, 0) == std::string()));
sp(ss, 0);
BOOST_REQUIRE((readl(ss, 100500) == gen(11) + gen(11)));
}
BOOST_AUTO_TEST_CASE(full)
{
auto ss_ = makeStream();
auto& ss = *ss_;
ss << gen(8);
BOOST_REQUIRE((readl(ss, 8) == gen(8)));
ss << gen(11);
BOOST_REQUIRE((readl(ss, 11) == gen(11)));
sp(ss, 0);
BOOST_REQUIRE((readl(ss, 100500) == gen(8) + gen(11)));
}
BOOST_AUTO_TEST_CASE(parts)
{
auto ss_ = makeStream();
auto& ss = *ss_;
ss << gen(6);
BOOST_REQUIRE((readl(ss, 6) == gen(6)));
ss << gen(8);
BOOST_REQUIRE((readl(ss, 8) == gen(8)));
ss << gen(8);
BOOST_REQUIRE((readl(ss, 8) == gen(8)));
sp(ss, 0);
BOOST_REQUIRE((readl(ss, 100500) == gen(6) + gen(8) + gen(8)));
}
BOOST_AUTO_TEST_CASE(seek)
{
auto ss_ = makeStream();
auto& ss = *ss_;