| [99d3123] | 1 | /*
|
|---|
| [d7f7a4a] | 2 | * SPDX-FileCopyrightText: 2018 Jiri Svoboda
|
|---|
| [99d3123] | 3 | *
|
|---|
| [d7f7a4a] | 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| [99d3123] | 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /* Prevent an error from being generated */
|
|---|
| [06e0f37] | 8 | #define _REALLY_WANT_STRING_H
|
|---|
| [99d3123] | 9 | #include <string.h>
|
|---|
| 10 | #include <pcut/pcut.h>
|
|---|
| 11 |
|
|---|
| [8591b31] | 12 | #ifndef __clang__
|
|---|
| [58e7b26] | 13 | #pragma GCC diagnostic ignored "-Wstringop-truncation"
|
|---|
| 14 | #pragma GCC diagnostic ignored "-Wstringop-overflow"
|
|---|
| [8591b31] | 15 | #endif
|
|---|
| [58e7b26] | 16 |
|
|---|
| [99d3123] | 17 | PCUT_INIT;
|
|---|
| 18 |
|
|---|
| 19 | PCUT_TEST_SUITE(string);
|
|---|
| 20 |
|
|---|
| 21 | /** strcpy function */
|
|---|
| 22 | PCUT_TEST(strcpy)
|
|---|
| 23 | {
|
|---|
| 24 | const char *s = "hello";
|
|---|
| 25 | char buf[7];
|
|---|
| 26 | size_t i;
|
|---|
| 27 | char *p;
|
|---|
| 28 |
|
|---|
| 29 | for (i = 0; i < 7; i++)
|
|---|
| 30 | buf[i] = 'X';
|
|---|
| 31 |
|
|---|
| 32 | p = strcpy(buf, s);
|
|---|
| 33 |
|
|---|
| 34 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 35 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
|---|
| 36 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
|---|
| 37 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
|---|
| 38 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
|---|
| 39 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
|---|
| 40 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
|---|
| 41 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | /** strncpy function with n == 0 */
|
|---|
| 45 | PCUT_TEST(strncpy_zero)
|
|---|
| 46 | {
|
|---|
| 47 | const char *s = "hello";
|
|---|
| 48 | char buf[1];
|
|---|
| 49 | char *p;
|
|---|
| 50 |
|
|---|
| 51 | buf[0] = 'X';
|
|---|
| 52 |
|
|---|
| 53 | p = strncpy(buf, s, 0);
|
|---|
| 54 |
|
|---|
| 55 | /* No characters are copied */
|
|---|
| 56 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 57 | PCUT_ASSERT_TRUE(buf[0] == 'X');
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /** strncpy function with string longer than n argument */
|
|---|
| 61 | PCUT_TEST(strncpy_long)
|
|---|
| 62 | {
|
|---|
| 63 | const char *s = "hello";
|
|---|
| 64 | char buf[5];
|
|---|
| 65 | size_t i;
|
|---|
| 66 | char *p;
|
|---|
| 67 |
|
|---|
| 68 | for (i = 0; i < 5; i++)
|
|---|
| 69 | buf[i] = 'X';
|
|---|
| 70 |
|
|---|
| 71 | p = strncpy(buf, s, 4);
|
|---|
| 72 |
|
|---|
| 73 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 74 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
|---|
| 75 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
|---|
| 76 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
|---|
| 77 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
|---|
| 78 | PCUT_ASSERT_TRUE(buf[4] == 'X');
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /** strncpy function with string containing exactly n characters */
|
|---|
| 82 | PCUT_TEST(strncpy_just)
|
|---|
| 83 | {
|
|---|
| 84 | const char *s = "hello";
|
|---|
| 85 | char buf[6];
|
|---|
| 86 | size_t i;
|
|---|
| 87 | char *p;
|
|---|
| 88 |
|
|---|
| 89 | for (i = 0; i < 6; i++)
|
|---|
| 90 | buf[i] = 'X';
|
|---|
| 91 |
|
|---|
| 92 | p = strncpy(buf, s, 5);
|
|---|
| 93 |
|
|---|
| 94 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 95 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
|---|
| 96 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
|---|
| 97 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
|---|
| 98 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
|---|
| 99 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
|---|
| 100 | PCUT_ASSERT_TRUE(buf[5] == 'X');
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /** strncpy function with string containing exactly n - 1 characters */
|
|---|
| 104 | PCUT_TEST(strncpy_just_over)
|
|---|
| 105 | {
|
|---|
| 106 | const char *s = "hello";
|
|---|
| 107 | char buf[7];
|
|---|
| 108 | size_t i;
|
|---|
| 109 | char *p;
|
|---|
| 110 |
|
|---|
| 111 | for (i = 0; i < 7; i++)
|
|---|
| 112 | buf[i] = 'X';
|
|---|
| 113 |
|
|---|
| 114 | p = strncpy(buf, s, 6);
|
|---|
| 115 |
|
|---|
| 116 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 117 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
|---|
| 118 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
|---|
| 119 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
|---|
| 120 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
|---|
| 121 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
|---|
| 122 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
|---|
| 123 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** strncpy function with string containing less than n - 1 characters */
|
|---|
| 127 | PCUT_TEST(strncpy_over)
|
|---|
| 128 | {
|
|---|
| 129 | const char *s = "hello";
|
|---|
| 130 | char buf[8];
|
|---|
| 131 | size_t i;
|
|---|
| 132 | char *p;
|
|---|
| 133 |
|
|---|
| 134 | for (i = 0; i < 8; i++)
|
|---|
| 135 | buf[i] = 'X';
|
|---|
| 136 |
|
|---|
| 137 | p = strncpy(buf, s, 7);
|
|---|
| 138 |
|
|---|
| 139 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 140 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
|---|
| 141 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
|---|
| 142 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
|---|
| 143 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
|---|
| 144 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
|---|
| 145 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
|---|
| 146 | PCUT_ASSERT_TRUE(buf[6] == '\0');
|
|---|
| 147 | PCUT_ASSERT_TRUE(buf[7] == 'X');
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /** strcat function */
|
|---|
| 151 | PCUT_TEST(strcat)
|
|---|
| 152 | {
|
|---|
| 153 | char buf[7];
|
|---|
| 154 | const char *s = "cde";
|
|---|
| 155 | size_t i;
|
|---|
| 156 | char *p;
|
|---|
| 157 |
|
|---|
| 158 | buf[0] = 'a';
|
|---|
| 159 | buf[1] = 'b';
|
|---|
| 160 | buf[2] = '\0';
|
|---|
| 161 |
|
|---|
| 162 | for (i = 3; i < 7; i++)
|
|---|
| 163 | buf[i] = 'X';
|
|---|
| 164 |
|
|---|
| 165 | p = strcat(buf, s);
|
|---|
| 166 |
|
|---|
| 167 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 168 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
|---|
| 169 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
|---|
| 170 | PCUT_ASSERT_TRUE(buf[2] == 'c');
|
|---|
| 171 | PCUT_ASSERT_TRUE(buf[3] == 'd');
|
|---|
| 172 | PCUT_ASSERT_TRUE(buf[4] == 'e');
|
|---|
| 173 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
|---|
| 174 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /** strncat function with n == 0 */
|
|---|
| 178 | PCUT_TEST(strncat_zero)
|
|---|
| 179 | {
|
|---|
| 180 | const char *s = "cde";
|
|---|
| 181 | char buf[4];
|
|---|
| 182 | char *p;
|
|---|
| 183 |
|
|---|
| 184 | buf[0] = 'a';
|
|---|
| 185 | buf[1] = 'b';
|
|---|
| 186 | buf[2] = '\0';
|
|---|
| 187 | buf[3] = 'X';
|
|---|
| 188 |
|
|---|
| 189 | p = strncat(buf, s, 0);
|
|---|
| 190 |
|
|---|
| 191 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 192 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
|---|
| 193 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
|---|
| 194 | PCUT_ASSERT_TRUE(buf[2] == '\0');
|
|---|
| 195 | PCUT_ASSERT_TRUE(buf[3] == 'X');
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /** strncat function with string longer than n argument */
|
|---|
| 199 | PCUT_TEST(strncat_long)
|
|---|
| 200 | {
|
|---|
| 201 | const char *s = "cde";
|
|---|
| 202 | char buf[6];
|
|---|
| 203 | size_t i;
|
|---|
| 204 | char *p;
|
|---|
| 205 |
|
|---|
| 206 | buf[0] = 'a';
|
|---|
| 207 | buf[1] = 'b';
|
|---|
| 208 | buf[2] = '\0';
|
|---|
| 209 |
|
|---|
| 210 | for (i = 3; i < 6; i++)
|
|---|
| 211 | buf[i] = 'X';
|
|---|
| 212 |
|
|---|
| 213 | p = strncat(buf, s, 2);
|
|---|
| 214 |
|
|---|
| 215 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 216 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
|---|
| 217 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
|---|
| 218 | PCUT_ASSERT_TRUE(buf[2] == 'c');
|
|---|
| 219 | PCUT_ASSERT_TRUE(buf[3] == 'd');
|
|---|
| 220 | PCUT_ASSERT_TRUE(buf[4] == '\0');
|
|---|
| 221 | PCUT_ASSERT_TRUE(buf[5] == 'X');
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | /** strncat function with string containing exactly n characters */
|
|---|
| 225 | PCUT_TEST(strncat_just)
|
|---|
| 226 | {
|
|---|
| 227 | const char *s = "cde";
|
|---|
| 228 | char buf[7];
|
|---|
| 229 | size_t i;
|
|---|
| 230 | char *p;
|
|---|
| 231 |
|
|---|
| 232 | buf[0] = 'a';
|
|---|
| 233 | buf[1] = 'b';
|
|---|
| 234 | buf[2] = '\0';
|
|---|
| 235 |
|
|---|
| 236 | for (i = 3; i < 7; i++)
|
|---|
| 237 | buf[i] = 'X';
|
|---|
| 238 |
|
|---|
| 239 | p = strncat(buf, s, 3);
|
|---|
| 240 |
|
|---|
| 241 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 242 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
|---|
| 243 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
|---|
| 244 | PCUT_ASSERT_TRUE(buf[2] == 'c');
|
|---|
| 245 | PCUT_ASSERT_TRUE(buf[3] == 'd');
|
|---|
| 246 | PCUT_ASSERT_TRUE(buf[4] == 'e');
|
|---|
| 247 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
|---|
| 248 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /** strncat function with string containing exactly n - 1 characters */
|
|---|
| 252 | PCUT_TEST(strncat_just_over)
|
|---|
| 253 | {
|
|---|
| 254 | const char *s = "cde";
|
|---|
| 255 | char buf[7];
|
|---|
| 256 | size_t i;
|
|---|
| 257 | char *p;
|
|---|
| 258 |
|
|---|
| 259 | buf[0] = 'a';
|
|---|
| 260 | buf[1] = 'b';
|
|---|
| 261 | buf[2] = '\0';
|
|---|
| 262 |
|
|---|
| 263 | for (i = 3; i < 7; i++)
|
|---|
| 264 | buf[i] = 'X';
|
|---|
| 265 |
|
|---|
| 266 | p = strncat(buf, s, 4);
|
|---|
| 267 |
|
|---|
| 268 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 269 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
|---|
| 270 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
|---|
| 271 | PCUT_ASSERT_TRUE(buf[2] == 'c');
|
|---|
| 272 | PCUT_ASSERT_TRUE(buf[3] == 'd');
|
|---|
| 273 | PCUT_ASSERT_TRUE(buf[4] == 'e');
|
|---|
| 274 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
|---|
| 275 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /** strncat function with string containing less than n - 1 characters */
|
|---|
| 279 | PCUT_TEST(strncat_over)
|
|---|
| 280 | {
|
|---|
| 281 | const char *s = "cde";
|
|---|
| 282 | char buf[7];
|
|---|
| 283 | size_t i;
|
|---|
| 284 | char *p;
|
|---|
| 285 |
|
|---|
| 286 | buf[0] = 'a';
|
|---|
| 287 | buf[1] = 'b';
|
|---|
| 288 | buf[2] = '\0';
|
|---|
| 289 |
|
|---|
| 290 | for (i = 3; i < 7; i++)
|
|---|
| 291 | buf[i] = 'X';
|
|---|
| 292 |
|
|---|
| 293 | p = strncat(buf, s, 5);
|
|---|
| 294 |
|
|---|
| 295 | PCUT_ASSERT_TRUE(p == buf);
|
|---|
| 296 | PCUT_ASSERT_TRUE(buf[0] == 'a');
|
|---|
| 297 | PCUT_ASSERT_TRUE(buf[1] == 'b');
|
|---|
| 298 | PCUT_ASSERT_TRUE(buf[2] == 'c');
|
|---|
| 299 | PCUT_ASSERT_TRUE(buf[3] == 'd');
|
|---|
| 300 | PCUT_ASSERT_TRUE(buf[4] == 'e');
|
|---|
| 301 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
|---|
| 302 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | /** strcmp function with different characters after terminating null */
|
|---|
| 306 | PCUT_TEST(strcmp_same)
|
|---|
| 307 | {
|
|---|
| 308 | PCUT_ASSERT_TRUE(strcmp("apples\0#", "apples\0$") == 0);
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | /** strcmp function with first string less than second */
|
|---|
| 312 | PCUT_TEST(strcmp_less_than)
|
|---|
| 313 | {
|
|---|
| 314 | PCUT_ASSERT_TRUE(strcmp("apples", "oranges") < 0);
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | /** strcmp function with first string greater than second */
|
|---|
| 318 | PCUT_TEST(strcmp_greater_than)
|
|---|
| 319 | {
|
|---|
| 320 | PCUT_ASSERT_TRUE(strcmp("oranges", "apples") > 0);
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /* strcmp function with first string a prefix of second string */
|
|---|
| 324 | PCUT_TEST(strcmp_prefix)
|
|---|
| 325 | {
|
|---|
| 326 | PCUT_ASSERT_TRUE(strcmp("apple", "apples") < 0);
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | /** strcoll function */
|
|---|
| 330 | PCUT_TEST(strcoll)
|
|---|
| 331 | {
|
|---|
| 332 | /* Same string with different characters after terminating null */
|
|---|
| 333 | PCUT_ASSERT_TRUE(strcoll("apples\0#", "apples\0$") == 0);
|
|---|
| 334 |
|
|---|
| 335 | /* First string less than second */
|
|---|
| 336 | PCUT_ASSERT_TRUE(strcoll("apples", "oranges") < 0);
|
|---|
| 337 |
|
|---|
| 338 | /* First string greater than second */
|
|---|
| 339 | PCUT_ASSERT_TRUE(strcoll("oranges", "apples") > 0);
|
|---|
| 340 |
|
|---|
| 341 | /* First string is prefix of second */
|
|---|
| 342 | PCUT_ASSERT_TRUE(strcoll("apple", "apples") < 0);
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | /** strncmp function with n == 0 */
|
|---|
| 346 | PCUT_TEST(strncmp_zero)
|
|---|
| 347 | {
|
|---|
| 348 | PCUT_ASSERT_TRUE(strncmp("apple", "orange", 0) == 0);
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | /** strncmp function with strings differing after n characters */
|
|---|
| 352 | PCUT_TEST(strncmp_long)
|
|---|
| 353 | {
|
|---|
| 354 | PCUT_ASSERT_TRUE(strncmp("apple", "apricot", 2) == 0);
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | /** strncmp function with strings differing in (n-1)th character */
|
|---|
| 358 | PCUT_TEST(strncmp_just)
|
|---|
| 359 | {
|
|---|
| 360 | PCUT_ASSERT_TRUE(strncmp("apple", "apricot", 3) < 0);
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | /** strncmp function with strings differing before (n-1)th character */
|
|---|
| 364 | PCUT_TEST(strncmp_over)
|
|---|
| 365 | {
|
|---|
| 366 | PCUT_ASSERT_TRUE(strncmp("dart", "tart", 3) < 0);
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | /** strxfrm function with null destination to determine size needed */
|
|---|
| 370 | PCUT_TEST(strxfrm_null)
|
|---|
| 371 | {
|
|---|
| 372 | size_t n;
|
|---|
| 373 |
|
|---|
| 374 | n = strxfrm(NULL, "hello", 0);
|
|---|
| 375 | PCUT_ASSERT_INT_EQUALS(5, n);
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | /** strxfrm function with string longer than n argument */
|
|---|
| 379 | PCUT_TEST(strxfrm_long)
|
|---|
| 380 | {
|
|---|
| 381 | const char *s = "hello";
|
|---|
| 382 | char buf[5];
|
|---|
| 383 | size_t i;
|
|---|
| 384 | size_t n;
|
|---|
| 385 |
|
|---|
| 386 | for (i = 0; i < 5; i++)
|
|---|
| 387 | buf[i] = 'X';
|
|---|
| 388 |
|
|---|
| 389 | n = strxfrm(buf, s, 4);
|
|---|
| 390 |
|
|---|
| 391 | PCUT_ASSERT_INT_EQUALS(5, n);
|
|---|
| 392 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
|---|
| 393 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
|---|
| 394 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
|---|
| 395 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
|---|
| 396 | PCUT_ASSERT_TRUE(buf[4] == 'X');
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | /** strxfrm function with string containing exactly n characters */
|
|---|
| 400 | PCUT_TEST(strxfrm_just)
|
|---|
| 401 | {
|
|---|
| 402 | const char *s = "hello";
|
|---|
| 403 | char buf[6];
|
|---|
| 404 | size_t i;
|
|---|
| 405 | size_t n;
|
|---|
| 406 |
|
|---|
| 407 | for (i = 0; i < 6; i++)
|
|---|
| 408 | buf[i] = 'X';
|
|---|
| 409 |
|
|---|
| 410 | n = strxfrm(buf, s, 5);
|
|---|
| 411 |
|
|---|
| 412 | PCUT_ASSERT_INT_EQUALS(5, n);
|
|---|
| 413 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
|---|
| 414 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
|---|
| 415 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
|---|
| 416 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
|---|
| 417 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
|---|
| 418 | PCUT_ASSERT_TRUE(buf[5] == 'X');
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | /** strxfrm function with string containing exactly n - 1 characters */
|
|---|
| 422 | PCUT_TEST(strxfrm_just_over)
|
|---|
| 423 | {
|
|---|
| 424 | const char *s = "hello";
|
|---|
| 425 | char buf[7];
|
|---|
| 426 | size_t i;
|
|---|
| 427 | size_t n;
|
|---|
| 428 |
|
|---|
| 429 | for (i = 0; i < 7; i++)
|
|---|
| 430 | buf[i] = 'X';
|
|---|
| 431 |
|
|---|
| 432 | n = strxfrm(buf, s, 6);
|
|---|
| 433 |
|
|---|
| 434 | PCUT_ASSERT_INT_EQUALS(5, n);
|
|---|
| 435 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
|---|
| 436 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
|---|
| 437 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
|---|
| 438 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
|---|
| 439 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
|---|
| 440 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
|---|
| 441 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | /** strxfrm function with string containing less than n - 1 characters */
|
|---|
| 445 | PCUT_TEST(strxfrm_over)
|
|---|
| 446 | {
|
|---|
| 447 | const char *s = "hello";
|
|---|
| 448 | char buf[8];
|
|---|
| 449 | size_t i;
|
|---|
| 450 | size_t n;
|
|---|
| 451 |
|
|---|
| 452 | for (i = 0; i < 8; i++)
|
|---|
| 453 | buf[i] = 'X';
|
|---|
| 454 |
|
|---|
| 455 | n = strxfrm(buf, s, 7);
|
|---|
| 456 |
|
|---|
| 457 | PCUT_ASSERT_INT_EQUALS(5, n);
|
|---|
| 458 | PCUT_ASSERT_TRUE(buf[0] == 'h');
|
|---|
| 459 | PCUT_ASSERT_TRUE(buf[1] == 'e');
|
|---|
| 460 | PCUT_ASSERT_TRUE(buf[2] == 'l');
|
|---|
| 461 | PCUT_ASSERT_TRUE(buf[3] == 'l');
|
|---|
| 462 | PCUT_ASSERT_TRUE(buf[4] == 'o');
|
|---|
| 463 | PCUT_ASSERT_TRUE(buf[5] == '\0');
|
|---|
| 464 | PCUT_ASSERT_TRUE(buf[6] == 'X');
|
|---|
| 465 | PCUT_ASSERT_TRUE(buf[7] == 'X');
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | /** strchr function searching for null character */
|
|---|
| 469 | PCUT_TEST(strchr_nullchar)
|
|---|
| 470 | {
|
|---|
| 471 | const char *s = "abcabc";
|
|---|
| 472 | char *p;
|
|---|
| 473 |
|
|---|
| 474 | p = strchr(s, '\0');
|
|---|
| 475 | PCUT_ASSERT_TRUE((const char *)p == &s[6]);
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | /** strchr function with character occurring in string */
|
|---|
| 479 | PCUT_TEST(strchr_found)
|
|---|
| 480 | {
|
|---|
| 481 | const char *s = "abcabc";
|
|---|
| 482 | char *p;
|
|---|
| 483 |
|
|---|
| 484 | p = strchr(s, 'b');
|
|---|
| 485 | PCUT_ASSERT_TRUE((const char *)p == &s[1]);
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | /** strchr function with character not occurring in string */
|
|---|
| 489 | PCUT_TEST(strchr_not_found)
|
|---|
| 490 | {
|
|---|
| 491 | const char *s = "abcabc";
|
|---|
| 492 | char *p;
|
|---|
| 493 |
|
|---|
| 494 | p = strchr(s, 'd');
|
|---|
| 495 | PCUT_ASSERT_TRUE(p == NULL);
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | /** strcspn function with empty search string */
|
|---|
| 499 | PCUT_TEST(strcspn_empty_str)
|
|---|
| 500 | {
|
|---|
| 501 | size_t n;
|
|---|
| 502 |
|
|---|
| 503 | n = strcspn("", "abc");
|
|---|
| 504 | PCUT_ASSERT_INT_EQUALS(0, n);
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | /** strcspn function with empty character set */
|
|---|
| 508 | PCUT_TEST(strcspn_empty_set)
|
|---|
| 509 | {
|
|---|
| 510 | size_t n;
|
|---|
| 511 |
|
|---|
| 512 | n = strcspn("abc", "");
|
|---|
| 513 | PCUT_ASSERT_INT_EQUALS(3, n);
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | /** strcspn function with regular arguments */
|
|---|
| 517 | PCUT_TEST(strcspn_regular)
|
|---|
| 518 | {
|
|---|
| 519 | size_t n;
|
|---|
| 520 |
|
|---|
| 521 | n = strcspn("baBAba", "AB");
|
|---|
| 522 | PCUT_ASSERT_INT_EQUALS(2, n);
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | /** strpbrk function with empty search string */
|
|---|
| 526 | PCUT_TEST(strpbrk_empty_string)
|
|---|
| 527 | {
|
|---|
| [58e7b26] | 528 | const char *p;
|
|---|
| [99d3123] | 529 |
|
|---|
| 530 | p = strpbrk("", "abc");
|
|---|
| 531 | PCUT_ASSERT_NULL(p);
|
|---|
| 532 | }
|
|---|
| 533 |
|
|---|
| 534 | /** strpbrk function with empty character set */
|
|---|
| 535 | PCUT_TEST(strpbrk_empty_set)
|
|---|
| 536 | {
|
|---|
| [58e7b26] | 537 | const char *p;
|
|---|
| [99d3123] | 538 |
|
|---|
| 539 | p = strpbrk("abc", "");
|
|---|
| 540 | PCUT_ASSERT_NULL(p);
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | /** strpbrk function with regular parameters */
|
|---|
| 544 | PCUT_TEST(strpbrk_regular)
|
|---|
| 545 | {
|
|---|
| 546 | const char *s = "baBAba";
|
|---|
| 547 | char *p;
|
|---|
| 548 |
|
|---|
| 549 | p = strpbrk(s, "ab");
|
|---|
| 550 | PCUT_ASSERT_TRUE((const char *)p == s);
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | /** strrchr function searching for null character */
|
|---|
| 554 | PCUT_TEST(strrchr_nullchar)
|
|---|
| 555 | {
|
|---|
| 556 | const char *s = "abcabc";
|
|---|
| 557 | char *p;
|
|---|
| 558 |
|
|---|
| 559 | p = strrchr(s, '\0');
|
|---|
| 560 | PCUT_ASSERT_TRUE((const char *)p == &s[6]);
|
|---|
| 561 | }
|
|---|
| 562 |
|
|---|
| 563 | /** strrchr function with character occurring in string */
|
|---|
| 564 | PCUT_TEST(strrchr_found)
|
|---|
| 565 | {
|
|---|
| 566 | const char *s = "abcabc";
|
|---|
| 567 | char *p;
|
|---|
| 568 |
|
|---|
| 569 | p = strrchr(s, 'b');
|
|---|
| 570 | PCUT_ASSERT_TRUE((const char *)p == &s[4]);
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | /** strrchr function with character not occurring in string */
|
|---|
| 574 | PCUT_TEST(strrchr_not_found)
|
|---|
| 575 | {
|
|---|
| 576 | const char *s = "abcabc";
|
|---|
| 577 | char *p;
|
|---|
| 578 |
|
|---|
| 579 | p = strrchr(s, 'd');
|
|---|
| 580 | PCUT_ASSERT_TRUE(p == NULL);
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | /** strspn function with empty search string */
|
|---|
| 584 | PCUT_TEST(strspn_empty_str)
|
|---|
| 585 | {
|
|---|
| 586 | size_t n;
|
|---|
| 587 |
|
|---|
| 588 | n = strspn("", "abc");
|
|---|
| 589 | PCUT_ASSERT_INT_EQUALS(0, n);
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | /** strspn function with empty character set */
|
|---|
| 593 | PCUT_TEST(strspn_empty_set)
|
|---|
| 594 | {
|
|---|
| 595 | size_t n;
|
|---|
| 596 |
|
|---|
| 597 | n = strspn("abc", "");
|
|---|
| 598 | PCUT_ASSERT_INT_EQUALS(0, n);
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | /** strspn function with regular arguments */
|
|---|
| 602 | PCUT_TEST(strspn_regular)
|
|---|
| 603 | {
|
|---|
| 604 | size_t n;
|
|---|
| 605 |
|
|---|
| 606 | n = strspn("baBAba", "ab");
|
|---|
| 607 | PCUT_ASSERT_INT_EQUALS(2, n);
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | /** strstr function looking for empty substring */
|
|---|
| 611 | PCUT_TEST(strstr_empty)
|
|---|
| 612 | {
|
|---|
| 613 | const char *str = "abcabcabcdabc";
|
|---|
| 614 | char *p;
|
|---|
| 615 |
|
|---|
| 616 | p = strstr(str, "");
|
|---|
| 617 | PCUT_ASSERT_TRUE((const char *) p == str);
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | /** strstr function looking for substring with success */
|
|---|
| 621 | PCUT_TEST(strstr_found)
|
|---|
| 622 | {
|
|---|
| 623 | const char *str = "abcabcabcdabc";
|
|---|
| 624 | char *p;
|
|---|
| 625 |
|
|---|
| 626 | p = strstr(str, "abcd");
|
|---|
| 627 | PCUT_ASSERT_TRUE((const char *) p == &str[6]);
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | /** strstr function looking for substring with failure */
|
|---|
| 631 | PCUT_TEST(strstr_notfound)
|
|---|
| 632 | {
|
|---|
| 633 | const char *str = "abcabcabcdabc";
|
|---|
| 634 | char *p;
|
|---|
| 635 |
|
|---|
| 636 | p = strstr(str, "abcde");
|
|---|
| 637 | PCUT_ASSERT_NULL(p);
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | /** strtok function */
|
|---|
| 641 | PCUT_TEST(strtok)
|
|---|
| 642 | {
|
|---|
| 643 | char str[] = ":a::b;;;$c";
|
|---|
| 644 | char *t;
|
|---|
| 645 |
|
|---|
| 646 | t = strtok(str, ":");
|
|---|
| 647 | PCUT_ASSERT_TRUE(t == &str[1]);
|
|---|
| 648 | PCUT_ASSERT_INT_EQUALS(0, strcmp(t, "a"));
|
|---|
| 649 |
|
|---|
| 650 | t = strtok(NULL, ";");
|
|---|
| 651 | PCUT_ASSERT_TRUE(t == &str[3]);
|
|---|
| 652 | PCUT_ASSERT_INT_EQUALS(0, strcmp(t, ":b"));
|
|---|
| 653 |
|
|---|
| 654 | t = strtok(NULL, "$;");
|
|---|
| 655 | PCUT_ASSERT_TRUE(t == &str[9]);
|
|---|
| 656 | PCUT_ASSERT_INT_EQUALS(0, strcmp(t, "c"));
|
|---|
| 657 |
|
|---|
| 658 | t = strtok(NULL, "$");
|
|---|
| 659 | PCUT_ASSERT_NULL(t);
|
|---|
| 660 | }
|
|---|
| 661 |
|
|---|
| 662 | /** strerror function with zero argument */
|
|---|
| 663 | PCUT_TEST(strerror_zero)
|
|---|
| 664 | {
|
|---|
| 665 | char *p;
|
|---|
| 666 |
|
|---|
| 667 | p = strerror(0);
|
|---|
| 668 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 669 | }
|
|---|
| 670 |
|
|---|
| 671 | /** strerror function with errno value argument */
|
|---|
| 672 | PCUT_TEST(strerror_errno)
|
|---|
| 673 | {
|
|---|
| 674 | char *p;
|
|---|
| 675 |
|
|---|
| 676 | p = strerror(EINVAL);
|
|---|
| 677 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 678 | }
|
|---|
| 679 |
|
|---|
| 680 | /** strerror function with negative argument */
|
|---|
| 681 | PCUT_TEST(strerror_negative)
|
|---|
| 682 | {
|
|---|
| 683 | char *p;
|
|---|
| 684 |
|
|---|
| 685 | p = strerror(-1);
|
|---|
| 686 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 | /** strlen function with empty string */
|
|---|
| 690 | PCUT_TEST(strlen_empty)
|
|---|
| 691 | {
|
|---|
| 692 | PCUT_ASSERT_INT_EQUALS(0, strlen(""));
|
|---|
| 693 | }
|
|---|
| 694 |
|
|---|
| 695 | /** strlen function with non-empty string */
|
|---|
| 696 | PCUT_TEST(strlen_nonempty)
|
|---|
| 697 | {
|
|---|
| 698 | PCUT_ASSERT_INT_EQUALS(3, strlen("abc"));
|
|---|
| 699 | }
|
|---|
| 700 |
|
|---|
| [ea4910b] | 701 | /** strlen function with empty string and non-zero limit */
|
|---|
| 702 | PCUT_TEST(strnlen_empty_short)
|
|---|
| 703 | {
|
|---|
| 704 | PCUT_ASSERT_INT_EQUALS(0, strnlen("", 1));
|
|---|
| 705 | }
|
|---|
| 706 |
|
|---|
| 707 | /** strlen function with empty string and zero limit */
|
|---|
| 708 | PCUT_TEST(strnlen_empty_eq)
|
|---|
| 709 | {
|
|---|
| 710 | PCUT_ASSERT_INT_EQUALS(0, strnlen("", 0));
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | /** strlen function with non empty string below limit */
|
|---|
| 714 | PCUT_TEST(strnlen_nonempty_short)
|
|---|
| 715 | {
|
|---|
| 716 | PCUT_ASSERT_INT_EQUALS(3, strnlen("abc", 5));
|
|---|
| 717 | }
|
|---|
| 718 |
|
|---|
| 719 | /** strlen function with non empty string just below limit */
|
|---|
| 720 | PCUT_TEST(strnlen_nonempty_just_short)
|
|---|
| 721 | {
|
|---|
| 722 | PCUT_ASSERT_INT_EQUALS(3, strnlen("abc", 4));
|
|---|
| 723 | }
|
|---|
| 724 |
|
|---|
| 725 | /** strlen function with non empty string of length equal to limit */
|
|---|
| 726 | PCUT_TEST(strnlen_nonempty_eq)
|
|---|
| 727 | {
|
|---|
| 728 | PCUT_ASSERT_INT_EQUALS(3, strnlen("abc", 3));
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | /** strlen function with non empty string of length above limit */
|
|---|
| 732 | PCUT_TEST(strnlen_nonempty_long)
|
|---|
| 733 | {
|
|---|
| 734 | PCUT_ASSERT_INT_EQUALS(2, strnlen("abc", 2));
|
|---|
| 735 | }
|
|---|
| 736 |
|
|---|
| 737 | /** strdup function with empty string */
|
|---|
| 738 | PCUT_TEST(strdup_empty)
|
|---|
| 739 | {
|
|---|
| 740 | char *d = strdup("");
|
|---|
| 741 | PCUT_ASSERT_NOT_NULL(d);
|
|---|
| 742 | PCUT_ASSERT_TRUE(d[0] == '\0');
|
|---|
| 743 | free(d);
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | /** strdup function with non-empty string */
|
|---|
| 747 | PCUT_TEST(strdup_nonempty)
|
|---|
| 748 | {
|
|---|
| 749 | char *d = strdup("abc");
|
|---|
| 750 | PCUT_ASSERT_NOT_NULL(d);
|
|---|
| 751 | PCUT_ASSERT_TRUE(d[0] == 'a');
|
|---|
| 752 | PCUT_ASSERT_TRUE(d[1] == 'b');
|
|---|
| 753 | PCUT_ASSERT_TRUE(d[2] == 'c');
|
|---|
| 754 | PCUT_ASSERT_TRUE(d[3] == '\0');
|
|---|
| 755 | free(d);
|
|---|
| 756 | }
|
|---|
| 757 |
|
|---|
| 758 | /** strndup function with empty string and non-zero limit */
|
|---|
| 759 | PCUT_TEST(strndup_empty_short)
|
|---|
| 760 | {
|
|---|
| 761 | char *d = strndup("", 1);
|
|---|
| 762 | PCUT_ASSERT_NOT_NULL(d);
|
|---|
| 763 | PCUT_ASSERT_TRUE(d[0] == '\0');
|
|---|
| 764 | free(d);
|
|---|
| 765 | }
|
|---|
| 766 |
|
|---|
| 767 | /** strndup function with empty string and zero limit */
|
|---|
| 768 | PCUT_TEST(strndup_empty_eq)
|
|---|
| 769 | {
|
|---|
| 770 | char *d = strndup("", 0);
|
|---|
| 771 | PCUT_ASSERT_NOT_NULL(d);
|
|---|
| 772 | PCUT_ASSERT_TRUE(d[0] == '\0');
|
|---|
| 773 | free(d);
|
|---|
| 774 | }
|
|---|
| 775 |
|
|---|
| 776 | /** strndup function with non-empty string of length below limit */
|
|---|
| 777 | PCUT_TEST(strndup_nonempty_short)
|
|---|
| 778 | {
|
|---|
| 779 | char *d = strndup("abc", 5);
|
|---|
| 780 | PCUT_ASSERT_NOT_NULL(d);
|
|---|
| 781 | PCUT_ASSERT_TRUE(d[0] == 'a');
|
|---|
| 782 | PCUT_ASSERT_TRUE(d[1] == 'b');
|
|---|
| 783 | PCUT_ASSERT_TRUE(d[2] == 'c');
|
|---|
| 784 | PCUT_ASSERT_TRUE(d[3] == '\0');
|
|---|
| 785 | free(d);
|
|---|
| 786 | }
|
|---|
| 787 |
|
|---|
| 788 | /** strndup function with non-empty string of length equal to limit */
|
|---|
| 789 | PCUT_TEST(strndup_nonempty_eq)
|
|---|
| 790 | {
|
|---|
| 791 | char *d = strndup("abc", 3);
|
|---|
| 792 | PCUT_ASSERT_NOT_NULL(d);
|
|---|
| 793 | PCUT_ASSERT_TRUE(d[0] == 'a');
|
|---|
| 794 | PCUT_ASSERT_TRUE(d[1] == 'b');
|
|---|
| 795 | PCUT_ASSERT_TRUE(d[2] == 'c');
|
|---|
| 796 | PCUT_ASSERT_TRUE(d[3] == '\0');
|
|---|
| 797 | free(d);
|
|---|
| 798 | }
|
|---|
| 799 |
|
|---|
| 800 | /** strndup function with non-empty string of length above limit */
|
|---|
| 801 | PCUT_TEST(strndup_nonempty_long)
|
|---|
| 802 | {
|
|---|
| 803 | char *d = strndup("abc", 2);
|
|---|
| 804 | PCUT_ASSERT_NOT_NULL(d);
|
|---|
| 805 | PCUT_ASSERT_TRUE(d[0] == 'a');
|
|---|
| 806 | PCUT_ASSERT_TRUE(d[1] == 'b');
|
|---|
| 807 | PCUT_ASSERT_TRUE(d[2] == '\0');
|
|---|
| 808 | free(d);
|
|---|
| 809 | }
|
|---|
| 810 |
|
|---|
| [99d3123] | 811 | PCUT_EXPORT(string);
|
|---|