LCOV - code coverage report
Current view: top level - lib/gltests/unistr/unistr - test-u8-uctomb.c (source / functions) Hit Total Coverage
Test: GNU Libidn Lines: 78 78 100.0 %
Date: 2014-08-10 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 54 102 52.9 %

           Branch data     Line data    Source code
       1                 :            : /* Test of u8_uctomb() function.
       2                 :            :    Copyright (C) 2010-2014 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 <http://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                 :            : #define MAGIC 0xBA
      26                 :            : 
      27                 :            : int
      28                 :          1 : main ()
      29                 :            : {
      30                 :            :   /* Test ISO 646 character, in particular the NUL character.  */
      31                 :            :   {
      32                 :            :     ucs4_t uc;
      33                 :            : 
      34         [ +  + ]:        129 :     for (uc = 0; uc < 0x80; uc++)
      35                 :            :       {
      36                 :        128 :         uint8_t buf[5] = { MAGIC, MAGIC, MAGIC, MAGIC, MAGIC };
      37                 :            :         int ret;
      38                 :            : 
      39                 :        128 :         ret = u8_uctomb (buf, uc, 0);
      40         [ -  + ]:        128 :         ASSERT (ret == -2);
      41         [ -  + ]:        128 :         ASSERT (buf[0] == MAGIC);
      42                 :            : 
      43                 :        128 :         ret = u8_uctomb (buf, uc, 1);
      44         [ -  + ]:        128 :         ASSERT (ret == 1);
      45         [ -  + ]:        128 :         ASSERT (buf[0] == uc);
      46         [ -  + ]:        128 :         ASSERT (buf[1] == MAGIC);
      47                 :            :       }
      48                 :            :   }
      49                 :            : 
      50                 :            :   /* Test 2-byte character.  */
      51                 :            :   {
      52                 :          1 :     ucs4_t uc = 0x00D7;
      53                 :          1 :     uint8_t buf[5] = { MAGIC, MAGIC, MAGIC, MAGIC, MAGIC };
      54                 :            :     int ret;
      55                 :            : 
      56                 :          1 :     ret = u8_uctomb (buf, uc, 0);
      57         [ -  + ]:          1 :     ASSERT (ret == -2);
      58         [ -  + ]:          1 :     ASSERT (buf[0] == MAGIC);
      59                 :            : 
      60                 :          1 :     ret = u8_uctomb (buf, uc, 1);
      61         [ -  + ]:          1 :     ASSERT (ret == -2);
      62         [ -  + ]:          1 :     ASSERT (buf[0] == MAGIC);
      63                 :            : 
      64                 :          1 :     ret = u8_uctomb (buf, uc, 2);
      65         [ -  + ]:          1 :     ASSERT (ret == 2);
      66         [ -  + ]:          1 :     ASSERT (buf[0] == 0xC3);
      67         [ -  + ]:          1 :     ASSERT (buf[1] == 0x97);
      68         [ -  + ]:          1 :     ASSERT (buf[2] == MAGIC);
      69                 :            :   }
      70                 :            : 
      71                 :            :   /* Test 3-byte character.  */
      72                 :            :   {
      73                 :          1 :     ucs4_t uc = 0x20AC;
      74                 :          1 :     uint8_t buf[5] = { MAGIC, MAGIC, MAGIC, MAGIC, MAGIC };
      75                 :            :     int ret;
      76                 :            : 
      77                 :          1 :     ret = u8_uctomb (buf, uc, 0);
      78         [ -  + ]:          1 :     ASSERT (ret == -2);
      79         [ -  + ]:          1 :     ASSERT (buf[0] == MAGIC);
      80                 :            : 
      81                 :          1 :     ret = u8_uctomb (buf, uc, 1);
      82         [ -  + ]:          1 :     ASSERT (ret == -2);
      83         [ -  + ]:          1 :     ASSERT (buf[0] == MAGIC);
      84                 :            : 
      85                 :          1 :     ret = u8_uctomb (buf, uc, 2);
      86         [ -  + ]:          1 :     ASSERT (ret == -2);
      87         [ -  + ]:          1 :     ASSERT (buf[0] == MAGIC);
      88         [ -  + ]:          1 :     ASSERT (buf[1] == MAGIC);
      89                 :            : 
      90                 :          1 :     ret = u8_uctomb (buf, uc, 3);
      91         [ -  + ]:          1 :     ASSERT (ret == 3);
      92         [ -  + ]:          1 :     ASSERT (buf[0] == 0xE2);
      93         [ -  + ]:          1 :     ASSERT (buf[1] == 0x82);
      94         [ -  + ]:          1 :     ASSERT (buf[2] == 0xAC);
      95         [ -  + ]:          1 :     ASSERT (buf[3] == MAGIC);
      96                 :            :   }
      97                 :            : 
      98                 :            :   /* Test 4-byte character.  */
      99                 :            :   {
     100                 :          1 :     ucs4_t uc = 0x10FFFD;
     101                 :          1 :     uint8_t buf[5] = { MAGIC, MAGIC, MAGIC, MAGIC, MAGIC };
     102                 :            :     int ret;
     103                 :            : 
     104                 :          1 :     ret = u8_uctomb (buf, uc, 0);
     105         [ -  + ]:          1 :     ASSERT (ret == -2);
     106         [ -  + ]:          1 :     ASSERT (buf[0] == MAGIC);
     107                 :            : 
     108                 :          1 :     ret = u8_uctomb (buf, uc, 1);
     109         [ -  + ]:          1 :     ASSERT (ret == -2);
     110         [ -  + ]:          1 :     ASSERT (buf[0] == MAGIC);
     111                 :            : 
     112                 :          1 :     ret = u8_uctomb (buf, uc, 2);
     113         [ -  + ]:          1 :     ASSERT (ret == -2);
     114         [ -  + ]:          1 :     ASSERT (buf[0] == MAGIC);
     115         [ -  + ]:          1 :     ASSERT (buf[1] == MAGIC);
     116                 :            : 
     117                 :          1 :     ret = u8_uctomb (buf, uc, 3);
     118         [ -  + ]:          1 :     ASSERT (ret == -2);
     119         [ -  + ]:          1 :     ASSERT (buf[0] == MAGIC);
     120         [ -  + ]:          1 :     ASSERT (buf[1] == MAGIC);
     121         [ -  + ]:          1 :     ASSERT (buf[2] == MAGIC);
     122                 :            : 
     123                 :          1 :     ret = u8_uctomb (buf, uc, 4);
     124         [ -  + ]:          1 :     ASSERT (ret == 4);
     125         [ -  + ]:          1 :     ASSERT (buf[0] == 0xF4);
     126         [ -  + ]:          1 :     ASSERT (buf[1] == 0x8F);
     127         [ -  + ]:          1 :     ASSERT (buf[2] == 0xBF);
     128         [ -  + ]:          1 :     ASSERT (buf[3] == 0xBD);
     129         [ -  + ]:          1 :     ASSERT (buf[4] == MAGIC);
     130                 :            :   }
     131                 :            : 
     132                 :            :   /* Test invalid characters.  */
     133                 :            :   {
     134                 :          1 :     ucs4_t invalid[] = { 0x110000, 0xD800, 0xDBFF, 0xDC00, 0xDFFF };
     135                 :          1 :     uint8_t buf[5] = { MAGIC, MAGIC, MAGIC, MAGIC, MAGIC };
     136                 :            :     size_t i;
     137                 :            : 
     138         [ +  + ]:          6 :     for (i = 0; i < SIZEOF (invalid); i++)
     139                 :            :       {
     140                 :          5 :         ucs4_t uc = invalid[i];
     141                 :            :         int n;
     142                 :            : 
     143         [ +  + ]:         30 :         for (n = 0; n <= 4; n++)
     144                 :            :           {
     145                 :         25 :             int ret = u8_uctomb (buf, uc, n);
     146         [ -  + ]:         25 :             ASSERT (ret == -1);
     147         [ -  + ]:         25 :             ASSERT (buf[0] == MAGIC);
     148         [ -  + ]:         25 :             ASSERT (buf[1] == MAGIC);
     149         [ -  + ]:         25 :             ASSERT (buf[2] == MAGIC);
     150         [ -  + ]:         25 :             ASSERT (buf[3] == MAGIC);
     151         [ -  + ]:         25 :             ASSERT (buf[4] == MAGIC);
     152                 :            :           }
     153                 :            :       }
     154                 :            :   }
     155                 :            : 
     156                 :          1 :   return 0;
     157                 :            : }

Generated by: LCOV version 1.9