1 /* Alignment/padding coverage test for string comparison.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* This performs test comparisons with various (mis)alignments and
20 characters in the padding. It is partly a regression test for bug
42 max_size_t (size_t left
, size_t right
)
50 /* Wrappers for strncmp and strncasecmp which determine the maximum
51 string length in some, either based on the input string length, or
52 using fixed constants. */
55 strncmp_no_terminator (const char *left
, const char *right
)
57 size_t left_len
= strlen (left
);
58 size_t right_len
= strlen (right
);
59 return strncmp (left
, right
, max_size_t (left_len
, right_len
));
63 strncasecmp_no_terminator (const char *left
, const char *right
)
65 size_t left_len
= strlen (left
);
66 size_t right_len
= strlen (right
);
67 return strncasecmp (left
, right
, max_size_t (left_len
, right_len
));
71 strncmp_terminator (const char *left
, const char *right
)
73 size_t left_len
= strlen (left
);
74 size_t right_len
= strlen (right
);
75 return strncmp (left
, right
, max_size_t (left_len
, right_len
));
79 strncasecmp_terminator (const char *left
, const char *right
)
81 size_t left_len
= strlen (left
);
82 size_t right_len
= strlen (right
);
83 return strncasecmp (left
, right
, max_size_t (left_len
, right_len
));
87 strncmp_64 (const char *left
, const char *right
)
89 return strncmp (left
, right
, 64);
93 strncasecmp_64 (const char *left
, const char *right
)
95 return strncasecmp (left
, right
, 64);
99 strncmp_max (const char *left
, const char *right
)
101 return strncmp (left
, right
, SIZE_MAX
);
105 strncasecmp_max (const char *left
, const char *right
)
107 return strncasecmp (left
, right
, SIZE_MAX
);
115 max_string_length
= 33
117 size_t blob_size
= max_align
+ max_string_length
+ 1;
118 char *left
= memalign (max_align
, blob_size
);
119 char *right
= memalign (max_align
, blob_size
);
120 if (left
== NULL
|| right
== NULL
)
122 printf ("error: out of memory\n");
129 int (*implementation
) (const char *, const char *);
132 { "strcmp", strcmp
},
133 { "strcasecmp", strcasecmp
},
134 { "strncmp (without NUL)", strncmp_no_terminator
},
135 { "strncasecmp (without NUL)", strncasecmp_no_terminator
},
136 { "strncmp (with NUL)", strncmp_terminator
},
137 { "strncasecmp (with NUL)", strncasecmp_terminator
},
138 { "strncmp (length 64)", strncmp_64
},
139 { "strncasecmp (length 64)", strncasecmp_64
},
140 { "strncmp (length SIZE_MAX)", strncmp_max
},
141 { "strncasecmp (length SIZE_MAX)", strncasecmp_max
},
144 const char *const strings
[] =
159 "abcdefghijklmnopqrstuvwxyzABCDEF",
162 const unsigned char pads
[] =
163 { 0, 1, 32, 64, 128, '0', '1', 'e', 'f', 'g', 127, 192, 255 };
166 for (int left_idx
= 0; strings
[left_idx
] != NULL
; ++left_idx
)
167 for (int left_align
= 0; left_align
< max_align
; ++left_align
)
168 for (unsigned pad_left
= 0; pad_left
< sizeof (pads
); ++pad_left
)
170 memset (left
, pads
[pad_left
], blob_size
);
171 strcpy (left
+ left_align
, strings
[left_idx
]);
173 for (int right_idx
= 0; strings
[right_idx
] != NULL
; ++right_idx
)
174 for (unsigned pad_right
= 0; pad_right
< sizeof (pads
);
176 for (int right_align
= 0; right_align
< max_align
;
179 memset (right
, pads
[pad_right
], blob_size
);
180 strcpy (right
+ right_align
, strings
[right_idx
]);
182 for (int func
= 0; functions
[func
].name
!= NULL
; ++func
)
184 int expected
= left_idx
- right_idx
;
185 int actual
= functions
[func
].implementation
186 (left
+ left_align
, right
+ right_align
);
187 if (signum (actual
) != signum (expected
))
189 printf ("error: mismatch for %s: %d\n"
192 " pad_left = %u, pad_right = %u,\n"
193 " left_align = %d, right_align = %d\n",
194 functions
[func
].name
, actual
,
195 strings
[left_idx
], strings
[right_idx
],
197 left_align
, right_align
);
208 /* The nested loops need a long time to complete on slower
212 #include <support/test-driver.c>