doc: clarify ln's --help output
[coreutils/ericb.git] / gl / tests / test-mbsalign.c
bloba9e177c144a15a75097f2b3838f0bdae460fc3f2
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. */
19 #include <config.h>
21 #include "mbsalign.h"
22 #include "macros.h"
23 #include <stdlib.h>
24 #include <locale.h>
26 int
27 main (void)
29 char dest[4 * 16 + 1];
30 size_t width, n;
32 /* Test unibyte truncation. */
33 width = 4;
34 n = mbsalign ("t\tés", dest, sizeof dest, &width, MBS_ALIGN_LEFT, 0);
35 ASSERT (n == 4);
37 /* Test center alignment. */
38 width = 4;
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. */
45 width = 4;
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 */
50 width = 4;
51 n = mbsalign ("t\xe1\xe2s", dest, sizeof dest, &width,
52 MBS_ALIGN_LEFT, MBA_UNIBYTE_FALLBACK);
53 ASSERT (n == 4);
55 /* Test multibyte center alignment. */
56 width = 4;
57 n = mbsalign ("és", dest, sizeof dest, &width, MBS_ALIGN_CENTER, 0);
58 ASSERT (*dest == ' ' && *(dest + n - 1) == ' ');
60 /* Test multibyte left alignment. */
61 width = 4;
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. */
66 width = 4;
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,
73 MBS_ALIGN_LEFT, 0);
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);
89 ASSERT (width == 3);
91 /* Test case where output is larger than input
92 (as tab converted to multi byte replacement char). */
93 width = 4;
94 n = mbsalign ("t\tés" /* 6 including NUL */ , dest, sizeof dest,
95 &width, MBS_ALIGN_LEFT, 0);
96 ASSERT (n == 7);
99 return 0;