atol_tc.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <inttypes.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. struct atol_data
  6. {
  7. char string[15]; // long max 2147483647
  8. int ret_num;
  9. };
  10. struct atol_data test_data1[] =
  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 atol_entry(void)
  41. {
  42. int i = 0;
  43. int res = 0;
  44. for (i = 0; i < sizeof(test_data1) / sizeof(test_data1[0]); i++)
  45. {
  46. res = atol(test_data1[i].string);
  47. uassert_int_equal(res, test_data1[i].ret_num);
  48. }
  49. return 0;
  50. }
  51. static void test_atol(void)
  52. {
  53. atol_entry();
  54. }
  55. static void testcase(void)
  56. {
  57. UTEST_UNIT_RUN(test_atol);
  58. }
  59. UTEST_TC_EXPORT(testcase, "posix.stdlib_h.atol_tc.c", RT_NULL, RT_NULL, 10);