struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / gcc-torture-execute-strncmp-1.c
blob39553484e8423c83918811fbf48ee5c0706751b3
1 /*
2 strncmp-1.c from the execute part of the gcc torture tests.
3 */
5 #include <testfwk.h>
7 /* { dg-xfail-if "kernel strncmp does not perform unsigned comparisons" { vxworks_kernel } } */
8 /* Copyright (C) 2002 Free Software Foundation.
10 Test strncmp with various combinations of pointer alignments and lengths to
11 make sure any optimizations in the library are correct.
13 Written by Michael Meissner, March 9, 2002. */
15 #include <string.h>
16 #include <stddef.h>
18 #ifndef MAX_OFFSET
19 #define MAX_OFFSET (sizeof (long long))
20 #endif
22 #ifndef MAX_TEST
23 #define MAX_TEST (2 * sizeof (long long)) /* Was (8 * sizeof (long long)) in GCC, reduced to speed up regression testing */
24 #endif
26 #ifndef MAX_EXTRA
27 #define MAX_EXTRA (sizeof (long long))
28 #endif
30 #define MAX_LENGTH (MAX_OFFSET + MAX_TEST + MAX_EXTRA)
32 #if !(defined(__SDCC_mcs51) && defined(__SDCC_MODEL_SMALL)) && !defined(__SDCC_pdk14) // Lack of memory
33 static union {
34 unsigned char buf[MAX_LENGTH];
35 long long align_int;
36 #if 0 // TODO: enable when SDCC supports long double!
37 long double align_fp;
38 #endif
39 } u1, u2;
40 #endif
42 void
43 test (const unsigned char *s1, const unsigned char *s2, size_t len, int expected)
45 int value = strncmp ((char *) s1, (char *) s2, len);
47 if (expected < 0 && value >= 0)
48 ASSERT (0);
49 else if (expected == 0 && value != 0)
50 ASSERT (0);
51 else if (expected > 0 && value <= 0)
52 ASSERT (0);
55 void
56 testTortureExecute (void)
58 #if !(defined(__SDCC_mcs51) && defined(__SDCC_MODEL_SMALL)) && !defined(__SDCC_pdk14) // Lack of memory
59 size_t off1, off2, len, i;
60 unsigned char *buf1, *buf2;
61 unsigned char *mod1, *mod2;
62 unsigned char *p1, *p2;
64 for (off1 = 0; off1 < MAX_OFFSET; off1++)
65 for (off2 = 0; off2 < MAX_OFFSET; off2++)
66 for (len = 0; len < MAX_TEST; len++)
68 p1 = u1.buf;
69 for (i = 0; i < off1; i++)
70 *p1++ = '\0';
72 buf1 = p1;
73 for (i = 0; i < len; i++)
74 *p1++ = 'a';
76 mod1 = p1;
77 for (i = 0; i < MAX_EXTRA; i++)
78 *p1++ = 'x';
80 p2 = u2.buf;
81 for (i = 0; i < off2; i++)
82 *p2++ = '\0';
84 buf2 = p2;
85 for (i = 0; i < len; i++)
86 *p2++ = 'a';
88 mod2 = p2;
89 for (i = 0; i < MAX_EXTRA; i++)
90 *p2++ = 'x';
92 mod1[0] = '\0';
93 mod2[0] = '\0';
94 test (buf1, buf2, MAX_LENGTH, 0);
95 test (buf1, buf2, len, 0);
97 mod1[0] = 'a';
98 mod1[1] = '\0';
99 mod2[0] = '\0';
100 test (buf1, buf2, MAX_LENGTH, +1);
101 test (buf1, buf2, len, 0);
103 mod1[0] = '\0';
104 mod2[0] = 'a';
105 mod2[1] = '\0';
106 test (buf1, buf2, MAX_LENGTH, -1);
107 test (buf1, buf2, len, 0);
109 mod1[0] = 'b';
110 mod1[1] = '\0';
111 mod2[0] = 'c';
112 mod2[1] = '\0';
113 test (buf1, buf2, MAX_LENGTH, -1);
114 test (buf1, buf2, len, 0);
116 mod1[0] = 'c';
117 mod1[1] = '\0';
118 mod2[0] = 'b';
119 mod2[1] = '\0';
120 test (buf1, buf2, MAX_LENGTH, +1);
121 test (buf1, buf2, len, 0);
123 mod1[0] = 'b';
124 mod1[1] = '\0';
125 mod2[0] = (unsigned char)'\251';
126 mod2[1] = '\0';
127 test (buf1, buf2, MAX_LENGTH, -1);
128 test (buf1, buf2, len, 0);
130 mod1[0] = (unsigned char)'\251';
131 mod1[1] = '\0';
132 mod2[0] = 'b';
133 mod2[1] = '\0';
134 test (buf1, buf2, MAX_LENGTH, +1);
135 test (buf1, buf2, len, 0);
137 mod1[0] = (unsigned char)'\251';
138 mod1[1] = '\0';
139 mod2[0] = (unsigned char)'\252';
140 mod2[1] = '\0';
141 test (buf1, buf2, MAX_LENGTH, -1);
142 test (buf1, buf2, len, 0);
144 mod1[0] = (unsigned char)'\252';
145 mod1[1] = '\0';
146 mod2[0] = (unsigned char)'\251';
147 mod2[1] = '\0';
148 test (buf1, buf2, MAX_LENGTH, +1);
149 test (buf1, buf2, len, 0);
151 #endif
152 return;