modernc.org/ccgo/v3@v3.16.14/lib/testdata/gcc-9.1.0/gcc/testsuite/gcc.c-torture/execute/builtins/strlen.c (about)

     1  /* Copyright (C) 2000, 2001, 2003, 2004  Free Software Foundation.
     2  
     3     Ensure all expected transformations of builtin strlen
     4     occur and perform correctly.
     5  
     6     Written by Jakub Jelinek, 11/7/2000.
     7  
     8     Additional tests written by Roger Sayle, 11/02/2001:
     9     Ensure all builtin strlen comparisons against zero are optimized
    10     and perform correctly. The multiple calls to strcpy are to prevent
    11     the potentially "pure" strlen calls from being removed by CSE.
    12  
    13     Modified by Ben Elliston, 2006-10-25:
    14     The multiple calls to strcpy that Roger mentions above are
    15     problematic on systems where strcpy is implemented using strlen
    16     (which this test overrides to call abort).  So, rather than use
    17     strcpy, we perform the identical operations using array indexing
    18     and char assignments.  */
    19  
    20  extern void abort (void);
    21  extern __SIZE_TYPE__ strlen (const char *);
    22  extern char *strcpy (char *, const char *);
    23  
    24  int x = 6;
    25  
    26  void
    27  main_test(void)
    28  {
    29    const char *const foo = "hello world";
    30    char str[8];
    31    char *ptr;
    32  
    33    if (strlen (foo) != 11)
    34      abort ();
    35    if (strlen (foo + 4) != 7)
    36      abort ();
    37    if (strlen (foo + (x++ & 7)) != 5)
    38      abort ();
    39    if (x != 7)
    40      abort ();
    41  
    42    ptr = str;
    43    ptr[0] = 'n'; ptr[1] = 't'; ptr[2] = 's'; ptr[3] = '\0';
    44    if (strlen (ptr) == 0)
    45      abort ();
    46  
    47    ptr[0] = 'n'; ptr[1] = 't'; ptr[2] = 's'; ptr[3] = '\0';
    48    if (strlen (ptr) < 1)
    49      abort ();
    50  
    51    ptr[0] = 'n'; ptr[1] = 't'; ptr[2] = 's'; ptr[3] = '\0';
    52    if (strlen (ptr) <= 0)
    53      abort ();
    54  
    55    ptr[0] = 'n'; ptr[1] = 't'; ptr[2] = 's'; ptr[3] = '\0';
    56    if (strlen (ptr+3) != 0)
    57      abort ();
    58  
    59    ptr[0] = 'n'; ptr[1] = 't'; ptr[2] = 's'; ptr[3] = '\0';
    60    if (strlen (ptr+3) > 0)
    61      abort ();
    62  
    63    ptr[0] = 'n'; ptr[1] = 't'; ptr[2] = 's'; ptr[3] = '\0';
    64    if (strlen (str+3) >= 1)
    65      abort ();
    66  
    67    /* Test at least one instance of the __builtin_ style.  We do this
    68       to ensure that it works and that the prototype is correct.  */
    69    if (__builtin_strlen (foo) != 11)
    70      abort ();
    71  }