atoi_tc.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <inttypes.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. struct atoi_data
  6. {
  7. char string[15]; // int max 2147483647
  8. int ret_num;
  9. };
  10. struct atoi_data test_data[] =
  11. {
  12. /* positive integer */
  13. {"0", 0},
  14. {"1", 1},
  15. {"1.123", 1},
  16. {"123", 123},
  17. {"98993489", 98993489},
  18. {"98993489.12", 98993489},
  19. {"2147483647", 2147483647},
  20. /* negtive integer */
  21. {"-1", -1},
  22. {"-1.123", -1},
  23. {"-123", -123},
  24. {"-98993489", -98993489},
  25. {"-98993489.12", -98993489},
  26. {"-2147483647", -2147483647},
  27. /* letters and numbers */
  28. {"12a45", 12},
  29. {"-12a45", -12},
  30. {"12/45", 12},
  31. {"-12/45", -12},
  32. /* cannot be resolved */
  33. {"", 0},
  34. {" ", 0},
  35. {"abc12", 0},
  36. {" abc12", 0},
  37. /* {NULL, -1} compiler warning */
  38. };
  39. #include <utest.h>
  40. int atoi_entry(void)
  41. {
  42. int i = 0;
  43. int res = 0;
  44. for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++)
  45. {
  46. res = atoi(test_data[i].string);
  47. uassert_int_equal(res, test_data[i].ret_num);
  48. }
  49. return 0;
  50. }
  51. static void test_atoi(void)
  52. {
  53. atoi_entry();
  54. }
  55. static void testcase(void)
  56. {
  57. UTEST_UNIT_RUN(test_atoi);
  58. }
  59. UTEST_TC_EXPORT(testcase, "posix.stdlib_h.atoi_tc.c", RT_NULL, RT_NULL, 10);