(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.c-torture/
execute/
builtins/
strncpy.c
       1  /* Copyright (C) 2000, 2005  Free Software Foundation.
       2  
       3     Ensure all expected transformations of builtin strncpy occur and
       4     perform correctly.
       5  
       6     Written by Kaveh R. Ghazi, 11/25/2000.  */
       7  
       8  extern void abort (void);
       9  typedef __SIZE_TYPE__ size_t;
      10  extern char *strncpy (char *, const char *, size_t);
      11  extern int memcmp (const void *, const void *, size_t);
      12  extern void *memset (void *, int, size_t);
      13  
      14  /* Reset the destination buffer to a known state. */
      15  #define RESET_DST memset(dst, 'X', sizeof(dst))
      16  
      17  int i;
      18  
      19  void
      20  main_test (void)
      21  {
      22    const char *const src = "hello world";
      23    const char *src2;
      24    char dst[64], *dst2;
      25    
      26    RESET_DST;
      27    if (strncpy (dst, src, 4) != dst || memcmp (dst, "hellXXX", 7))
      28      abort();
      29  
      30    RESET_DST;
      31    if (strncpy (dst+16, src, 4) != dst+16 || memcmp (dst+16, "hellXXX", 7))
      32      abort();
      33  
      34    RESET_DST;
      35    if (strncpy (dst+32, src+5, 4) != dst+32 || memcmp (dst+32, " worXXX", 7))
      36      abort();
      37  
      38    RESET_DST;
      39    dst2 = dst;
      40    if (strncpy (++dst2, src+5, 4) != dst+1 || memcmp (dst2, " worXXX", 7)
      41        || dst2 != dst+1)
      42      abort();
      43  
      44    RESET_DST;
      45    if (strncpy (dst, src, 0) != dst || memcmp (dst, "XXX", 3))
      46      abort();
      47    
      48    RESET_DST;
      49    dst2 = dst; src2 = src;
      50    if (strncpy (++dst2, ++src2, 0) != dst+1 || memcmp (dst2, "XXX", 3)
      51        || dst2 != dst+1 || src2 != src+1)
      52      abort();
      53  
      54    RESET_DST;
      55    dst2 = dst; src2 = src;
      56    if (strncpy (++dst2+5, ++src2+5, 0) != dst+6 || memcmp (dst2+5, "XXX", 3)
      57        || dst2 != dst+1 || src2 != src+1)
      58      abort();
      59  
      60    RESET_DST;
      61    if (strncpy (dst, src, 12) != dst || memcmp (dst, "hello world\0XXX", 15))
      62      abort();
      63  
      64    /* Test at least one instance of the __builtin_ style.  We do this
      65       to ensure that it works and that the prototype is correct.  */
      66    RESET_DST;
      67    if (__builtin_strncpy (dst, src, 4) != dst || memcmp (dst, "hellXXX", 7))
      68      abort();
      69  
      70    RESET_DST;
      71    if (strncpy (dst, i++ ? "xfoo" + 1 : "bar", 4) != dst
      72        || memcmp (dst, "bar\0XXX", 7)
      73        || i != 1)
      74      abort ();
      75  }