diff --git a/libcxx/test/support/make_string.h b/libcxx/test/support/make_string.h new file mode 100644 --- /dev/null +++ b/libcxx/test/support/make_string.h @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_TEST_MAKE_STRING_H +#define SUPPORT_TEST_MAKE_STRING_H + +#include "test_macros.h" + +#if TEST_STD_VER < 11 +#error This header requires C++11 or greater +#endif + +#include +#include +#include + +// Some of these std::codecvt specializations have been deprecated. At the +// moment are no replacements in the Standard so use the deprecated facilities. +_LIBCPP_SUPPRESS_DEPRECATED_PUSH +// Helper function to convert a const char* string to a basic_string. +// This helper is used in unit tests to make them generic. The input should be +// valid ASCII which means the input is also valid UTF-8. +template +std::basic_string make_string(const char* str) { + struct convertor_type : public std::codecvt { + using std::codecvt::codecvt; + ~convertor_type() = default; + }; + + std::wstring_convert convertor; + return convertor.from_bytes(str); +} +_LIBCPP_SUPPRESS_DEPRECATED_POP + +template <> +std::basic_string make_string(const char* str) { + return std::basic_string(str); +} + +#ifndef _LIBCPP_NO_HAS_CHAR8_T +template <> +std::basic_string make_string(const char* str) { + std::basic_string result; + auto out = std::back_inserter(result); + while (*str) + out = *str++; + + return result; +} +#endif + +#endif diff --git a/libcxx/test/support/test.support/make_string_header.pass.cpp b/libcxx/test/support/test.support/make_string_header.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/support/test.support/make_string_header.pass.cpp @@ -0,0 +1,52 @@ +//===----------------------------------------------------------------------===// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03 +// UNSUPPORTED: libcpp-has-no-localization + +// "support/make_string.h" + +#include "make_string.h" +#include + +#include "test_macros.h" + +int main(int, char**) { + assert( + make_string(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" + "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~") == + " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" + "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); + + assert(make_string( + " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" + "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~") == + L" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" + "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); +#ifndef _LIBCPP_NO_HAS_CHAR8_T + assert(make_string( + " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" + "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~") == + u8" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" + "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); +#endif +#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS + assert(make_string( + " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" + "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~") == + u" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" + "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); + + assert(make_string( + " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" + "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~") == + U" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" + "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); +#endif + + return 0; +}