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

Commit d3bc47d

Browse files
authored
Merge pull request #940 from KFilipek/tests-capacity_api
string_view: implement capacity API and port libcxx tests
2 parents ca9562b + e23c384 commit d3bc47d

3 files changed

Lines changed: 138 additions & 6 deletions

File tree

include/libpmemobj++/string_view.hpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define LIBPMEMOBJ_CPP_STRING_VIEW
1111

1212
#include <algorithm>
13-
#include <cassert>
13+
#include <limits>
1414
#include <stdexcept>
1515
#include <string>
1616

@@ -38,8 +38,8 @@ using u32string_view = std::basic_string_view<char32_t>;
3838
/**
3939
* Our partial std::string_view implementation.
4040
*
41-
* If C++17's std::string_view implementation is not available, this one is
42-
* used to avoid unnecessary string copying.
41+
* If C++17's std::string_view implementation is not available, this one
42+
* is used to avoid unnecessary string copying.
4343
*/
4444
template <typename CharT, typename Traits = std::char_traits<CharT>>
4545
class basic_string_view {
@@ -67,6 +67,8 @@ class basic_string_view {
6767
constexpr const CharT *data() const noexcept;
6868
constexpr size_type size() const noexcept;
6969
constexpr size_type length() const noexcept;
70+
constexpr bool empty() const noexcept;
71+
constexpr size_type max_size() const noexcept;
7072

7173
const CharT &at(size_type pos) const;
7274
constexpr const CharT &operator[](size_type pos) const noexcept;
@@ -135,8 +137,8 @@ constexpr inline basic_string_view<CharT, Traits>::basic_string_view(
135137
}
136138

137139
/**
138-
* Returns pointer to data stored in this pmem::obj::string_view. It may not
139-
* contain the terminating null character.
140+
* Returns pointer to data stored in this pmem::obj::string_view. It may
141+
*not contain the terminating null character.
140142
*
141143
* @return pointer to C-like string (char *), it may not end with null
142144
* character.
@@ -149,7 +151,8 @@ basic_string_view<CharT, Traits>::data() const noexcept
149151
}
150152

151153
/**
152-
* Returns count of characters stored in this pmem::obj::string_view data.
154+
* Returns count of characters stored in this pmem::obj::string_view
155+
* data.
153156
*
154157
* @return the number of CharT elements in the view.
155158
*/
@@ -160,6 +163,31 @@ basic_string_view<CharT, Traits>::size() const noexcept
160163
return size_;
161164
}
162165

166+
/**
167+
* Returns that view is empty or not.
168+
*
169+
* @return true when size() == 0.
170+
*/
171+
template <typename CharT, typename Traits>
172+
constexpr inline bool
173+
basic_string_view<CharT, Traits>::empty() const noexcept
174+
{
175+
return size() == 0;
176+
}
177+
178+
/**
179+
* Returns the largest possible number of char-like objects that can be
180+
* referred to by a basic_string_view.
181+
*
182+
* @return maximum number of characters.
183+
*/
184+
template <typename CharT, typename Traits>
185+
constexpr inline typename basic_string_view<CharT, Traits>::size_type
186+
basic_string_view<CharT, Traits>::max_size() const noexcept
187+
{
188+
return (std::numeric_limits<size_type>::max)();
189+
}
190+
163191
/**
164192
* Returns count of characters stored in this pmem::obj::string_view data.
165193
*

tests/external/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@ if (TEST_STRING)
755755
build_test(string_libcxx_string_view_index libcxx/string.view/string.view.access/index.pass.cpp)
756756
add_test_generic(NAME string_libcxx_string_view_index TRACERS none pmemcheck memcheck)
757757

758+
build_test(string_libcxx_string_view_capacity libcxx/string.view/string.view.capacity/capacity.pass.cpp)
759+
add_test_generic(NAME string_libcxx_string_view_capacity TRACERS none pmemcheck memcheck)
760+
758761
if(MSVC_VERSION GREATER_EQUAL 1920)
759762
build_test(string_libcxx_string_view_opeq_string_view libcxx/string.view/string.view.comparison/opeq.string_view.string_view.pass.cpp)
760763
add_test_generic(NAME string_libcxx_string_view_opeq_string_view TRACERS none pmemcheck memcheck)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
// <string_view>
10+
11+
// [string.view.capacity], capacity
12+
// constexpr size_type size() const noexcept;
13+
// constexpr size_type length() const noexcept;
14+
// constexpr size_type max_size() const noexcept;
15+
// constexpr bool empty() const noexcept;
16+
17+
#include "unittest.hpp"
18+
19+
#include <libpmemobj++/string_view.hpp>
20+
21+
template <typename SV>
22+
void
23+
test1()
24+
{
25+
{
26+
constexpr SV sv1;
27+
static_assert(sv1.size() == 0, "");
28+
static_assert(sv1.empty(), "");
29+
static_assert(sv1.size() == sv1.length(), "");
30+
static_assert(sv1.max_size() > sv1.size(), "");
31+
}
32+
33+
{
34+
SV sv1;
35+
static_assert(noexcept(sv1.size()),
36+
"Operation must be noexcept");
37+
static_assert(noexcept(sv1.empty()),
38+
"Operation must be noexcept");
39+
static_assert(noexcept(sv1.max_size()),
40+
"Operation must be noexcept");
41+
static_assert(noexcept(sv1.length()),
42+
"Operation must be noexcept");
43+
UT_ASSERT(sv1.size() == 0);
44+
UT_ASSERT(sv1.empty());
45+
UT_ASSERT(sv1.size() == sv1.length());
46+
UT_ASSERT(sv1.max_size() > sv1.size());
47+
}
48+
}
49+
50+
template <typename CharT>
51+
void
52+
test2(const CharT *s, size_t len)
53+
{
54+
{
55+
pmem::obj::basic_string_view<CharT> sv1(s);
56+
UT_ASSERT(sv1.size() == len);
57+
UT_ASSERT(sv1.data() == s);
58+
UT_ASSERT(sv1.empty() == (len == 0));
59+
UT_ASSERT(sv1.size() == sv1.length());
60+
UT_ASSERT(sv1.max_size() > sv1.size());
61+
}
62+
}
63+
64+
static void
65+
run()
66+
{
67+
test1<pmem::obj::string_view>();
68+
test1<pmem::obj::u16string_view>();
69+
test1<pmem::obj::u32string_view>();
70+
test1<pmem::obj::wstring_view>();
71+
72+
test2("ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
73+
105);
74+
test2("ABCDE", 5);
75+
test2("a", 1);
76+
test2("", 0);
77+
78+
test2(L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
79+
105);
80+
test2(L"ABCDE", 5);
81+
test2(L"a", 1);
82+
test2(L"", 0);
83+
84+
test2(u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
85+
105);
86+
test2(u"ABCDE", 5);
87+
test2(u"a", 1);
88+
test2(u"", 0);
89+
90+
test2(U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
91+
105);
92+
test2(U"ABCDE", 5);
93+
test2(U"a", 1);
94+
test2(U"", 0);
95+
}
96+
97+
int
98+
main(int argc, char *argv[])
99+
{
100+
return run_test([&] { run(); });
101+
}

0 commit comments

Comments
 (0)