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

Commit 1ec67b0

Browse files
committed
string_view: add string_view capacity API libcxx tests
- taken from: llvm-mirror/libcxx@78d6a77
1 parent 55351ae commit 1ec67b0

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed
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+
// <string_view>
10+
11+
// void remove_prefix(size_type _n)
12+
13+
#include <cassert>
14+
#include <string_view>
15+
16+
#include "test_macros.h"
17+
18+
template <typename CharT>
19+
void
20+
test(const CharT *s, size_t len)
21+
{
22+
typedef std::basic_string_view<CharT> SV;
23+
{
24+
SV sv1(s);
25+
assert(sv1.size() == len);
26+
assert(sv1.data() == s);
27+
28+
if (len > 0) {
29+
sv1.remove_prefix(1);
30+
assert(sv1.size() == (len - 1));
31+
assert(sv1.data() == (s + 1));
32+
sv1.remove_prefix(len - 1);
33+
}
34+
35+
assert(sv1.size() == 0);
36+
sv1.remove_prefix(0);
37+
assert(sv1.size() == 0);
38+
}
39+
}
40+
41+
#if TEST_STD_VER > 11
42+
constexpr size_t
43+
test_ce(size_t n, size_t k)
44+
{
45+
typedef std::basic_string_view<char> SV;
46+
SV sv1{"ABCDEFGHIJKL", n};
47+
sv1.remove_prefix(k);
48+
return sv1.size();
49+
}
50+
#endif
51+
52+
int
53+
main(int, char **)
54+
{
55+
test("ABCDE", 5);
56+
test("a", 1);
57+
test("", 0);
58+
59+
test(L"ABCDE", 5);
60+
test(L"a", 1);
61+
test(L"", 0);
62+
63+
#if TEST_STD_VER >= 11
64+
test(u"ABCDE", 5);
65+
test(u"a", 1);
66+
test(u"", 0);
67+
68+
test(U"ABCDE", 5);
69+
test(U"a", 1);
70+
test(U"", 0);
71+
#endif
72+
73+
#if TEST_STD_VER > 11
74+
{
75+
static_assert(test_ce(5, 0) == 5, "");
76+
static_assert(test_ce(5, 1) == 4, "");
77+
static_assert(test_ce(5, 5) == 0, "");
78+
static_assert(test_ce(9, 3) == 6, "");
79+
}
80+
#endif
81+
82+
return 0;
83+
}
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+
// <string_view>
10+
11+
// void remove_suffix(size_type _n)
12+
13+
#include <cassert>
14+
#include <string_view>
15+
16+
#include "test_macros.h"
17+
18+
template <typename CharT>
19+
void
20+
test(const CharT *s, size_t len)
21+
{
22+
typedef std::basic_string_view<CharT> SV;
23+
{
24+
SV sv1(s);
25+
assert(sv1.size() == len);
26+
assert(sv1.data() == s);
27+
28+
if (len > 0) {
29+
sv1.remove_suffix(1);
30+
assert(sv1.size() == (len - 1));
31+
assert(sv1.data() == s);
32+
sv1.remove_suffix(len - 1);
33+
}
34+
35+
assert(sv1.size() == 0);
36+
sv1.remove_suffix(0);
37+
assert(sv1.size() == 0);
38+
}
39+
}
40+
41+
#if TEST_STD_VER > 11
42+
constexpr size_t
43+
test_ce(size_t n, size_t k)
44+
{
45+
typedef std::basic_string_view<char> SV;
46+
SV sv1{"ABCDEFGHIJKL", n};
47+
sv1.remove_suffix(k);
48+
return sv1.size();
49+
}
50+
#endif
51+
52+
int
53+
main(int, char **)
54+
{
55+
test("ABCDE", 5);
56+
test("a", 1);
57+
test("", 0);
58+
59+
test(L"ABCDE", 5);
60+
test(L"a", 1);
61+
test(L"", 0);
62+
63+
#if TEST_STD_VER >= 11
64+
test(u"ABCDE", 5);
65+
test(u"a", 1);
66+
test(u"", 0);
67+
68+
test(U"ABCDE", 5);
69+
test(U"a", 1);
70+
test(U"", 0);
71+
#endif
72+
73+
#if TEST_STD_VER > 11
74+
{
75+
static_assert(test_ce(5, 0) == 5, "");
76+
static_assert(test_ce(5, 1) == 4, "");
77+
static_assert(test_ce(5, 5) == 0, "");
78+
static_assert(test_ce(9, 3) == 6, "");
79+
}
80+
#endif
81+
82+
return 0;
83+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
// void swap(basic_string_view& _other) noexcept
12+
13+
#include <cassert>
14+
#include <string_view>
15+
16+
#include "test_macros.h"
17+
18+
template <typename CharT>
19+
void
20+
test(const CharT *s, size_t len)
21+
{
22+
typedef std::basic_string_view<CharT> SV;
23+
{
24+
SV sv1(s);
25+
SV sv2;
26+
27+
assert(sv1.size() == len);
28+
assert(sv1.data() == s);
29+
assert(sv2.size() == 0);
30+
31+
sv1.swap(sv2);
32+
assert(sv1.size() == 0);
33+
assert(sv2.size() == len);
34+
assert(sv2.data() == s);
35+
}
36+
}
37+
38+
#if TEST_STD_VER > 11
39+
constexpr size_t
40+
test_ce(size_t n, size_t k)
41+
{
42+
typedef std::basic_string_view<char> SV;
43+
SV sv1{"ABCDEFGHIJKL", n};
44+
SV sv2{sv1.data(), k};
45+
sv1.swap(sv2);
46+
return sv1.size();
47+
}
48+
#endif
49+
50+
int
51+
main(int, char **)
52+
{
53+
test("ABCDE", 5);
54+
test("a", 1);
55+
test("", 0);
56+
57+
test(L"ABCDE", 5);
58+
test(L"a", 1);
59+
test(L"", 0);
60+
61+
#if TEST_STD_VER >= 11
62+
test(u"ABCDE", 5);
63+
test(u"a", 1);
64+
test(u"", 0);
65+
66+
test(U"ABCDE", 5);
67+
test(U"a", 1);
68+
test(U"", 0);
69+
#endif
70+
71+
#if TEST_STD_VER > 11
72+
{
73+
static_assert(test_ce(2, 3) == 3, "");
74+
static_assert(test_ce(5, 3) == 3, "");
75+
static_assert(test_ce(0, 1) == 1, "");
76+
}
77+
#endif
78+
79+
return 0;
80+
}

0 commit comments

Comments
 (0)