diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/stream/ios_ut.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/stream/ios_ut.cpp')
-rw-r--r-- | util/stream/ios_ut.cpp | 468 |
1 files changed, 234 insertions, 234 deletions
diff --git a/util/stream/ios_ut.cpp b/util/stream/ios_ut.cpp index 139f4296e5..ca1dd45e7f 100644 --- a/util/stream/ios_ut.cpp +++ b/util/stream/ios_ut.cpp @@ -1,57 +1,57 @@ #include "output.h" -#include "tokenizer.h" +#include "tokenizer.h" #include "buffer.h" #include "buffered.h" #include "walk.h" - + #include <library/cpp/testing/unittest/registar.h> - -#include <util/string/cast.h> + +#include <util/string/cast.h> #include <util/memory/tempbuf.h> -#include <util/charset/wide.h> - -#include <string> -#include <iostream> - -class TStreamsTest: public TTestBase { - UNIT_TEST_SUITE(TStreamsTest); - UNIT_TEST(TestGenericRead); - UNIT_TEST(TestGenericWrite); - UNIT_TEST(TestReadLine); - UNIT_TEST(TestMemoryStream); - UNIT_TEST(TestBufferedIO); - UNIT_TEST(TestBufferStream); - UNIT_TEST(TestStringStream); - UNIT_TEST(TestWtrokaInput); +#include <util/charset/wide.h> + +#include <string> +#include <iostream> + +class TStreamsTest: public TTestBase { + UNIT_TEST_SUITE(TStreamsTest); + UNIT_TEST(TestGenericRead); + UNIT_TEST(TestGenericWrite); + UNIT_TEST(TestReadLine); + UNIT_TEST(TestMemoryStream); + UNIT_TEST(TestBufferedIO); + UNIT_TEST(TestBufferStream); + UNIT_TEST(TestStringStream); + UNIT_TEST(TestWtrokaInput); UNIT_TEST(TestStrokaInput); UNIT_TEST(TestReadTo); - UNIT_TEST(TestWtrokaOutput); - UNIT_TEST(TestIStreamOperators); + UNIT_TEST(TestWtrokaOutput); + UNIT_TEST(TestIStreamOperators); UNIT_TEST(TestWchar16Output); UNIT_TEST(TestWchar32Output); UNIT_TEST(TestUtf16StingOutputByChars); - UNIT_TEST_SUITE_END(); - -public: - void TestGenericRead(); - void TestGenericWrite(); - void TestReadLine(); - void TestMemoryStream(); - void TestBufferedIO(); - void TestBufferStream(); - void TestStringStream(); - void TestWtrokaInput(); + UNIT_TEST_SUITE_END(); + +public: + void TestGenericRead(); + void TestGenericWrite(); + void TestReadLine(); + void TestMemoryStream(); + void TestBufferedIO(); + void TestBufferStream(); + void TestStringStream(); + void TestWtrokaInput(); void TestStrokaInput(); - void TestWtrokaOutput(); - void TestIStreamOperators(); + void TestWtrokaOutput(); + void TestIStreamOperators(); void TestReadTo(); void TestWchar16Output(); void TestWchar32Output(); void TestUtf16StingOutputByChars(); -}; - -UNIT_TEST_SUITE_REGISTRATION(TStreamsTest); - +}; + +UNIT_TEST_SUITE_REGISTRATION(TStreamsTest); + void TStreamsTest::TestIStreamOperators() { TString data("first line\r\nsecond\t\xd1\x82\xd0\xb5\xd1\x81\xd1\x82 line\r\n 1 -4 59 4320000000009999999 c\n -1.5 1e-110"); TStringInput si(data); @@ -86,22 +86,22 @@ void TStreamsTest::TestIStreamOperators() { UNIT_ASSERT_EQUAL(f2, 1e-110); } -void TStreamsTest::TestStringStream() { - TStringStream s; - - s << "qw\r\n1234" - << "\n" - << 34; - - UNIT_ASSERT_EQUAL(s.ReadLine(), "qw"); - UNIT_ASSERT_EQUAL(s.ReadLine(), "1234"); - - s << "\r\n" - << 123.1; - - UNIT_ASSERT_EQUAL(s.ReadLine(), "34"); +void TStreamsTest::TestStringStream() { + TStringStream s; + + s << "qw\r\n1234" + << "\n" + << 34; + + UNIT_ASSERT_EQUAL(s.ReadLine(), "qw"); + UNIT_ASSERT_EQUAL(s.ReadLine(), "1234"); + + s << "\r\n" + << 123.1; + + UNIT_ASSERT_EQUAL(s.ReadLine(), "34"); UNIT_ASSERT_EQUAL(s.ReadLine(), "123.1"); - + UNIT_ASSERT_EQUAL(s.Str(), "qw\r\n1234\n34\r\n123.1"); // Test stream copying @@ -117,148 +117,148 @@ void TStreamsTest::TestStringStream() { ss = s; s << "... and some trash"; UNIT_ASSERT_EQUAL(ss.Str(), "qw\r\n1234\n34\r\n123.1-666-13"); -} - -void TStreamsTest::TestGenericRead() { +} + +void TStreamsTest::TestGenericRead() { TString s("1234567890"); - TStringInput si(s); - char buf[1024]; - - UNIT_ASSERT_EQUAL(si.Read(buf, 6), 6); - UNIT_ASSERT_EQUAL(memcmp(buf, "123456", 6), 0); - UNIT_ASSERT_EQUAL(si.Read(buf, 6), 4); - UNIT_ASSERT_EQUAL(memcmp(buf, "7890", 4), 0); -} - -void TStreamsTest::TestGenericWrite() { + TStringInput si(s); + char buf[1024]; + + UNIT_ASSERT_EQUAL(si.Read(buf, 6), 6); + UNIT_ASSERT_EQUAL(memcmp(buf, "123456", 6), 0); + UNIT_ASSERT_EQUAL(si.Read(buf, 6), 4); + UNIT_ASSERT_EQUAL(memcmp(buf, "7890", 4), 0); +} + +void TStreamsTest::TestGenericWrite() { TString s; - TStringOutput so(s); - - so.Write("123456", 6); - so.Write("7890", 4); - - UNIT_ASSERT_EQUAL(s, "1234567890"); -} - -void TStreamsTest::TestReadLine() { + TStringOutput so(s); + + so.Write("123456", 6); + so.Write("7890", 4); + + UNIT_ASSERT_EQUAL(s, "1234567890"); +} + +void TStreamsTest::TestReadLine() { TString data("1234\r\n5678\nqw"); - TStringInput si(data); - - UNIT_ASSERT_EQUAL(si.ReadLine(), "1234"); - UNIT_ASSERT_EQUAL(si.ReadLine(), "5678"); - UNIT_ASSERT_EQUAL(si.ReadLine(), "qw"); -} - -void TStreamsTest::TestMemoryStream() { - char buf[1024]; - TMemoryOutput mo(buf, sizeof(buf)); - bool ehandled = false; - - try { - for (size_t i = 0; i < sizeof(buf) + 1; ++i) { - mo.Write(i % 127); - } - } catch (...) { - ehandled = true; - } - - UNIT_ASSERT_EQUAL(ehandled, true); - - for (size_t i = 0; i < sizeof(buf); ++i) { - UNIT_ASSERT_EQUAL(buf[i], (char)(i % 127)); - } -} - + TStringInput si(data); + + UNIT_ASSERT_EQUAL(si.ReadLine(), "1234"); + UNIT_ASSERT_EQUAL(si.ReadLine(), "5678"); + UNIT_ASSERT_EQUAL(si.ReadLine(), "qw"); +} + +void TStreamsTest::TestMemoryStream() { + char buf[1024]; + TMemoryOutput mo(buf, sizeof(buf)); + bool ehandled = false; + + try { + for (size_t i = 0; i < sizeof(buf) + 1; ++i) { + mo.Write(i % 127); + } + } catch (...) { + ehandled = true; + } + + UNIT_ASSERT_EQUAL(ehandled, true); + + for (size_t i = 0; i < sizeof(buf); ++i) { + UNIT_ASSERT_EQUAL(buf[i], (char)(i % 127)); + } +} + class TMyStringOutput: public IOutputStream { -public: +public: inline TMyStringOutput(TString& s, size_t buflen) noexcept - : S_(s) - , BufLen_(buflen) - { - } - + : S_(s) + , BufLen_(buflen) + { + } + ~TMyStringOutput() override = default; - + void DoWrite(const void* data, size_t len) override { - S_.Write(data, len); - UNIT_ASSERT(len < BufLen_ || ((len % BufLen_) == 0)); - } - + S_.Write(data, len); + UNIT_ASSERT(len < BufLen_ || ((len % BufLen_) == 0)); + } + void DoWriteV(const TPart* p, size_t count) override { TString s; - - for (size_t i = 0; i < count; ++i) { - s.append((const char*)p[i].buf, p[i].len); - } - + + for (size_t i = 0; i < count; ++i) { + s.append((const char*)p[i].buf, p[i].len); + } + DoWrite(s.data(), s.size()); - } - -private: - TStringOutput S_; - const size_t BufLen_; -}; - -void TStreamsTest::TestBufferedIO() { + } + +private: + TStringOutput S_; + const size_t BufLen_; +}; + +void TStreamsTest::TestBufferedIO() { TString s; - - { - const size_t buflen = 7; - TBuffered<TMyStringOutput> bo(buflen, s, buflen); - - for (size_t i = 0; i < 1000; ++i) { + + { + const size_t buflen = 7; + TBuffered<TMyStringOutput> bo(buflen, s, buflen); + + for (size_t i = 0; i < 1000; ++i) { TString str(" "); - str += ToString(i % 10); - + str += ToString(i % 10); + bo.Write(str.data(), str.size()); - } - - bo.Finish(); - } - + } + + bo.Finish(); + } + UNIT_ASSERT_EQUAL(s.size(), 2000); - - { - const size_t buflen = 11; - TBuffered<TStringInput> bi(buflen, s); - - for (size_t i = 0; i < 1000; ++i) { + + { + const size_t buflen = 11; + TBuffered<TStringInput> bi(buflen, s); + + for (size_t i = 0; i < 1000; ++i) { TString str(" "); - str += ToString(i % 10); - - char buf[3]; - - UNIT_ASSERT_EQUAL(bi.Load(buf, 2), 2); - - buf[2] = 0; - - UNIT_ASSERT_EQUAL(str, buf); - } - } - - s.clear(); - - { - const size_t buflen = 13; - TBuffered<TMyStringOutput> bo(buflen, s, buflen); + str += ToString(i % 10); + + char buf[3]; + + UNIT_ASSERT_EQUAL(bi.Load(buf, 2), 2); + + buf[2] = 0; + + UNIT_ASSERT_EQUAL(str, buf); + } + } + + s.clear(); + + { + const size_t buflen = 13; + TBuffered<TMyStringOutput> bo(buflen, s, buflen); TString f = "1234567890"; - - for (size_t i = 0; i < 10; ++i) { - f += f; - } - - for (size_t i = 0; i < 1000; ++i) { + + for (size_t i = 0; i < 10; ++i) { + f += f; + } + + for (size_t i = 0; i < 1000; ++i) { bo.Write(f.data(), i); - } - - bo.Finish(); - } -} + } + + bo.Finish(); + } +} -void TStreamsTest::TestBufferStream() { +void TStreamsTest::TestBufferStream() { TBufferStream stream; TString s = "test"; - + stream.Write(s.data(), s.size()); char buf[5]; size_t bytesRead = stream.Read(buf, 4); @@ -269,7 +269,7 @@ void TStreamsTest::TestBufferStream() { bytesRead = stream.Read(buf, 2); UNIT_ASSERT_EQUAL(2, bytesRead); UNIT_ASSERT_EQUAL(0, strncmp("te", buf, 2)); - + bytesRead = stream.Read(buf, 2); UNIT_ASSERT_EQUAL(2, bytesRead); UNIT_ASSERT_EQUAL(0, strncmp("st", buf, 2)); @@ -330,70 +330,70 @@ namespace { "", "\rone two", "123", - "\t\r "}; + "\t\r "}; void TestStreamReadTo1(IInputStream& input, const char* comment) { TString tmp; - input.ReadTo(tmp, 'c'); - UNIT_ASSERT_VALUES_EQUAL_C(tmp, "111a222b333", comment); + input.ReadTo(tmp, 'c'); + UNIT_ASSERT_VALUES_EQUAL_C(tmp, "111a222b333", comment); - char tmp2; - input.Read(&tmp2, 1); - UNIT_ASSERT_VALUES_EQUAL_C(tmp2, '4', comment); + char tmp2; + input.Read(&tmp2, 1); + UNIT_ASSERT_VALUES_EQUAL_C(tmp2, '4', comment); - input.ReadTo(tmp, '6'); - UNIT_ASSERT_VALUES_EQUAL_C(tmp, "44d555e", comment); + input.ReadTo(tmp, '6'); + UNIT_ASSERT_VALUES_EQUAL_C(tmp, "44d555e", comment); - tmp = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL_C(tmp, "66f", comment); - } + tmp = input.ReadAll(); + UNIT_ASSERT_VALUES_EQUAL_C(tmp, "66f", comment); + } void TestStreamReadTo2(IInputStream& input, const char* comment) { TString s; - size_t i = 0; - while (input.ReadLine(s)) { - UNIT_ASSERT_C(i < Y_ARRAY_SIZE(Expected), comment); - UNIT_ASSERT_VALUES_EQUAL_C(s, Expected[i], comment); - ++i; + size_t i = 0; + while (input.ReadLine(s)) { + UNIT_ASSERT_C(i < Y_ARRAY_SIZE(Expected), comment); + UNIT_ASSERT_VALUES_EQUAL_C(s, Expected[i], comment); + ++i; } - } + } void TestStreamReadTo3(IInputStream& input, const char* comment) { - UNIT_ASSERT_VALUES_EQUAL_C(input.ReadLine(), "111a222b333c444d555e666f", comment); - } + UNIT_ASSERT_VALUES_EQUAL_C(input.ReadLine(), "111a222b333c444d555e666f", comment); + } void TestStreamReadTo4(IInputStream& input, const char* comment) { - UNIT_ASSERT_VALUES_EQUAL_C(input.ReadTo('\0'), "one", comment); - UNIT_ASSERT_VALUES_EQUAL_C(input.ReadTo('\0'), "two", comment); - UNIT_ASSERT_VALUES_EQUAL_C(input.ReadTo('\0'), "three", comment); - } + UNIT_ASSERT_VALUES_EQUAL_C(input.ReadTo('\0'), "one", comment); + UNIT_ASSERT_VALUES_EQUAL_C(input.ReadTo('\0'), "two", comment); + UNIT_ASSERT_VALUES_EQUAL_C(input.ReadTo('\0'), "three", comment); + } void TestStrokaInput(IInputStream& input, const char* comment) { TString line; - ui32 i = 0; - TInstant start = Now(); - while (input.ReadLine(line)) { - ++i; + ui32 i = 0; + TInstant start = Now(); + while (input.ReadLine(line)) { + ++i; } - Cout << comment << ":" << (Now() - start).SecondsFloat() << Endl; - UNIT_ASSERT_VALUES_EQUAL(i, 100000); - } + Cout << comment << ":" << (Now() - start).SecondsFloat() << Endl; + UNIT_ASSERT_VALUES_EQUAL(i, 100000); + } - template <class T> + template <class T> void TestStreamReadTo(const TString& text, T test) { - TStringInput is(text); - test(is, "TStringInput"); + TStringInput is(text); + test(is, "TStringInput"); TMemoryInput mi(text.data(), text.size()); - test(mi, "TMemoryInput"); + test(mi, "TMemoryInput"); TBuffer b(text.data(), text.size()); - TBufferInput bi(b); - test(bi, "TBufferInput"); - TStringInput slave(text); - TBufferedInput bdi(&slave); - test(bdi, "TBufferedInput"); + TBufferInput bi(b); + test(bi, "TBufferInput"); + TStringInput slave(text); + TBufferedInput bdi(&slave); + test(bdi, "TBufferedInput"); TVector<TString> lst(1, text); - TStringListInput sli(lst); + TStringListInput sli(lst); test(sli, "IWalkInput"); - } + } } void TStreamsTest::TestReadTo() { @@ -412,18 +412,18 @@ void TStreamsTest::TestStrokaInput() { s.append(d.data(), d.size()); s.append('\n'); } - TestStreamReadTo(s, ::TestStrokaInput); + TestStreamReadTo(s, ::TestStrokaInput); } void TStreamsTest::TestWtrokaInput() { const TString s(Text); TStringInput is(s); TUtf16String w; - size_t i = 0; + size_t i = 0; while (is.ReadLine(w)) { UNIT_ASSERT(i < Y_ARRAY_SIZE(Expected)); - UNIT_ASSERT_VALUES_EQUAL(w, UTF8ToWide(Expected[i])); + UNIT_ASSERT_VALUES_EQUAL(w, UTF8ToWide(Expected[i])); ++i; } @@ -438,14 +438,14 @@ void TStreamsTest::TestWtrokaOutput() { TUtf16String w = UTF8ToWide(Expected[i]); os << w; - - if (i == 1 || i == 5 || i == 8) { + + if (i == 1 || i == 5 || i == 8) { os << '\r'; - } - - if (i < n - 1) { + } + + if (i < n - 1) { os << '\n'; - } + } } UNIT_ASSERT(s == Text); @@ -455,28 +455,28 @@ void TStreamsTest::TestWchar16Output() { TString s; TStringOutput os(s); os << wchar16(97); // latin a - os << u'\u044E'; // cyrillic ю + os << u'\u044E'; // cyrillic ю os << u'я'; os << wchar16(0xD801); // high surrogate is printed as replacement character U+FFFD os << u'b'; - UNIT_ASSERT_VALUES_EQUAL(s, "aюя" - "\xEF\xBF\xBD" - "b"); + UNIT_ASSERT_VALUES_EQUAL(s, "aюя" + "\xEF\xBF\xBD" + "b"); } void TStreamsTest::TestWchar32Output() { TString s; TStringOutput os(s); os << wchar32(97); // latin a - os << U'\u044E'; // cyrillic ю + os << U'\u044E'; // cyrillic ю os << U'я'; os << U'\U0001F600'; // grinning face os << u'b'; - UNIT_ASSERT_VALUES_EQUAL(s, "aюя" - "\xF0\x9F\x98\x80" - "b"); + UNIT_ASSERT_VALUES_EQUAL(s, "aюя" + "\xF0\x9F\x98\x80" + "b"); } void TStreamsTest::TestUtf16StingOutputByChars() { |