1 /* Copyright (C) 2002  Free Software Foundation.
2 
3    Test strncmp 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)
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
test(const unsigned char * s1,const unsigned char * s2,size_t len,int expected)32 test (const unsigned char *s1, const unsigned char *s2, size_t len, int expected)
33 {
34   int value = strncmp ((char *) s1, (char *) s2, len);
35 
36   if (expected < 0 && value >= 0)
37     __builtin_abort ();
38   else if (expected == 0 && value != 0)
39     __builtin_abort ();
40   else if (expected > 0 && value <= 0)
41     __builtin_abort ();
42 }
43 
main()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; 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; i++)
77 	    *p2++ = 'x';
78 
79 	  mod1[0] = '\0';
80 	  mod2[0] = '\0';
81 	  test (buf1, buf2, MAX_LENGTH, 0);
82 	  test (buf1, buf2, len, 0);
83 
84 	  mod1[0] = 'a';
85 	  mod1[1] = '\0';
86 	  mod2[0] = '\0';
87 	  test (buf1, buf2, MAX_LENGTH, +1);
88 	  test (buf1, buf2, len, 0);
89 
90 	  mod1[0] = '\0';
91 	  mod2[0] = 'a';
92 	  mod2[1] = '\0';
93 	  test (buf1, buf2, MAX_LENGTH, -1);
94 	  test (buf1, buf2, len, 0);
95 
96 	  mod1[0] = 'b';
97 	  mod1[1] = '\0';
98 	  mod2[0] = 'c';
99 	  mod2[1] = '\0';
100 	  test (buf1, buf2, MAX_LENGTH, -1);
101 	  test (buf1, buf2, len, 0);
102 
103 	  mod1[0] = 'c';
104 	  mod1[1] = '\0';
105 	  mod2[0] = 'b';
106 	  mod2[1] = '\0';
107 	  test (buf1, buf2, MAX_LENGTH, +1);
108 	  test (buf1, buf2, len, 0);
109 
110 	  mod1[0] = 'b';
111 	  mod1[1] = '\0';
112 	  mod2[0] = (unsigned char)'\251';
113 	  mod2[1] = '\0';
114 	  test (buf1, buf2, MAX_LENGTH, -1);
115 	  test (buf1, buf2, len, 0);
116 
117 	  mod1[0] = (unsigned char)'\251';
118 	  mod1[1] = '\0';
119 	  mod2[0] = 'b';
120 	  mod2[1] = '\0';
121 	  test (buf1, buf2, MAX_LENGTH, +1);
122 	  test (buf1, buf2, len, 0);
123 
124 	  mod1[0] = (unsigned char)'\251';
125 	  mod1[1] = '\0';
126 	  mod2[0] = (unsigned char)'\252';
127 	  mod2[1] = '\0';
128 	  test (buf1, buf2, MAX_LENGTH, -1);
129 	  test (buf1, buf2, len, 0);
130 
131 	  mod1[0] = (unsigned char)'\252';
132 	  mod1[1] = '\0';
133 	  mod2[0] = (unsigned char)'\251';
134 	  mod2[1] = '\0';
135 	  test (buf1, buf2, MAX_LENGTH, +1);
136 	  test (buf1, buf2, len, 0);
137 	}
138 
139   __builtin_exit (0);
140 }
141