strtol_tc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <inttypes.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #define TEST(r, f, x, m) uassert_int_equal(f, x)
  6. #define TEST2(r, f, x, m) uassert_int_equal(f, x)
  7. #define TEST3(r, f, x, m) uassert_int_not_equal(f, x)
  8. #include <utest.h>
  9. #define ERANGE 34
  10. #define EINVAL 22
  11. int strtol_entry(void)
  12. {
  13. __attribute__((unused)) int i;
  14. __attribute__((unused)) long l;
  15. __attribute__((unused)) unsigned long ul;
  16. __attribute__((unused)) long long ll;
  17. __attribute__((unused)) unsigned long long ull;
  18. __attribute__((unused)) char *msg = "";
  19. __attribute__((unused)) char *s, *c;
  20. TEST(l, atol("2147483647"), 2147483647L, "max 32bit signed %ld != %ld");
  21. TEST(l, strtol("2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld");
  22. TEST(ul, strtoul("4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu");
  23. if (sizeof(long) == 4)
  24. {
  25. TEST(l, strtol(s = "2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld");
  26. TEST2(i, c - s, 10, "wrong final position %d != %d");
  27. TEST(l, strtol(s = "-2147483649", &c, 0), -2147483647L - 1, "uncaught overflow %ld != %ld");
  28. TEST2(i, c - s, 11, "wrong final position %d != %d");
  29. TEST(ul, strtoul(s = "4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
  30. TEST2(i, c - s, 10, "wrong final position %d != %d");
  31. TEST(ul, strtoul(s = "-1", &c, 0), -1UL, "rejected negative %lu != %lu");
  32. TEST2(i, c - s, 2, "wrong final position %d != %d");
  33. TEST(ul, strtoul(s = "-2", &c, 0), -2UL, "rejected negative %lu != %lu");
  34. TEST2(i, c - s, 2, "wrong final position %d != %d");
  35. TEST(ul, strtoul(s = "-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu");
  36. TEST2(i, c - s, 11, "wrong final position %d != %d");
  37. TEST(ul, strtoul(s = "-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu");
  38. TEST2(i, c - s, 11, "wrong final position %d != %d");
  39. TEST(ul, strtoul(s = "-4294967296", &c, 0), 4294967295UL, "uncaught negative overflow %lu != %lu");
  40. TEST2(i, c - s, 11, "wrong final position %d != %d");
  41. }
  42. else if (sizeof(long) == 8)
  43. {
  44. TEST(l, strtol(s = "9223372036854775808", &c, 0), 9223372036854775807L, "uncaught overflow %ld != %ld");
  45. TEST2(i, c - s, 19, "wrong final position %d != %d");
  46. TEST(l, strtol(s = "-9223372036854775809", &c, 0), -9223372036854775807L - 1, "uncaught overflow %ld != %ld");
  47. TEST2(i, c - s, 20, "wrong final position %d != %d");
  48. TEST(ul, strtoul(s = "18446744073709551616", &c, 0), 18446744073709551615UL, "uncaught overflow %lu != %lu");
  49. TEST2(i, c - s, 20, "wrong final position %d != %d");
  50. TEST(ul, strtoul(s = "-1", &c, 0), -1UL, "rejected negative %lu != %lu");
  51. TEST2(i, c - s, 2, "wrong final position %d != %d");
  52. TEST(ul, strtoul(s = "-2", &c, 0), -2UL, "rejected negative %lu != %lu");
  53. TEST2(i, c - s, 2, "wrong final position %d != %d");
  54. TEST(ul, strtoul(s = "-9223372036854775808", &c, 0), -9223372036854775808UL, "rejected negative %lu != %lu");
  55. TEST2(i, c - s, 20, "wrong final position %d != %d");
  56. TEST(ul, strtoul(s = "-9223372036854775809", &c, 0), -9223372036854775809UL, "rejected negative %lu != %lu");
  57. TEST2(i, c - s, 20, "wrong final position %d != %d");
  58. TEST(ul, strtoul(s = "-18446744073709551616", &c, 0), 18446744073709551615UL, "uncaught negative overflow %lu != %lu");
  59. TEST2(i, c - s, 21, "wrong final position %d != %d");
  60. }
  61. else
  62. {
  63. printf("sizeof(long) == %d, not implemented\n", (int)sizeof(long));
  64. }
  65. if (sizeof(long long) == 8)
  66. {
  67. TEST(ll, strtoll(s = "9223372036854775808", &c, 0), 9223372036854775807LL, "uncaught overflow %lld != %lld");
  68. TEST2(i, c - s, 19, "wrong final position %d != %d");
  69. TEST(ll, strtoll(s = "-9223372036854775809", &c, 0), -9223372036854775807LL - 1, "uncaught overflow %lld != %lld");
  70. TEST2(i, c - s, 20, "wrong final position %d != %d");
  71. TEST(ull, strtoull(s = "18446744073709551616", &c, 0), 18446744073709551615ULL, "uncaught overflow %llu != %llu");
  72. TEST2(i, c - s, 20, "wrong final position %d != %d");
  73. TEST(ull, strtoull(s = "-1", &c, 0), -1ULL, "rejected negative %llu != %llu");
  74. TEST2(i, c - s, 2, "wrong final position %d != %d");
  75. TEST(ull, strtoull(s = "-2", &c, 0), -2ULL, "rejected negative %llu != %llu");
  76. TEST2(i, c - s, 2, "wrong final position %d != %d");
  77. TEST(ull, strtoull(s = "-9223372036854775808", &c, 0), -9223372036854775808ULL, "rejected negative %llu != %llu");
  78. TEST2(i, c - s, 20, "wrong final position %d != %d");
  79. TEST(ull, strtoull(s = "-9223372036854775809", &c, 0), -9223372036854775809ULL, "rejected negative %llu != %llu");
  80. TEST2(i, c - s, 20, "wrong final position %d != %d");
  81. TEST(ull, strtoull(s = "-18446744073709551616", &c, 0), 18446744073709551615ULL, "uncaught negative overflow %llu != %llu");
  82. TEST2(i, c - s, 21, "wrong final position %d != %d");
  83. }
  84. else
  85. {
  86. printf("sizeof(long long) == %d, not implemented\n", (int)sizeof(long long));
  87. }
  88. TEST(l, strtol("z", 0, 36), 35L, "%ld != %ld");
  89. TEST(l, strtol("00010010001101000101011001111000", 0, 2), 0x12345678L, "%ld != %ld");
  90. TEST(l, strtol(s = "0F5F", &c, 16), 0x0f5fL, "%ld != %ld");
  91. TEST(l, strtol(s = "0xz", &c, 16), 0L, "%ld != %ld");
  92. TEST2(i, c - s, 1, "wrong final position %ld != %ld");
  93. TEST(l, strtol(s = "0x1234", &c, 16), 0x1234, "%ld != %ld");
  94. TEST2(i, c - s, 6, "wrong final position %ld != %ld");
  95. c = NULL;
  96. TEST3(l, strtol(s = "123", &c, 36), 0, "%ld != %ld");
  97. TEST3(i, c - s, 0, "wrong final position %d != %d");
  98. TEST(l, strtol(s = " 15437", &c, 8), 015437, "%ld != %ld");
  99. TEST2(i, c - s, 7, "wrong final position %d != %d");
  100. TEST(l, strtol(s = " 1", &c, 0), 1, "%ld != %ld");
  101. TEST2(i, c - s, 3, "wrong final position %d != %d");
  102. return 0;
  103. }
  104. static void test_strtol(void)
  105. {
  106. strtol_entry();
  107. }
  108. static void testcase(void)
  109. {
  110. UTEST_UNIT_RUN(test_strtol);
  111. }
  112. UTEST_TC_EXPORT(testcase, "posix.stdlib_h.strtol_tc.c", RT_NULL, RT_NULL, 10);