libstdc++: Fix Unicode property detection functions
[official-gcc.git] / libstdc++-v3 / testsuite / ext / unicode / grapheme_view.cc
blobac1e8c50b0596ec26ba8d834446a265cc3a74236
1 // { dg-do compile { target c++20 } }
3 #include <format> // includes <bits/unicode.h>
4 #include <string_view>
5 #include <ranges>
6 #include <testsuite_hooks.h>
8 namespace uc = std::__unicode;
9 using namespace std::string_view_literals;
11 constexpr void
12 test_breaks()
14 VERIFY(uc::__field_width(u8"\N{LATIN SMALL LETTER E WITH ACUTE}"sv) == 1 );
16 auto sv = u8"ee\N{COMBINING ACUTE ACCENT}e"sv;
17 auto data = sv.data();
18 VERIFY( uc::__field_width(sv) == 3 );
19 VERIFY( uc::__truncate(sv, 3) == 3 );
20 VERIFY( uc::__truncate(sv, 4) == 3 );
21 VERIFY( sv == data );
23 VERIFY( uc::__truncate(sv, 2) == 2 );
24 VERIFY( sv == u8"ee\N{COMBINING ACUTE ACCENT}"sv );
26 sv = data;
27 sv.remove_prefix(1);
28 VERIFY( uc::__field_width(sv) == 2 );
29 VERIFY( uc::__truncate(sv, 3) == 2 );
30 VERIFY( sv == data+1 );
32 sv = u8"\N{REGIONAL INDICATOR SYMBOL LETTER G}"
33 "\N{REGIONAL INDICATOR SYMBOL LETTER B}"; // GB flag emoji
34 data = sv.data();
35 VERIFY( uc::__field_width(sv) == 1 );
36 VERIFY( uc::__truncate(sv, 2) == 1 );
37 VERIFY( sv == data );
38 VERIFY( uc::__truncate(sv, 1) == 1 ); // Do not break inside a flag emoji.
39 VERIFY( sv == data );
41 sv = u8"abcd"
42 "\N{REGIONAL INDICATOR SYMBOL LETTER G}" // 4 bytes
43 "\N{REGIONAL INDICATOR SYMBOL LETTER B}" // 4 bytes
44 "\N{DEVANAGARI LETTER KA}" // 3 bytes
45 "\N{DEVANAGARI SIGN VIRAMA}" // 3 bytes
46 "\N{DEVANAGARI LETTER RA}" // 3 bytes
47 "\N{MAN}\N{ZERO WIDTH JOINER}" // 4+3 bytes
48 "\N{WOMAN}\N{ZERO WIDTH JOINER}" // 4+3 bytes
49 "\N{GIRL}\N{ZERO WIDTH JOINER}" // 4+3 bytes
50 "\N{BOY}\N{ZERO WIDTH JOINER}" // 4+3 bytes
51 "\N{HANGUL CHOSEONG KIYEOK}" // 3 bytes
52 "\N{HANGUL CHOSEONG KIYEOK}" // 3 bytes
53 "\N{HANGUL CHOSEONG KIYEOK}" // 3 bytes
54 "\N{HANGUL CHOSEONG KIYEOK}" // 3 bytes
55 "\N{HANGUL JUNGSEONG A}" // 3 bytes
56 "\N{HANGUL JONGSEONG KIYEOK}" // 3 bytes
57 "\N{HANGUL JONGSEONG KIYEOK}" // 3 bytes
58 "\N{HANGUL JONGSEONG KIYEOK}"; // 3 bytes
60 uc::_Grapheme_cluster_view gv(sv);
61 auto iter = gv.begin();
62 VERIFY( iter.base() == sv.data() );
63 VERIFY( *iter == U'a' );
64 std::ranges::advance(iter, 3);
65 VERIFY( *iter == U'd' );
66 VERIFY( iter.base() == sv.data() + 3 );
67 ++iter;
68 VERIFY( *iter == U'\N{REGIONAL INDICATOR SYMBOL LETTER G}' );
69 VERIFY( iter.base() == sv.data() + 4 );
70 ++iter;
71 VERIFY( *iter == U'\N{DEVANAGARI LETTER KA}' );
72 VERIFY( iter.base() == sv.data() + 4 + 8 );
73 ++iter;
74 VERIFY( *iter == U'\N{MAN}' );
75 VERIFY( iter.base() == sv.data() + 4 + 8 + 9 );
76 ++iter;
77 VERIFY( iter.base() == sv.data() + 4 + 8 + 9 + 28 );
78 VERIFY( *iter == U'\N{HANGUL CHOSEONG KIYEOK}' );
79 ++iter;
80 VERIFY( iter.base() == sv.data() + 4 + 8 + 9 + 28 + 24 );
81 VERIFY( iter == gv.end() );
82 ++iter;
83 VERIFY( iter == gv.end() );
86 int main()
88 auto run_tests = []{
89 test_breaks();
90 return true;
93 VERIFY( run_tests() );
94 static_assert( run_tests() );