Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | /* libraries.c - test access to the minimal C libraries */ /* * Copyright (c) 2014 Wind River Systems, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* DESCRIPTION This module verifies that the various minimal C libraries can be used. IMPORTANT: The module only ensures that each supported library is present, and that a bare minimum of its functionality is operating correctly. It does NOT guarantee that ALL standards-defined functionality is present, nor does it guarantee that ALL functionality provided is working correctly. */ #include <zephyr.h> #include <tc_util.h> #include <limits.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <string.h> /* * variables used during limits library testing; must be marked as "volatile" * to prevent compiler from computing results at compile time */ volatile long longMax = LONG_MAX; volatile long longOne = 1L; /** * * @brief Test implementation-defined constants library * * @return TC_PASS or TC_FAIL */ int limitsTest(void) { TC_PRINT("Testing limits.h library ...\n"); if (longMax + longOne != LONG_MIN) { return TC_FAIL; } return TC_PASS; } /** * * @brief Test boolean types and values library * * @return TC_PASS or TC_FAIL */ int stdboolTest(void) { TC_PRINT("Testing stdbool.h library ...\n"); if ((true != 1) || (false != 0)) { return TC_FAIL; } return TC_PASS; } /* * variables used during stddef library testing; must be marked as "volatile" * to prevent compiler from computing results at compile time */ volatile long longVariable; volatile size_t sizeOfLongVariable = sizeof(longVariable); /** * * @brief Test standard type definitions library * * @return TC_PASS or TC_FAIL */ int stddefTest(void) { TC_PRINT("Testing stddef.h library ...\n"); if (sizeOfLongVariable != 4) { return TC_FAIL; } return TC_PASS; } /* * variables used during stdint library testing; must be marked as "volatile" * to prevent compiler from computing results at compile time */ volatile uint8_t unsignedByte = 0xff; volatile uint32_t unsignedInt = 0xffffff00; /** * * @brief Test integer types library * * @return TC_PASS or TC_FAIL */ int stdintTest(void) { TC_PRINT("Testing stdint.h library ...\n"); if (unsignedInt + unsignedByte + 1u != 0) { return TC_FAIL; } return TC_PASS; } /* * variables used during string library testing */ #define BUFSIZE 10 char buffer[BUFSIZE]; /** * * @brief Test string memset * * @return TC_PASS or TC_FAIL */ int memset_test(void) { TC_PRINT("\tmemset ...\t"); memset(buffer, 'a', BUFSIZE); if (buffer[0] != 'a' || buffer[BUFSIZE-1] != 'a') { TC_PRINT("failed\n"); return TC_FAIL; } TC_PRINT("passed\n"); return TC_PASS; } /** * * @brief Test string length function * * @return TC_PASS or TC_FAIL */ int strlen_test(void) { TC_PRINT("\tstrlen ...\t"); memset(buffer, '\0', BUFSIZE); memset(buffer, 'b', 5); /* 5 is BUFSIZE / 2 */ if (strlen(buffer) != 5) { TC_PRINT("failed\n"); return TC_FAIL; } TC_PRINT("passed\n"); return TC_PASS; } /** * * @brief Test string compare function * * @return TC_PASS or TC_FAIL */ int strcmp_test(void) { strcpy(buffer, "eeeee"); TC_PRINT("\tstrcmp less ...\t"); if (strcmp(buffer, "fffff") >= 0) { TC_PRINT("failed\n"); return TC_FAIL; } else { TC_PRINT("passed\n"); } TC_PRINT("\tstrcmp equal ...\t"); if (strcmp(buffer, "eeeee") != 0) { TC_PRINT("failed\n"); return TC_FAIL; } else { TC_PRINT("passed\n"); } TC_PRINT("\tstrcmp greater ...\t"); if (strcmp(buffer, "ddddd") <= 0) { TC_PRINT("failed\n"); return TC_FAIL; } else { TC_PRINT("passed\n"); } return TC_PASS; } /** * * @brief Test string N compare function * * @return TC_PASS or TC_FAIL */ int strncmp_test(void) { strncpy(buffer, "eeeeeeeeeeee", BUFSIZE); TC_PRINT("\tstrncmp 0 ...\t"); if (strncmp(buffer, "fffff", 0) != 0) { TC_PRINT("failed\n"); return TC_FAIL; } else { TC_PRINT("passed\n"); } TC_PRINT("\tstrncmp 3 ...\t"); if (strncmp(buffer, "eeeff", 3) != 0) { TC_PRINT("failed\n"); return TC_FAIL; } else { TC_PRINT("passed\n"); } TC_PRINT("\tstrncmp 10 ...\t"); if (strncmp(buffer, "eeeeeeeeeeeff", BUFSIZE) != 0) { TC_PRINT("failed\n"); return TC_FAIL; } else { TC_PRINT("passed\n"); } return TC_PASS; } /** * * @brief Test string copy function * * @return TC_PASS or TC_FAIL */ int strcpy_test(void) { TC_PRINT("\tstrcpy ...\t"); memset(buffer, '\0', BUFSIZE); strcpy(buffer, "10 chars!!\0"); if (strcmp(buffer, "10 chars!!\0") != 0) { TC_PRINT("failed\n"); return TC_FAIL; } TC_PRINT("passed\n"); return TC_PASS; } /** * * @brief Test string N copy function * * @return TC_PASS or TC_FAIL */ int strncpy_test(void) { TC_PRINT("\tstrncpy ...\t"); memset(buffer, '\0', BUFSIZE); strncpy(buffer, "This is over 10 characters", BUFSIZE); /* Purposely different values */ if (strncmp(buffer, "This is over 20 characters", BUFSIZE) != 0) { TC_PRINT("failed\n"); return TC_FAIL; } TC_PRINT("passed\n"); return TC_PASS; } /** * * @brief Test string scanning function * * @return TC_PASS or TC_FAIL */ int strchr_test(void) { char *rs = NULL; TC_PRINT("\tstrchr ...\t"); memset(buffer, '\0', BUFSIZE); strncpy(buffer, "Copy 10", BUFSIZE); rs = strchr(buffer, '1'); if (!rs) { TC_PRINT("failed\n"); return TC_FAIL; } if (strncmp(rs, "10", 2) != 0) { TC_PRINT("failed\n"); return TC_FAIL; } TC_PRINT("passed\n"); return TC_PASS; } /** * * @brief Test memory comparison function * * @return TC_PASS or TC_FAIL */ int memcmp_test(void) { unsigned char m1[5] = { 1, 2, 3, 4, 5 }; unsigned char m2[5] = { 1, 2, 3, 4, 6 }; TC_PRINT("\tmemcmp ...\t"); if (memcmp(m1, m2, 4)) { TC_PRINT("failed\n"); return TC_FAIL; } if (!memcmp(m1, m2, 5)) { TC_PRINT("failed\n"); return TC_FAIL; } TC_PRINT("passed\n"); return TC_PASS; } /** * * @brief Test string operations library * * @return TC_PASS or TC_FAIL */ int stringTest(void) { TC_PRINT("Testing string.h library ...\n"); if (memset_test() || strlen_test() || strcmp_test() || strcpy_test() || strncpy_test() || strncmp_test() || strchr_test() || memcmp_test()) { return TC_FAIL; } return TC_PASS; } /** * * @brief Main task in the test suite * * This is the entry point to the main task used by the standard libraries test * suite. It tests each library in turn until a failure is detected or all * libraries have been tested successfully. * * @return TC_PASS or TC_FAIL */ int RegressionTask(void) { TC_PRINT("Validating access to supported libraries\n"); if (limitsTest() || stdboolTest() || stddefTest() || stdintTest() || stringTest()) { TC_PRINT("Library validation failed\n"); return TC_FAIL; } TC_PRINT("Validation complete\n"); return TC_PASS; } |