(root)/
gettext-0.22.4/
gettext-tools/
gnulib-tests/
unistr/
test-u8-mbtoucr.c
       1  /* Test of u8_mbtoucr() function.
       2     Copyright (C) 2010-2023 Free Software Foundation, Inc.
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation, either version 3 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  /* Written by Bruno Haible <bruno@clisp.org>, 2010.  */
      18  
      19  #include <config.h>
      20  
      21  #include "unistr.h"
      22  
      23  #include "macros.h"
      24  
      25  int
      26  main ()
      27  {
      28    ucs4_t uc;
      29    int ret;
      30  
      31    /* Test NUL unit input.  */
      32    {
      33      static const uint8_t input[] = "";
      34      uc = 0xBADFACE;
      35      ret = u8_mbtoucr (&uc, input, 1);
      36      ASSERT (ret == 1);
      37      ASSERT (uc == 0);
      38    }
      39  
      40    /* Test ISO 646 unit input.  */
      41    {
      42      ucs4_t c;
      43      uint8_t buf[1];
      44  
      45      for (c = 0; c < 0x80; c++)
      46        {
      47          buf[0] = c;
      48          uc = 0xBADFACE;
      49          ret = u8_mbtoucr (&uc, buf, 1);
      50          ASSERT (ret == 1);
      51          ASSERT (uc == c);
      52        }
      53    }
      54  
      55    /* Test 2-byte character input.  */
      56    {
      57      static const uint8_t input[] = { 0xC3, 0x97 };
      58      uc = 0xBADFACE;
      59      ret = u8_mbtoucr (&uc, input, 2);
      60      ASSERT (ret == 2);
      61      ASSERT (uc == 0x00D7);
      62    }
      63  
      64    /* Test 3-byte character input.  */
      65    {
      66      static const uint8_t input[] = { 0xE2, 0x82, 0xAC };
      67      uc = 0xBADFACE;
      68      ret = u8_mbtoucr (&uc, input, 3);
      69      ASSERT (ret == 3);
      70      ASSERT (uc == 0x20AC);
      71    }
      72  
      73    /* Test 4-byte character input.  */
      74    {
      75      static const uint8_t input[] = { 0xF4, 0x8F, 0xBF, 0xBD };
      76      uc = 0xBADFACE;
      77      ret = u8_mbtoucr (&uc, input, 4);
      78      ASSERT (ret == 4);
      79      ASSERT (uc == 0x10FFFD);
      80    }
      81  
      82    /* Test incomplete/invalid 1-byte input.  */
      83    {
      84      static const uint8_t input[] = { 0xC1 };
      85      uc = 0xBADFACE;
      86      ret = u8_mbtoucr (&uc, input, 1);
      87      ASSERT (ret == -1);
      88      ASSERT (uc == 0xFFFD);
      89    }
      90    {
      91      static const uint8_t input[] = { 0xC3 };
      92      uc = 0xBADFACE;
      93      ret = u8_mbtoucr (&uc, input, 1);
      94      ASSERT (ret == -2);
      95      ASSERT (uc == 0xFFFD);
      96    }
      97    {
      98      static const uint8_t input[] = { 0xE2 };
      99      uc = 0xBADFACE;
     100      ret = u8_mbtoucr (&uc, input, 1);
     101      ASSERT (ret == -2);
     102      ASSERT (uc == 0xFFFD);
     103    }
     104    {
     105      static const uint8_t input[] = { 0xF4 };
     106      uc = 0xBADFACE;
     107      ret = u8_mbtoucr (&uc, input, 1);
     108      ASSERT (ret == -2);
     109      ASSERT (uc == 0xFFFD);
     110    }
     111    {
     112      static const uint8_t input[] = { 0xFE };
     113      uc = 0xBADFACE;
     114      ret = u8_mbtoucr (&uc, input, 1);
     115      ASSERT (ret == -1);
     116      ASSERT (uc == 0xFFFD);
     117    }
     118  
     119    /* Test incomplete/invalid 2-byte input.  */
     120    {
     121      static const uint8_t input[] = { 0xE0, 0x9F };
     122      uc = 0xBADFACE;
     123      ret = u8_mbtoucr (&uc, input, 2);
     124      ASSERT (ret == -1);
     125      ASSERT (uc == 0xFFFD);
     126    }
     127    {
     128      static const uint8_t input[] = { 0xE2, 0x82 };
     129      uc = 0xBADFACE;
     130      ret = u8_mbtoucr (&uc, input, 2);
     131      ASSERT (ret == -2);
     132      ASSERT (uc == 0xFFFD);
     133    }
     134    {
     135      static const uint8_t input[] = { 0xE2, 0xD0 };
     136      uc = 0xBADFACE;
     137      ret = u8_mbtoucr (&uc, input, 2);
     138      ASSERT (ret == -1);
     139      ASSERT (uc == 0xFFFD);
     140    }
     141    {
     142      static const uint8_t input[] = { 0xF0, 0x8F };
     143      uc = 0xBADFACE;
     144      ret = u8_mbtoucr (&uc, input, 2);
     145      ASSERT (ret == -1);
     146      ASSERT (uc == 0xFFFD);
     147    }
     148    {
     149      static const uint8_t input[] = { 0xF3, 0x8F };
     150      uc = 0xBADFACE;
     151      ret = u8_mbtoucr (&uc, input, 2);
     152      ASSERT (ret == -2);
     153      ASSERT (uc == 0xFFFD);
     154    }
     155    {
     156      static const uint8_t input[] = { 0xF3, 0xD0 };
     157      uc = 0xBADFACE;
     158      ret = u8_mbtoucr (&uc, input, 2);
     159      ASSERT (ret == -1);
     160      ASSERT (uc == 0xFFFD);
     161    }
     162  
     163    /* Test incomplete/invalid 3-byte input.  */
     164    {
     165      static const uint8_t input[] = { 0xF3, 0x8F, 0xBF };
     166      uc = 0xBADFACE;
     167      ret = u8_mbtoucr (&uc, input, 3);
     168      ASSERT (ret == -2);
     169      ASSERT (uc == 0xFFFD);
     170    }
     171    {
     172      static const uint8_t input[] = { 0xF3, 0xD0, 0xBF };
     173      uc = 0xBADFACE;
     174      ret = u8_mbtoucr (&uc, input, 3);
     175      ASSERT (ret == -1);
     176      ASSERT (uc == 0xFFFD);
     177    }
     178    {
     179      static const uint8_t input[] = { 0xF3, 0x8F, 0xD0 };
     180      uc = 0xBADFACE;
     181      ret = u8_mbtoucr (&uc, input, 3);
     182      ASSERT (ret == -1);
     183      ASSERT (uc == 0xFFFD);
     184    }
     185  
     186    return 0;
     187  }