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

Commit 928acd8

Browse files
Merge pull request #946 from KFilipek/test-modifiers
string_view: add modifiers API and related libcxx tests
2 parents 55351ae + 5f02ba8 commit 928acd8

File tree

5 files changed

+259
-0
lines changed

5 files changed

+259
-0
lines changed

include/libpmemobj++/string_view.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <limits>
1414
#include <stdexcept>
1515
#include <string>
16+
#include <utility>
1617

1718
#if __cpp_lib_string_view
1819
#include <string_view>
@@ -88,6 +89,10 @@ class basic_string_view {
8889
constexpr const_reference front() const noexcept;
8990
constexpr const_reference back() const noexcept;
9091

92+
void remove_prefix(size_type n);
93+
void remove_suffix(size_type n);
94+
void swap(basic_string_view &v) noexcept;
95+
9196
int compare(const basic_string_view &other) const noexcept;
9297

9398
private:
@@ -320,6 +325,47 @@ basic_string_view<CharT, Traits>::front() const noexcept
320325
return operator[](0);
321326
}
322327

328+
/**
329+
* Moves the start of the view forward by n characters.
330+
* The behavior is undefined if n > size().
331+
*
332+
* @param[in] n number of characters to remove from the start of the view
333+
*/
334+
template <typename CharT, typename Traits>
335+
void
336+
basic_string_view<CharT, Traits>::remove_prefix(size_type n)
337+
{
338+
data_ += n;
339+
size_ -= n;
340+
}
341+
342+
/**
343+
* Moves the end of the view back by n characters.
344+
* The behavior is undefined if n > size().
345+
*
346+
* @param[in] n number of characters to remove from the end of the view
347+
*/
348+
template <typename CharT, typename Traits>
349+
void
350+
basic_string_view<CharT, Traits>::remove_suffix(size_type n)
351+
{
352+
size_ -= n;
353+
}
354+
355+
/**
356+
* Exchanges the view with that of v.
357+
*
358+
* @param[in] v view to swap with
359+
*/
360+
template <typename CharT, typename Traits>
361+
void
362+
basic_string_view<CharT, Traits>::swap(
363+
basic_string_view<CharT, Traits> &v) noexcept
364+
{
365+
std::swap(data_, v.data_);
366+
std::swap(size_, v.size_);
367+
}
368+
323369
/**
324370
* Compares this string_view with other. Works in the same way as
325371
* std::basic_string::compare.

tests/external/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,15 @@ if (TEST_STRING)
764764
build_test(string_libcxx_string_view_end libcxx/string.view/string.view.iterators/end.pass.cpp)
765765
add_test_generic(NAME string_libcxx_string_view_end TRACERS none pmemcheck memcheck)
766766

767+
build_test(string_libcxx_string_view_swap libcxx/string.view/string.view.modifiers/swap.pass.cpp)
768+
add_test_generic(NAME string_libcxx_string_view_swap TRACERS none pmemcheck memcheck)
769+
770+
build_test(string_libcxx_string_view_remove_prefix libcxx/string.view/string.view.modifiers/remove_prefix.pass.cpp)
771+
add_test_generic(NAME string_libcxx_string_view_remove_prefix TRACERS none pmemcheck memcheck)
772+
773+
build_test(string_libcxx_string_view_remove_suffix libcxx/string.view/string.view.modifiers/remove_suffix.pass.cpp)
774+
add_test_generic(NAME string_libcxx_string_view_remove_suffix TRACERS none pmemcheck memcheck)
775+
767776
if(MSVC_VERSION GREATER_EQUAL 1920)
768777
build_test(string_libcxx_string_view_opeq_string_view libcxx/string.view/string.view.comparison/opeq.string_view.string_view.pass.cpp)
769778
add_test_generic(NAME string_libcxx_string_view_opeq_string_view TRACERS none pmemcheck memcheck)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// void remove_prefix(size_type _n)
17+
18+
#include "unittest.hpp"
19+
20+
#include <libpmemobj++/string_view.hpp>
21+
22+
template <typename CharT>
23+
void
24+
test(const CharT *s, size_t len)
25+
{
26+
typedef pmem::obj::basic_string_view<CharT> SV;
27+
{
28+
SV sv1(s);
29+
UT_ASSERT(sv1.size() == len);
30+
UT_ASSERT(sv1.data() == s);
31+
32+
if (len > 0) {
33+
sv1.remove_prefix(1);
34+
UT_ASSERT(sv1.size() == (len - 1));
35+
UT_ASSERT(sv1.data() == (s + 1));
36+
sv1.remove_prefix(len - 1);
37+
}
38+
39+
UT_ASSERT(sv1.size() == 0);
40+
sv1.remove_prefix(0);
41+
UT_ASSERT(sv1.size() == 0);
42+
}
43+
}
44+
45+
static void
46+
run()
47+
{
48+
test("ABCDE", 5);
49+
test("a", 1);
50+
test("", 0);
51+
52+
test(L"ABCDE", 5);
53+
test(L"a", 1);
54+
test(L"", 0);
55+
56+
test(u"ABCDE", 5);
57+
test(u"a", 1);
58+
test(u"", 0);
59+
60+
test(U"ABCDE", 5);
61+
test(U"a", 1);
62+
test(U"", 0);
63+
}
64+
65+
int
66+
main(int argc, char *argv[])
67+
{
68+
return run_test([&] { run(); });
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// void remove_suffix(size_type _n)
17+
18+
#include "unittest.hpp"
19+
20+
#include <libpmemobj++/string_view.hpp>
21+
22+
template <typename CharT>
23+
void
24+
test(const CharT *s, size_t len)
25+
{
26+
typedef pmem::obj::basic_string_view<CharT> SV;
27+
{
28+
SV sv1(s);
29+
UT_ASSERT(sv1.size() == len);
30+
UT_ASSERT(sv1.data() == s);
31+
32+
if (len > 0) {
33+
sv1.remove_suffix(1);
34+
UT_ASSERT(sv1.size() == (len - 1));
35+
UT_ASSERT(sv1.data() == s);
36+
sv1.remove_suffix(len - 1);
37+
}
38+
39+
UT_ASSERT(sv1.size() == 0);
40+
sv1.remove_suffix(0);
41+
UT_ASSERT(sv1.size() == 0);
42+
}
43+
}
44+
45+
static void
46+
run()
47+
{
48+
test("ABCDE", 5);
49+
test("a", 1);
50+
test("", 0);
51+
52+
test(L"ABCDE", 5);
53+
test(L"a", 1);
54+
test(L"", 0);
55+
56+
test(u"ABCDE", 5);
57+
test(u"a", 1);
58+
test(u"", 0);
59+
60+
test(U"ABCDE", 5);
61+
test(U"a", 1);
62+
test(U"", 0);
63+
}
64+
65+
int
66+
main(int argc, char *argv[])
67+
{
68+
return run_test([&] { run(); });
69+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
// void swap(basic_string_view& _other) noexcept
17+
18+
#include "unittest.hpp"
19+
20+
#include <libpmemobj++/string_view.hpp>
21+
22+
template <typename CharT>
23+
void
24+
test(const CharT *s, size_t len)
25+
{
26+
typedef pmem::obj::basic_string_view<CharT> SV;
27+
{
28+
SV sv1(s);
29+
SV sv2;
30+
31+
UT_ASSERT(sv1.size() == len);
32+
UT_ASSERT(sv1.data() == s);
33+
UT_ASSERT(sv2.size() == 0);
34+
35+
sv1.swap(sv2);
36+
UT_ASSERT(sv1.size() == 0);
37+
UT_ASSERT(sv2.size() == len);
38+
UT_ASSERT(sv2.data() == s);
39+
}
40+
}
41+
42+
static void
43+
run()
44+
{
45+
test("ABCDE", 5);
46+
test("a", 1);
47+
test("", 0);
48+
49+
test(L"ABCDE", 5);
50+
test(L"a", 1);
51+
test(L"", 0);
52+
53+
test(u"ABCDE", 5);
54+
test(u"a", 1);
55+
test(u"", 0);
56+
57+
test(U"ABCDE", 5);
58+
test(U"a", 1);
59+
test(U"", 0);
60+
}
61+
62+
int
63+
main(int argc, char *argv[])
64+
{
65+
return run_test([&] { run(); });
66+
}

0 commit comments

Comments
 (0)