(root)/
glibc-2.38/
wcsmbs/
tst-wcslcat.c
       1  /* Test the wcslcat function.
       2     Copyright (C) 2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library 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 GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <array_length.h>
      20  #include <stdio.h>
      21  #include <stdlib.h>
      22  #include <support/check.h>
      23  #include <wchar.h>
      24  
      25  static int
      26  do_test (void)
      27  {
      28    struct {
      29      wchar_t buf1[16];
      30      wchar_t buf2[16];
      31    } s;
      32  
      33    /* Nothing is written to the destination if its size is 0.  */
      34    wmemset (s.buf1, '@', array_length (s.buf1));
      35    wmemset (s.buf2, '@', array_length (s.buf2));
      36    TEST_COMPARE (wcslcat (s.buf1, L"", 0), 0);
      37    TEST_COMPARE_BLOB (&s, sizeof (s), L"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
      38    TEST_COMPARE (wcslcat (s.buf1, L"Hello!", 0), 6);
      39    TEST_COMPARE_BLOB (&s, sizeof (s), L"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
      40  
      41    /* No bytes are are modified in the target buffer if the source
      42       string is short enough.  */
      43    wmemset (s.buf1, '@', array_length (s.buf1));
      44    wmemset (s.buf2, '@', array_length (s.buf2));
      45    wcscpy (s.buf1, L"He");
      46    TEST_COMPARE (wcslcat (s.buf1, L"llo!", array_length (s.buf1)), 6);
      47    TEST_COMPARE_BLOB (&s, sizeof (s), L"Hello!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
      48  
      49    /* A source string which fits exactly into the destination buffer is
      50       not truncated.  */
      51    wmemset (s.buf1, '@', array_length (s.buf1));
      52    wmemset (s.buf2, '@', array_length (s.buf2));
      53    wcscpy (s.buf1, L"H");
      54    TEST_COMPARE (wcslcat (s.buf1, L"ello, world!!!", array_length (s.buf1)),
      55                  15);
      56    TEST_COMPARE_BLOB (&s, sizeof (s),
      57  		     L"Hello, world!!!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
      58  
      59    /* A source string one character longer than the destination buffer
      60       is truncated by one character.  The total length is returned.  */
      61    wmemset (s.buf1, '@', array_length (s.buf1));
      62    wmemset (s.buf2, '@', array_length (s.buf2));
      63    wcscpy (s.buf1, L"Hello");
      64    TEST_COMPARE (wcslcat (s.buf1, L", world!!!!", array_length (s.buf1)), 16);
      65    TEST_COMPARE_BLOB (&s, sizeof (s),
      66  		     L"Hello, world!!!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
      67  
      68    /* An even longer source string is truncated as well, and the total
      69       length is returned.  */
      70    wmemset (s.buf1, '@', array_length (s.buf1));
      71    wmemset (s.buf2, '@', array_length (s.buf2));
      72    wcscpy (s.buf1, L"Hello,");
      73    TEST_COMPARE (wcslcat (s.buf1, L" world!!!!!!!!", array_length (s.buf1)),
      74                  20);
      75    TEST_COMPARE_BLOB (&s, sizeof (s),
      76  		     L"Hello, world!!!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
      77  
      78    /* A destination string which is not NUL-terminated does not result
      79       in any changes to the buffer.  */
      80    wmemset (s.buf1, '$', array_length (s.buf1));
      81    wmemset (s.buf2, '@', array_length (s.buf2));
      82    TEST_COMPARE (wcslcat (s.buf1, L"", array_length (s.buf1)), 16);
      83    TEST_COMPARE_BLOB (&s, sizeof (s), L"$$$$$$$$$$$$$$$$@@@@@@@@@@@@@@@@", 128);
      84    TEST_COMPARE (wcslcat (s.buf1, L"Hello!", array_length (s.buf1)), 22);
      85    TEST_COMPARE_BLOB (&s, sizeof (s), L"$$$$$$$$$$$$$$$$@@@@@@@@@@@@@@@@", 128);
      86    TEST_COMPARE (wcslcat (s.buf1, L"Hello, world!!!!!!!!",
      87                           array_length (s.buf1)), 36);
      88    TEST_COMPARE_BLOB (&s, sizeof (s), L"$$$$$$$$$$$$$$$$@@@@@@@@@@@@@@@@", 128);
      89  
      90    return 0;
      91  }
      92  
      93  #include <support/test-driver.c>