[379db9ef] | 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 | /** @addtogroup libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[9912f49] | 34 | * @brief Test General utilities (stdlib.h)
|
---|
[379db9ef] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <pcut/pcut.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 |
|
---|
| 40 | PCUT_INIT;
|
---|
| 41 |
|
---|
| 42 | PCUT_TEST_SUITE(stdlib);
|
---|
| 43 |
|
---|
| 44 | PCUT_TEST(decls)
|
---|
| 45 | {
|
---|
| 46 | /* Make sure size_t is defined */
|
---|
| 47 | size_t sz = 0;
|
---|
| 48 | (void) sz;
|
---|
| 49 |
|
---|
| 50 | /* Make sure wchar_t is defined */
|
---|
| 51 | wchar_t wc = L'\0';
|
---|
| 52 | (void) wc;
|
---|
| 53 |
|
---|
| 54 | /* Make sure EXIT_FAILURE and EXIT_SUCCESS are defined */
|
---|
| 55 | if (0)
|
---|
| 56 | exit(EXIT_FAILURE);
|
---|
| 57 | if (0)
|
---|
| 58 | exit(EXIT_SUCCESS);
|
---|
| 59 |
|
---|
| 60 | /* Make sure NULL is defined */
|
---|
| 61 | void *ptr = NULL;
|
---|
| 62 | (void) ptr;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[099c834] | 65 | /** atoi function */
|
---|
| 66 | PCUT_TEST(atoi)
|
---|
| 67 | {
|
---|
| 68 | int i;
|
---|
| 69 |
|
---|
| 70 | i = atoi(" \t42");
|
---|
| 71 | PCUT_ASSERT_TRUE(i == 42);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /** atol function */
|
---|
| 75 | PCUT_TEST(atol)
|
---|
| 76 | {
|
---|
| 77 | long li;
|
---|
| 78 |
|
---|
| 79 | li = atol(" \t42");
|
---|
| 80 | PCUT_ASSERT_TRUE(li == 42);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** atoll function */
|
---|
| 84 | PCUT_TEST(atoll)
|
---|
| 85 | {
|
---|
| 86 | long long lli;
|
---|
| 87 |
|
---|
| 88 | lli = atoll(" \t42");
|
---|
| 89 | PCUT_ASSERT_TRUE(lli == 42);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[9d8307a] | 92 | /** strtold function */
|
---|
| 93 | #include <stdio.h>
|
---|
| 94 | PCUT_TEST(strtold)
|
---|
| 95 | {
|
---|
| 96 | long double ld;
|
---|
| 97 | const char *str = " \t4.2e1@";
|
---|
| 98 | char *endptr;
|
---|
| 99 |
|
---|
| 100 | ld = strtold(str, &endptr);
|
---|
| 101 | printf("ld=%.10lf\n", (double)ld);
|
---|
| 102 | PCUT_ASSERT_TRUE(ld == 42.0);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[099c834] | 105 | /** strtol function */
|
---|
| 106 | PCUT_TEST(strtol)
|
---|
| 107 | {
|
---|
| 108 | long li;
|
---|
| 109 | char *ep;
|
---|
| 110 |
|
---|
| 111 | li = strtol(" \t42x", &ep, 10);
|
---|
| 112 | PCUT_ASSERT_TRUE(li == 42);
|
---|
| 113 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /** strtol function with auto-detected base 10 */
|
---|
| 117 | PCUT_TEST(strtol_dec_auto)
|
---|
| 118 | {
|
---|
| 119 | long li;
|
---|
| 120 | char *ep;
|
---|
| 121 |
|
---|
| 122 | li = strtol(" \t42x", &ep, 0);
|
---|
| 123 | PCUT_ASSERT_TRUE(li == 42);
|
---|
| 124 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /** strtol function with octal number */
|
---|
| 128 | PCUT_TEST(strtol_oct)
|
---|
| 129 | {
|
---|
| 130 | long li;
|
---|
| 131 | char *ep;
|
---|
| 132 |
|
---|
| 133 | li = strtol(" \t052x", &ep, 8);
|
---|
| 134 | PCUT_ASSERT_TRUE(li == 052);
|
---|
| 135 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /** strtol function with octal number with prefix */
|
---|
| 139 | #include <stdio.h>
|
---|
| 140 | PCUT_TEST(strtol_oct_prefix)
|
---|
| 141 | {
|
---|
| 142 | long li;
|
---|
| 143 | char *ep;
|
---|
| 144 |
|
---|
| 145 | li = strtol(" \t052x", &ep, 0);
|
---|
| 146 | printf("li=%ld (0%lo)\n", li, li);
|
---|
| 147 | PCUT_ASSERT_TRUE(li == 052);
|
---|
| 148 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /** strtol function with hex number */
|
---|
| 152 | PCUT_TEST(strtol_hex)
|
---|
| 153 | {
|
---|
| 154 | long li;
|
---|
| 155 | char *ep;
|
---|
| 156 |
|
---|
| 157 | li = strtol(" \t2ax", &ep, 16);
|
---|
| 158 | PCUT_ASSERT_TRUE(li == 0x2a);
|
---|
| 159 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /** strtol function with hex number with hex prefix */
|
---|
| 163 | PCUT_TEST(strtol_hex_prefixed)
|
---|
| 164 | {
|
---|
| 165 | long li;
|
---|
| 166 | char *ep;
|
---|
| 167 |
|
---|
| 168 | li = strtol(" \t0x2ax", &ep, 0);
|
---|
| 169 | PCUT_ASSERT_TRUE(li == 0x2a);
|
---|
| 170 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /** strtol function with base 16 and number with 0x prefix */
|
---|
| 174 | PCUT_TEST(strtol_base16_prefix)
|
---|
| 175 | {
|
---|
| 176 | long li;
|
---|
| 177 | char *ep;
|
---|
| 178 |
|
---|
| 179 | li = strtol(" \t0x1y", &ep, 16);
|
---|
| 180 | printf("li=%ld\n", li);
|
---|
| 181 | PCUT_ASSERT_TRUE(li == 1);
|
---|
| 182 | PCUT_ASSERT_TRUE(*ep == 'y');
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /** strtol function with base 36 number */
|
---|
| 186 | PCUT_TEST(strtol_base36)
|
---|
| 187 | {
|
---|
| 188 | long li;
|
---|
| 189 | char *ep;
|
---|
| 190 |
|
---|
| 191 | li = strtol(" \tz1.", &ep, 36);
|
---|
| 192 | PCUT_ASSERT_TRUE(li == 35 * 36 + 1);
|
---|
| 193 | PCUT_ASSERT_TRUE(*ep == '.');
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /** rand function */
|
---|
| 197 | PCUT_TEST(rand)
|
---|
| 198 | {
|
---|
| 199 | int i;
|
---|
| 200 | int r;
|
---|
| 201 |
|
---|
| 202 | for (i = 0; i < 100; i++) {
|
---|
| 203 | r = rand();
|
---|
| 204 | PCUT_ASSERT_TRUE(r >= 0);
|
---|
| 205 | PCUT_ASSERT_TRUE(r <= RAND_MAX);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | PCUT_ASSERT_TRUE(RAND_MAX >= 32767);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /** srand function */
|
---|
| 212 | PCUT_TEST(srand)
|
---|
| 213 | {
|
---|
| 214 | int r1;
|
---|
| 215 | int r2;
|
---|
| 216 |
|
---|
| 217 | srand(1);
|
---|
| 218 | r1 = rand();
|
---|
| 219 | srand(1);
|
---|
| 220 | r2 = rand();
|
---|
| 221 |
|
---|
| 222 | PCUT_ASSERT_INT_EQUALS(r2, r1);
|
---|
| 223 |
|
---|
| 224 | srand(42);
|
---|
| 225 | r1 = rand();
|
---|
| 226 | srand(42);
|
---|
| 227 | r2 = rand();
|
---|
| 228 |
|
---|
| 229 | PCUT_ASSERT_INT_EQUALS(r2, r1);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /** Just make sure we have memory allocation function prototypes */
|
---|
| 233 | PCUT_TEST(malloc)
|
---|
| 234 | {
|
---|
| 235 | void *p;
|
---|
| 236 |
|
---|
| 237 | #if 0
|
---|
| 238 | // TODO
|
---|
| 239 | p = aligned_alloc(4, 8);
|
---|
| 240 | PCUT_ASSERT_NOT_NULL(p);
|
---|
| 241 | free(p);
|
---|
| 242 | #endif
|
---|
| 243 | p = calloc(4, 4);
|
---|
| 244 | PCUT_ASSERT_NOT_NULL(p);
|
---|
| 245 | free(p);
|
---|
| 246 |
|
---|
| 247 | p = malloc(4);
|
---|
| 248 | PCUT_ASSERT_NOT_NULL(p);
|
---|
| 249 | p = realloc(p, 2);
|
---|
| 250 | PCUT_ASSERT_NOT_NULL(p);
|
---|
| 251 | free(p);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | /** Just check abort() is defined */
|
---|
| 255 | PCUT_TEST(abort)
|
---|
| 256 | {
|
---|
| 257 | if (0)
|
---|
| 258 | abort();
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | static void dummy_exit_handler(void)
|
---|
| 262 | {
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /** atexit function */
|
---|
| 266 | PCUT_TEST(atexit)
|
---|
| 267 | {
|
---|
| 268 | int rc;
|
---|
| 269 |
|
---|
| 270 | rc = atexit(dummy_exit_handler);
|
---|
| 271 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | /** exit function -- just make sure it is declared */
|
---|
| 275 | PCUT_TEST(exit)
|
---|
| 276 | {
|
---|
| 277 | if (0)
|
---|
| 278 | exit(0);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | /** at_quick_exit function */
|
---|
| 282 | PCUT_TEST(at_quick_exit)
|
---|
| 283 | {
|
---|
| 284 | int rc;
|
---|
| 285 |
|
---|
| 286 | rc = at_quick_exit(dummy_exit_handler);
|
---|
| 287 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /** quick_exit function -- just make sure it is declared */
|
---|
| 291 | PCUT_TEST(quick_exit)
|
---|
| 292 | {
|
---|
| 293 | if (0)
|
---|
| 294 | quick_exit(0);
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[75c430e3] | 297 | /** getenv function */
|
---|
| 298 | PCUT_TEST(getenv)
|
---|
| 299 | {
|
---|
| 300 | char *s;
|
---|
| 301 |
|
---|
| 302 | s = getenv("FOO");
|
---|
| 303 | PCUT_ASSERT_NULL(s);
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | /** Test availability of command processor */
|
---|
| 307 | PCUT_TEST(system_null)
|
---|
| 308 | {
|
---|
| 309 | int rc;
|
---|
| 310 |
|
---|
| 311 | rc = system(NULL);
|
---|
| 312 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | /** Test running a command */
|
---|
| 316 | PCUT_TEST(system_cmd)
|
---|
| 317 | {
|
---|
| 318 | int rc;
|
---|
| 319 |
|
---|
| 320 | /* This should fail as system is just a stub */
|
---|
| 321 | rc = system("/app/bdsh");
|
---|
| 322 | PCUT_ASSERT_INT_EQUALS(1, rc);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | /** Comparison function for bsearch test */
|
---|
| 326 | static int test_compar(const void *a, const void *b)
|
---|
| 327 | {
|
---|
| 328 | const int *ia, *ib;
|
---|
| 329 |
|
---|
| 330 | ia = (const int *)a;
|
---|
| 331 | ib = (const int *)b;
|
---|
| 332 |
|
---|
| 333 | return *ia - *ib;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | PCUT_TEST(bsearch)
|
---|
| 337 | {
|
---|
| 338 | int numbers[] = { 1, 2, 6, 7, 7, 10, 100, 120 };
|
---|
| 339 | int k;
|
---|
| 340 | void *r;
|
---|
| 341 |
|
---|
| 342 | k = 0;
|
---|
| 343 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 344 | test_compar);
|
---|
| 345 | PCUT_ASSERT_NULL(r);
|
---|
| 346 |
|
---|
| 347 | k = 1;
|
---|
| 348 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 349 | test_compar);
|
---|
| 350 | PCUT_ASSERT_NOT_NULL(r);
|
---|
| 351 | PCUT_ASSERT_INT_EQUALS(1, *(int *)r);
|
---|
| 352 |
|
---|
| 353 | k = 3;
|
---|
| 354 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 355 | test_compar);
|
---|
| 356 | PCUT_ASSERT_NULL(r);
|
---|
| 357 |
|
---|
| 358 | k = 6;
|
---|
| 359 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 360 | test_compar);
|
---|
| 361 | PCUT_ASSERT_NOT_NULL(r);
|
---|
| 362 | PCUT_ASSERT_INT_EQUALS(6, *(int *)r);
|
---|
| 363 |
|
---|
| 364 | k = 7;
|
---|
| 365 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 366 | test_compar);
|
---|
| 367 | PCUT_ASSERT_NOT_NULL(r);
|
---|
| 368 | PCUT_ASSERT_INT_EQUALS(7, *(int *)r);
|
---|
| 369 |
|
---|
| 370 | k = 200;
|
---|
| 371 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 372 | test_compar);
|
---|
| 373 | PCUT_ASSERT_NULL(r);
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[7d7bc09] | 376 | /** abs function of positive number */
|
---|
| 377 | PCUT_TEST(abs_pos)
|
---|
| 378 | {
|
---|
| 379 | int i;
|
---|
| 380 |
|
---|
| 381 | i = abs(1);
|
---|
| 382 | PCUT_ASSERT_TRUE(i == 1);
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | /** abs function of negative number */
|
---|
| 386 | PCUT_TEST(abs_neg)
|
---|
| 387 | {
|
---|
| 388 | int i;
|
---|
| 389 |
|
---|
| 390 | i = abs(-1);
|
---|
| 391 | PCUT_ASSERT_TRUE(i == 1);
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | /** labs function of positive number */
|
---|
| 395 | PCUT_TEST(labs_pos)
|
---|
| 396 | {
|
---|
| 397 | long li;
|
---|
| 398 |
|
---|
| 399 | li = labs(1);
|
---|
| 400 | PCUT_ASSERT_TRUE(li == 1);
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | /** labs function of negative number */
|
---|
| 404 | PCUT_TEST(labs_neg)
|
---|
| 405 | {
|
---|
| 406 | long li;
|
---|
| 407 |
|
---|
| 408 | li = labs(-1);
|
---|
| 409 | PCUT_ASSERT_TRUE(li == 1);
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | /** llabs function of positive number */
|
---|
| 413 | PCUT_TEST(llabs_pos)
|
---|
| 414 | {
|
---|
| 415 | long long lli;
|
---|
| 416 |
|
---|
| 417 | lli = llabs(1);
|
---|
| 418 | PCUT_ASSERT_TRUE(lli == 1);
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | /** llabs function of negative number */
|
---|
| 422 | PCUT_TEST(llabs_neg)
|
---|
| 423 | {
|
---|
| 424 | long long lli;
|
---|
| 425 |
|
---|
| 426 | lli = llabs(-1);
|
---|
| 427 | PCUT_ASSERT_TRUE(lli == 1);
|
---|
| 428 | }
|
---|
| 429 |
|
---|
[379db9ef] | 430 | /** Integer division */
|
---|
| 431 | PCUT_TEST(div_func)
|
---|
| 432 | {
|
---|
| 433 | div_t d;
|
---|
| 434 |
|
---|
| 435 | d = div(41, 7);
|
---|
| 436 | PCUT_ASSERT_INT_EQUALS(5, d.quot);
|
---|
| 437 | PCUT_ASSERT_INT_EQUALS(6, d.rem);
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | /** Long integer division */
|
---|
| 441 | PCUT_TEST(ldiv_func)
|
---|
| 442 | {
|
---|
| 443 | ldiv_t d;
|
---|
| 444 |
|
---|
| 445 | d = ldiv(41, 7);
|
---|
| 446 | PCUT_ASSERT_INT_EQUALS(5, d.quot);
|
---|
| 447 | PCUT_ASSERT_INT_EQUALS(6, d.rem);
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | /** Long long integer division */
|
---|
| 451 | PCUT_TEST(lldiv_func)
|
---|
| 452 | {
|
---|
| 453 | lldiv_t d;
|
---|
| 454 |
|
---|
| 455 | d = lldiv(41, 7);
|
---|
| 456 | PCUT_ASSERT_INT_EQUALS(5, d.quot);
|
---|
| 457 | PCUT_ASSERT_INT_EQUALS(6, d.rem);
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | PCUT_EXPORT(stdlib);
|
---|