1  /* Copyright (C) 2002  Free Software Foundation.
       2  
       3     Test strcmp with various combinations of pointer alignments and lengths to
       4     make sure any optimizations in the library are correct.
       5  
       6     Written by Michael Meissner, March 9, 2002.  */
       7  
       8  #include <string.h>
       9  #include <stddef.h>
      10  
      11  #ifndef MAX_OFFSET
      12  #define MAX_OFFSET (sizeof (long long))
      13  #endif
      14  
      15  #ifndef MAX_TEST
      16  #define MAX_TEST (8 * sizeof (long long))
      17  #endif
      18  
      19  #ifndef MAX_EXTRA
      20  #define MAX_EXTRA (sizeof (long long))
      21  #endif
      22  
      23  #define MAX_LENGTH (MAX_OFFSET + MAX_TEST + MAX_EXTRA + 2)
      24  
      25  static union {
      26    unsigned char buf[MAX_LENGTH];
      27    long long align_int;
      28    long double align_fp;
      29  } u1, u2;
      30  
      31  void
      32  test (const unsigned char *s1, const unsigned char *s2, int expected)
      33  {
      34    int value = strcmp ((char *) s1, (char *) s2);
      35  
      36    if (expected < 0 && value >= 0)
      37      abort ();
      38    else if (expected == 0 && value != 0)
      39      abort ();
      40    else if (expected > 0 && value <= 0)
      41      abort ();
      42  }
      43  
      44  main ()
      45  {
      46    size_t off1, off2, len, i;
      47    unsigned char *buf1, *buf2;
      48    unsigned char *mod1, *mod2;
      49    unsigned char *p1, *p2;
      50  
      51    for (off1 = 0; off1 < MAX_OFFSET; off1++)
      52      for (off2 = 0; off2 < MAX_OFFSET; off2++)
      53        for (len = 0; len < MAX_TEST; len++)
      54  	{
      55  	  p1 = u1.buf;
      56  	  for (i = 0; i < off1; i++)
      57  	    *p1++ = '\0';
      58  
      59  	  buf1 = p1;
      60  	  for (i = 0; i < len; i++)
      61  	    *p1++ = 'a';
      62  
      63  	  mod1 = p1;
      64  	  for (i = 0; i < MAX_EXTRA+2; i++)
      65  	    *p1++ = 'x';
      66  
      67  	  p2 = u2.buf;
      68  	  for (i = 0; i < off2; i++)
      69  	    *p2++ = '\0';
      70  
      71  	  buf2 = p2;
      72  	  for (i = 0; i < len; i++)
      73  	    *p2++ = 'a';
      74  
      75  	  mod2 = p2;
      76  	  for (i = 0; i < MAX_EXTRA+2; i++)
      77  	    *p2++ = 'x';
      78  
      79  	  mod1[0] = '\0';
      80  	  mod2[0] = '\0';
      81  	  test (buf1, buf2, 0);
      82  
      83  	  mod1[0] = 'a';
      84  	  mod1[1] = '\0';
      85  	  mod2[0] = '\0';
      86  	  test (buf1, buf2, +1);
      87  
      88  	  mod1[0] = '\0';
      89  	  mod2[0] = 'a';
      90  	  mod2[1] = '\0';
      91  	  test (buf1, buf2, -1);
      92  
      93  	  mod1[0] = 'b';
      94  	  mod1[1] = '\0';
      95  	  mod2[0] = 'c';
      96  	  mod2[1] = '\0';
      97  	  test (buf1, buf2, -1);
      98  
      99  	  mod1[0] = 'c';
     100  	  mod1[1] = '\0';
     101  	  mod2[0] = 'b';
     102  	  mod2[1] = '\0';
     103  	  test (buf1, buf2, +1);
     104  
     105  	  mod1[0] = 'b';
     106  	  mod1[1] = '\0';
     107  	  mod2[0] = (unsigned char)'\251';
     108  	  mod2[1] = '\0';
     109  	  test (buf1, buf2, -1);
     110  
     111  	  mod1[0] = (unsigned char)'\251';
     112  	  mod1[1] = '\0';
     113  	  mod2[0] = 'b';
     114  	  mod2[1] = '\0';
     115  	  test (buf1, buf2, +1);
     116  
     117  	  mod1[0] = (unsigned char)'\251';
     118  	  mod1[1] = '\0';
     119  	  mod2[0] = (unsigned char)'\252';
     120  	  mod2[1] = '\0';
     121  	  test (buf1, buf2, -1);
     122  
     123  	  mod1[0] = (unsigned char)'\252';
     124  	  mod1[1] = '\0';
     125  	  mod2[0] = (unsigned char)'\251';
     126  	  mod2[1] = '\0';
     127  	  test (buf1, buf2, +1);
     128  	}
     129  
     130    exit (0);
     131  }