utf_sanity_check.pass.cpp revision bc8d3f97eb5c958007f2713238472e0c1c8fe02c
1//===----------------------------------------------------------------------===//
2//
3// ��������������������The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <locale>
11
12// template <> class codecvt<char32_t, char, mbstate_t>
13// template <> class codecvt<char16_t, char, mbstate_t>
14// template <> class codecvt<char32_t, char16_t, mbstate_t>  // extension
15
16// sanity check
17
18#include <locale>
19#include <cassert>
20
21#include <stdio.h>
22
23int main()
24{
25    typedef std::codecvt<char32_t, char, std::mbstate_t> F32_8;
26    typedef std::codecvt<char16_t, char, std::mbstate_t> F16_8;
27    typedef std::codecvt<char32_t, char16_t, std::mbstate_t> F32_16;
28    std::locale l = std::locale(std::locale::classic(), new F32_16);
29    const F32_8& f32_8 = std::use_facet<F32_8>(l);
30    const F32_16& f32_16 = std::use_facet<F32_16>(l);
31    const F16_8& f16_8 = std::use_facet<F16_8>(l);
32    std::mbstate_t mbs = {0};
33    F32_8::intern_type* c32p;
34    F16_8::intern_type* c16p;
35    F32_8::extern_type* c8p;
36    const F32_8::intern_type* c_c32p;
37    const F16_8::intern_type* c_c16p;
38    const F32_8::extern_type* c_c8p;
39    F32_8::intern_type c32;
40    F16_8::intern_type c16[2];
41    F32_8::extern_type c8[4];
42    for (F32_8::intern_type c32x = 0; c32x < 0x110003; ++c32x)
43    {
44        if (0xD800 <= c32x && c32x < 0xE000 || c32x >= 0x110000)
45        {
46            assert(f32_16.out(mbs, &c32x, &c32x+1, c_c32p, c16+0, c16+2, c16p) == F32_8::error);
47            assert(f32_8.out(mbs, &c32x, &c32x+1, c_c32p, c8, c8+4, c8p) == F32_8::error);
48        }
49        else
50        {
51            assert(f32_16.out(mbs, &c32x, &c32x+1, c_c32p, c16, c16+2, c16p) == F32_8::ok);
52            assert(c_c32p-&c32x == 1);
53            if (c32x < 0x10000)
54                assert(c16p-c16 == 1);
55            else
56                assert(c16p-c16 == 2);
57            c_c16p = c16p;
58            assert(f16_8.out(mbs, c16, c_c16p, c_c16p, c8, c8+4, c8p) == F32_8::ok);
59            if (c32x < 0x10000)
60                assert(c_c16p-c16 == 1);
61            else
62                assert(c_c16p-c16 == 2);
63            if (c32x < 0x80)
64                assert(c8p-c8 == 1);
65            else if (c32x < 0x800)
66                assert(c8p-c8 == 2);
67            else if (c32x < 0x10000)
68                assert(c8p-c8 == 3);
69            else
70                assert(c8p-c8 == 4);
71            c_c8p = c8p;
72            assert(f32_8.in(mbs, c8, c_c8p, c_c8p, &c32, &c32+1, c32p) == F32_8::ok);
73            if (c32x < 0x80)
74                assert(c_c8p-c8 == 1);
75            else if (c32x < 0x800)
76                assert(c_c8p-c8 == 2);
77            else if (c32x < 0x10000)
78                assert(c_c8p-c8 == 3);
79            else
80                assert(c_c8p-c8 == 4);
81            assert(c32p-&c32 == 1);
82            assert(c32 == c32x);
83            assert(f32_8.out(mbs, &c32x, &c32x+1, c_c32p, c8, c8+4, c8p) == F32_8::ok);
84            assert(c_c32p-&c32x == 1);
85            if (c32x < 0x80)
86                assert(c8p-c8 == 1);
87            else if (c32x < 0x800)
88                assert(c8p-c8 == 2);
89            else if (c32x < 0x10000)
90                assert(c8p-c8 == 3);
91            else
92                assert(c8p-c8 == 4);
93            c_c8p = c8p;
94            assert(f16_8.in(mbs, c8, c_c8p, c_c8p, c16, c16+2, c16p) == F32_8::ok);
95            if (c32x < 0x80)
96                assert(c_c8p-c8 == 1);
97            else if (c32x < 0x800)
98                assert(c_c8p-c8 == 2);
99            else if (c32x < 0x10000)
100                assert(c_c8p-c8 == 3);
101            else
102                assert(c_c8p-c8 == 4);
103            if (c32x < 0x10000)
104                assert(c16p-c16 == 1);
105            else
106                assert(c16p-c16 == 2);
107            c_c16p = c16p;
108            assert(f32_16.in(mbs, c16, c_c16p, c_c16p, &c32, &c32+1, c32p) == F32_8::ok);
109            if (c32x < 0x10000)
110                assert(c_c16p-c16 == 1);
111            else
112                assert(c_c16p-c16 == 2);
113            assert(c32p-&c32 == 1);
114            assert(c32 == c32x);
115        }
116    }
117}
118