| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jiri Svoboda
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /** @addtogroup libc
|
|---|
| 8 | * @{
|
|---|
| 9 | */
|
|---|
| 10 | /**
|
|---|
| 11 | * @file
|
|---|
| 12 | * @brief Test General utilities (stdlib.h)
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | #include <pcut/pcut.h>
|
|---|
| 16 | #include <stdlib.h>
|
|---|
| 17 |
|
|---|
| 18 | PCUT_INIT;
|
|---|
| 19 |
|
|---|
| 20 | PCUT_TEST_SUITE(stdlib);
|
|---|
| 21 |
|
|---|
| 22 | PCUT_TEST(decls)
|
|---|
| 23 | {
|
|---|
| 24 | /* Make sure size_t is defined */
|
|---|
| 25 | size_t sz = 0;
|
|---|
| 26 | (void) sz;
|
|---|
| 27 |
|
|---|
| 28 | /* Make sure char32_t is defined */
|
|---|
| 29 | char32_t wc = L'\0';
|
|---|
| 30 | (void) wc;
|
|---|
| 31 |
|
|---|
| 32 | /* Make sure EXIT_FAILURE and EXIT_SUCCESS are defined */
|
|---|
| 33 | if (0)
|
|---|
| 34 | exit(EXIT_FAILURE);
|
|---|
| 35 | if (0)
|
|---|
| 36 | exit(EXIT_SUCCESS);
|
|---|
| 37 |
|
|---|
| 38 | /* Make sure NULL is defined */
|
|---|
| 39 | void *ptr = NULL;
|
|---|
| 40 | (void) ptr;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | /** strtold function */
|
|---|
| 44 | #include <stdio.h>
|
|---|
| 45 | PCUT_TEST(strtold)
|
|---|
| 46 | {
|
|---|
| 47 | long double ld;
|
|---|
| 48 | const char *str = " \t4.2e1@";
|
|---|
| 49 | char *endptr;
|
|---|
| 50 |
|
|---|
| 51 | ld = strtold(str, &endptr);
|
|---|
| 52 | printf("ld=%.10lf\n", (double)ld);
|
|---|
| 53 | PCUT_ASSERT_TRUE(ld == 42.0);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /** rand function */
|
|---|
| 57 | PCUT_TEST(rand)
|
|---|
| 58 | {
|
|---|
| 59 | int i;
|
|---|
| 60 | int r;
|
|---|
| 61 |
|
|---|
| 62 | for (i = 0; i < 100; i++) {
|
|---|
| 63 | r = rand();
|
|---|
| 64 | PCUT_ASSERT_TRUE(r >= 0);
|
|---|
| 65 | PCUT_ASSERT_TRUE(r <= RAND_MAX);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | PCUT_ASSERT_TRUE(RAND_MAX >= 32767);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /** srand function */
|
|---|
| 72 | PCUT_TEST(srand)
|
|---|
| 73 | {
|
|---|
| 74 | int r1;
|
|---|
| 75 | int r2;
|
|---|
| 76 |
|
|---|
| 77 | srand(1);
|
|---|
| 78 | r1 = rand();
|
|---|
| 79 | srand(1);
|
|---|
| 80 | r2 = rand();
|
|---|
| 81 |
|
|---|
| 82 | PCUT_ASSERT_INT_EQUALS(r2, r1);
|
|---|
| 83 |
|
|---|
| 84 | srand(42);
|
|---|
| 85 | r1 = rand();
|
|---|
| 86 | srand(42);
|
|---|
| 87 | r2 = rand();
|
|---|
| 88 |
|
|---|
| 89 | PCUT_ASSERT_INT_EQUALS(r2, r1);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /** Just make sure we have memory allocation function prototypes */
|
|---|
| 93 | PCUT_TEST(malloc)
|
|---|
| 94 | {
|
|---|
| 95 | void *p;
|
|---|
| 96 |
|
|---|
| 97 | #if 0
|
|---|
| 98 | // TODO
|
|---|
| 99 | p = aligned_alloc(4, 8);
|
|---|
| 100 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 101 | free(p);
|
|---|
| 102 | #endif
|
|---|
| 103 | p = calloc(4, 4);
|
|---|
| 104 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 105 | free(p);
|
|---|
| 106 |
|
|---|
| 107 | p = malloc(4);
|
|---|
| 108 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 109 | p = realloc(p, 2);
|
|---|
| 110 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 111 | free(p);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /** Just check abort() is defined */
|
|---|
| 115 | PCUT_TEST(abort)
|
|---|
| 116 | {
|
|---|
| 117 | if (0)
|
|---|
| 118 | abort();
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | static void dummy_exit_handler(void)
|
|---|
| 122 | {
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /** atexit function */
|
|---|
| 126 | PCUT_TEST(atexit)
|
|---|
| 127 | {
|
|---|
| 128 | int rc;
|
|---|
| 129 |
|
|---|
| 130 | rc = atexit(dummy_exit_handler);
|
|---|
| 131 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /** exit function -- just make sure it is declared */
|
|---|
| 135 | PCUT_TEST(exit)
|
|---|
| 136 | {
|
|---|
| 137 | if (0)
|
|---|
| 138 | exit(0);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /** at_quick_exit function */
|
|---|
| 142 | PCUT_TEST(at_quick_exit)
|
|---|
| 143 | {
|
|---|
| 144 | int rc;
|
|---|
| 145 |
|
|---|
| 146 | rc = at_quick_exit(dummy_exit_handler);
|
|---|
| 147 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /** quick_exit function -- just make sure it is declared */
|
|---|
| 151 | PCUT_TEST(quick_exit)
|
|---|
| 152 | {
|
|---|
| 153 | if (0)
|
|---|
| 154 | quick_exit(0);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /** getenv function */
|
|---|
| 158 | PCUT_TEST(getenv)
|
|---|
| 159 | {
|
|---|
| 160 | char *s;
|
|---|
| 161 |
|
|---|
| 162 | s = getenv("FOO");
|
|---|
| 163 | PCUT_ASSERT_NULL(s);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /** Test availability of command processor */
|
|---|
| 167 | PCUT_TEST(system_null)
|
|---|
| 168 | {
|
|---|
| 169 | int rc;
|
|---|
| 170 |
|
|---|
| 171 | rc = system(NULL);
|
|---|
| 172 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /** Test running a command */
|
|---|
| 176 | PCUT_TEST(system_cmd)
|
|---|
| 177 | {
|
|---|
| 178 | int rc;
|
|---|
| 179 |
|
|---|
| 180 | /* This should fail as system is just a stub */
|
|---|
| 181 | rc = system("/app/bdsh");
|
|---|
| 182 | PCUT_ASSERT_INT_EQUALS(1, rc);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /** Comparison function for bsearch test */
|
|---|
| 186 | static int test_compar(const void *a, const void *b)
|
|---|
| 187 | {
|
|---|
| 188 | const int *ia, *ib;
|
|---|
| 189 |
|
|---|
| 190 | ia = (const int *)a;
|
|---|
| 191 | ib = (const int *)b;
|
|---|
| 192 |
|
|---|
| 193 | return *ia - *ib;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | PCUT_TEST(bsearch)
|
|---|
| 197 | {
|
|---|
| 198 | int numbers[] = { 1, 2, 6, 7, 7, 10, 100, 120 };
|
|---|
| 199 | int k;
|
|---|
| 200 | void *r;
|
|---|
| 201 |
|
|---|
| 202 | k = 0;
|
|---|
| 203 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
|---|
| 204 | test_compar);
|
|---|
| 205 | PCUT_ASSERT_NULL(r);
|
|---|
| 206 |
|
|---|
| 207 | k = 1;
|
|---|
| 208 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
|---|
| 209 | test_compar);
|
|---|
| 210 | PCUT_ASSERT_NOT_NULL(r);
|
|---|
| 211 | PCUT_ASSERT_INT_EQUALS(1, *(int *)r);
|
|---|
| 212 |
|
|---|
| 213 | k = 3;
|
|---|
| 214 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
|---|
| 215 | test_compar);
|
|---|
| 216 | PCUT_ASSERT_NULL(r);
|
|---|
| 217 |
|
|---|
| 218 | k = 6;
|
|---|
| 219 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
|---|
| 220 | test_compar);
|
|---|
| 221 | PCUT_ASSERT_NOT_NULL(r);
|
|---|
| 222 | PCUT_ASSERT_INT_EQUALS(6, *(int *)r);
|
|---|
| 223 |
|
|---|
| 224 | k = 7;
|
|---|
| 225 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
|---|
| 226 | test_compar);
|
|---|
| 227 | PCUT_ASSERT_NOT_NULL(r);
|
|---|
| 228 | PCUT_ASSERT_INT_EQUALS(7, *(int *)r);
|
|---|
| 229 |
|
|---|
| 230 | k = 200;
|
|---|
| 231 | r = bsearch(&k, numbers, sizeof(numbers) / sizeof(int), sizeof(int),
|
|---|
| 232 | test_compar);
|
|---|
| 233 | PCUT_ASSERT_NULL(r);
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /** abs function of positive number */
|
|---|
| 237 | PCUT_TEST(abs_pos)
|
|---|
| 238 | {
|
|---|
| 239 | int i;
|
|---|
| 240 |
|
|---|
| 241 | i = abs(1);
|
|---|
| 242 | PCUT_ASSERT_TRUE(i == 1);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /** abs function of negative number */
|
|---|
| 246 | PCUT_TEST(abs_neg)
|
|---|
| 247 | {
|
|---|
| 248 | int i;
|
|---|
| 249 |
|
|---|
| 250 | i = abs(-1);
|
|---|
| 251 | PCUT_ASSERT_TRUE(i == 1);
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /** labs function of positive number */
|
|---|
| 255 | PCUT_TEST(labs_pos)
|
|---|
| 256 | {
|
|---|
| 257 | long li;
|
|---|
| 258 |
|
|---|
| 259 | li = labs(1);
|
|---|
| 260 | PCUT_ASSERT_TRUE(li == 1);
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /** labs function of negative number */
|
|---|
| 264 | PCUT_TEST(labs_neg)
|
|---|
| 265 | {
|
|---|
| 266 | long li;
|
|---|
| 267 |
|
|---|
| 268 | li = labs(-1);
|
|---|
| 269 | PCUT_ASSERT_TRUE(li == 1);
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /** llabs function of positive number */
|
|---|
| 273 | PCUT_TEST(llabs_pos)
|
|---|
| 274 | {
|
|---|
| 275 | long long lli;
|
|---|
| 276 |
|
|---|
| 277 | lli = llabs(1);
|
|---|
| 278 | PCUT_ASSERT_TRUE(lli == 1);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | /** llabs function of negative number */
|
|---|
| 282 | PCUT_TEST(llabs_neg)
|
|---|
| 283 | {
|
|---|
| 284 | long long lli;
|
|---|
| 285 |
|
|---|
| 286 | lli = llabs(-1);
|
|---|
| 287 | PCUT_ASSERT_TRUE(lli == 1);
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | /** Integer division */
|
|---|
| 291 | PCUT_TEST(div_func)
|
|---|
| 292 | {
|
|---|
| 293 | div_t d;
|
|---|
| 294 |
|
|---|
| 295 | d = div(41, 7);
|
|---|
| 296 | PCUT_ASSERT_INT_EQUALS(5, d.quot);
|
|---|
| 297 | PCUT_ASSERT_INT_EQUALS(6, d.rem);
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | /** Long integer division */
|
|---|
| 301 | PCUT_TEST(ldiv_func)
|
|---|
| 302 | {
|
|---|
| 303 | ldiv_t d;
|
|---|
| 304 |
|
|---|
| 305 | d = ldiv(41, 7);
|
|---|
| 306 | PCUT_ASSERT_INT_EQUALS(5, d.quot);
|
|---|
| 307 | PCUT_ASSERT_INT_EQUALS(6, d.rem);
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | /** Long long integer division */
|
|---|
| 311 | PCUT_TEST(lldiv_func)
|
|---|
| 312 | {
|
|---|
| 313 | lldiv_t d;
|
|---|
| 314 |
|
|---|
| 315 | d = lldiv(41, 7);
|
|---|
| 316 | PCUT_ASSERT_INT_EQUALS(5, d.quot);
|
|---|
| 317 | PCUT_ASSERT_INT_EQUALS(6, d.rem);
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | PCUT_EXPORT(stdlib);
|
|---|