Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2021-02-21 00:05:27 +0300
committerJacques Lucke <jacques@blender.org>2021-02-21 00:05:50 +0300
commit6c2e1f33983766ee5ee028e14a24e36c28d0a566 (patch)
tree101fc34380934b0ce99af0ce010b2b5c4d3e1e05 /source/blender/blenlib/tests/BLI_span_test.cc
parentd8b4246b9fde78d6eaab170eb34951d7c46778f3 (diff)
BLI: cleanup StringRef and Span and improve parameter validation
Previously, methods like `Span.drop_front` would crash when more elements would be dropped than are available. While this is most efficient, it is not very practical in some use cases. Also other languages silently clamp the index, so one can easily write wrong code accidentally. Now, `Span.drop_front` and similar methods will only crash when n is negative. Too large values will be clamped down to their maximum possible value. While this is slightly less efficient, I did not have a case where this actually mattered yet. If it does matter in the future, we can add a separate `*_unchecked` method. This should not change the behavior of existing code.
Diffstat (limited to 'source/blender/blenlib/tests/BLI_span_test.cc')
-rw-r--r--source/blender/blenlib/tests/BLI_span_test.cc42
1 files changed, 41 insertions, 1 deletions
diff --git a/source/blender/blenlib/tests/BLI_span_test.cc b/source/blender/blenlib/tests/BLI_span_test.cc
index d1c9f312b97..002c97b0c7d 100644
--- a/source/blender/blenlib/tests/BLI_span_test.cc
+++ b/source/blender/blenlib/tests/BLI_span_test.cc
@@ -62,6 +62,15 @@ TEST(span, DropFront)
EXPECT_EQ(slice[2], 7);
}
+TEST(span, DropFrontLargeN)
+{
+ Vector<int> a = {1, 2, 3, 4, 5};
+ Span<int> slice1 = Span<int>(a).drop_front(100);
+ MutableSpan<int> slice2 = MutableSpan<int>(a).drop_front(100);
+ EXPECT_TRUE(slice1.is_empty());
+ EXPECT_TRUE(slice2.is_empty());
+}
+
TEST(span, DropFrontAll)
{
Vector<int> a = {4, 5, 6, 7};
@@ -78,6 +87,15 @@ TEST(span, TakeFront)
EXPECT_EQ(slice[1], 5);
}
+TEST(span, TakeFrontLargeN)
+{
+ Vector<int> a = {4, 5, 6, 7};
+ Span<int> slice1 = Span<int>(a).take_front(100);
+ MutableSpan<int> slice2 = MutableSpan<int>(a).take_front(100);
+ EXPECT_EQ(slice1.size(), 4);
+ EXPECT_EQ(slice2.size(), 4);
+}
+
TEST(span, TakeBack)
{
Vector<int> a = {5, 6, 7, 8};
@@ -87,6 +105,15 @@ TEST(span, TakeBack)
EXPECT_EQ(slice[1], 8);
}
+TEST(span, TakeBackLargeN)
+{
+ Vector<int> a = {3, 4, 5, 6};
+ Span<int> slice1 = Span<int>(a).take_back(100);
+ MutableSpan<int> slice2 = MutableSpan<int>(a).take_back(100);
+ EXPECT_EQ(slice1.size(), 4);
+ EXPECT_EQ(slice2.size(), 4);
+}
+
TEST(span, Slice)
{
Vector<int> a = {4, 5, 6, 7};
@@ -112,6 +139,19 @@ TEST(span, SliceRange)
EXPECT_EQ(slice[1], 4);
}
+TEST(span, SliceLargeN)
+{
+ Vector<int> a = {1, 2, 3, 4, 5};
+ Span<int> slice1 = Span<int>(a).slice(3, 100);
+ MutableSpan<int> slice2 = MutableSpan<int>(a).slice(3, 100);
+ EXPECT_EQ(slice1.size(), 2);
+ EXPECT_EQ(slice2.size(), 2);
+ EXPECT_EQ(slice1[0], 4);
+ EXPECT_EQ(slice2[0], 4);
+ EXPECT_EQ(slice1[1], 5);
+ EXPECT_EQ(slice2[1], 5);
+}
+
TEST(span, Contains)
{
Vector<int> a = {4, 5, 6, 7};
@@ -337,7 +377,7 @@ TEST(span, MutableReverseIterator)
EXPECT_EQ_ARRAY(src.data(), Span({14, 15, 16, 17}).data(), 4);
}
-TEST(span, constexpr_)
+TEST(span, Constexpr)
{
static constexpr std::array<int, 3> src = {3, 2, 1};
constexpr Span<int> span(src);