You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by bn...@apache.org on 2024/01/30 16:26:09 UTC
(trafficserver-libswoc) 08/08: Fix bugs in MemSpan prefix, suffix methods.
This is an automated email from the ASF dual-hosted git repository.
bneradt pushed a commit to branch dev-1-2-13
in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git
commit 338f76174ef8c3a5d16445f1534aabf46cd83c2c
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Wed Nov 18 18:26:34 2020 -0600
Fix bugs in MemSpan<void> prefix, suffix methods.
---
code/include/swoc/MemSpan.h | 6 +++---
unit_tests/test_MemSpan.cc | 22 ++++++++++++++++++++++
2 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/code/include/swoc/MemSpan.h b/code/include/swoc/MemSpan.h
index 4bc63d2..97bb523 100644
--- a/code/include/swoc/MemSpan.h
+++ b/code/include/swoc/MemSpan.h
@@ -879,7 +879,7 @@ MemSpan<void>::prefix(size_t n) const {
inline MemSpan<void> &
MemSpan<void>::remove_prefix(size_t n) {
- n = std::max(_size, n);
+ n = std::min(_size, n);
_size -= n;
_ptr = static_cast<char *>(_ptr) + n;
return *this;
@@ -887,13 +887,13 @@ MemSpan<void>::remove_prefix(size_t n) {
inline MemSpan<void>
MemSpan<void>::suffix(size_t count) const {
- count = std::max(count, _size);
+ count = std::min(count, _size);
return {static_cast<char *>(this->data_end()) - count, count};
}
inline MemSpan<void> &
MemSpan<void>::remove_suffix(size_t count) {
- _size -= std::max(count, _size);
+ _size -= std::min(count, _size);
return *this;
}
diff --git a/unit_tests/test_MemSpan.cc b/unit_tests/test_MemSpan.cc
index 2808daa..64aaf72 100644
--- a/unit_tests/test_MemSpan.cc
+++ b/unit_tests/test_MemSpan.cc
@@ -39,6 +39,7 @@ TEST_CASE("MemSpan", "[libswoc][MemSpan]")
left.assign(buff, sizeof(buff));
span = left.suffix(768);
+ REQUIRE(span.size() == 768);
left.remove_suffix(768);
REQUIRE(left.end() == span.begin());
REQUIRE(left.size() + span.size() == 1024);
@@ -90,3 +91,24 @@ TEST_CASE("MemSpan", "[libswoc][MemSpan]")
REQUIRE(fspan.count() == f2span.count());
REQUIRE(fspan.is_same(f2span));
};
+
+TEST_CASE("MemSpan<void>", "[libswoc][MemSpan]")
+{
+ char buff[1024];
+
+ MemSpan<void> span(buff, sizeof(buff));
+ auto left = span.prefix(512);
+ REQUIRE(left.size() == 512);
+ REQUIRE(span.size() == 1024);
+ span.remove_prefix(512);
+ REQUIRE(span.size() == 512);
+ REQUIRE(left.data_end() == span.data());
+
+ left.assign(buff, sizeof(buff));
+ span = left.suffix(700);
+ REQUIRE(span.size() == 700);
+ left.remove_suffix(700);
+ REQUIRE(left.data_end() == span.data());
+ REQUIRE(left.size() + span.size() == 1024);
+
+};