diff options
author | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/memory/blob_ut.cpp |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/memory/blob_ut.cpp')
-rw-r--r-- | util/memory/blob_ut.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/util/memory/blob_ut.cpp b/util/memory/blob_ut.cpp new file mode 100644 index 00000000000..023f9a04871 --- /dev/null +++ b/util/memory/blob_ut.cpp @@ -0,0 +1,79 @@ +#include "blob.h" + +#include <library/cpp/testing/unittest/registar.h> + +#include <util/system/tempfile.h> +#include <util/folder/path.h> +#include <util/stream/output.h> +#include <util/stream/file.h> +#include <util/generic/buffer.h> +#include <util/generic/array_ref.h> + +Y_UNIT_TEST_SUITE(TBlobTest) { + Y_UNIT_TEST(TestSubBlob) { + TBlob child; + const char* p = nullptr; + + { + TBlob parent = TBlob::CopySingleThreaded("0123456789", 10); + UNIT_ASSERT_EQUAL(parent.Length(), 10); + p = parent.AsCharPtr(); + UNIT_ASSERT_EQUAL(memcmp(p, "0123456789", 10), 0); + child = parent.SubBlob(2, 5); + } // Don't worry about parent + + UNIT_ASSERT_EQUAL(child.Length(), 3); + UNIT_ASSERT_EQUAL(memcmp(child.AsCharPtr(), "234", 3), 0); + UNIT_ASSERT_EQUAL(p + 2, child.AsCharPtr()); + } + + Y_UNIT_TEST(TestFromStream) { + TString s("sjklfgsdyutfuyas54fa78s5f89a6df790asdf7"); + TMemoryInput mi(s.data(), s.size()); + TBlob b = TBlob::FromStreamSingleThreaded(mi); + + UNIT_ASSERT_EQUAL(TString((const char*)b.Data(), b.Length()), s); + } + + Y_UNIT_TEST(TestFromString) { + TString s("dsfkjhgsadftusadtf"); + TBlob b(TBlob::FromString(s)); + + UNIT_ASSERT_EQUAL(TString((const char*)b.Data(), b.Size()), s); + const auto expectedRef = TArrayRef<const ui8>{(ui8*)s.data(), s.size()}; + UNIT_ASSERT_EQUAL(TArrayRef<const ui8>{b}, expectedRef); + } + + Y_UNIT_TEST(TestFromBuffer) { + const size_t sz = 1234u; + TBuffer buf; + buf.Resize(sz); + UNIT_ASSERT_EQUAL(buf.Size(), sz); + TBlob b = TBlob::FromBuffer(buf); + UNIT_ASSERT_EQUAL(buf.Size(), 0u); + UNIT_ASSERT_EQUAL(b.Size(), sz); + } + + Y_UNIT_TEST(TestFromFile) { + TString path = "testfile"; + + TOFStream stream(path); + stream.Write("1234", 4); + stream.Finish(); + + auto testMode = [](TBlob blob) { + UNIT_ASSERT_EQUAL(blob.Size(), 4); + UNIT_ASSERT_EQUAL(TStringBuf(static_cast<const char*>(blob.Data()), 4), "1234"); + }; + + testMode(TBlob::FromFile(path)); + testMode(TBlob::PrechargedFromFile(path)); + testMode(TBlob::LockedFromFile(path)); + } + + Y_UNIT_TEST(TestEmptyLockedFiles) { + TString path = MakeTempName(); + TFsPath(path).Touch(); + TBlob::LockedFromFile(path); + } +}; |