summaryrefslogtreecommitdiff
path: root/gl/tests/test-mbsalign.c
blob: 86aa87739dcc479547c36c81e3a89292f5febee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* Test that mbsalign works as advertised.
   Copyright (C) 2010-2012 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* Written by Pádraig Brady.  */

#include <config.h>

#include "mbsalign.h"
#include "macros.h"
#include <stdlib.h>
#include <locale.h>

int
main (void)
{
  char dest[4 * 16 + 1];
  size_t width, n;

  /* Test unibyte truncation.  */
  width = 4;
  n = mbsalign ("t\tés", dest, sizeof dest, &width, MBS_ALIGN_LEFT, 0);
  ASSERT (n == 4);

  /* Test center alignment.  */
  width = 4;
  n = mbsalign ("es", dest, sizeof dest, &width, MBS_ALIGN_CENTER, 0);
  ASSERT (*dest == ' ' && *(dest + n - 1) == ' ');

  if (setlocale (LC_ALL, "en_US.UTF8"))
    {
      /* Check invalid input is flagged.  */
      width = 4;
      n = mbsalign ("t\xe1\xe2s", dest, sizeof dest, &width, MBS_ALIGN_LEFT, 0);
      ASSERT (n == (size_t) -1);

      /* Check invalid input is treated as unibyte  */
      width = 4;
      n = mbsalign ("t\xe1\xe2s", dest, sizeof dest, &width,
                    MBS_ALIGN_LEFT, MBA_UNIBYTE_FALLBACK);
      ASSERT (n == 4);

      /* Test multibyte center alignment.  */
      width = 4;
      n = mbsalign ("és", dest, sizeof dest, &width, MBS_ALIGN_CENTER, 0);
      ASSERT (*dest == ' ' && *(dest + n - 1) == ' ');

      /* Test multibyte left alignment.  */
      width = 4;
      n = mbsalign ("és", dest, sizeof dest, &width, MBS_ALIGN_LEFT, 0);
      ASSERT (*(dest + n - 1) == ' ' && *(dest + n - 2) == ' ');

      /* Test multibyte right alignment.  */
      width = 4;
      n = mbsalign ("és", dest, sizeof dest, &width, MBS_ALIGN_RIGHT, 0);
      ASSERT (*(dest) == ' ' && *(dest + 1) == ' ');

      /* multibyte multicell truncation.  */
      width = 4;                /* cells */
      n = mbsalign ("日月火水", dest, sizeof dest, &width,
                    MBS_ALIGN_LEFT, 0);
      ASSERT (n == 6);          /* 2 characters */

      /* multibyte unicell truncation.  */
      width = 3;                /* cells */
      n = mbsalign ("¹²³⁴", dest, sizeof dest, &width, MBS_ALIGN_LEFT, 0);
      ASSERT (n == 6);          /* 3 characters */

      /* Check independence from dest buffer. */
      width = 4;                /* cells */
      n = mbsalign ("¹²³⁴", dest, 0, &width, MBS_ALIGN_LEFT, 0);
      ASSERT (n == 9);          /* 4 characters */

      /* Check that width is updated with cells required before padding.  */
      width = 4;                /* cells */
      n = mbsalign ("¹²³", dest, 0, &width, MBS_ALIGN_LEFT, 0);
      ASSERT (width == 3);

      /* Test case where output is larger than input
         (as tab converted to multi byte replacement char).  */
      width = 4;
      n = mbsalign ("t\tés" /* 6 including NUL */ , dest, sizeof dest,
                    &width, MBS_ALIGN_LEFT, 0);
      ASSERT (n == 7);
    }

  return 0;
}