[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
|
---|
| 34 | * @brief Test formatted input (scanf family)
|
---|
| 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 |
|
---|
| 92 | /** strtol function */
|
---|
| 93 | PCUT_TEST(strtol)
|
---|
| 94 | {
|
---|
| 95 | long li;
|
---|
| 96 | char *ep;
|
---|
| 97 |
|
---|
| 98 | li = strtol(" \t42x", &ep, 10);
|
---|
| 99 | PCUT_ASSERT_TRUE(li == 42);
|
---|
| 100 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /** strtol function with auto-detected base 10 */
|
---|
| 104 | PCUT_TEST(strtol_dec_auto)
|
---|
| 105 | {
|
---|
| 106 | long li;
|
---|
| 107 | char *ep;
|
---|
| 108 |
|
---|
| 109 | li = strtol(" \t42x", &ep, 0);
|
---|
| 110 | PCUT_ASSERT_TRUE(li == 42);
|
---|
| 111 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /** strtol function with octal number */
|
---|
| 115 | PCUT_TEST(strtol_oct)
|
---|
| 116 | {
|
---|
| 117 | long li;
|
---|
| 118 | char *ep;
|
---|
| 119 |
|
---|
| 120 | li = strtol(" \t052x", &ep, 8);
|
---|
| 121 | PCUT_ASSERT_TRUE(li == 052);
|
---|
| 122 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /** strtol function with octal number with prefix */
|
---|
| 126 | #include <stdio.h>
|
---|
| 127 | PCUT_TEST(strtol_oct_prefix)
|
---|
| 128 | {
|
---|
| 129 | long li;
|
---|
| 130 | char *ep;
|
---|
| 131 |
|
---|
| 132 | li = strtol(" \t052x", &ep, 0);
|
---|
| 133 | printf("li=%ld (0%lo)\n", li, li);
|
---|
| 134 | PCUT_ASSERT_TRUE(li == 052);
|
---|
| 135 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /** strtol function with hex number */
|
---|
| 139 | PCUT_TEST(strtol_hex)
|
---|
| 140 | {
|
---|
| 141 | long li;
|
---|
| 142 | char *ep;
|
---|
| 143 |
|
---|
| 144 | li = strtol(" \t2ax", &ep, 16);
|
---|
| 145 | PCUT_ASSERT_TRUE(li == 0x2a);
|
---|
| 146 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /** strtol function with hex number with hex prefix */
|
---|
| 150 | PCUT_TEST(strtol_hex_prefixed)
|
---|
| 151 | {
|
---|
| 152 | long li;
|
---|
| 153 | char *ep;
|
---|
| 154 |
|
---|
| 155 | li = strtol(" \t0x2ax", &ep, 0);
|
---|
| 156 | PCUT_ASSERT_TRUE(li == 0x2a);
|
---|
| 157 | PCUT_ASSERT_TRUE(*ep == 'x');
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /** strtol function with base 16 and number with 0x prefix */
|
---|
| 161 | PCUT_TEST(strtol_base16_prefix)
|
---|
| 162 | {
|
---|
| 163 | long li;
|
---|
| 164 | char *ep;
|
---|
| 165 |
|
---|
| 166 | li = strtol(" \t0x1y", &ep, 16);
|
---|
| 167 | printf("li=%ld\n", li);
|
---|
| 168 | PCUT_ASSERT_TRUE(li == 1);
|
---|
| 169 | PCUT_ASSERT_TRUE(*ep == 'y');
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /** strtol function with base 36 number */
|
---|
| 173 | PCUT_TEST(strtol_base36)
|
---|
| 174 | {
|
---|
| 175 | long li;
|
---|
| 176 | char *ep;
|
---|
| 177 |
|
---|
| 178 | li = strtol(" \tz1.", &ep, 36);
|
---|
| 179 | PCUT_ASSERT_TRUE(li == 35 * 36 + 1);
|
---|
| 180 | PCUT_ASSERT_TRUE(*ep == '.');
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /** rand function */
|
---|
| 184 | PCUT_TEST(rand)
|
---|
| 185 | {
|
---|
| 186 | int i;
|
---|
| 187 | int r;
|
---|
| 188 |
|
---|
| 189 | for (i = 0; i < 100; i++) {
|
---|
| 190 | r = rand();
|
---|
| 191 | PCUT_ASSERT_TRUE(r >= 0);
|
---|
| 192 | PCUT_ASSERT_TRUE(r <= RAND_MAX);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | PCUT_ASSERT_TRUE(RAND_MAX >= 32767);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /** srand function */
|
---|
| 199 | PCUT_TEST(srand)
|
---|
| 200 | {
|
---|
| 201 | int r1;
|
---|
| 202 | int r2;
|
---|
| 203 |
|
---|
| 204 | srand(1);
|
---|
| 205 | r1 = rand();
|
---|
| 206 | srand(1);
|
---|
| 207 | r2 = rand();
|
---|
| 208 |
|
---|
| 209 | PCUT_ASSERT_INT_EQUALS(r2, r1);
|
---|
| 210 |
|
---|
| 211 | srand(42);
|
---|
| 212 | r1 = rand();
|
---|
| 213 | srand(42);
|
---|
| 214 | r2 = rand();
|
---|
| 215 |
|
---|
| 216 | PCUT_ASSERT_INT_EQUALS(r2, r1);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /** Just make sure we have memory allocation function prototypes */
|
---|
| 220 | PCUT_TEST(malloc)
|
---|
| 221 | {
|
---|
| 222 | void *p;
|
---|
| 223 |
|
---|
| 224 | #if 0
|
---|
| 225 | // TODO
|
---|
| 226 | p = aligned_alloc(4, 8);
|
---|
| 227 | PCUT_ASSERT_NOT_NULL(p);
|
---|
| 228 | free(p);
|
---|
| 229 | #endif
|
---|
| 230 | p = calloc(4, 4);
|
---|
| 231 | PCUT_ASSERT_NOT_NULL(p);
|
---|
| 232 | free(p);
|
---|
| 233 |
|
---|
| 234 | p = malloc(4);
|
---|
| 235 | PCUT_ASSERT_NOT_NULL(p);
|
---|
| 236 | p = realloc(p, 2);
|
---|
| 237 | PCUT_ASSERT_NOT_NULL(p);
|
---|
| 238 | free(p);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /** Just check abort() is defined */
|
---|
| 242 | PCUT_TEST(abort)
|
---|
| 243 | {
|
---|
| 244 | if (0)
|
---|
| 245 | abort();
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | static void dummy_exit_handler(void)
|
---|
| 249 | {
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | /** atexit function */
|
---|
| 253 | PCUT_TEST(atexit)
|
---|
| 254 | {
|
---|
| 255 | int rc;
|
---|
| 256 |
|
---|
| 257 | rc = atexit(dummy_exit_handler);
|
---|
| 258 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | /** exit function -- just make sure it is declared */
|
---|
| 262 | PCUT_TEST(exit)
|
---|
| 263 | {
|
---|
| 264 | if (0)
|
---|
| 265 | exit(0);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | /** at_quick_exit function */
|
---|
| 269 | PCUT_TEST(at_quick_exit)
|
---|
| 270 | {
|
---|
| 271 | int rc;
|
---|
| 272 |
|
---|
| 273 | rc = at_quick_exit(dummy_exit_handler);
|
---|
| 274 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | /** quick_exit function -- just make sure it is declared */
|
---|
| 278 | PCUT_TEST(quick_exit)
|
---|
| 279 | {
|
---|
| 280 | if (0)
|
---|
| 281 | quick_exit(0);
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[75c430e3] | 284 | /** getenv function */
|
---|
| 285 | PCUT_TEST(getenv)
|
---|
| 286 | {
|
---|
| 287 | char *s;
|
---|
| 288 |
|
---|
| 289 | s = getenv("FOO");
|
---|
| 290 | PCUT_ASSERT_NULL(s);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | /** Test availability of command processor */
|
---|
| 294 | PCUT_TEST(system_null)
|
---|
| 295 | {
|
---|
| 296 | int rc;
|
---|
| 297 |
|
---|
| 298 | rc = system(NULL);
|
---|
| 299 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | /** Test running a command */
|
---|
| 303 | PCUT_TEST(system_cmd)
|
---|
| 304 | {
|
---|
| 305 | int rc;
|
---|
| 306 |
|
---|
| 307 | /* This should fail as system is just a stub */
|
---|
| 308 | rc = system("/app/bdsh");
|
---|
| 309 | PCUT_ASSERT_INT_EQUALS(1, rc);
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | /** Comparison function for bsearch test */
|
---|
| 313 | static int test_compar(const void *a, const void *b)
|
---|
| 314 | {
|
---|
| 315 | const int *ia, *ib;
|
---|
| 316 |
|
---|
| 317 | ia = (const int *)a;
|
---|
| 318 | ib = (const int *)b;
|
---|
| 319 |
|
---|
| 320 | return *ia - *ib;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | PCUT_TEST(bsearch)
|
---|
| 324 | {
|
---|
| 325 | int numbers[] = { 1, 2, 6, 7, 7, 10, 100, 120 };
|
---|
| 326 | int k;
|
---|
| 327 | void *r;
|
---|
| 328 |
|
---|
| 329 | k = 0;
|
---|
| 330 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 331 | test_compar);
|
---|
| 332 | PCUT_ASSERT_NULL(r);
|
---|
| 333 |
|
---|
| 334 | k = 1;
|
---|
| 335 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 336 | test_compar);
|
---|
| 337 | PCUT_ASSERT_NOT_NULL(r);
|
---|
| 338 | PCUT_ASSERT_INT_EQUALS(1, *(int *)r);
|
---|
| 339 |
|
---|
| 340 | k = 3;
|
---|
| 341 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 342 | test_compar);
|
---|
| 343 | PCUT_ASSERT_NULL(r);
|
---|
| 344 |
|
---|
| 345 | k = 6;
|
---|
| 346 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 347 | test_compar);
|
---|
| 348 | PCUT_ASSERT_NOT_NULL(r);
|
---|
| 349 | PCUT_ASSERT_INT_EQUALS(6, *(int *)r);
|
---|
| 350 |
|
---|
| 351 | k = 7;
|
---|
| 352 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 353 | test_compar);
|
---|
| 354 | PCUT_ASSERT_NOT_NULL(r);
|
---|
| 355 | PCUT_ASSERT_INT_EQUALS(7, *(int *)r);
|
---|
| 356 |
|
---|
| 357 | k = 200;
|
---|
| 358 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
---|
| 359 | test_compar);
|
---|
| 360 | PCUT_ASSERT_NULL(r);
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[379db9ef] | 363 | /** Integer division */
|
---|
| 364 | PCUT_TEST(div_func)
|
---|
| 365 | {
|
---|
| 366 | div_t d;
|
---|
| 367 |
|
---|
| 368 | d = div(41, 7);
|
---|
| 369 | PCUT_ASSERT_INT_EQUALS(5, d.quot);
|
---|
| 370 | PCUT_ASSERT_INT_EQUALS(6, d.rem);
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | /** Long integer division */
|
---|
| 374 | PCUT_TEST(ldiv_func)
|
---|
| 375 | {
|
---|
| 376 | ldiv_t d;
|
---|
| 377 |
|
---|
| 378 | d = ldiv(41, 7);
|
---|
| 379 | PCUT_ASSERT_INT_EQUALS(5, d.quot);
|
---|
| 380 | PCUT_ASSERT_INT_EQUALS(6, d.rem);
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | /** Long long integer division */
|
---|
| 384 | PCUT_TEST(lldiv_func)
|
---|
| 385 | {
|
---|
| 386 | lldiv_t d;
|
---|
| 387 |
|
---|
| 388 | d = lldiv(41, 7);
|
---|
| 389 | PCUT_ASSERT_INT_EQUALS(5, d.quot);
|
---|
| 390 | PCUT_ASSERT_INT_EQUALS(6, d.rem);
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | PCUT_EXPORT(stdlib);
|
---|