[libc++] Refactor strings operator+ tests

This avoids duplicating the test data for all the different tests.
This commit is contained in:
Nikolas Klauser 2025-02-07 14:06:29 +01:00
parent 191d7d64e5
commit 60cc48d900
5 changed files with 134 additions and 227 deletions

View File

@ -16,39 +16,32 @@
// basic_string<charT,traits,Allocator>&&
// operator+(charT lhs, basic_string<charT,traits,Allocator>&& rhs); // constexpr since C++20
#include <cassert>
#include <string>
#include <utility>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
#include "asan_testing.h"
template <class S>
TEST_CONSTEXPR_CXX20 void test0(typename S::value_type lhs, const S& rhs, const S& x) {
assert(lhs + rhs == x);
LIBCPP_ASSERT(is_string_asan_correct(lhs + rhs));
}
#if TEST_STD_VER >= 11
template <class S>
TEST_CONSTEXPR_CXX20 void test1(typename S::value_type lhs, S&& rhs, const S& x) {
assert(lhs + std::move(rhs) == x);
}
#endif
template <class S>
TEST_CONSTEXPR_CXX20 void test_string() {
test0('a', S(""), S("a"));
test0('a', S("12345"), S("a12345"));
test0('a', S("1234567890"), S("a1234567890"));
test0('a', S("12345678901234567890"), S("a12345678901234567890"));
const char* test_data[] = {"", "12345", "1234567890", "12345678901234567890"};
const char* results[] = {"a", "a12345", "a1234567890", "a12345678901234567890"};
for (size_t i = 0; i != 4; ++i) {
{ // operator+(value_type, const string&);
const S str(test_data[i]);
assert('a' + str == results[i]);
LIBCPP_ASSERT(is_string_asan_correct('a' + str));
}
#if TEST_STD_VER >= 11
test1('a', S(""), S("a"));
test1('a', S("12345"), S("a12345"));
test1('a', S("1234567890"), S("a1234567890"));
test1('a', S("12345678901234567890"), S("a12345678901234567890"));
{ // operator+(value_type, string&&);
S str(test_data[i]);
assert('a' + std::move(str) == results[i]);
}
#endif
}
}
TEST_CONSTEXPR_CXX20 bool test() {
@ -63,7 +56,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
int main(int, char**) {
test();
#if TEST_STD_VER > 17
#if TEST_STD_VER >= 20
static_assert(test());
#endif

View File

@ -16,68 +16,49 @@
// basic_string<charT,traits,Allocator>&&
// operator+(const charT* lhs, basic_string<charT,traits,Allocator>&& rhs); // constexpr since C++20
#include <cassert>
#include <string>
#include <utility>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void test0(const typename S::value_type* lhs, const S& rhs, const S& x) {
assert(lhs + rhs == x);
}
#if TEST_STD_VER >= 11
template <class S>
TEST_CONSTEXPR_CXX20 void test1(const typename S::value_type* lhs, S&& rhs, const S& x) {
assert(lhs + std::move(rhs) == x);
}
#endif
template <class S>
TEST_CONSTEXPR_CXX20 void test_string() {
test0("", S(""), S(""));
test0("", S("12345"), S("12345"));
test0("", S("1234567890"), S("1234567890"));
test0("", S("12345678901234567890"), S("12345678901234567890"));
test0("abcde", S(""), S("abcde"));
test0("abcde", S("12345"), S("abcde12345"));
test0("abcde", S("1234567890"), S("abcde1234567890"));
test0("abcde", S("12345678901234567890"), S("abcde12345678901234567890"));
test0("abcdefghij", S(""), S("abcdefghij"));
test0("abcdefghij", S("12345"), S("abcdefghij12345"));
test0("abcdefghij", S("1234567890"), S("abcdefghij1234567890"));
test0("abcdefghij", S("12345678901234567890"), S("abcdefghij12345678901234567890"));
test0("abcdefghijklmnopqrst", S(""), S("abcdefghijklmnopqrst"));
test0("abcdefghijklmnopqrst", S("12345"), S("abcdefghijklmnopqrst12345"));
test0("abcdefghijklmnopqrst", S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
test0("abcdefghijklmnopqrst", S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
const char* test_data[2][4] = {
{"", "abcde", "abcdefghij", "abcdefghijklmnopqrst"}, {"", "12345", "1234567890", "12345678901234567890"}};
const char* results[4][4] = {
{"", "12345", "1234567890", "12345678901234567890"},
{"abcde", "abcde12345", "abcde1234567890", "abcde12345678901234567890"},
{"abcdefghij", "abcdefghij12345", "abcdefghij1234567890", "abcdefghij12345678901234567890"},
{"abcdefghijklmnopqrst",
"abcdefghijklmnopqrst12345",
"abcdefghijklmnopqrst1234567890",
"abcdefghijklmnopqrst12345678901234567890"}};
for (size_t i = 0; i != 4; ++i) {
for (size_t k = 0; k != 4; ++k) {
{ // operator+(const value_type*, const string&);
const char* lhs = test_data[0][i];
const S rhs(test_data[1][k]);
assert(lhs + rhs == results[i][k]);
}
#if TEST_STD_VER >= 11
test1("", S(""), S(""));
test1("", S("12345"), S("12345"));
test1("", S("1234567890"), S("1234567890"));
test1("", S("12345678901234567890"), S("12345678901234567890"));
test1("abcde", S(""), S("abcde"));
test1("abcde", S("12345"), S("abcde12345"));
test1("abcde", S("1234567890"), S("abcde1234567890"));
test1("abcde", S("12345678901234567890"), S("abcde12345678901234567890"));
test1("abcdefghij", S(""), S("abcdefghij"));
test1("abcdefghij", S("12345"), S("abcdefghij12345"));
test1("abcdefghij", S("1234567890"), S("abcdefghij1234567890"));
test1("abcdefghij", S("12345678901234567890"), S("abcdefghij12345678901234567890"));
test1("abcdefghijklmnopqrst", S(""), S("abcdefghijklmnopqrst"));
test1("abcdefghijklmnopqrst", S("12345"), S("abcdefghijklmnopqrst12345"));
test1("abcdefghijklmnopqrst", S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
test1("abcdefghijklmnopqrst", S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
{ // operator+(const value_type*, string&&);
const char* lhs = test_data[0][i];
S rhs(test_data[1][k]);
assert(lhs + std::move(rhs) == results[i][k]);
}
#endif
}
}
}
TEST_CONSTEXPR_CXX20 bool test() {
test_string<std::string>();
#if TEST_STD_VER >= 11
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
#endif
return true;
@ -85,7 +66,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
int main(int, char**) {
test();
#if TEST_STD_VER > 17
#if TEST_STD_VER >= 20
static_assert(test());
#endif

View File

@ -16,41 +16,32 @@
// basic_string<charT,traits,Allocator>&&
// operator+(basic_string<charT,traits,Allocator>&& lhs, charT rhs); // constexpr since C++20
#include <cassert>
#include <string>
#include <utility>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
#include "asan_testing.h"
template <class S>
TEST_CONSTEXPR_CXX20 void test0(const S& lhs, typename S::value_type rhs, const S& x) {
assert(lhs + rhs == x);
LIBCPP_ASSERT(is_string_asan_correct(lhs + rhs));
}
#if TEST_STD_VER >= 11
template <class S>
TEST_CONSTEXPR_CXX20 void test1(S&& lhs, typename S::value_type rhs, const S& x) {
assert(std::move(lhs) + rhs == x);
}
#endif
#include "min_allocator.h"
#include "test_macros.h"
template <class S>
TEST_CONSTEXPR_CXX20 void test_string() {
test0(S(""), '1', S("1"));
test0(S(""), '1', S("1"));
test0(S("abcde"), '1', S("abcde1"));
test0(S("abcdefghij"), '1', S("abcdefghij1"));
test0(S("abcdefghijklmnopqrst"), '1', S("abcdefghijklmnopqrst1"));
const char* test_data[] = {"", "12345", "1234567890", "12345678901234567890"};
const char* results[] = {"a", "12345a", "1234567890a", "12345678901234567890a"};
for (size_t i = 0; i != 4; ++i) {
{ // operator+(const string&, value_type);
const S str(test_data[i]);
assert(str + 'a' == results[i]);
LIBCPP_ASSERT(is_string_asan_correct(str + 'a'));
}
#if TEST_STD_VER >= 11
test1(S(""), '1', S("1"));
test1(S("abcde"), '1', S("abcde1"));
test1(S("abcdefghij"), '1', S("abcdefghij1"));
test1(S("abcdefghijklmnopqrst"), '1', S("abcdefghijklmnopqrst1"));
{ // operator+(string&&, value_type);
S str(test_data[i]);
assert(std::move(str) + 'a' == results[i]);
}
#endif
}
}
TEST_CONSTEXPR_CXX20 bool test() {
@ -65,7 +56,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
int main(int, char**) {
test();
#if TEST_STD_VER > 17
#if TEST_STD_VER >= 20
static_assert(test());
#endif

View File

@ -16,13 +16,13 @@
// basic_string<charT,traits,Allocator>&&
// operator+(basic_string<charT,traits,Allocator>&& lhs, const charT* rhs); // constexpr since C++20
#include <cassert>
#include <string>
#include <utility>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
#include "asan_testing.h"
#include "min_allocator.h"
#include "test_macros.h"
template <class S>
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
template <class S>
TEST_CONSTEXPR_CXX20 void test_string() {
test0(S(""), "", S(""));
test0(S(""), "12345", S("12345"));
test0(S(""), "1234567890", S("1234567890"));
test0(S(""), "12345678901234567890", S("12345678901234567890"));
test0(S("abcde"), "", S("abcde"));
test0(S("abcde"), "12345", S("abcde12345"));
test0(S("abcde"), "1234567890", S("abcde1234567890"));
test0(S("abcde"), "12345678901234567890", S("abcde12345678901234567890"));
test0(S("abcdefghij"), "", S("abcdefghij"));
test0(S("abcdefghij"), "12345", S("abcdefghij12345"));
test0(S("abcdefghij"), "1234567890", S("abcdefghij1234567890"));
test0(S("abcdefghij"), "12345678901234567890", S("abcdefghij12345678901234567890"));
test0(S("abcdefghijklmnopqrst"), "", S("abcdefghijklmnopqrst"));
test0(S("abcdefghijklmnopqrst"), "12345", S("abcdefghijklmnopqrst12345"));
test0(S("abcdefghijklmnopqrst"), "1234567890", S("abcdefghijklmnopqrst1234567890"));
test0(S("abcdefghijklmnopqrst"), "12345678901234567890", S("abcdefghijklmnopqrst12345678901234567890"));
const char* test_data[2][4] = {
{"", "abcde", "abcdefghij", "abcdefghijklmnopqrst"}, {"", "12345", "1234567890", "12345678901234567890"}};
const char* results[4][4] = {
{"", "12345", "1234567890", "12345678901234567890"},
{"abcde", "abcde12345", "abcde1234567890", "abcde12345678901234567890"},
{"abcdefghij", "abcdefghij12345", "abcdefghij1234567890", "abcdefghij12345678901234567890"},
{"abcdefghijklmnopqrst",
"abcdefghijklmnopqrst12345",
"abcdefghijklmnopqrst1234567890",
"abcdefghijklmnopqrst12345678901234567890"}};
for (size_t i = 0; i != 4; ++i) {
for (size_t k = 0; k != 4; ++k) {
{ // operator+(const value_type*, const string&);
const S lhs(test_data[0][i]);
const char* rhs = test_data[1][k];
assert(lhs + rhs == results[i][k]);
}
#if TEST_STD_VER >= 11
test1(S(""), "", S(""));
test1(S(""), "12345", S("12345"));
test1(S(""), "1234567890", S("1234567890"));
test1(S(""), "12345678901234567890", S("12345678901234567890"));
test1(S("abcde"), "", S("abcde"));
test1(S("abcde"), "12345", S("abcde12345"));
test1(S("abcde"), "1234567890", S("abcde1234567890"));
test1(S("abcde"), "12345678901234567890", S("abcde12345678901234567890"));
test1(S("abcdefghij"), "", S("abcdefghij"));
test1(S("abcdefghij"), "12345", S("abcdefghij12345"));
test1(S("abcdefghij"), "1234567890", S("abcdefghij1234567890"));
test1(S("abcdefghij"), "12345678901234567890", S("abcdefghij12345678901234567890"));
test1(S("abcdefghijklmnopqrst"), "", S("abcdefghijklmnopqrst"));
test1(S("abcdefghijklmnopqrst"), "12345", S("abcdefghijklmnopqrst12345"));
test1(S("abcdefghijklmnopqrst"), "1234567890", S("abcdefghijklmnopqrst1234567890"));
test1(S("abcdefghijklmnopqrst"), "12345678901234567890", S("abcdefghijklmnopqrst12345678901234567890"));
{ // operator+(const value_type*, string&&);
S lhs(test_data[0][i]);
const char* rhs = test_data[1][k];
assert(std::move(lhs) + rhs == results[i][k]);
}
#endif
}
}
}
TEST_CONSTEXPR_CXX20 bool test() {
@ -87,7 +81,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
int main(int, char**) {
test();
#if TEST_STD_VER > 17
#if TEST_STD_VER >= 20
static_assert(test());
#endif

View File

@ -28,114 +28,62 @@
// operator+(const basic_string<charT,traits,Allocator>&& lhs,
// const basic_string<charT,traits,Allocator>&& rhs); // constexpr since C++20
#include <cassert>
#include <string>
#include <utility>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
#include "asan_testing.h"
template <class S>
TEST_CONSTEXPR_CXX20 void test0(const S& lhs, const S& rhs, const S& x) {
assert(lhs + rhs == x);
LIBCPP_ASSERT(is_string_asan_correct(lhs + rhs));
}
#if TEST_STD_VER >= 11
template <class S>
TEST_CONSTEXPR_CXX20 void test1(S&& lhs, const S& rhs, const S& x) {
assert(std::move(lhs) + rhs == x);
}
template <class S>
TEST_CONSTEXPR_CXX20 void test2(const S& lhs, S&& rhs, const S& x) {
assert(lhs + std::move(rhs) == x);
}
template <class S>
TEST_CONSTEXPR_CXX20 void test3(S&& lhs, S&& rhs, const S& x) {
assert(std::move(lhs) + std::move(rhs) == x);
}
#endif
#include "min_allocator.h"
#include "test_macros.h"
template <class S>
TEST_CONSTEXPR_CXX20 void test_string() {
test0(S(""), S(""), S(""));
test0(S(""), S("12345"), S("12345"));
test0(S(""), S("1234567890"), S("1234567890"));
test0(S(""), S("12345678901234567890"), S("12345678901234567890"));
test0(S("abcde"), S(""), S("abcde"));
test0(S("abcde"), S("12345"), S("abcde12345"));
test0(S("abcde"), S("1234567890"), S("abcde1234567890"));
test0(S("abcde"), S("12345678901234567890"), S("abcde12345678901234567890"));
test0(S("abcdefghij"), S(""), S("abcdefghij"));
test0(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
test0(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
test0(S("abcdefghij"), S("12345678901234567890"), S("abcdefghij12345678901234567890"));
test0(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
test0(S("abcdefghijklmnopqrst"), S("12345"), S("abcdefghijklmnopqrst12345"));
test0(S("abcdefghijklmnopqrst"), S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
test0(S("abcdefghijklmnopqrst"), S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
const char* test_data[2][4] = {
{"", "abcde", "abcdefghij", "abcdefghijklmnopqrst"}, {"", "12345", "1234567890", "12345678901234567890"}};
const char* results[4][4] = {
{"", "12345", "1234567890", "12345678901234567890"},
{"abcde", "abcde12345", "abcde1234567890", "abcde12345678901234567890"},
{"abcdefghij", "abcdefghij12345", "abcdefghij1234567890", "abcdefghij12345678901234567890"},
{"abcdefghijklmnopqrst",
"abcdefghijklmnopqrst12345",
"abcdefghijklmnopqrst1234567890",
"abcdefghijklmnopqrst12345678901234567890"}};
for (size_t i = 0; i != 4; ++i) {
for (size_t k = 0; k != 4; ++k) {
{ // operator+(const string&, const string&);
const S lhs(test_data[0][i]);
const S rhs(test_data[1][k]);
assert(lhs + rhs == results[i][k]);
LIBCPP_ASSERT(is_string_asan_correct(lhs + rhs));
}
#if TEST_STD_VER >= 11
test1(S(""), S(""), S(""));
test1(S(""), S("12345"), S("12345"));
test1(S(""), S("1234567890"), S("1234567890"));
test1(S(""), S("12345678901234567890"), S("12345678901234567890"));
test1(S("abcde"), S(""), S("abcde"));
test1(S("abcde"), S("12345"), S("abcde12345"));
test1(S("abcde"), S("1234567890"), S("abcde1234567890"));
test1(S("abcde"), S("12345678901234567890"), S("abcde12345678901234567890"));
test1(S("abcdefghij"), S(""), S("abcdefghij"));
test1(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
test1(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
test1(S("abcdefghij"), S("12345678901234567890"), S("abcdefghij12345678901234567890"));
test1(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
test1(S("abcdefghijklmnopqrst"), S("12345"), S("abcdefghijklmnopqrst12345"));
test1(S("abcdefghijklmnopqrst"), S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
test1(S("abcdefghijklmnopqrst"), S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
test2(S(""), S(""), S(""));
test2(S(""), S("12345"), S("12345"));
test2(S(""), S("1234567890"), S("1234567890"));
test2(S(""), S("12345678901234567890"), S("12345678901234567890"));
test2(S("abcde"), S(""), S("abcde"));
test2(S("abcde"), S("12345"), S("abcde12345"));
test2(S("abcde"), S("1234567890"), S("abcde1234567890"));
test2(S("abcde"), S("12345678901234567890"), S("abcde12345678901234567890"));
test2(S("abcdefghij"), S(""), S("abcdefghij"));
test2(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
test2(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
test2(S("abcdefghij"), S("12345678901234567890"), S("abcdefghij12345678901234567890"));
test2(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
test2(S("abcdefghijklmnopqrst"), S("12345"), S("abcdefghijklmnopqrst12345"));
test2(S("abcdefghijklmnopqrst"), S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
test2(S("abcdefghijklmnopqrst"), S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
test3(S(""), S(""), S(""));
test3(S(""), S("12345"), S("12345"));
test3(S(""), S("1234567890"), S("1234567890"));
test3(S(""), S("12345678901234567890"), S("12345678901234567890"));
test3(S("abcde"), S(""), S("abcde"));
test3(S("abcde"), S("12345"), S("abcde12345"));
test3(S("abcde"), S("1234567890"), S("abcde1234567890"));
test3(S("abcde"), S("12345678901234567890"), S("abcde12345678901234567890"));
test3(S("abcdefghij"), S(""), S("abcdefghij"));
test3(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
test3(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
test3(S("abcdefghij"), S("12345678901234567890"), S("abcdefghij12345678901234567890"));
test3(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
test3(S("abcdefghijklmnopqrst"), S("12345"), S("abcdefghijklmnopqrst12345"));
test3(S("abcdefghijklmnopqrst"), S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
test3(S("abcdefghijklmnopqrst"), S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
{ // operator+(string&&, const string&);
S lhs(test_data[0][i]);
const S rhs(test_data[1][k]);
assert(std::move(lhs) + rhs == results[i][k]);
}
{ // operator+(const string&, string&&);
const S lhs(test_data[0][i]);
S rhs(test_data[1][k]);
assert(lhs + std::move(rhs) == results[i][k]);
}
{ // operator+(string&&, string&&);
S lhs(test_data[0][i]);
S rhs(test_data[1][k]);
assert(std::move(lhs) + std::move(rhs) == results[i][k]);
}
#endif
}
}
}
TEST_CONSTEXPR_CXX20 bool test() {
test_string<std::string>();
#if TEST_STD_VER >= 11
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char> > >();
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
#endif
return true;
@ -143,7 +91,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
int main(int, char**) {
test();
#if TEST_STD_VER > 17
#if TEST_STD_VER >= 20
static_assert(test());
#endif