Skip to content

Commit c25555b

Browse files
Fredrik Olssonfacebook-github-bot
Fredrik Olsson
authored andcommitted
fixed reverse_iterator
Summary: The reverse iterator was incorrectly implemented. Added test case fails when ran without the fix: ``` xplat/folly/container/test/span_test.cpp:333: Failure Expected equality of these values: 2 *it++ Which is: 0 xplat/folly/container/test/span_test.cpp:334: Failure Expected equality of these values: 1 *it++ Which is: 2 xplat/folly/container/test/span_test.cpp:335: Failure Expected equality of these values: it Which is: 8-byte object <18-FF 4A-8A FF-7F 00-00> obj.rend() Which is: 8-byte object <28-FF 4A-8A FF-7F 00-00> ``` Reviewed By: ilvokhin Differential Revision: D70909341 fbshipit-source-id: 61efb3b87761dc1f13dae75964b06bee798803f4
1 parent efe6b15 commit c25555b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

folly/container/span.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ class span {
180180
constexpr iterator begin() const noexcept { return data_; }
181181
constexpr iterator end() const noexcept { return data_ + size(); }
182182
constexpr reverse_iterator rbegin() const noexcept {
183-
return std::make_reverse_iterator(begin());
183+
return std::make_reverse_iterator(end());
184184
}
185185
constexpr reverse_iterator rend() const noexcept {
186-
return std::make_reverse_iterator(end());
186+
return std::make_reverse_iterator(begin());
187187
}
188188

189189
constexpr reference front() const {

folly/container/test/span_test.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,17 @@ TYPED_TEST_P(SpanTest, fix_as_bytes) {
324324
EXPECT_EQ(20, wbytes.size());
325325
}
326326

327+
TYPED_TEST_P(SpanTest, reverse_iterator) {
328+
using span = typename TypeParam::template apply<int, size_t(-1)>;
329+
330+
int data[2] = {1, 2};
331+
auto const obj = span{data};
332+
auto it = obj.rbegin();
333+
EXPECT_EQ(2, *it++);
334+
EXPECT_EQ(1, *it++);
335+
EXPECT_EQ(it, obj.rend());
336+
}
337+
327338
namespace fallback_span_ctad {
328339

329340
namespace fallback = folly::detail::fallback_span;
@@ -390,6 +401,7 @@ REGISTER_TYPED_TEST_SUITE_P(
390401
, fix_static_subspan
391402
, fix_dynamic_subspan
392403
, fix_as_bytes
404+
, reverse_iterator
393405
);
394406
// clang-format on
395407

0 commit comments

Comments
 (0)