Skip to content

Commit 60cc48d

Browse files
committed
[libc++] Refactor strings operator+ tests
This avoids duplicating the test data for all the different tests.
1 parent 191d7d6 commit 60cc48d

File tree

5 files changed

+134
-227
lines changed

5 files changed

+134
-227
lines changed

libcxx/test/std/strings/basic.string/string.nonmembers/string_op+/char_string.pass.cpp

+16-23
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,32 @@
1616
// basic_string<charT,traits,Allocator>&&
1717
// operator+(charT lhs, basic_string<charT,traits,Allocator>&& rhs); // constexpr since C++20
1818

19+
#include <cassert>
1920
#include <string>
2021
#include <utility>
21-
#include <cassert>
2222

2323
#include "test_macros.h"
2424
#include "min_allocator.h"
2525
#include "asan_testing.h"
2626

27-
template <class S>
28-
TEST_CONSTEXPR_CXX20 void test0(typename S::value_type lhs, const S& rhs, const S& x) {
29-
assert(lhs + rhs == x);
30-
LIBCPP_ASSERT(is_string_asan_correct(lhs + rhs));
31-
}
32-
33-
#if TEST_STD_VER >= 11
34-
template <class S>
35-
TEST_CONSTEXPR_CXX20 void test1(typename S::value_type lhs, S&& rhs, const S& x) {
36-
assert(lhs + std::move(rhs) == x);
37-
}
38-
#endif
39-
4027
template <class S>
4128
TEST_CONSTEXPR_CXX20 void test_string() {
42-
test0('a', S(""), S("a"));
43-
test0('a', S("12345"), S("a12345"));
44-
test0('a', S("1234567890"), S("a1234567890"));
45-
test0('a', S("12345678901234567890"), S("a12345678901234567890"));
29+
const char* test_data[] = {"", "12345", "1234567890", "12345678901234567890"};
30+
const char* results[] = {"a", "a12345", "a1234567890", "a12345678901234567890"};
31+
32+
for (size_t i = 0; i != 4; ++i) {
33+
{ // operator+(value_type, const string&);
34+
const S str(test_data[i]);
35+
assert('a' + str == results[i]);
36+
LIBCPP_ASSERT(is_string_asan_correct('a' + str));
37+
}
4638
#if TEST_STD_VER >= 11
47-
test1('a', S(""), S("a"));
48-
test1('a', S("12345"), S("a12345"));
49-
test1('a', S("1234567890"), S("a1234567890"));
50-
test1('a', S("12345678901234567890"), S("a12345678901234567890"));
39+
{ // operator+(value_type, string&&);
40+
S str(test_data[i]);
41+
assert('a' + std::move(str) == results[i]);
42+
}
5143
#endif
44+
}
5245
}
5346

5447
TEST_CONSTEXPR_CXX20 bool test() {
@@ -63,7 +56,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
6356

6457
int main(int, char**) {
6558
test();
66-
#if TEST_STD_VER > 17
59+
#if TEST_STD_VER >= 20
6760
static_assert(test());
6861
#endif
6962

libcxx/test/std/strings/basic.string/string.nonmembers/string_op+/pointer_string.pass.cpp

+28-47
Original file line numberDiff line numberDiff line change
@@ -16,76 +16,57 @@
1616
// basic_string<charT,traits,Allocator>&&
1717
// operator+(const charT* lhs, basic_string<charT,traits,Allocator>&& rhs); // constexpr since C++20
1818

19+
#include <cassert>
1920
#include <string>
2021
#include <utility>
21-
#include <cassert>
2222

2323
#include "test_macros.h"
2424
#include "min_allocator.h"
2525

26-
template <class S>
27-
TEST_CONSTEXPR_CXX20 void test0(const typename S::value_type* lhs, const S& rhs, const S& x) {
28-
assert(lhs + rhs == x);
29-
}
30-
31-
#if TEST_STD_VER >= 11
32-
template <class S>
33-
TEST_CONSTEXPR_CXX20 void test1(const typename S::value_type* lhs, S&& rhs, const S& x) {
34-
assert(lhs + std::move(rhs) == x);
35-
}
36-
#endif
37-
3826
template <class S>
3927
TEST_CONSTEXPR_CXX20 void test_string() {
40-
test0("", S(""), S(""));
41-
test0("", S("12345"), S("12345"));
42-
test0("", S("1234567890"), S("1234567890"));
43-
test0("", S("12345678901234567890"), S("12345678901234567890"));
44-
test0("abcde", S(""), S("abcde"));
45-
test0("abcde", S("12345"), S("abcde12345"));
46-
test0("abcde", S("1234567890"), S("abcde1234567890"));
47-
test0("abcde", S("12345678901234567890"), S("abcde12345678901234567890"));
48-
test0("abcdefghij", S(""), S("abcdefghij"));
49-
test0("abcdefghij", S("12345"), S("abcdefghij12345"));
50-
test0("abcdefghij", S("1234567890"), S("abcdefghij1234567890"));
51-
test0("abcdefghij", S("12345678901234567890"), S("abcdefghij12345678901234567890"));
52-
test0("abcdefghijklmnopqrst", S(""), S("abcdefghijklmnopqrst"));
53-
test0("abcdefghijklmnopqrst", S("12345"), S("abcdefghijklmnopqrst12345"));
54-
test0("abcdefghijklmnopqrst", S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
55-
test0("abcdefghijklmnopqrst", S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
28+
const char* test_data[2][4] = {
29+
{"", "abcde", "abcdefghij", "abcdefghijklmnopqrst"}, {"", "12345", "1234567890", "12345678901234567890"}};
30+
31+
const char* results[4][4] = {
32+
{"", "12345", "1234567890", "12345678901234567890"},
33+
{"abcde", "abcde12345", "abcde1234567890", "abcde12345678901234567890"},
34+
{"abcdefghij", "abcdefghij12345", "abcdefghij1234567890", "abcdefghij12345678901234567890"},
35+
{"abcdefghijklmnopqrst",
36+
"abcdefghijklmnopqrst12345",
37+
"abcdefghijklmnopqrst1234567890",
38+
"abcdefghijklmnopqrst12345678901234567890"}};
5639

40+
for (size_t i = 0; i != 4; ++i) {
41+
for (size_t k = 0; k != 4; ++k) {
42+
{ // operator+(const value_type*, const string&);
43+
const char* lhs = test_data[0][i];
44+
const S rhs(test_data[1][k]);
45+
assert(lhs + rhs == results[i][k]);
46+
}
5747
#if TEST_STD_VER >= 11
58-
test1("", S(""), S(""));
59-
test1("", S("12345"), S("12345"));
60-
test1("", S("1234567890"), S("1234567890"));
61-
test1("", S("12345678901234567890"), S("12345678901234567890"));
62-
test1("abcde", S(""), S("abcde"));
63-
test1("abcde", S("12345"), S("abcde12345"));
64-
test1("abcde", S("1234567890"), S("abcde1234567890"));
65-
test1("abcde", S("12345678901234567890"), S("abcde12345678901234567890"));
66-
test1("abcdefghij", S(""), S("abcdefghij"));
67-
test1("abcdefghij", S("12345"), S("abcdefghij12345"));
68-
test1("abcdefghij", S("1234567890"), S("abcdefghij1234567890"));
69-
test1("abcdefghij", S("12345678901234567890"), S("abcdefghij12345678901234567890"));
70-
test1("abcdefghijklmnopqrst", S(""), S("abcdefghijklmnopqrst"));
71-
test1("abcdefghijklmnopqrst", S("12345"), S("abcdefghijklmnopqrst12345"));
72-
test1("abcdefghijklmnopqrst", S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
73-
test1("abcdefghijklmnopqrst", S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
48+
{ // operator+(const value_type*, string&&);
49+
const char* lhs = test_data[0][i];
50+
S rhs(test_data[1][k]);
51+
assert(lhs + std::move(rhs) == results[i][k]);
52+
}
7453
#endif
54+
}
55+
}
7556
}
7657

7758
TEST_CONSTEXPR_CXX20 bool test() {
7859
test_string<std::string>();
7960
#if TEST_STD_VER >= 11
80-
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
61+
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
8162
#endif
8263

8364
return true;
8465
}
8566

8667
int main(int, char**) {
8768
test();
88-
#if TEST_STD_VER > 17
69+
#if TEST_STD_VER >= 20
8970
static_assert(test());
9071
#endif
9172

libcxx/test/std/strings/basic.string/string.nonmembers/string_op+/string_char.pass.cpp

+17-26
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,32 @@
1616
// basic_string<charT,traits,Allocator>&&
1717
// operator+(basic_string<charT,traits,Allocator>&& lhs, charT rhs); // constexpr since C++20
1818

19+
#include <cassert>
1920
#include <string>
2021
#include <utility>
21-
#include <cassert>
2222

23-
#include "test_macros.h"
24-
#include "min_allocator.h"
2523
#include "asan_testing.h"
26-
27-
template <class S>
28-
TEST_CONSTEXPR_CXX20 void test0(const S& lhs, typename S::value_type rhs, const S& x) {
29-
assert(lhs + rhs == x);
30-
LIBCPP_ASSERT(is_string_asan_correct(lhs + rhs));
31-
}
32-
33-
#if TEST_STD_VER >= 11
34-
template <class S>
35-
TEST_CONSTEXPR_CXX20 void test1(S&& lhs, typename S::value_type rhs, const S& x) {
36-
assert(std::move(lhs) + rhs == x);
37-
}
38-
#endif
24+
#include "min_allocator.h"
25+
#include "test_macros.h"
3926

4027
template <class S>
4128
TEST_CONSTEXPR_CXX20 void test_string() {
42-
test0(S(""), '1', S("1"));
43-
test0(S(""), '1', S("1"));
44-
test0(S("abcde"), '1', S("abcde1"));
45-
test0(S("abcdefghij"), '1', S("abcdefghij1"));
46-
test0(S("abcdefghijklmnopqrst"), '1', S("abcdefghijklmnopqrst1"));
29+
const char* test_data[] = {"", "12345", "1234567890", "12345678901234567890"};
30+
const char* results[] = {"a", "12345a", "1234567890a", "12345678901234567890a"};
4731

32+
for (size_t i = 0; i != 4; ++i) {
33+
{ // operator+(const string&, value_type);
34+
const S str(test_data[i]);
35+
assert(str + 'a' == results[i]);
36+
LIBCPP_ASSERT(is_string_asan_correct(str + 'a'));
37+
}
4838
#if TEST_STD_VER >= 11
49-
test1(S(""), '1', S("1"));
50-
test1(S("abcde"), '1', S("abcde1"));
51-
test1(S("abcdefghij"), '1', S("abcdefghij1"));
52-
test1(S("abcdefghijklmnopqrst"), '1', S("abcdefghijklmnopqrst1"));
39+
{ // operator+(string&&, value_type);
40+
S str(test_data[i]);
41+
assert(std::move(str) + 'a' == results[i]);
42+
}
5343
#endif
44+
}
5445
}
5546

5647
TEST_CONSTEXPR_CXX20 bool test() {
@@ -65,7 +56,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
6556

6657
int main(int, char**) {
6758
test();
68-
#if TEST_STD_VER > 17
59+
#if TEST_STD_VER >= 20
6960
static_assert(test());
7061
#endif
7162

libcxx/test/std/strings/basic.string/string.nonmembers/string_op+/string_pointer.pass.cpp

+30-36
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
// basic_string<charT,traits,Allocator>&&
1717
// operator+(basic_string<charT,traits,Allocator>&& lhs, const charT* rhs); // constexpr since C++20
1818

19+
#include <cassert>
1920
#include <string>
2021
#include <utility>
21-
#include <cassert>
2222

23-
#include "test_macros.h"
24-
#include "min_allocator.h"
2523
#include "asan_testing.h"
24+
#include "min_allocator.h"
25+
#include "test_macros.h"
2626

2727
template <class S>
2828
TEST_CONSTEXPR_CXX20 void test0(const S& lhs, const typename S::value_type* rhs, const S& x) {
@@ -39,40 +39,34 @@ TEST_CONSTEXPR_CXX20 void test1(S&& lhs, const typename S::value_type* rhs, cons
3939

4040
template <class S>
4141
TEST_CONSTEXPR_CXX20 void test_string() {
42-
test0(S(""), "", S(""));
43-
test0(S(""), "12345", S("12345"));
44-
test0(S(""), "1234567890", S("1234567890"));
45-
test0(S(""), "12345678901234567890", S("12345678901234567890"));
46-
test0(S("abcde"), "", S("abcde"));
47-
test0(S("abcde"), "12345", S("abcde12345"));
48-
test0(S("abcde"), "1234567890", S("abcde1234567890"));
49-
test0(S("abcde"), "12345678901234567890", S("abcde12345678901234567890"));
50-
test0(S("abcdefghij"), "", S("abcdefghij"));
51-
test0(S("abcdefghij"), "12345", S("abcdefghij12345"));
52-
test0(S("abcdefghij"), "1234567890", S("abcdefghij1234567890"));
53-
test0(S("abcdefghij"), "12345678901234567890", S("abcdefghij12345678901234567890"));
54-
test0(S("abcdefghijklmnopqrst"), "", S("abcdefghijklmnopqrst"));
55-
test0(S("abcdefghijklmnopqrst"), "12345", S("abcdefghijklmnopqrst12345"));
56-
test0(S("abcdefghijklmnopqrst"), "1234567890", S("abcdefghijklmnopqrst1234567890"));
57-
test0(S("abcdefghijklmnopqrst"), "12345678901234567890", S("abcdefghijklmnopqrst12345678901234567890"));
42+
const char* test_data[2][4] = {
43+
{"", "abcde", "abcdefghij", "abcdefghijklmnopqrst"}, {"", "12345", "1234567890", "12345678901234567890"}};
44+
45+
const char* results[4][4] = {
46+
{"", "12345", "1234567890", "12345678901234567890"},
47+
{"abcde", "abcde12345", "abcde1234567890", "abcde12345678901234567890"},
48+
{"abcdefghij", "abcdefghij12345", "abcdefghij1234567890", "abcdefghij12345678901234567890"},
49+
{"abcdefghijklmnopqrst",
50+
"abcdefghijklmnopqrst12345",
51+
"abcdefghijklmnopqrst1234567890",
52+
"abcdefghijklmnopqrst12345678901234567890"}};
53+
54+
for (size_t i = 0; i != 4; ++i) {
55+
for (size_t k = 0; k != 4; ++k) {
56+
{ // operator+(const value_type*, const string&);
57+
const S lhs(test_data[0][i]);
58+
const char* rhs = test_data[1][k];
59+
assert(lhs + rhs == results[i][k]);
60+
}
5861
#if TEST_STD_VER >= 11
59-
test1(S(""), "", S(""));
60-
test1(S(""), "12345", S("12345"));
61-
test1(S(""), "1234567890", S("1234567890"));
62-
test1(S(""), "12345678901234567890", S("12345678901234567890"));
63-
test1(S("abcde"), "", S("abcde"));
64-
test1(S("abcde"), "12345", S("abcde12345"));
65-
test1(S("abcde"), "1234567890", S("abcde1234567890"));
66-
test1(S("abcde"), "12345678901234567890", S("abcde12345678901234567890"));
67-
test1(S("abcdefghij"), "", S("abcdefghij"));
68-
test1(S("abcdefghij"), "12345", S("abcdefghij12345"));
69-
test1(S("abcdefghij"), "1234567890", S("abcdefghij1234567890"));
70-
test1(S("abcdefghij"), "12345678901234567890", S("abcdefghij12345678901234567890"));
71-
test1(S("abcdefghijklmnopqrst"), "", S("abcdefghijklmnopqrst"));
72-
test1(S("abcdefghijklmnopqrst"), "12345", S("abcdefghijklmnopqrst12345"));
73-
test1(S("abcdefghijklmnopqrst"), "1234567890", S("abcdefghijklmnopqrst1234567890"));
74-
test1(S("abcdefghijklmnopqrst"), "12345678901234567890", S("abcdefghijklmnopqrst12345678901234567890"));
62+
{ // operator+(const value_type*, string&&);
63+
S lhs(test_data[0][i]);
64+
const char* rhs = test_data[1][k];
65+
assert(std::move(lhs) + rhs == results[i][k]);
66+
}
7567
#endif
68+
}
69+
}
7670
}
7771

7872
TEST_CONSTEXPR_CXX20 bool test() {
@@ -87,7 +81,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
8781

8882
int main(int, char**) {
8983
test();
90-
#if TEST_STD_VER > 17
84+
#if TEST_STD_VER >= 20
9185
static_assert(test());
9286
#endif
9387

0 commit comments

Comments
 (0)