maint: update copyright
[gnulib.git] / tests / unistr / test-u32-uctomb.c
1 /* Test of u32_uctomb() function.
2    Copyright (C) 2010-2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2010.  */
18
19 #include <config.h>
20
21 #include "unistr.h"
22
23 #include "macros.h"
24
25 #define MAGIC 0xBADFACE
26
27 int
28 main ()
29 {
30   /* Test ISO 646 character, in particular the NUL character.  */
31   {
32     ucs4_t uc;
33
34     for (uc = 0; uc < 0x80; uc++)
35       {
36         uint32_t buf[2] = { MAGIC, MAGIC };
37         int ret;
38
39         ret = u32_uctomb (buf, uc, 0);
40         ASSERT (ret == -2);
41         ASSERT (buf[0] == MAGIC);
42
43         ret = u32_uctomb (buf, uc, 1);
44         ASSERT (ret == 1);
45         ASSERT (buf[0] == uc);
46         ASSERT (buf[1] == MAGIC);
47       }
48   }
49
50   /* Test BMP character.  */
51   {
52     ucs4_t uc = 0x20AC;
53     uint32_t buf[2] = { MAGIC, MAGIC };
54     int ret;
55
56     ret = u32_uctomb (buf, uc, 0);
57     ASSERT (ret == -2);
58     ASSERT (buf[0] == MAGIC);
59
60     ret = u32_uctomb (buf, uc, 1);
61     ASSERT (ret == 1);
62     ASSERT (buf[0] == uc);
63     ASSERT (buf[1] == MAGIC);
64   }
65
66   /* Test non-BMP character.  */
67   {
68     ucs4_t uc = 0x10FFFD;
69     uint32_t buf[2] = { MAGIC, MAGIC };
70     int ret;
71
72     ret = u32_uctomb (buf, uc, 0);
73     ASSERT (ret == -2);
74     ASSERT (buf[0] == MAGIC);
75
76     ret = u32_uctomb (buf, uc, 1);
77     ASSERT (ret == 1);
78     ASSERT (buf[0] == uc);
79     ASSERT (buf[1] == MAGIC);
80   }
81
82   /* Test invalid characters.  */
83   {
84     ucs4_t invalid[] = { 0x110000, 0xD800, 0xDBFF, 0xDC00, 0xDFFF };
85     uint32_t buf[2] = { MAGIC, MAGIC };
86     size_t i;
87
88     for (i = 0; i < SIZEOF (invalid); i++)
89       {
90         ucs4_t uc = invalid[i];
91         int n;
92
93         for (n = 0; n <= 2; n++)
94           {
95             int ret = u32_uctomb (buf, uc, n);
96             ASSERT (ret == -1);
97             ASSERT (buf[0] == MAGIC);
98             ASSERT (buf[1] == MAGIC);
99           }
100       }
101   }
102
103   return 0;
104 }