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