Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 55351ae

Browse files
Merge pull request #948 from KFilipek/test-iterators
string_view: implement iterator API for string_view
2 parents 63110eb + 196ac21 commit 55351ae

4 files changed

Lines changed: 245 additions & 1 deletion

File tree

include/libpmemobj++/string_view.hpp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class basic_string_view {
5353
using const_reference = const value_type &;
5454
using pointer = value_type *;
5555
using const_pointer = const value_type *;
56+
using const_iterator = const_pointer;
57+
using iterator = const_iterator;
5658

5759
constexpr basic_string_view() noexcept;
5860
constexpr basic_string_view(const CharT *data, size_type size);
@@ -64,6 +66,17 @@ class basic_string_view {
6466
basic_string_view &
6567
operator=(const basic_string_view &rhs) noexcept = default;
6668

69+
constexpr const_iterator begin() const noexcept;
70+
constexpr const_iterator cbegin() const noexcept;
71+
constexpr const_iterator end() const noexcept;
72+
constexpr const_iterator cend() const noexcept;
73+
#ifdef XXX
74+
constexpr const_reverse_iterator rbegin() const noexcept;
75+
constexpr const_reverse_iterator crbegin() const noexcept;
76+
constexpr const_reverse_iterator rend() const noexcept;
77+
constexpr const_reverse_iterator crend() const noexcept;
78+
#endif
79+
6780
constexpr const CharT *data() const noexcept;
6881
constexpr size_type size() const noexcept;
6982
constexpr size_type length() const noexcept;
@@ -136,9 +149,61 @@ constexpr inline basic_string_view<CharT, Traits>::basic_string_view(
136149
{
137150
}
138151

152+
/**
153+
* Returns an iterator to the first character of the view.
154+
*
155+
* @return const_iterator to the first character
156+
*/
157+
template <typename CharT, typename Traits>
158+
constexpr typename basic_string_view<CharT, Traits>::const_iterator
159+
basic_string_view<CharT, Traits>::begin() const noexcept
160+
{
161+
return cbegin();
162+
}
163+
164+
/**
165+
* Returns an iterator to the first character of the view.
166+
*
167+
* @return const_iterator to the first character
168+
*/
169+
template <typename CharT, typename Traits>
170+
constexpr typename basic_string_view<CharT, Traits>::const_iterator
171+
basic_string_view<CharT, Traits>::cbegin() const noexcept
172+
{
173+
return data_;
174+
}
175+
176+
/**
177+
* Returns an iterator to the character following the last character of the
178+
* view. This character acts as a placeholder, attempting to access it results
179+
* in undefined behavior.
180+
*
181+
* @return const_iterator to the character following the last character.
182+
*/
183+
template <typename CharT, typename Traits>
184+
constexpr typename basic_string_view<CharT, Traits>::const_iterator
185+
basic_string_view<CharT, Traits>::end() const noexcept
186+
{
187+
return cend();
188+
}
189+
190+
/**
191+
* Returns an iterator to the character following the last character of the
192+
* view. This character acts as a placeholder, attempting to access it results
193+
* in undefined behavior.
194+
*
195+
* @return const_iterator to the character following the last character.
196+
*/
197+
template <typename CharT, typename Traits>
198+
constexpr typename basic_string_view<CharT, Traits>::const_iterator
199+
basic_string_view<CharT, Traits>::cend() const noexcept
200+
{
201+
return data_ + size_;
202+
}
203+
139204
/**
140205
* Returns pointer to data stored in this pmem::obj::string_view. It may
141-
*not contain the terminating null character.
206+
* not contain the terminating null character.
142207
*
143208
* @return pointer to C-like string (char *), it may not end with null
144209
* character.

tests/external/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,12 @@ if (TEST_STRING)
758758
build_test(string_libcxx_string_view_capacity libcxx/string.view/string.view.capacity/capacity.pass.cpp)
759759
add_test_generic(NAME string_libcxx_string_view_capacity TRACERS none pmemcheck memcheck)
760760

761+
build_test(string_libcxx_string_view_begin libcxx/string.view/string.view.iterators/begin.pass.cpp)
762+
add_test_generic(NAME string_libcxx_string_view_begin TRACERS none pmemcheck memcheck)
763+
764+
build_test(string_libcxx_string_view_end libcxx/string.view/string.view.iterators/end.pass.cpp)
765+
add_test_generic(NAME string_libcxx_string_view_end TRACERS none pmemcheck memcheck)
766+
761767
if(MSVC_VERSION GREATER_EQUAL 1920)
762768
build_test(string_libcxx_string_view_opeq_string_view libcxx/string.view/string.view.comparison/opeq.string_view.string_view.pass.cpp)
763769
add_test_generic(NAME string_libcxx_string_view_opeq_string_view TRACERS none pmemcheck memcheck)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Copyright 2020, Intel Corporation
10+
//
11+
// Modified to test pmem::obj containers
12+
//
13+
14+
// <string_view>
15+
16+
// constexpr const_iterator begin() const;
17+
18+
#include "unittest.hpp"
19+
20+
#include <libpmemobj++/string_view.hpp>
21+
22+
template <class S>
23+
void
24+
test(S s)
25+
{
26+
const S &cs = s;
27+
typename S::iterator b = s.begin();
28+
typename S::const_iterator cb1 = cs.begin();
29+
typename S::const_iterator cb2 = s.cbegin();
30+
if (!s.empty()) {
31+
UT_ASSERT(*b == s[0]);
32+
UT_ASSERT(&*b == &s[0]);
33+
UT_ASSERT(*cb1 == s[0]);
34+
UT_ASSERT(&*cb1 == &s[0]);
35+
UT_ASSERT(*cb2 == s[0]);
36+
UT_ASSERT(&*cb2 == &s[0]);
37+
}
38+
UT_ASSERT(b == cb1);
39+
UT_ASSERT(b == cb2);
40+
UT_ASSERT(cb1 == cb2);
41+
}
42+
43+
static void
44+
run()
45+
{
46+
typedef pmem::obj::string_view string_view;
47+
typedef pmem::obj::u16string_view u16string_view;
48+
typedef pmem::obj::u32string_view u32string_view;
49+
typedef pmem::obj::wstring_view wstring_view;
50+
51+
test(string_view());
52+
test(u16string_view());
53+
test(u32string_view());
54+
test(wstring_view());
55+
test(string_view("123"));
56+
test(wstring_view(L"123"));
57+
test(u16string_view{u"123"});
58+
test(u32string_view{U"123"});
59+
60+
{
61+
constexpr string_view sv{"123", 3};
62+
63+
constexpr u16string_view u16sv{u"123", 3};
64+
constexpr u32string_view u32sv{U"123", 3};
65+
constexpr wstring_view wsv{L"123", 3};
66+
67+
static_assert(*sv.begin() == sv[0], "");
68+
static_assert(*u16sv.begin() == u16sv[0], "");
69+
static_assert(*u32sv.begin() == u32sv[0], "");
70+
static_assert(*wsv.begin() == wsv[0], "");
71+
72+
static_assert(*sv.cbegin() == sv[0], "");
73+
static_assert(*u16sv.cbegin() == u16sv[0], "");
74+
static_assert(*u32sv.cbegin() == u32sv[0], "");
75+
static_assert(*wsv.cbegin() == wsv[0], "");
76+
}
77+
}
78+
79+
int
80+
main(int argc, char *argv[])
81+
{
82+
return run_test([&] { run(); });
83+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Copyright 2020, Intel Corporation
10+
//
11+
// Modified to test pmem::obj containers
12+
//
13+
14+
// <string_view>
15+
16+
// constexpr const_iterator end() const;
17+
18+
#include "unittest.hpp"
19+
20+
#include <cstddef>
21+
#include <libpmemobj++/string_view.hpp>
22+
23+
template <class S>
24+
void
25+
test(S s)
26+
{
27+
const S &cs = s;
28+
typename S::iterator e = s.end();
29+
typename S::const_iterator ce1 = cs.end();
30+
typename S::const_iterator ce2 = s.cend();
31+
32+
if (s.empty()) {
33+
UT_ASSERT(e == s.begin());
34+
UT_ASSERT(ce1 == cs.begin());
35+
UT_ASSERT(ce2 == s.begin());
36+
} else {
37+
UT_ASSERT(e != s.begin());
38+
UT_ASSERT(ce1 != cs.begin());
39+
UT_ASSERT(ce2 != s.begin());
40+
}
41+
42+
UT_ASSERT(static_cast<std::size_t>(e - s.begin()) == s.size());
43+
UT_ASSERT(static_cast<std::size_t>(ce1 - cs.begin()) == cs.size());
44+
UT_ASSERT(static_cast<std::size_t>(ce2 - s.cbegin()) == s.size());
45+
46+
UT_ASSERT(e == ce1);
47+
UT_ASSERT(e == ce2);
48+
UT_ASSERT(ce1 == ce2);
49+
}
50+
51+
static void
52+
run()
53+
{
54+
typedef pmem::obj::string_view string_view;
55+
typedef pmem::obj::u16string_view u16string_view;
56+
typedef pmem::obj::u32string_view u32string_view;
57+
typedef pmem::obj::wstring_view wstring_view;
58+
59+
test(string_view());
60+
test(u16string_view());
61+
test(u32string_view());
62+
test(wstring_view());
63+
test(string_view("123"));
64+
test(wstring_view(L"123"));
65+
test(u16string_view{u"123"});
66+
test(u32string_view{U"123"});
67+
68+
{
69+
constexpr string_view sv{"123", 3};
70+
constexpr u16string_view u16sv{u"123", 3};
71+
constexpr u32string_view u32sv{U"123", 3};
72+
constexpr wstring_view wsv{L"123", 3};
73+
74+
static_assert(sv.begin() != sv.end(), "");
75+
static_assert(u16sv.begin() != u16sv.end(), "");
76+
static_assert(u32sv.begin() != u32sv.end(), "");
77+
static_assert(wsv.begin() != wsv.end(), "");
78+
79+
static_assert(sv.begin() != sv.cend(), "");
80+
static_assert(u16sv.begin() != u16sv.cend(), "");
81+
static_assert(u32sv.begin() != u32sv.cend(), "");
82+
static_assert(wsv.begin() != wsv.cend(), "");
83+
}
84+
}
85+
86+
int
87+
main(int argc, char *argv[])
88+
{
89+
return run_test([&] { run(); });
90+
}

0 commit comments

Comments
 (0)