diff options
author | Pádraig Brady <P@draigBrady.com> | 2010-03-15 14:04:31 +0000 |
---|---|---|
committer | Pádraig Brady <P@draigBrady.com> | 2010-03-19 19:23:45 +0000 |
commit | dfe0d336a00940c8e13c24b6d5d7485a2d7310b0 (patch) | |
tree | de0b56d0af4ba442f070cef2ec717de9c4066a48 /gl/tests | |
parent | 3dcbcf98f427f8ab6ceafd27396adae4b2276bb8 (diff) | |
download | coreutils-dfe0d336a00940c8e13c24b6d5d7485a2d7310b0.tar.xz |
maint: update the mbsalign module
* gl/lib/mbsalign.c (mbsalign): Support the MBA_UNIBYTE_FALLBACK
flag which reverts to unibyte mode if one can't allocate memory
or if there are invalid multibyte characters present.
Note memory is no longer dynamically allocated in unibyte mode so
one can assume that mbsalign() will not return an error if this
flag is present. Don't calculate twice, the number of spaces,
when centering. Suppress a signed/unsigned comparison warning.
(ambsalign): A new wrapper function to dynamically allocate
the minimum memory required to hold the aligned string.
* gl/lib/mbsalign.h: Add the MBA_UNIBYTE_FALLBACK flag and
also document others that may be implemented in future.
(ambsalign): A prototype for the new wrapper.
* gl/tests/test-mbsalign.c (main): New test program.
* gl/modules/mbsalign-tests: A new index to reference the tests.
* .x-sc_program_name: Exclude test-mbsalign.c from this check.
Diffstat (limited to 'gl/tests')
-rw-r--r-- | gl/tests/test-mbsalign.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/gl/tests/test-mbsalign.c b/gl/tests/test-mbsalign.c new file mode 100644 index 000000000..9f8935732 --- /dev/null +++ b/gl/tests/test-mbsalign.c @@ -0,0 +1,93 @@ +/* Test that mbsalign works as advertised. + Copyright (C) 2010 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); + } + + return 0; +} |