aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/util/contiguous_data_ut.cpp
diff options
context:
space:
mode:
authorinnokentii <innokentii@yandex-team.com>2022-09-29 17:04:39 +0300
committerinnokentii <innokentii@yandex-team.com>2022-09-29 17:04:39 +0300
commitb9c33d2970daa903dfdd7cfd222c293593e83536 (patch)
tree8adc97a90bb850ba7e5c0b6dc48959709ed89dd0 /library/cpp/actors/util/contiguous_data_ut.cpp
parentd562e7c54fcae2c816d67834310ecfec90e8a69d (diff)
downloadydb-b9c33d2970daa903dfdd7cfd222c293593e83536.tar.gz
Extend TContiguousData interface
extend TContiguousData interface
Diffstat (limited to 'library/cpp/actors/util/contiguous_data_ut.cpp')
-rw-r--r--library/cpp/actors/util/contiguous_data_ut.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/library/cpp/actors/util/contiguous_data_ut.cpp b/library/cpp/actors/util/contiguous_data_ut.cpp
index b9361b3f39..a792015ce3 100644
--- a/library/cpp/actors/util/contiguous_data_ut.cpp
+++ b/library/cpp/actors/util/contiguous_data_ut.cpp
@@ -49,4 +49,121 @@ Y_UNIT_TEST_SUITE(TContiguousData) {
sharedData,
constSharedData);
}
+
+ Y_UNIT_TEST(Resize) {
+ TContiguousData data = TContiguousData::Uninitialized(10, 20, 30);
+ UNIT_ASSERT_EQUAL(data.size(), 10);
+ UNIT_ASSERT_EQUAL(data.Headroom(), 20);
+ UNIT_ASSERT_EQUAL(data.Tailroom(), 30);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), 76);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), data.size() + data.Headroom() + data.Tailroom() + 2 * sizeof(char*));
+ data.GrowFront(5);
+ UNIT_ASSERT_EQUAL(data.size(), 15);
+ UNIT_ASSERT_EQUAL(data.Headroom(), 15);
+ UNIT_ASSERT_EQUAL(data.Tailroom(), 30);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), 76);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), data.size() + data.Headroom() + data.Tailroom() + 2 * sizeof(char*));
+ data.GrowBack(5);
+ UNIT_ASSERT_EQUAL(data.size(), 20);
+ UNIT_ASSERT_EQUAL(data.Headroom(), 15);
+ UNIT_ASSERT_EQUAL(data.Tailroom(), 25);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), 76);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), data.size() + data.Headroom() + data.Tailroom() + 2 * sizeof(char*));
+ data.GrowFront(21);
+ UNIT_ASSERT_EQUAL(data.size(), 41);
+ UNIT_ASSERT_EQUAL(data.Headroom(), 0);
+ UNIT_ASSERT_EQUAL(data.Tailroom(), 25);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), 82);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), data.size() + data.Headroom() + data.Tailroom() + 2 * sizeof(char*));
+ data.GrowBack(26);
+ UNIT_ASSERT_EQUAL(data.size(), 67);
+ UNIT_ASSERT_EQUAL(data.Headroom(), 0);
+ UNIT_ASSERT_EQUAL(data.Tailroom(), 0);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), 67);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), data.size() + data.Headroom() + data.Tailroom());
+ }
+
+ Y_UNIT_TEST(ResizeUnshare) {
+ TContiguousData data = TContiguousData::Uninitialized(10, 20, 30);
+ TContiguousData otherData(data);
+ UNIT_ASSERT_EQUAL(data.data(), otherData.data());
+ UNIT_ASSERT_EQUAL(data.size(), 10);
+ UNIT_ASSERT_EQUAL(data.Headroom(), 20);
+ UNIT_ASSERT_EQUAL(data.Tailroom(), 30);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), 76);
+ UNIT_ASSERT_EQUAL(otherData.size(), 10);
+ UNIT_ASSERT_EQUAL(otherData.Headroom(), 20);
+ UNIT_ASSERT_EQUAL(otherData.Tailroom(), 30);
+ UNIT_ASSERT_EQUAL(otherData.GetOccupiedMemorySize(), 76);
+ data.GrowFront(5);
+ data.GrowBack(5);
+ UNIT_ASSERT_EQUAL(data.data() + 5, otherData.data());
+ UNIT_ASSERT_EQUAL(data.size(), 20);
+ UNIT_ASSERT_EQUAL(data.Headroom(), 15);
+ UNIT_ASSERT_EQUAL(data.Tailroom(), 25);
+ UNIT_ASSERT_EQUAL(data.GetOccupiedMemorySize(), 76);
+ otherData.GrowFront(5);
+ UNIT_ASSERT_UNEQUAL(data.data(), otherData.data());
+ UNIT_ASSERT_EQUAL(otherData.size(), 15);
+ UNIT_ASSERT_EQUAL(otherData.Headroom(), 15);
+ UNIT_ASSERT_EQUAL(otherData.Tailroom(), 30);
+ UNIT_ASSERT_EQUAL(otherData.GetOccupiedMemorySize(), 76);
+ }
+
+ Y_UNIT_TEST(Trim) {
+ TContiguousData data = TContiguousData::Uninitialized(10, 20, 30);
+ TContiguousData otherData(data);
+ otherData.Trim(5);
+ UNIT_ASSERT_EQUAL(data.data(), otherData.data());
+ UNIT_ASSERT_EQUAL(otherData.Headroom(), 0);
+ UNIT_ASSERT_EQUAL(otherData.Tailroom(), 0);
+ TContiguousData otherData2(data);
+ otherData2.Trim(1, 1);
+ UNIT_ASSERT_EQUAL(data.data() + 1, otherData2.data());
+ UNIT_ASSERT_EQUAL(otherData2.Headroom(), 0);
+ UNIT_ASSERT_EQUAL(otherData2.Tailroom(), 0);
+ otherData.Trim(1, 1);
+ UNIT_ASSERT_EQUAL(otherData.data(), otherData2.data());
+ data.GrowFront(5);
+ data.GrowBack(5);
+ UNIT_ASSERT_EQUAL(data.data() + 6, otherData2.data());
+ UNIT_ASSERT_EQUAL(data.data() + 6, otherData.data());
+ otherData.GrowFront(1);
+ UNIT_ASSERT_UNEQUAL(data.data() + 7, otherData.data());
+ otherData2.GrowBack(1);
+ UNIT_ASSERT_UNEQUAL(data.data() + 6, otherData2.data());
+ data = TContiguousData::Uninitialized(10);
+ otherData = data;
+ data.Trim(5);
+ UNIT_ASSERT_EQUAL(data.data(), otherData.data());
+ UNIT_ASSERT_EQUAL(data.size(), 5);
+ }
+
+ Y_UNIT_TEST(SliceUnshare) {
+ TContiguousData data = TContiguousData::Uninitialized(10, 20, 30);
+ TContiguousData otherData(TContiguousData::Slice, data.data(), data.size(), data);
+ UNIT_ASSERT_EQUAL(otherData.Headroom(), 0);
+ UNIT_ASSERT_EQUAL(otherData.Tailroom(), 0);
+ }
+
+ Y_UNIT_TEST(Extract) {
+ TContiguousData data = TContiguousData::Uninitialized(10, 20, 30);
+ NActors::TSharedData extracted = data.ExtractUnderlyingContainerOrCopy<NActors::TSharedData>();
+ UNIT_ASSERT_UNEQUAL(data.data(), extracted.data());
+ UNIT_ASSERT_UNEQUAL(data.data(), extracted.data() + 20);
+ data = TContiguousData::Uninitialized(10);
+ extracted = data.ExtractUnderlyingContainerOrCopy<NActors::TSharedData>();
+ UNIT_ASSERT_EQUAL(data.data(), extracted.data());
+
+ // TODO(innokentii) add microoptimization for this case
+ // TContiguousData data = TContiguousData::Uninitialized(10, 20, 30);
+ // data.GrowFront(20);
+ // extracted = data.ExtractUnderlyingContainerOrCopy<NActors::TSharedData>();
+ // UNIT_ASSERT_EQUAL(data.data(), extracted.data());
+ // UNIT_ASSERT_EQUAL(data.size(), extracted.size());
+ // data.GrowBack(30);
+ // extracted = data.ExtractUnderlyingContainerOrCopy<NActors::TSharedData>();
+ // UNIT_ASSERT_EQUAL(data.data(), extracted.data());
+ // UNIT_ASSERT_EQUAL(data.size(), extracted.size());
+ }
}