buffer_ut.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "buffer.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/buffer.h>
  4. #include <cstring>
  5. #include "str.h"
  6. Y_UNIT_TEST_SUITE(TBufferTest) {
  7. Y_UNIT_TEST(Transfer) {
  8. TBuffer buffer("razrazraz", 9);
  9. TBufferInput input(buffer);
  10. input.Skip(3);
  11. TStringStream output;
  12. TransferData(&input, &output);
  13. UNIT_ASSERT_VALUES_EQUAL(output.Str(), "razraz");
  14. }
  15. Y_UNIT_TEST(ReadTo) {
  16. TBuffer buffer("1234567890", 10);
  17. TBufferInput input(buffer);
  18. TString tmp;
  19. UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, '3'), 3);
  20. UNIT_ASSERT_VALUES_EQUAL(tmp, "12");
  21. UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, 'z'), 7);
  22. UNIT_ASSERT_VALUES_EQUAL(tmp, "4567890");
  23. }
  24. Y_UNIT_TEST(WriteViaNextAndUndo) {
  25. TBuffer buffer;
  26. TBufferOutput output(buffer);
  27. TString str;
  28. for (size_t i = 0; i < 10000; ++i) {
  29. str.push_back('a' + (i % 20));
  30. }
  31. size_t written = 0;
  32. void* ptr = nullptr;
  33. while (written < str.size()) {
  34. size_t bufferSize = output.Next(&ptr);
  35. UNIT_ASSERT(ptr && bufferSize > 0);
  36. size_t toWrite = Min(bufferSize, str.size() - written);
  37. memcpy(ptr, str.begin() + written, toWrite);
  38. written += toWrite;
  39. if (toWrite < bufferSize) {
  40. output.Undo(bufferSize - toWrite);
  41. }
  42. }
  43. UNIT_ASSERT(0 == memcmp(buffer.data(), str.begin(), buffer.size()));
  44. }
  45. Y_UNIT_TEST(Write) {
  46. TBuffer buffer;
  47. TBufferOutput output(buffer);
  48. output << "1"
  49. << "22"
  50. << "333"
  51. << "4444"
  52. << "55555";
  53. UNIT_ASSERT(0 == memcmp(buffer.data(), "1"
  54. "22"
  55. "333"
  56. "4444"
  57. "55555",
  58. buffer.size()));
  59. }
  60. Y_UNIT_TEST(WriteChars) {
  61. TBuffer buffer;
  62. TBufferOutput output(buffer);
  63. output << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9' << '0';
  64. UNIT_ASSERT(0 == memcmp(buffer.data(), "1234567890", buffer.size()));
  65. }
  66. }