blob_ut.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "blob.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/system/tempfile.h>
  4. #include <util/folder/path.h>
  5. #include <util/stream/output.h>
  6. #include <util/stream/file.h>
  7. #include <util/generic/buffer.h>
  8. #include <util/generic/array_ref.h>
  9. Y_UNIT_TEST_SUITE(TBlobTest) {
  10. Y_UNIT_TEST(TestSubBlob) {
  11. TBlob child;
  12. const char* p = nullptr;
  13. {
  14. TBlob parent = TBlob::CopySingleThreaded("0123456789", 10);
  15. UNIT_ASSERT_EQUAL(parent.Length(), 10);
  16. p = parent.AsCharPtr();
  17. UNIT_ASSERT_EQUAL(memcmp(p, "0123456789", 10), 0);
  18. child = parent.SubBlob(2, 5);
  19. } // Don't worry about parent
  20. UNIT_ASSERT_EQUAL(child.Length(), 3);
  21. UNIT_ASSERT_EQUAL(memcmp(child.AsCharPtr(), "234", 3), 0);
  22. UNIT_ASSERT_EQUAL(p + 2, child.AsCharPtr());
  23. }
  24. Y_UNIT_TEST(TestFromStream) {
  25. TString s("sjklfgsdyutfuyas54fa78s5f89a6df790asdf7");
  26. TMemoryInput mi(s.data(), s.size());
  27. TBlob b = TBlob::FromStreamSingleThreaded(mi);
  28. UNIT_ASSERT_EQUAL(TString((const char*)b.Data(), b.Length()), s);
  29. }
  30. Y_UNIT_TEST(TestFromString) {
  31. TString s("dsfkjhgsadftusadtf");
  32. TBlob b(TBlob::FromString(s));
  33. UNIT_ASSERT_EQUAL(TString((const char*)b.Data(), b.Size()), s);
  34. const auto expectedRef = TArrayRef<const ui8>{(ui8*)s.data(), s.size()};
  35. UNIT_ASSERT_EQUAL(TArrayRef<const ui8>{b}, expectedRef);
  36. }
  37. Y_UNIT_TEST(TestFromBuffer) {
  38. const size_t sz = 1234u;
  39. TBuffer buf;
  40. buf.Resize(sz);
  41. UNIT_ASSERT_EQUAL(buf.Size(), sz);
  42. TBlob b = TBlob::FromBuffer(buf);
  43. UNIT_ASSERT_EQUAL(buf.Size(), 0u);
  44. UNIT_ASSERT_EQUAL(b.Size(), sz);
  45. }
  46. Y_UNIT_TEST(TestFromFile) {
  47. TString path = "testfile";
  48. TOFStream stream(path);
  49. stream.Write("1234", 4);
  50. stream.Finish();
  51. auto testMode = [](TBlob blob) {
  52. UNIT_ASSERT_EQUAL(blob.Size(), 4);
  53. UNIT_ASSERT_EQUAL(TStringBuf(static_cast<const char*>(blob.Data()), 4), "1234");
  54. };
  55. testMode(TBlob::FromFile(path));
  56. testMode(TBlob::PrechargedFromFile(path));
  57. testMode(TBlob::LockedFromFile(path));
  58. }
  59. Y_UNIT_TEST(TestEmptyLockedFiles) {
  60. TString path = MakeTempName();
  61. TFsPath(path).Touch();
  62. TBlob::LockedFromFile(path);
  63. }
  64. }