summaryrefslogtreecommitdiffstats
path: root/test/localization/locale.categories/category.ctype/locale.codecvt/locale.codecvt.members/utf_sanity_check.pass.cpp
blob: 33fd85b0263bdfb9380080abe463b61f00af20ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// <locale>

// template <> class codecvt<char32_t, char, mbstate_t>
// template <> class codecvt<char16_t, char, mbstate_t>
// template <> class codecvt<char32_t, char16_t, mbstate_t>  // extension

// sanity check

#include <locale>
#include <cassert>

#include <stdio.h>

int main()
{
    typedef std::codecvt<char32_t, char, std::mbstate_t> F32_8;
    typedef std::codecvt<char16_t, char, std::mbstate_t> F16_8;
    typedef std::codecvt<char32_t, char16_t, std::mbstate_t> F32_16;
    std::locale l = std::locale(std::locale::classic(), new F32_16);
    const F32_8& f32_8 = std::use_facet<F32_8>(l);
    const F32_16& f32_16 = std::use_facet<F32_16>(l);
    const F16_8& f16_8 = std::use_facet<F16_8>(l);
    std::mbstate_t mbs = {0};
    F32_8::intern_type* c32p;
    F16_8::intern_type* c16p;
    F32_8::extern_type* c8p;
    const F32_8::intern_type* c_c32p;
    const F16_8::intern_type* c_c16p;
    const F32_8::extern_type* c_c8p;
    F32_8::intern_type c32;
    F16_8::intern_type c16[2];
    F32_8::extern_type c8[4];
    for (F32_8::intern_type c32x = 0; c32x < 0x110003; ++c32x)
    {
        if (0xD800 <= c32x && c32x < 0xE000 || c32x >= 0x110000)
        {
            assert(f32_16.out(mbs, &c32x, &c32x+1, c_c32p, c16+0, c16+2, c16p) == F32_8::error);
            assert(f32_8.out(mbs, &c32x, &c32x+1, c_c32p, c8, c8+4, c8p) == F32_8::error);
        }
        else
        {
            assert(f32_16.out(mbs, &c32x, &c32x+1, c_c32p, c16, c16+2, c16p) == F32_8::ok);
            assert(c_c32p-&c32x == 1);
            if (c32x < 0x10000)
                assert(c16p-c16 == 1);
            else
                assert(c16p-c16 == 2);
            c_c16p = c16p;
            assert(f16_8.out(mbs, c16, c_c16p, c_c16p, c8, c8+4, c8p) == F32_8::ok);
            if (c32x < 0x10000)
                assert(c_c16p-c16 == 1);
            else
                assert(c_c16p-c16 == 2);
            if (c32x < 0x80)
                assert(c8p-c8 == 1);
            else if (c32x < 0x800)
                assert(c8p-c8 == 2);
            else if (c32x < 0x10000)
                assert(c8p-c8 == 3);
            else
                assert(c8p-c8 == 4);
            c_c8p = c8p;
            assert(f32_8.in(mbs, c8, c_c8p, c_c8p, &c32, &c32+1, c32p) == F32_8::ok);
            if (c32x < 0x80)
                assert(c_c8p-c8 == 1);
            else if (c32x < 0x800)
                assert(c_c8p-c8 == 2);
            else if (c32x < 0x10000)
                assert(c_c8p-c8 == 3);
            else
                assert(c_c8p-c8 == 4);
            assert(c32p-&c32 == 1);
            assert(c32 == c32x);
            assert(f32_8.out(mbs, &c32x, &c32x+1, c_c32p, c8, c8+4, c8p) == F32_8::ok);
            assert(c_c32p-&c32x == 1);
            if (c32x < 0x80)
                assert(c8p-c8 == 1);
            else if (c32x < 0x800)
                assert(c8p-c8 == 2);
            else if (c32x < 0x10000)
                assert(c8p-c8 == 3);
            else
                assert(c8p-c8 == 4);
            c_c8p = c8p;
            assert(f16_8.in(mbs, c8, c_c8p, c_c8p, c16, c16+2, c16p) == F32_8::ok);
            if (c32x < 0x80)
                assert(c_c8p-c8 == 1);
            else if (c32x < 0x800)
                assert(c_c8p-c8 == 2);
            else if (c32x < 0x10000)
                assert(c_c8p-c8 == 3);
            else
                assert(c_c8p-c8 == 4);
            if (c32x < 0x10000)
                assert(c16p-c16 == 1);
            else
                assert(c16p-c16 == 2);
            c_c16p = c16p;
            assert(f32_16.in(mbs, c16, c_c16p, c_c16p, &c32, &c32+1, c32p) == F32_8::ok);
            if (c32x < 0x10000)
                assert(c_c16p-c16 == 1);
            else
                assert(c_c16p-c16 == 2);
            assert(c32p-&c32 == 1);
            assert(c32 == c32x);
        }
    }
}