direct_io_ut.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <util/generic/string.h>
  3. #include <util/generic/array_size.h>
  4. #include "buffered.h"
  5. #include "direct_io.h"
  6. Y_UNIT_TEST_SUITE(TDirectIOTests) {
  7. // Decrease numBufToWrite further if tests continue to time out
  8. static void Y_NO_INLINE Test(EOpenMode mode, size_t numBufToWrite) {
  9. const char TEMPLATE[] = "qwertyuiopQWERTYUIOPasdfghjklASD";
  10. const auto TEMPLATE_SIZE = Y_ARRAY_SIZE(TEMPLATE) - 1;
  11. static_assert(TEMPLATE_SIZE > 0, "must be greater than zero");
  12. const size_t BUFFER_SIZE = 32 * 1024;
  13. static_assert(0 == BUFFER_SIZE % TEMPLATE_SIZE, "must be divisible");
  14. const size_t CHUNK_SIZE_TO_READ = 512;
  15. static_assert(0 == CHUNK_SIZE_TO_READ % TEMPLATE_SIZE, "must be divisible");
  16. // filling buffer
  17. // TEMPLATE|TEMPLATE|TEMPLATE|...
  18. auto&& buffer = TBuffer{BUFFER_SIZE};
  19. for (size_t i = 0; i < BUFFER_SIZE / TEMPLATE_SIZE; ++i) {
  20. buffer.Append(TEMPLATE, TEMPLATE_SIZE);
  21. }
  22. // filling file
  23. // TEMPLATE|TEMPLATE|TEMPLATE|...
  24. const auto fileName = TString("test.file");
  25. auto&& directIOBuffer = TDirectIOBufferedFile{fileName, RdWr | CreateAlways | mode};
  26. {
  27. auto&& output = TRandomAccessFileOutput{directIOBuffer};
  28. for (size_t i = 0; i < numBufToWrite; ++i) {
  29. output.Write(buffer.Data(), BUFFER_SIZE);
  30. }
  31. }
  32. auto&& reader = TRandomAccessFileInput{directIOBuffer, 0};
  33. auto&& input = TBufferedInput{&reader, 1 << 17};
  34. auto bytesRead = size_t{};
  35. while (auto len = input.Read(buffer.Data(), CHUNK_SIZE_TO_READ)) {
  36. bytesRead += len;
  37. while (len) {
  38. if (len < TEMPLATE_SIZE) {
  39. UNIT_ASSERT(!memcmp(buffer.Data(), TEMPLATE, len));
  40. len = 0;
  41. } else {
  42. UNIT_ASSERT(!memcmp(buffer.Data(), TEMPLATE, TEMPLATE_SIZE));
  43. len -= TEMPLATE_SIZE;
  44. }
  45. }
  46. }
  47. UNIT_ASSERT_VALUES_EQUAL(bytesRead, numBufToWrite * BUFFER_SIZE);
  48. }
  49. Y_UNIT_TEST(ReadWriteTest) {
  50. Test(0, 100 * 32);
  51. }
  52. Y_UNIT_TEST(ReadWriteDirectTest) {
  53. Test(Direct, 100 * 4);
  54. }
  55. Y_UNIT_TEST(ReadWriteDirectSeqTest) {
  56. Test(Direct | Seq, 100 * 4);
  57. }
  58. } // Y_UNIT_TEST_SUITE(TDirectIOTests)