mem_ut.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "mem.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. Y_UNIT_TEST_SUITE(TestMemIO) {
  4. Y_UNIT_TEST(TestReadTo) {
  5. TString s("0123456789abc");
  6. TMemoryInput in(s);
  7. TString t;
  8. UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8);
  9. UNIT_ASSERT_VALUES_EQUAL(t, "0123456");
  10. UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5);
  11. UNIT_ASSERT_VALUES_EQUAL(t, "89abc");
  12. }
  13. Y_UNIT_TEST(NextAndUndo) {
  14. char buffer[20];
  15. TMemoryOutput output(buffer, sizeof(buffer));
  16. char* ptr = nullptr;
  17. size_t bufferSize = output.Next(&ptr);
  18. UNIT_ASSERT_GE(bufferSize, 1);
  19. *ptr = '1';
  20. if (bufferSize > 1) {
  21. output.Undo(bufferSize - 1);
  22. }
  23. bufferSize = output.Next(&ptr);
  24. UNIT_ASSERT_GE(bufferSize, 2);
  25. *ptr = '2';
  26. *(ptr + 1) = '2';
  27. if (bufferSize > 2) {
  28. output.Undo(bufferSize - 2);
  29. }
  30. bufferSize = output.Next(&ptr);
  31. UNIT_ASSERT_GE(bufferSize, 3);
  32. *ptr = '3';
  33. *(ptr + 1) = '3';
  34. *(ptr + 2) = '3';
  35. if (bufferSize > 3) {
  36. output.Undo(bufferSize - 3);
  37. }
  38. output.Finish();
  39. const char* const result = "1"
  40. "22"
  41. "333";
  42. UNIT_ASSERT(0 == memcmp(buffer, result, strlen(result)));
  43. }
  44. Y_UNIT_TEST(Write) {
  45. char buffer[20];
  46. TMemoryOutput output(buffer, sizeof(buffer));
  47. output << "1"
  48. << "22"
  49. << "333"
  50. << "4444"
  51. << "55555";
  52. const char* const result = "1"
  53. "22"
  54. "333"
  55. "4444"
  56. "55555";
  57. UNIT_ASSERT(0 == memcmp(buffer, result, strlen(result)));
  58. }
  59. Y_UNIT_TEST(WriteChars) {
  60. char buffer[20];
  61. TMemoryOutput output(buffer, sizeof(buffer));
  62. output << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9' << '0';
  63. const char* const result = "1234567890";
  64. UNIT_ASSERT(0 == memcmp(buffer, result, strlen(result)));
  65. }
  66. }