summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/strcmpopt_6.c
blob: 06b7e3480dbb473b8a5910e134231af6ca2417fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Verify that strcmp and strncmp calls with mixed constant and
   non-constant strings are evaluated correctly.
   { dg-do run }
   { dg-options "-O2" } */

#include "strlenopt.h"

#define A(expr)                                                 \
  ((expr)                                                       \
   ? (void)0                                                    \
   : (__builtin_printf ("assertion failed on line %i: %s\n",    \
                        __LINE__, #expr),                       \
      __builtin_abort ()))

__attribute__ ((noclone, noinline)) int
test_strlen_gt2_strcmp_abcd (const char *s)
{
  if (strlen (s) < 3)
    return -1;

  return strcmp (s, "abcd") == 0;
}

__attribute__ ((noclone, noinline)) int
test_strlen_lt6_strcmp_abcd (const char *s)
{
  if (strlen (s) > 5)
    return -1;

  return strcmp (s, "abcd") == 0;
}

__attribute__ ((noclone, noinline)) int
test_strcpy_strcmp_abc (const char *s)
{
  char a[5];
  strcpy (a, s);
  return strcmp (a, "abc") == 0;
}

__attribute__ ((noclone, noinline)) int
test_strcpy_abc_strcmp (const char *s)
{
  char a[4], b[6];
  strcpy (a, "abc");
  strcpy (b, s);
  return strcmp (a, b) == 0;
}

/* Exercise strcmp of two strings between 1 and 3 characters long
   stored in arrays of the same known size.  */
char ga4[4], gb4[4];

__attribute__ ((noclone, noinline)) int
test_store_0_nulterm_strcmp_same_size_arrays (void)
{
  ga4[0] = gb4[0] = 'x';
  ga4[3] = gb4[3] = '\0';
  return strcmp (ga4, gb4) == 0;
}

__attribute__ ((noclone, noinline)) int
test_store_0_nulterm_strncmp_bound_2_same_size_arrays (void)
{
  ga4[0] = gb4[0] = 'x';
  ga4[3] = gb4[3] = '\0';
  return strncmp (ga4, gb4, 2) == 0;
}

__attribute__ ((noclone, noinline)) int
test_store_0_nulterm_strncmp_bound_equal_same_size_arrays (void)
{
  ga4[0] = gb4[0] = 'x';
  ga4[3] = gb4[3] = '\0';
  return strncmp (ga4, gb4, 4) == 0;
}

/* Exercise strcmp of two strings between 0 and 3 characters long
   stored in arrays of the same known size.  */

__attribute__ ((noclone, noinline)) int
test_nulterm_strcmp_same_size_arrays (void)
{
  ga4[3] = gb4[3] = '\0';
  return strcmp (ga4, gb4) == 0;
}

/* Exercise strcmp of two strings between 1 and 3 and 1 and 4 characters
   long, respectively, stored in arrays of known but different sizes.  */
char gc5[5];

__attribute__ ((noclone, noinline)) int
test_store_0_nulterm_strcmp_arrays (void)
{
  ga4[0] = gc5[0] = 'x';
  ga4[3] = gc5[4] = '\0';
  return strcmp (ga4, gc5) == 0;
}

/* Exercise strcmp of two strings between 0 and 3 and 1 and 4 characters
   long, respectively, stored in arrays of known but different sizes.  */

__attribute__ ((noclone, noinline)) int
test_nulterm_strcmp_arrays (void)
{
  ga4[3] = gc5[4] = '\0';
  return strcmp (ga4, gc5) == 0;
}


__attribute__ ((noclone, noinline)) int
test_strcpy_strncmp_abcd (const char *s)
{
  char a[6];
  strcpy (a, s);
  return strcmp (a, "abcd") == 0;
}

__attribute__ ((noclone, noinline)) int
test_strcpy_abcd_strncmp_3 (const char *s)
{
  char a[6], b[8];
  strcpy (a, "abcd");
  strcpy (b, s);
  return strncmp (a, b, 3) == 0;
}

__attribute__ ((noclone, noinline)) int
test_strcpy_abcd_strncmp_4 (const char *s)
{
  char a[6], b[8];
  strcpy (a, "abcd");
  strcpy (b, s);
  return strncmp (a, b, 4) == 0;
}


int main (void)
{
  test_strlen_gt2_strcmp_abcd ("abcd");
  test_strlen_lt6_strcmp_abcd ("abcd");

  A (0 == test_strcpy_strcmp_abc ("ab"));
  A (0 != test_strcpy_strcmp_abc ("abc"));
  A (0 == test_strcpy_strcmp_abc ("abcd"));

  A (0 == test_strcpy_abc_strcmp ("ab"));
  A (0 != test_strcpy_abc_strcmp ("abc"));
  A (0 == test_strcpy_abc_strcmp ("abcd"));

  strcpy (ga4, "abc"); strcpy (gb4, "abd");
  A (0 == test_store_0_nulterm_strcmp_same_size_arrays ());
  strcpy (ga4, "abd"); strcpy (gb4, "abc");
  A (0 == test_store_0_nulterm_strcmp_same_size_arrays ());
  strcpy (ga4, "abc"); strcpy (gb4, "abc");
  A (0 != test_store_0_nulterm_strcmp_same_size_arrays ());

  strcpy (ga4, "abc"); strcpy (gb4, "acd");
  A (0 == test_store_0_nulterm_strncmp_bound_2_same_size_arrays ());
  strcpy (ga4, "acd"); strcpy (gb4, "abc");
  A (0 == test_store_0_nulterm_strncmp_bound_2_same_size_arrays ());
  strcpy (ga4, "abc"); strcpy (gb4, "abc");
  A (0 != test_store_0_nulterm_strncmp_bound_2_same_size_arrays ());

  strcpy (ga4, "abc"); strcpy (gb4, "abd");
  A (0 == test_store_0_nulterm_strncmp_bound_equal_same_size_arrays ());
  strcpy (ga4, "abd"); strcpy (gb4, "abc");
  A (0 == test_store_0_nulterm_strncmp_bound_equal_same_size_arrays ());
  strcpy (ga4, "abc"); strcpy (gb4, "abc");
  A (0 != test_store_0_nulterm_strncmp_bound_equal_same_size_arrays ());

  strcpy (ga4, "abc"); strcpy (gb4, "abd");
  A (0 == test_nulterm_strcmp_same_size_arrays ());
  strcpy (ga4, "abd"); strcpy (gb4, "abc");
  A (0 == test_nulterm_strcmp_same_size_arrays ());
  strcpy (ga4, "abc"); strcpy (gb4, "abc");
  A (0 != test_nulterm_strcmp_same_size_arrays ());

  strcpy (ga4, "abc"); strcpy (gc5, "abcd");
  A (0 == test_store_0_nulterm_strcmp_arrays ());
  strcpy (ga4, "abd"); strcpy (gc5, "abcd");
  A (0 == test_store_0_nulterm_strcmp_arrays ());
  strcpy (ga4, "abc"); strcpy (gc5, "abc");
  A (0 != test_store_0_nulterm_strcmp_arrays ());

  strcpy (ga4, "abc"); strcpy (gc5, "abcd");
  A (0 == test_nulterm_strcmp_arrays ());
  strcpy (ga4, "abd"); strcpy (gc5, "abc");
  A (0 == test_nulterm_strcmp_arrays ());
  strcpy (ga4, "abc"); strcpy (gc5, "abc");
  A (0 != test_nulterm_strcmp_arrays ());

  A (0 == test_strcpy_strncmp_abcd ("ab"));
  A (0 == test_strcpy_strncmp_abcd ("abc"));
  A (0 != test_strcpy_strncmp_abcd ("abcd"));
  A (0 == test_strcpy_strncmp_abcd ("abcde"));

  A (0 == test_strcpy_abcd_strncmp_3 ("ab"));
  A (0 != test_strcpy_abcd_strncmp_3 ("abc"));
  A (0 != test_strcpy_abcd_strncmp_3 ("abcd"));
  A (0 != test_strcpy_abcd_strncmp_3 ("abcde"));

  A (0 == test_strcpy_abcd_strncmp_4 ("ab"));
  A (0 == test_strcpy_abcd_strncmp_4 ("abc"));
  A (0 != test_strcpy_abcd_strncmp_4 ("abcd"));
  A (0 != test_strcpy_abcd_strncmp_4 ("abcde"));
}