Skip to content

Commit

Permalink
[libc++][test] Add test coverage for codecvt<char(16|32)_t, char8_t, …
Browse files Browse the repository at this point in the history
…mbstate_t>

This change adds test coverage for the `codecvt<char16_t, char8_t, mbstate_t>` and `codecvt<char32_t, char8_t, mbstate_t>` ctype facets added to the C++20 WD by [P0482R6](https://wg21.link/P0428R6). Note that libc++ does not implement these facets despite implementing the remainder of P0482, presumably for ABI reasons, so these tests are marked `UNSUPPORTED: libc++`.
  • Loading branch information
CaseyCarter committed May 8, 2020
1 parent ad811a2 commit 197f185
Show file tree
Hide file tree
Showing 19 changed files with 851 additions and 102 deletions.
@@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char16_t, char8_t, mbstate_t>

// explicit codecvt(size_t refs = 0);

// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// C++20 codecvt specializations for char8_t are not yet implemented:
// UNSUPPORTED: libc++

#include <cassert>
#include <locale>

using F = std::codecvt<char16_t, char8_t, std::mbstate_t>;

struct my_facet : F {
static int count;

explicit my_facet(std::size_t refs = 0) : F(refs) { ++count; }

~my_facet() { --count; }
};

int my_facet::count = 0;

int main(int, char**) {
{
std::locale l(std::locale::classic(), new my_facet);
assert(my_facet::count == 1);
}
assert(my_facet::count == 0);
{
my_facet f(1);
assert(my_facet::count == 1);
{
std::locale l(std::locale::classic(), &f);
assert(my_facet::count == 1);
}
assert(my_facet::count == 1);
}
assert(my_facet::count == 0);

return 0;
}
@@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char32_t, char8_t, mbstate_t>

// explicit codecvt(size_t refs = 0);

// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// C++20 codecvt specializations for char8_t are not yet implemented:
// UNSUPPORTED: libc++

#include <cassert>
#include <locale>

using F = std::codecvt<char32_t, char8_t, std::mbstate_t>;

struct my_facet : F {
static int count;

explicit my_facet(std::size_t refs = 0) : F(refs) { ++count; }

~my_facet() { --count; }
};

int my_facet::count = 0;

int main(int, char**) {
{
std::locale l(std::locale::classic(), new my_facet);
assert(my_facet::count == 1);
}
assert(my_facet::count == 0);
{
my_facet f(1);
assert(my_facet::count == 1);
{
std::locale l(std::locale::classic(), &f);
assert(my_facet::count == 1);
}
assert(my_facet::count == 1);
}
assert(my_facet::count == 0);

return 0;
}
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char16_t, char8_t, mbstate_t>

// bool always_noconv() const noexcept;

// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// C++20 codecvt specializations for char8_t are not yet implemented:
// UNSUPPORTED: libc++

#include <cassert>
#include <locale>

int main(int, char**) {
using F = std::codecvt<char16_t, char8_t, std::mbstate_t>;
const F& f = std::use_facet<F>(std::locale::classic());
assert(!f.always_noconv());
static_assert(noexcept(f.always_noconv()));
return 0;
}
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char16_t, char8_t, mbstate_t>

// int encoding() const noexcept;

// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// C++20 codecvt specializations for char8_t are not yet implemented:
// UNSUPPORTED: libc++

#include <cassert>
#include <locale>

int main(int, char**) {
using F = std::codecvt<char16_t, char8_t, std::mbstate_t>;
const F& f = std::use_facet<F>(std::locale::classic());
assert(f.encoding() == 0);
static_assert(noexcept(f.encoding()));
return 0;
}
@@ -0,0 +1,39 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char16_t, char8_t, mbstate_t>

// result in(stateT& state,
// const externT* from, const externT* from_end, const externT*& from_next,
// internT* to, internT* to_end, internT*& to_next) const;

// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// C++20 codecvt specializations for char8_t are not yet implemented:
// UNSUPPORTED: libc++

#include <cassert>
#include <locale>

int main(int, char**) {
using F = std::codecvt<char16_t, char8_t, std::mbstate_t>;
const F::extern_type from[] = u8"some text";
F::intern_type to[9];
const F& f = std::use_facet<F>(std::locale::classic());
std::mbstate_t mbs = {};
const F::extern_type* from_next = nullptr;
F::intern_type* to_next = nullptr;
assert(f.in(mbs, from, from + 9, from_next, to, to + 9, to_next) == F::ok);
assert(from_next - from == 9);
assert(to_next - to == 9);
for (unsigned i = 0; i < 9; ++i)
assert(to[i] == from[i]);
return 0;
}
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char16_t, char8_t, mbstate_t>

// int length(stateT& state, const externT* from, const externT* from_end, size_t max) const;

// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// C++20 codecvt specializations for char8_t are not yet implemented:
// UNSUPPORTED: libc++

#include <cassert>
#include <locale>

int main(int, char**) {
using F = std::codecvt<char16_t, char8_t, std::mbstate_t>;
const F& f = std::use_facet<F>(std::locale::classic());
std::mbstate_t mbs = {};
const char8_t from[] = u8"some text";
assert(f.length(mbs, from, from + 10, 0) == 0);
assert(f.length(mbs, from, from + 10, 8) == 8);
assert(f.length(mbs, from, from + 10, 9) == 9);
assert(f.length(mbs, from, from + 10, 10) == 10);
assert(f.length(mbs, from, from + 10, 100) == 10);
return 0;
}
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char16_t, char8_t, mbstate_t>

// int max_length() const noexcept;

// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// C++20 codecvt specializations for char8_t are not yet implemented:
// UNSUPPORTED: libc++

#include <cassert>
#include <locale>

int main(int, char**) {
using F = std::codecvt<char16_t, char8_t, std::mbstate_t>;
const F& f = std::use_facet<F>(std::locale::classic());
assert(f.max_length() == 4);
static_assert(noexcept(f.max_length()));
return 0;
}
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char16_t, char8_t, mbstate_t>

// result out(stateT& state,
// const internT* from, const internT* from_end, const internT*& from_next,
// externT* to, externT* to_end, externT*& to_next) const;

// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// C++20 codecvt specializations for char8_t are not yet implemented:
// UNSUPPORTED: libc++

#include <cassert>
#include <locale>

int main(int, char**) {
using F = std::codecvt<char16_t, char8_t, std::mbstate_t>;
const F& f = std::use_facet<F>(std::locale::classic());
F::intern_type from[9] = {'s', 'o', 'm', 'e', ' ', 't', 'e', 'x', 't'};
F::extern_type to[9] = {0};
std::mbstate_t mbs = {};
const F::intern_type* from_next = nullptr;
F::extern_type* to_next = nullptr;
F::result r = f.out(mbs, from, from + 9, from_next, to, to + 9, to_next);
assert(r == F::ok);
assert(from_next - from == 9);
assert(to_next - to == 9);
for (unsigned i = 0; i < 9; ++i)
assert(to[i] == from[i]);
return 0;
}
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char16_t, char8_t, mbstate_t>

// result unshift(stateT& state,
// externT* to, externT* to_end, externT*& to_next) const;

// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// C++20 codecvt specializations for char8_t are not yet implemented:
// UNSUPPORTED: libc++

#include <cassert>
#include <locale>

int main(int, char**) {
using F = std::codecvt<char16_t, char8_t, std::mbstate_t>;
const F& f = std::use_facet<F>(std::locale::classic());
std::mbstate_t mbs = {};
char8_t to[3];
char8_t* to_next = nullptr;
assert(f.unshift(mbs, to, to + 3, to_next) == F::noconv);
assert(to_next == to);
return 0;
}
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char32_t, char8_t, mbstate_t>

// bool always_noconv() const noexcept;

// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// C++20 codecvt specializations for char8_t are not yet implemented:
// UNSUPPORTED: libc++

#include <cassert>
#include <locale>

int main(int, char**) {
using F = std::codecvt<char32_t, char8_t, std::mbstate_t>;
const F& f = std::use_facet<F>(std::locale::classic());
assert(!f.always_noconv());
static_assert(noexcept(f.always_noconv()));
return 0;
}

0 comments on commit 197f185

Please sign in to comment.