1 /* Test that mbsalign works as advertised.
2 Copyright (C) 2010-2011 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Pádraig Brady. */
29 char dest
[4 * 16 + 1];
32 /* Test unibyte truncation. */
34 n
= mbsalign ("t\tés", dest
, sizeof dest
, &width
, MBS_ALIGN_LEFT
, 0);
37 /* Test center alignment. */
39 n
= mbsalign ("es", dest
, sizeof dest
, &width
, MBS_ALIGN_CENTER
, 0);
40 ASSERT (*dest
== ' ' && *(dest
+ n
- 1) == ' ');
42 if (setlocale (LC_ALL
, "en_US.UTF8"))
44 /* Check invalid input is flagged. */
46 n
= mbsalign ("t\xe1\xe2s", dest
, sizeof dest
, &width
, MBS_ALIGN_LEFT
, 0);
47 ASSERT (n
== (size_t) -1);
49 /* Check invalid input is treated as unibyte */
51 n
= mbsalign ("t\xe1\xe2s", dest
, sizeof dest
, &width
,
52 MBS_ALIGN_LEFT
, MBA_UNIBYTE_FALLBACK
);
55 /* Test multibyte center alignment. */
57 n
= mbsalign ("és", dest
, sizeof dest
, &width
, MBS_ALIGN_CENTER
, 0);
58 ASSERT (*dest
== ' ' && *(dest
+ n
- 1) == ' ');
60 /* Test multibyte left alignment. */
62 n
= mbsalign ("és", dest
, sizeof dest
, &width
, MBS_ALIGN_LEFT
, 0);
63 ASSERT (*(dest
+ n
- 1) == ' ' && *(dest
+ n
- 2) == ' ');
65 /* Test multibyte right alignment. */
67 n
= mbsalign ("és", dest
, sizeof dest
, &width
, MBS_ALIGN_RIGHT
, 0);
68 ASSERT (*(dest
) == ' ' && *(dest
+ 1) == ' ');
70 /* multibyte multicell truncation. */
71 width
= 4; /* cells */
72 n
= mbsalign ("日月火水", dest
, sizeof dest
, &width
,
74 ASSERT (n
== 6); /* 2 characters */
76 /* multibyte unicell truncation. */
77 width
= 3; /* cells */
78 n
= mbsalign ("¹²³⁴", dest
, sizeof dest
, &width
, MBS_ALIGN_LEFT
, 0);
79 ASSERT (n
== 6); /* 3 characters */
81 /* Check independence from dest buffer. */
82 width
= 4; /* cells */
83 n
= mbsalign ("¹²³⁴", dest
, 0, &width
, MBS_ALIGN_LEFT
, 0);
84 ASSERT (n
== 9); /* 4 characters */
86 /* Check that width is updated with cells required before padding. */
87 width
= 4; /* cells */
88 n
= mbsalign ("¹²³", dest
, 0, &width
, MBS_ALIGN_LEFT
, 0);
91 /* Test case where output is larger than input
92 (as tab converted to multi byte replacement char). */
94 n
= mbsalign ("t\tés" /* 6 including NUL */ , dest
, sizeof dest
,
95 &width
, MBS_ALIGN_LEFT
, 0);