Skip to content

Commit 786366b

Browse files
committed
[libc++][NFC] Remove some of the code duplication in the string tests
Reviewed By: ldionne, #libc, huixie90 Spies: huixie90, libcxx-commits, arphaman Differential Revision: https://reviews.llvm.org/D131856
1 parent 9343ec8 commit 786366b

File tree

67 files changed

+1694
-3033
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1694
-3033
lines changed

libcxx/test/std/strings/basic.string/string.access/at.pass.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,22 @@ test(S s, typename S::size_type pos)
5454
#endif
5555
}
5656

57+
template <class S>
58+
TEST_CONSTEXPR_CXX20 void test_string() {
59+
test(S(), 0);
60+
test(S("123"), 0);
61+
test(S("123"), 1);
62+
test(S("123"), 2);
63+
test(S("123"), 3);
64+
}
65+
5766
TEST_CONSTEXPR_CXX20 bool test() {
58-
{
59-
typedef std::string S;
60-
test(S(), 0);
61-
test(S("123"), 0);
62-
test(S("123"), 1);
63-
test(S("123"), 2);
64-
test(S("123"), 3);
65-
}
67+
test_string<std::string>();
6668
#if TEST_STD_VER >= 11
67-
{
68-
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
69-
test(S(), 0);
70-
test(S("123"), 0);
71-
test(S("123"), 1);
72-
test(S("123"), 2);
73-
test(S("123"), 3);
74-
}
69+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
7570
#endif
7671

77-
return true;
72+
return true;
7873
}
7974

8075
int main(int, char**)

libcxx/test/std/strings/basic.string/string.access/back.pass.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@ test(S s)
3232
assert(s.back() == typename S::value_type('z'));
3333
}
3434

35+
template <class S>
36+
TEST_CONSTEXPR_CXX20 void test_string() {
37+
test(S("1"));
38+
test(S("1234567890123456789012345678901234567890"));
39+
}
40+
3541
TEST_CONSTEXPR_CXX20 bool test() {
36-
{
37-
typedef std::string S;
38-
test(S("1"));
39-
test(S("1234567890123456789012345678901234567890"));
40-
}
42+
test_string<std::string>();
4143
#if TEST_STD_VER >= 11
42-
{
43-
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
44-
test(S("1"));
45-
test(S("1234567890123456789012345678901234567890"));
46-
}
44+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
4745
#endif
4846

4947
return true;

libcxx/test/std/strings/basic.string/string.access/front.pass.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@ test(S s)
3232
assert(s.front() == typename S::value_type('z'));
3333
}
3434

35+
template <class S>
36+
TEST_CONSTEXPR_CXX20 void test_string() {
37+
test(S("1"));
38+
test(S("1234567890123456789012345678901234567890"));
39+
}
40+
3541
TEST_CONSTEXPR_CXX20 bool test() {
36-
{
37-
typedef std::string S;
38-
test(S("1"));
39-
test(S("1234567890123456789012345678901234567890"));
40-
}
42+
test_string<std::string>();
4143
#if TEST_STD_VER >= 11
42-
{
43-
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
44-
test(S("1"));
45-
test(S("1234567890123456789012345678901234567890"));
46-
}
44+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
4745
#endif
4846

4947
return true;

libcxx/test/std/strings/basic.string/string.access/index.pass.cpp

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,28 @@
1717
#include "test_macros.h"
1818
#include "min_allocator.h"
1919

20-
TEST_CONSTEXPR_CXX20 bool test() {
20+
template <class S>
21+
TEST_CONSTEXPR_CXX20 void test_string() {
22+
S s("0123456789");
23+
const S& cs = s;
24+
ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
25+
ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
26+
LIBCPP_ASSERT_NOEXCEPT( s[0]);
27+
LIBCPP_ASSERT_NOEXCEPT( cs[0]);
28+
for (typename S::size_type i = 0; i < cs.size(); ++i)
2129
{
22-
typedef std::string S;
23-
S s("0123456789");
24-
const S& cs = s;
25-
ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
26-
ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
27-
LIBCPP_ASSERT_NOEXCEPT( s[0]);
28-
LIBCPP_ASSERT_NOEXCEPT( cs[0]);
29-
for (S::size_type i = 0; i < cs.size(); ++i)
30-
{
31-
assert(s[i] == static_cast<char>('0' + i));
32-
assert(cs[i] == s[i]);
33-
}
34-
assert(cs[cs.size()] == '\0');
35-
const S s2 = S();
36-
assert(s2[0] == '\0');
30+
assert(s[i] == static_cast<char>('0' + i));
31+
assert(cs[i] == s[i]);
3732
}
33+
assert(cs[cs.size()] == '\0');
34+
const S s2 = S();
35+
assert(s2[0] == '\0');
36+
}
37+
38+
TEST_CONSTEXPR_CXX20 bool test() {
39+
test_string<std::string>();
3840
#if TEST_STD_VER >= 11
39-
{
40-
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
41-
S s("0123456789");
42-
const S& cs = s;
43-
ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
44-
ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
45-
LIBCPP_ASSERT_NOEXCEPT( s[0]);
46-
LIBCPP_ASSERT_NOEXCEPT( cs[0]);
47-
for (S::size_type i = 0; i < cs.size(); ++i)
48-
{
49-
assert(s[i] == static_cast<char>('0' + i));
50-
assert(cs[i] == s[i]);
51-
}
52-
assert(cs[cs.size()] == '\0');
53-
const S s2 = S();
54-
assert(s2[0] == '\0');
55-
}
41+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
5642
#endif
5743

5844
return true;

libcxx/test/std/strings/basic.string/string.capacity/clear.pass.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,24 @@ test(S s)
2424
assert(s.size() == 0);
2525
}
2626

27-
TEST_CONSTEXPR_CXX20 bool test() {
28-
{
29-
typedef std::string S;
30-
S s;
31-
test(s);
32-
33-
s.assign(10, 'a');
34-
s.erase(5);
35-
test(s);
27+
template <class S>
28+
TEST_CONSTEXPR_CXX20 void test_string() {
29+
S s;
30+
test(s);
3631

37-
s.assign(100, 'a');
38-
s.erase(50);
39-
test(s);
40-
}
41-
#if TEST_STD_VER >= 11
42-
{
43-
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
44-
S s;
45-
test(s);
32+
s.assign(10, 'a');
33+
s.erase(5);
34+
test(s);
4635

47-
s.assign(10, 'a');
48-
s.erase(5);
49-
test(s);
36+
s.assign(100, 'a');
37+
s.erase(50);
38+
test(s);
39+
}
5040

51-
s.assign(100, 'a');
52-
s.erase(50);
53-
test(s);
54-
}
41+
TEST_CONSTEXPR_CXX20 bool test() {
42+
test_string<std::string>();
43+
#if TEST_STD_VER >= 11
44+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
5545
#endif
5646

5747
return true;

libcxx/test/std/strings/basic.string/string.capacity/empty.pass.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,17 @@ test(const S& s)
2424
assert(s.empty() == (s.size() == 0));
2525
}
2626

27+
template <class S>
28+
TEST_CONSTEXPR_CXX20 void test_string() {
29+
test(S());
30+
test(S("123"));
31+
test(S("12345678901234567890123456789012345678901234567890"));
32+
}
33+
2734
TEST_CONSTEXPR_CXX20 bool test() {
28-
{
29-
typedef std::string S;
30-
test(S());
31-
test(S("123"));
32-
test(S("12345678901234567890123456789012345678901234567890"));
33-
}
35+
test_string<std::string>();
3436
#if TEST_STD_VER >= 11
35-
{
36-
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
37-
test(S());
38-
test(S("123"));
39-
test(S("12345678901234567890123456789012345678901234567890"));
40-
}
37+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
4138
#endif
4239

4340
return true;

libcxx/test/std/strings/basic.string/string.capacity/length.pass.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,17 @@ test(const S& s)
2323
assert(s.length() == s.size());
2424
}
2525

26+
template <class S>
27+
TEST_CONSTEXPR_CXX20 void test_string() {
28+
test(S());
29+
test(S("123"));
30+
test(S("12345678901234567890123456789012345678901234567890"));
31+
}
32+
2633
TEST_CONSTEXPR_CXX20 bool test() {
27-
{
28-
typedef std::string S;
29-
test(S());
30-
test(S("123"));
31-
test(S("12345678901234567890123456789012345678901234567890"));
32-
}
34+
test_string<std::string>();
3335
#if TEST_STD_VER >= 11
34-
{
35-
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
36-
test(S());
37-
test(S("123"));
38-
test(S("12345678901234567890123456789012345678901234567890"));
39-
}
36+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
4037
#endif
4138

4239
return true;

libcxx/test/std/strings/basic.string/string.capacity/max_size.pass.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,20 @@ test(const S& s)
5555
test2(s);
5656
}
5757

58-
void test() {
59-
{
60-
typedef std::string S;
61-
test(S());
62-
test(S("123"));
63-
test(S("12345678901234567890123456789012345678901234567890"));
64-
}
58+
template <class S>
59+
TEST_CONSTEXPR_CXX20 void test_string() {
60+
test(S());
61+
test(S("123"));
62+
test(S("12345678901234567890123456789012345678901234567890"));
63+
}
64+
65+
TEST_CONSTEXPR_CXX20 bool test() {
66+
test_string<std::string>();
6567
#if TEST_STD_VER >= 11
66-
{
67-
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
68-
test(S());
69-
test(S("123"));
70-
test(S("12345678901234567890123456789012345678901234567890"));
71-
}
68+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
7269
#endif
70+
71+
return true;
7372
}
7473

7574
#if TEST_STD_VER > 17

libcxx/test/std/strings/basic.string/string.capacity/over_max_size.pass.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,17 @@ test(const S& s)
3737
assert ( false );
3838
}
3939

40+
template <class S>
41+
void test_string() {
42+
test(S());
43+
test(S("123"));
44+
test(S("12345678901234567890123456789012345678901234567890"));
45+
}
46+
4047
bool test() {
41-
{
42-
typedef std::string S;
43-
test(S());
44-
test(S("123"));
45-
test(S("12345678901234567890123456789012345678901234567890"));
46-
}
48+
test_string<std::string>();
4749
#if TEST_STD_VER >= 11
48-
{
49-
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
50-
test(S());
51-
test(S("123"));
52-
test(S("12345678901234567890123456789012345678901234567890"));
53-
}
50+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
5451
#endif
5552

5653
return true;

libcxx/test/std/strings/basic.string/string.capacity/reserve.pass.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,17 @@ test(typename S::size_type min_cap, typename S::size_type erased_index)
3737
assert(s.capacity() >= s.size());
3838
}
3939

40+
template <class S>
41+
void test_string() {
42+
test<S>(0, 0);
43+
test<S>(10, 5);
44+
test<S>(100, 50);
45+
}
46+
4047
bool test() {
41-
{
42-
typedef std::string S;
43-
{
44-
test<S>(0, 0);
45-
test<S>(10, 5);
46-
test<S>(100, 50);
47-
}
48-
}
48+
test_string<std::string>();
4949
#if TEST_STD_VER >= 11
50-
{
51-
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
52-
{
53-
test<S>(0, 0);
54-
test<S>(10, 5);
55-
test<S>(100, 50);
56-
}
57-
}
50+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
5851
#endif
5952

6053
return true;

0 commit comments

Comments
 (0)