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