buffered_ut.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "buffered.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/string.h>
  4. #include <util/random/mersenne.h>
  5. Y_UNIT_TEST_SUITE(TestBufferedIO) {
  6. template <class TOut>
  7. inline void Run(TOut&& out) {
  8. TMersenne<ui64> r;
  9. for (size_t i = 0; i < 1000; ++i) {
  10. const size_t c = r.GenRand() % 10000;
  11. TString s;
  12. for (size_t j = 0; j < c; ++j) {
  13. s.append('A' + (r.GenRand() % 10));
  14. }
  15. out.Write(s.data(), s.size());
  16. }
  17. }
  18. Y_UNIT_TEST(TestEqual) {
  19. TString s1;
  20. TString s2;
  21. Run(TBuffered<TStringOutput>(8000, s1));
  22. Run(TAdaptivelyBuffered<TStringOutput>(s2));
  23. UNIT_ASSERT_VALUES_EQUAL(s1, s2);
  24. }
  25. Y_UNIT_TEST(Test1) {
  26. TString s;
  27. TBuffered<TStringOutput>(100, s).Write("1", 1);
  28. UNIT_ASSERT_VALUES_EQUAL(s, "1");
  29. }
  30. Y_UNIT_TEST(Test2) {
  31. TString s;
  32. TBuffered<TStringOutput>(1, s).Write("12", 2);
  33. UNIT_ASSERT_VALUES_EQUAL(s, "12");
  34. }
  35. Y_UNIT_TEST(Test3) {
  36. TString s;
  37. auto&& b = TBuffered<TStringOutput>(1, s);
  38. b.Write("1", 1);
  39. b.Write("12", 2);
  40. b.Finish();
  41. UNIT_ASSERT_VALUES_EQUAL(s, "112");
  42. }
  43. Y_UNIT_TEST(Test4) {
  44. TString s;
  45. auto&& b = TBuffered<TStringOutput>(1, s);
  46. b.Write('1');
  47. b.Write('2');
  48. b.Write('3');
  49. b.Finish();
  50. UNIT_ASSERT_VALUES_EQUAL(s, "123");
  51. }
  52. template <class TOut>
  53. inline void DoGenAndWrite(TOut&& output, TString& str) {
  54. TMersenne<ui64> r;
  55. for (size_t i = 0; i < 43210; ++i) {
  56. str.append('A' + (r.GenRand() % 10));
  57. }
  58. size_t written = 0;
  59. void* ptr = nullptr;
  60. while (written < str.size()) {
  61. size_t bufferSize = output.Next(&ptr);
  62. UNIT_ASSERT(ptr && bufferSize > 0);
  63. size_t toWrite = Min(bufferSize, str.size() - written);
  64. memcpy(ptr, str.begin() + written, toWrite);
  65. written += toWrite;
  66. if (toWrite < bufferSize) {
  67. output.Undo(bufferSize - toWrite);
  68. }
  69. }
  70. }
  71. Y_UNIT_TEST(TestWriteViaNextAndUndo) {
  72. TString str1, str2;
  73. DoGenAndWrite(TBuffered<TStringOutput>(5000, str1), str2);
  74. UNIT_ASSERT_STRINGS_EQUAL(str1, str2);
  75. }
  76. Y_UNIT_TEST(TestWriteViaNextAndUndoAdaptive) {
  77. TString str1, str2;
  78. DoGenAndWrite(TAdaptivelyBuffered<TStringOutput>(str1), str2);
  79. UNIT_ASSERT_STRINGS_EQUAL(str1, str2);
  80. }
  81. Y_UNIT_TEST(TestInput) {
  82. TString s("0123456789abcdefghijklmn");
  83. TBuffered<TStringInput> in(5, s);
  84. char c;
  85. UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); // 1
  86. UNIT_ASSERT_VALUES_EQUAL(c, '0');
  87. UNIT_ASSERT_VALUES_EQUAL(in.Skip(4), 4); // 5 end of buffer
  88. UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); // 6
  89. UNIT_ASSERT_VALUES_EQUAL(c, '5');
  90. UNIT_ASSERT_VALUES_EQUAL(in.Skip(3), 3); // 9
  91. UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); // 10 end of buffer
  92. UNIT_ASSERT_VALUES_EQUAL(c, '9');
  93. UNIT_ASSERT_VALUES_EQUAL(in.Skip(3), 3); // 13
  94. UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); // 14 start new buffer
  95. UNIT_ASSERT_VALUES_EQUAL(c, 'd');
  96. UNIT_ASSERT_VALUES_EQUAL(in.Skip(6), 6); // 20
  97. UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); // 21 start new buffer
  98. UNIT_ASSERT_VALUES_EQUAL(c, 'k');
  99. UNIT_ASSERT_VALUES_EQUAL(in.Skip(6), 3); // 24 eof
  100. }
  101. Y_UNIT_TEST(TestReadTo) {
  102. TString s("0123456789abc");
  103. TBuffered<TStringInput> in(2, s);
  104. TString t;
  105. UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8);
  106. UNIT_ASSERT_VALUES_EQUAL(t, "0123456");
  107. UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '8'), 1);
  108. UNIT_ASSERT_VALUES_EQUAL(t, "");
  109. UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 4);
  110. UNIT_ASSERT_VALUES_EQUAL(t, "9abc");
  111. }
  112. } // Y_UNIT_TEST_SUITE(TestBufferedIO)