Home | History | Annotate | Download | only in locale.stdcvt
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <codecvt>
     11 
     12 // template <class Elem, unsigned long Maxcode = 0x10ffff,
     13 //           codecvt_mode Mode = (codecvt_mode)0>
     14 // class codecvt_utf8
     15 //     : public codecvt<Elem, char, mbstate_t>
     16 // {
     17 //     // unspecified
     18 // };
     19 
     20 // result
     21 // out(stateT& state,
     22 //     const internT* from, const internT* from_end, const internT*& from_next,
     23 //     externT* to, externT* to_end, externT*& to_next) const;
     24 
     25 #include <codecvt>
     26 #include <cassert>
     27 
     28 int main()
     29 {
     30     {
     31         typedef std::codecvt_utf8<wchar_t> C;
     32         C c;
     33         wchar_t w = 0x40003;
     34         char n[4] = {0};
     35         const wchar_t* wp = nullptr;
     36         std::mbstate_t m;
     37         char* np = nullptr;
     38         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+4, np);
     39         assert(r == std::codecvt_base::ok);
     40         assert(wp == &w+1);
     41         assert(np == n+4);
     42         assert(n[0] == char(0xF1));
     43         assert(n[1] == char(0x80));
     44         assert(n[2] == char(0x80));
     45         assert(n[3] == char(0x83));
     46 
     47         w = 0x1005;
     48         r = c.out(m, &w, &w+1, wp, n, n+4, np);
     49         assert(r == std::codecvt_base::ok);
     50         assert(wp == &w+1);
     51         assert(np == n+3);
     52         assert(n[0] == char(0xE1));
     53         assert(n[1] == char(0x80));
     54         assert(n[2] == char(0x85));
     55         assert(n[3] == char(0x83));
     56 
     57         w = 0x453;
     58         r = c.out(m, &w, &w+1, wp, n, n+4, np);
     59         assert(r == std::codecvt_base::ok);
     60         assert(wp == &w+1);
     61         assert(np == n+2);
     62         assert(n[0] == char(0xD1));
     63         assert(n[1] == char(0x93));
     64         assert(n[2] == char(0x85));
     65         assert(n[3] == char(0x83));
     66 
     67         w = 0x56;
     68         r = c.out(m, &w, &w+1, wp, n, n+4, np);
     69         assert(r == std::codecvt_base::ok);
     70         assert(wp == &w+1);
     71         assert(np == n+1);
     72         assert(n[0] == char(0x56));
     73         assert(n[1] == char(0x93));
     74         assert(n[2] == char(0x85));
     75         assert(n[3] == char(0x83));
     76     }
     77     {
     78         typedef std::codecvt_utf8<wchar_t, 0x1000> C;
     79         C c;
     80         wchar_t w = 0x40003;
     81         char n[4] = {0};
     82         const wchar_t* wp = nullptr;
     83         std::mbstate_t m;
     84         char* np = nullptr;
     85         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+4, np);
     86         assert(r == std::codecvt_base::error);
     87         assert(wp == &w);
     88         assert(np == n);
     89         assert(n[0] == char(0));
     90         assert(n[1] == char(0));
     91         assert(n[2] == char(0));
     92         assert(n[3] == char(0));
     93 
     94         w = 0x1005;
     95         r = c.out(m, &w, &w+1, wp, n, n+4, np);
     96         assert(r == std::codecvt_base::error);
     97         assert(wp == &w);
     98         assert(np == n);
     99         assert(n[0] == char(0));
    100         assert(n[1] == char(0));
    101         assert(n[2] == char(0));
    102         assert(n[3] == char(0));
    103 
    104         w = 0x453;
    105         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    106         assert(r == std::codecvt_base::ok);
    107         assert(wp == &w+1);
    108         assert(np == n+2);
    109         assert(n[0] == char(0xD1));
    110         assert(n[1] == char(0x93));
    111         assert(n[2] == char(0));
    112         assert(n[3] == char(0));
    113 
    114         w = 0x56;
    115         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    116         assert(r == std::codecvt_base::ok);
    117         assert(wp == &w+1);
    118         assert(np == n+1);
    119         assert(n[0] == char(0x56));
    120         assert(n[1] == char(0x93));
    121         assert(n[2] == char(0));
    122         assert(n[3] == char(0));
    123     }
    124     {
    125         typedef std::codecvt_utf8<wchar_t, 0xFFFFFFFF, std::generate_header> C;
    126         C c;
    127         wchar_t w = 0x40003;
    128         char n[7] = {0};
    129         const wchar_t* wp = nullptr;
    130         std::mbstate_t m;
    131         char* np = nullptr;
    132         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+7, np);
    133         assert(r == std::codecvt_base::ok);
    134         assert(wp == &w+1);
    135         assert(np == n+7);
    136         assert(n[0] == char(0xEF));
    137         assert(n[1] == char(0xBB));
    138         assert(n[2] == char(0xBF));
    139         assert(n[3] == char(0xF1));
    140         assert(n[4] == char(0x80));
    141         assert(n[5] == char(0x80));
    142         assert(n[6] == char(0x83));
    143 
    144         w = 0x1005;
    145         r = c.out(m, &w, &w+1, wp, n, n+7, np);
    146         assert(r == std::codecvt_base::ok);
    147         assert(wp == &w+1);
    148         assert(np == n+6);
    149         assert(n[0] == char(0xEF));
    150         assert(n[1] == char(0xBB));
    151         assert(n[2] == char(0xBF));
    152         assert(n[3] == char(0xE1));
    153         assert(n[4] == char(0x80));
    154         assert(n[5] == char(0x85));
    155         assert(n[6] == char(0x83));
    156 
    157         w = 0x453;
    158         r = c.out(m, &w, &w+1, wp, n, n+7, np);
    159         assert(r == std::codecvt_base::ok);
    160         assert(wp == &w+1);
    161         assert(np == n+5);
    162         assert(n[0] == char(0xEF));
    163         assert(n[1] == char(0xBB));
    164         assert(n[2] == char(0xBF));
    165         assert(n[3] == char(0xD1));
    166         assert(n[4] == char(0x93));
    167         assert(n[5] == char(0x85));
    168         assert(n[6] == char(0x83));
    169 
    170         w = 0x56;
    171         r = c.out(m, &w, &w+1, wp, n, n+7, np);
    172         assert(r == std::codecvt_base::ok);
    173         assert(wp == &w+1);
    174         assert(np == n+4);
    175         assert(n[0] == char(0xEF));
    176         assert(n[1] == char(0xBB));
    177         assert(n[2] == char(0xBF));
    178         assert(n[3] == char(0x56));
    179         assert(n[4] == char(0x93));
    180         assert(n[5] == char(0x85));
    181         assert(n[6] == char(0x83));
    182     }
    183     {
    184         typedef std::codecvt_utf8<char32_t> C;
    185         C c;
    186         char32_t w = 0x40003;
    187         char n[4] = {0};
    188         const char32_t* wp = nullptr;
    189         std::mbstate_t m;
    190         char* np = nullptr;
    191         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+4, np);
    192         assert(r == std::codecvt_base::ok);
    193         assert(wp == &w+1);
    194         assert(np == n+4);
    195         assert(n[0] == char(0xF1));
    196         assert(n[1] == char(0x80));
    197         assert(n[2] == char(0x80));
    198         assert(n[3] == char(0x83));
    199 
    200         w = 0x1005;
    201         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    202         assert(r == std::codecvt_base::ok);
    203         assert(wp == &w+1);
    204         assert(np == n+3);
    205         assert(n[0] == char(0xE1));
    206         assert(n[1] == char(0x80));
    207         assert(n[2] == char(0x85));
    208         assert(n[3] == char(0x83));
    209 
    210         w = 0x453;
    211         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    212         assert(r == std::codecvt_base::ok);
    213         assert(wp == &w+1);
    214         assert(np == n+2);
    215         assert(n[0] == char(0xD1));
    216         assert(n[1] == char(0x93));
    217         assert(n[2] == char(0x85));
    218         assert(n[3] == char(0x83));
    219 
    220         w = 0x56;
    221         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    222         assert(r == std::codecvt_base::ok);
    223         assert(wp == &w+1);
    224         assert(np == n+1);
    225         assert(n[0] == char(0x56));
    226         assert(n[1] == char(0x93));
    227         assert(n[2] == char(0x85));
    228         assert(n[3] == char(0x83));
    229     }
    230     {
    231         typedef std::codecvt_utf8<char32_t, 0x1000> C;
    232         C c;
    233         char32_t w = 0x40003;
    234         char n[4] = {0};
    235         const char32_t* wp = nullptr;
    236         std::mbstate_t m;
    237         char* np = nullptr;
    238         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+4, np);
    239         assert(r == std::codecvt_base::error);
    240         assert(wp == &w);
    241         assert(np == n);
    242         assert(n[0] == char(0));
    243         assert(n[1] == char(0));
    244         assert(n[2] == char(0));
    245         assert(n[3] == char(0));
    246 
    247         w = 0x1005;
    248         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    249         assert(r == std::codecvt_base::error);
    250         assert(wp == &w);
    251         assert(np == n);
    252         assert(n[0] == char(0));
    253         assert(n[1] == char(0));
    254         assert(n[2] == char(0));
    255         assert(n[3] == char(0));
    256 
    257         w = 0x453;
    258         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    259         assert(r == std::codecvt_base::ok);
    260         assert(wp == &w+1);
    261         assert(np == n+2);
    262         assert(n[0] == char(0xD1));
    263         assert(n[1] == char(0x93));
    264         assert(n[2] == char(0));
    265         assert(n[3] == char(0));
    266 
    267         w = 0x56;
    268         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    269         assert(r == std::codecvt_base::ok);
    270         assert(wp == &w+1);
    271         assert(np == n+1);
    272         assert(n[0] == char(0x56));
    273         assert(n[1] == char(0x93));
    274         assert(n[2] == char(0));
    275         assert(n[3] == char(0));
    276     }
    277     {
    278         typedef std::codecvt_utf8<char32_t, 0xFFFFFFFF, std::generate_header> C;
    279         C c;
    280         char32_t w = 0x40003;
    281         char n[7] = {0};
    282         const char32_t* wp = nullptr;
    283         std::mbstate_t m;
    284         char* np = nullptr;
    285         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+7, np);
    286         assert(r == std::codecvt_base::ok);
    287         assert(wp == &w+1);
    288         assert(np == n+7);
    289         assert(n[0] == char(0xEF));
    290         assert(n[1] == char(0xBB));
    291         assert(n[2] == char(0xBF));
    292         assert(n[3] == char(0xF1));
    293         assert(n[4] == char(0x80));
    294         assert(n[5] == char(0x80));
    295         assert(n[6] == char(0x83));
    296 
    297         w = 0x1005;
    298         r = c.out(m, &w, &w+1, wp, n, n+7, np);
    299         assert(r == std::codecvt_base::ok);
    300         assert(wp == &w+1);
    301         assert(np == n+6);
    302         assert(n[0] == char(0xEF));
    303         assert(n[1] == char(0xBB));
    304         assert(n[2] == char(0xBF));
    305         assert(n[3] == char(0xE1));
    306         assert(n[4] == char(0x80));
    307         assert(n[5] == char(0x85));
    308         assert(n[6] == char(0x83));
    309 
    310         w = 0x453;
    311         r = c.out(m, &w, &w+1, wp, n, n+7, np);
    312         assert(r == std::codecvt_base::ok);
    313         assert(wp == &w+1);
    314         assert(np == n+5);
    315         assert(n[0] == char(0xEF));
    316         assert(n[1] == char(0xBB));
    317         assert(n[2] == char(0xBF));
    318         assert(n[3] == char(0xD1));
    319         assert(n[4] == char(0x93));
    320         assert(n[5] == char(0x85));
    321         assert(n[6] == char(0x83));
    322 
    323         w = 0x56;
    324         r = c.out(m, &w, &w+1, wp, n, n+7, np);
    325         assert(r == std::codecvt_base::ok);
    326         assert(wp == &w+1);
    327         assert(np == n+4);
    328         assert(n[0] == char(0xEF));
    329         assert(n[1] == char(0xBB));
    330         assert(n[2] == char(0xBF));
    331         assert(n[3] == char(0x56));
    332         assert(n[4] == char(0x93));
    333         assert(n[5] == char(0x85));
    334         assert(n[6] == char(0x83));
    335     }
    336     {
    337         typedef std::codecvt_utf8<char16_t> C;
    338         C c;
    339         char16_t w = 0x1005;
    340         char n[4] = {0};
    341         const char16_t* wp = nullptr;
    342         std::mbstate_t m;
    343         char* np = nullptr;
    344         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+4, np);
    345         assert(r == std::codecvt_base::ok);
    346         assert(wp == &w+1);
    347         assert(np == n+3);
    348         assert(n[0] == char(0xE1));
    349         assert(n[1] == char(0x80));
    350         assert(n[2] == char(0x85));
    351         assert(n[3] == char(0));
    352 
    353         w = 0x453;
    354         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    355         assert(r == std::codecvt_base::ok);
    356         assert(wp == &w+1);
    357         assert(np == n+2);
    358         assert(n[0] == char(0xD1));
    359         assert(n[1] == char(0x93));
    360         assert(n[2] == char(0x85));
    361         assert(n[3] == char(0));
    362 
    363         w = 0x56;
    364         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    365         assert(r == std::codecvt_base::ok);
    366         assert(wp == &w+1);
    367         assert(np == n+1);
    368         assert(n[0] == char(0x56));
    369         assert(n[1] == char(0x93));
    370         assert(n[2] == char(0x85));
    371         assert(n[3] == char(0));
    372     }
    373     {
    374         typedef std::codecvt_utf8<char16_t, 0x1000> C;
    375         C c;
    376         char16_t w = 0x1005;
    377         char n[4] = {0};
    378         const char16_t* wp = nullptr;
    379         std::mbstate_t m;
    380         char* np = nullptr;
    381         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+4, np);
    382         assert(r == std::codecvt_base::error);
    383         assert(wp == &w);
    384         assert(np == n);
    385         assert(n[0] == char(0));
    386         assert(n[1] == char(0));
    387         assert(n[2] == char(0));
    388         assert(n[3] == char(0));
    389 
    390         w = 0x453;
    391         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    392         assert(r == std::codecvt_base::ok);
    393         assert(wp == &w+1);
    394         assert(np == n+2);
    395         assert(n[0] == char(0xD1));
    396         assert(n[1] == char(0x93));
    397         assert(n[2] == char(0));
    398         assert(n[3] == char(0));
    399 
    400         w = 0x56;
    401         r = c.out(m, &w, &w+1, wp, n, n+4, np);
    402         assert(r == std::codecvt_base::ok);
    403         assert(wp == &w+1);
    404         assert(np == n+1);
    405         assert(n[0] == char(0x56));
    406         assert(n[1] == char(0x93));
    407         assert(n[2] == char(0));
    408         assert(n[3] == char(0));
    409     }
    410     {
    411         typedef std::codecvt_utf8<char16_t, 0xFFFFFFFF, std::generate_header> C;
    412         C c;
    413         char16_t w = 0x1005;
    414         char n[7] = {0};
    415         const char16_t* wp = nullptr;
    416         std::mbstate_t m;
    417         char* np = nullptr;
    418         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+7, np);
    419         assert(r == std::codecvt_base::ok);
    420         assert(wp == &w+1);
    421         assert(np == n+6);
    422         assert(n[0] == char(0xEF));
    423         assert(n[1] == char(0xBB));
    424         assert(n[2] == char(0xBF));
    425         assert(n[3] == char(0xE1));
    426         assert(n[4] == char(0x80));
    427         assert(n[5] == char(0x85));
    428         assert(n[6] == char(0));
    429 
    430         w = 0x453;
    431         r = c.out(m, &w, &w+1, wp, n, n+7, np);
    432         assert(r == std::codecvt_base::ok);
    433         assert(wp == &w+1);
    434         assert(np == n+5);
    435         assert(n[0] == char(0xEF));
    436         assert(n[1] == char(0xBB));
    437         assert(n[2] == char(0xBF));
    438         assert(n[3] == char(0xD1));
    439         assert(n[4] == char(0x93));
    440         assert(n[5] == char(0x85));
    441         assert(n[6] == char(0));
    442 
    443         w = 0x56;
    444         r = c.out(m, &w, &w+1, wp, n, n+7, np);
    445         assert(r == std::codecvt_base::ok);
    446         assert(wp == &w+1);
    447         assert(np == n+4);
    448         assert(n[0] == char(0xEF));
    449         assert(n[1] == char(0xBB));
    450         assert(n[2] == char(0xBF));
    451         assert(n[3] == char(0x56));
    452         assert(n[4] == char(0x93));
    453         assert(n[5] == char(0x85));
    454         assert(n[6] == char(0));
    455     }
    456 }
    457