zerocopy_output_ut.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "zerocopy_output.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/string.h>
  4. // This version of string output stream is written here only
  5. // for testing IZeroCopyOutput implementation of DoWrite.
  6. class TSimpleStringOutput: public IZeroCopyOutput {
  7. public:
  8. TSimpleStringOutput(TString& s) noexcept
  9. : S_(s)
  10. {
  11. }
  12. private:
  13. size_t DoNext(void** ptr) override {
  14. if (S_.size() == S_.capacity()) {
  15. S_.reserve(FastClp2(S_.capacity() + 1));
  16. }
  17. size_t previousSize = S_.size();
  18. S_.resize(S_.capacity());
  19. *ptr = S_.begin() + previousSize;
  20. return S_.size() - previousSize;
  21. }
  22. void DoUndo(size_t len) override {
  23. Y_ENSURE(len <= S_.size());
  24. S_.resize(S_.size() - len);
  25. }
  26. TString& S_;
  27. };
  28. Y_UNIT_TEST_SUITE(TestZerocopyOutput) {
  29. Y_UNIT_TEST(Write) {
  30. TString str;
  31. TSimpleStringOutput output(str);
  32. TString result;
  33. for (size_t i = 0; i < 1000; ++i) {
  34. result.push_back('a' + (i % 20));
  35. }
  36. output.Write(result.begin(), result.size());
  37. UNIT_ASSERT_STRINGS_EQUAL(str, result);
  38. }
  39. } // Y_UNIT_TEST_SUITE(TestZerocopyOutput)