Blame gnulib-tests/test-wcwidth.c

Packit Service fdd496
/* Test of wcwidth() function.
Packit Service fdd496
   Copyright (C) 2007-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
#include <wchar.h>
Packit Service fdd496
Packit Service fdd496
#include "signature.h"
Packit Service fdd496
SIGNATURE_CHECK (wcwidth, int, (wchar_t));
Packit Service fdd496
Packit Service fdd496
#include <locale.h>
Packit Service fdd496
#include <string.h>
Packit Service fdd496
Packit Service fdd496
#include "c-ctype.h"
Packit Service fdd496
#include "localcharset.h"
Packit Service fdd496
#include "macros.h"
Packit Service fdd496
Packit Service fdd496
int
Packit Service fdd496
main ()
Packit Service fdd496
{
Packit Service fdd496
  wchar_t wc;
Packit Service fdd496
Packit Service fdd496
#ifdef C_CTYPE_ASCII
Packit Service fdd496
  /* Test width of ASCII characters.  */
Packit Service fdd496
  for (wc = 0x20; wc < 0x7F; wc++)
Packit Service fdd496
    ASSERT (wcwidth (wc) == 1);
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
  /* Switch to an UTF-8 locale.  */
Packit Service fdd496
  if (setlocale (LC_ALL, "fr_FR.UTF-8") != NULL
Packit Service fdd496
      /* Check whether it's really an UTF-8 locale.
Packit Service fdd496
         On OpenBSD 4.0, the setlocale call succeeds only for the LC_CTYPE
Packit Service fdd496
         category and therefore returns "C/fr_FR.UTF-8/C/C/C/C", but the
Packit Service fdd496
         LC_CTYPE category is effectively set to an ASCII LC_CTYPE category;
Packit Service fdd496
         in particular, locale_charset() returns "ASCII".  */
Packit Service fdd496
      && strcmp (locale_charset (), "UTF-8") == 0)
Packit Service fdd496
    {
Packit Service fdd496
      /* Test width of ASCII characters.  */
Packit Service fdd496
      for (wc = 0x20; wc < 0x7F; wc++)
Packit Service fdd496
        ASSERT (wcwidth (wc) == 1);
Packit Service fdd496
Packit Service fdd496
      /* Test width of some non-spacing characters.  */
Packit Service fdd496
      ASSERT (wcwidth (0x0301) == 0);
Packit Service fdd496
      ASSERT (wcwidth (0x05B0) == 0);
Packit Service fdd496
Packit Service fdd496
      /* Test width of some format control characters.  */
Packit Service fdd496
      ASSERT (wcwidth (0x200E) <= 0);
Packit Service fdd496
      ASSERT (wcwidth (0x2060) <= 0);
Packit Service fdd496
#if 0  /* wchar_t may be only 16 bits.  */
Packit Service fdd496
      ASSERT (wcwidth (0xE0001) <= 0);
Packit Service fdd496
      ASSERT (wcwidth (0xE0044) <= 0);
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
      /* Test width of some zero width characters.  */
Packit Service fdd496
      ASSERT (wcwidth (0x200B) == 0);
Packit Service fdd496
      ASSERT (wcwidth (0xFEFF) <= 0);
Packit Service fdd496
Packit Service fdd496
      /* Test width of some CJK characters.  */
Packit Service fdd496
      ASSERT (wcwidth (0x3000) == 2);
Packit Service fdd496
      ASSERT (wcwidth (0xB250) == 2);
Packit Service fdd496
      ASSERT (wcwidth (0xFF1A) == 2);
Packit Service fdd496
#if 0  /* wchar_t may be only 16 bits.  */
Packit Service fdd496
      ASSERT (wcwidth (0x20369) == 2);
Packit Service fdd496
      ASSERT (wcwidth (0x2F876) == 2);
Packit Service fdd496
#endif
Packit Service fdd496
    }
Packit Service fdd496
Packit Service fdd496
  return 0;
Packit Service fdd496
}