Blame gnulib-tests/test-wcwidth.c

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