diff options
author | Jacques Lucke <jacques@blender.org> | 2021-02-21 00:05:27 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-02-21 00:05:50 +0300 |
commit | 6c2e1f33983766ee5ee028e14a24e36c28d0a566 (patch) | |
tree | 101fc34380934b0ce99af0ce010b2b5c4d3e1e05 /source/blender/blenlib/tests | |
parent | d8b4246b9fde78d6eaab170eb34951d7c46778f3 (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')
-rw-r--r-- | source/blender/blenlib/tests/BLI_span_test.cc | 42 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_string_ref_test.cc | 22 |
2 files changed, 60 insertions, 4 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); diff --git a/source/blender/blenlib/tests/BLI_string_ref_test.cc b/source/blender/blenlib/tests/BLI_string_ref_test.cc index 401a7bc1118..fb8b894bfd5 100644 --- a/source/blender/blenlib/tests/BLI_string_ref_test.cc +++ b/source/blender/blenlib/tests/BLI_string_ref_test.cc @@ -246,10 +246,18 @@ TEST(string_ref, DropPrefixN) EXPECT_EQ(ref3, ""); } -TEST(string_ref, DropPrefix) +TEST(string_ref, DropPrefixLargeN) { StringRef ref("test"); - StringRef ref2 = ref.drop_prefix("tes"); + StringRef ref2 = ref.drop_prefix(100); + EXPECT_EQ(ref2.size(), 0); + EXPECT_EQ(ref2, ""); +} + +TEST(string_ref, DropKnownPrefix) +{ + StringRef ref("test"); + StringRef ref2 = ref.drop_known_prefix("tes"); EXPECT_EQ(ref2.size(), 1); EXPECT_EQ(ref2, "t"); } @@ -262,6 +270,14 @@ TEST(string_ref, DropSuffix) EXPECT_EQ(ref2, "tes"); } +TEST(string_ref, DropSuffixLargeN) +{ + StringRef ref("test"); + StringRef ref2 = ref.drop_suffix(100); + EXPECT_EQ(ref2.size(), 0); + EXPECT_EQ(ref2, ""); +} + TEST(string_ref, Substr) { StringRef ref("hello world"); @@ -298,7 +314,7 @@ TEST(string_ref, ToStringView) EXPECT_EQ(view, "hello"); } -TEST(string_ref, constexpr_) +TEST(string_ref, Constexpr) { constexpr StringRef sref("World"); BLI_STATIC_ASSERT(sref[2] == 'r', ""); |