root/maint/gnulib/tests/test-unicodeio.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. success_callback
  2. failure_callback
  3. main

   1 /* Tests for Unicode character output.
   2 
   3    Copyright (C) 2020-2021 Free Software Foundation, Inc.
   4 
   5    This program is free software: you can redistribute it and/or modify
   6    it under the terms of the GNU General Public License as published by
   7    the Free Software Foundation; either version 3 of the License, or
   8    (at your option) any later version.
   9 
  10    This program is distributed in the hope that it will be useful,
  11    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13    GNU General Public License for more details.
  14 
  15    You should have received a copy of the GNU General Public License
  16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  17 
  18 /* Written by Bruno Haible, 2020.  */
  19 
  20 #include <config.h>
  21 
  22 /* Specification.  */
  23 #include "unicodeio.h"
  24 
  25 #include <locale.h>
  26 #include <string.h>
  27 
  28 #include "macros.h"
  29 
  30 #define TEST_CODE 0x2022
  31 #define TEST_CODE_AS_UTF8 "\xe2\x80\xa2"
  32 #define TEST_CODE_AS_GB18030 "\x81\x36\xa6\x31"
  33 
  34 static char result[64];
  35 
  36 static long
  37 success_callback (const char *buf, size_t buflen, void *callback_arg)
     /* [previous][next][first][last][top][bottom][index][help] */
  38 {
  39   memcpy (result, buf, buflen);
  40   result[buflen] = '\0';
  41   return 42;
  42 }
  43 
  44 static long
  45 failure_callback (unsigned int code, const char *msg, void *callback_arg)
     /* [previous][next][first][last][top][bottom][index][help] */
  46 {
  47   ASSERT (code == TEST_CODE);
  48   strcpy (result, ".");
  49   return 55;
  50 }
  51 
  52 int
  53 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  54 {
  55   /* configure should already have checked that the locale is supported.  */
  56   if (setlocale (LC_ALL, "") == NULL)
  57     return 1;
  58 
  59   switch (unicode_to_mb (TEST_CODE, success_callback, failure_callback, NULL))
  60     {
  61     case 42:
  62       if (argc > 1)
  63         switch (argv[1][0])
  64           {
  65           case '1': /* On some platforms, the "C" locale has UTF-8 encoding.  */
  66           case '2':
  67             ASSERT (strcmp (result, TEST_CODE_AS_UTF8) == 0);
  68             break;
  69           case '3':
  70             ASSERT (strcmp (result, TEST_CODE_AS_GB18030) == 0);
  71             break;
  72           }
  73       break;
  74     case 55:
  75       ASSERT (strcmp (result, ".") == 0);
  76       break;
  77     default:
  78       ASSERT (0);
  79     }
  80 
  81   return 0;
  82 }

/* [previous][next][first][last][top][bottom][index][help] */