You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_parse_int64.c 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #ifdef NDEBUG
  2. #undef NDEBUG
  3. #endif
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "config.h"
  7. #include "json_inttypes.h"
  8. #include "json_util.h"
  9. void checkit(const char *buf)
  10. {
  11. int64_t cint64 = -666;
  12. int retval = json_parse_int64(buf, &cint64);
  13. printf("buf=%s parseit=%d, value=%" PRId64 " \n", buf, retval, cint64);
  14. }
  15. void checkit_uint(const char *buf)
  16. {
  17. uint64_t cuint64 = 666;
  18. int retval = json_parse_uint64(buf, &cuint64);
  19. printf("buf=%s parseit=%d, value=%" PRIu64 " \n", buf, retval, cuint64);
  20. }
  21. /**
  22. * This test calls json_parse_int64 and json_parse_int64 with a variety
  23. * of different strings. It's purpose is to ensure that the results are
  24. * consistent across all different environments that it might be executed in.
  25. *
  26. * This always exits with a 0 exit value. The output should be compared
  27. * against previously saved expected output.
  28. */
  29. int main(int argc, char **argv)
  30. {
  31. char buf[100];
  32. printf("==========json_parse_int64() test===========\n");
  33. checkit("x");
  34. checkit("0");
  35. checkit("-0");
  36. checkit("00000000");
  37. checkit("-00000000");
  38. checkit("1");
  39. strcpy(buf, "2147483647"); // aka INT32_MAX
  40. checkit(buf);
  41. strcpy(buf, "-1");
  42. checkit(buf);
  43. strcpy(buf, " -1");
  44. checkit(buf);
  45. strcpy(buf, "00001234");
  46. checkit(buf);
  47. strcpy(buf, "0001234x");
  48. checkit(buf);
  49. strcpy(buf, "-00001234");
  50. checkit(buf);
  51. strcpy(buf, "-00001234x");
  52. checkit(buf);
  53. strcpy(buf, "4294967295"); // aka UINT32_MAX
  54. checkit(buf);
  55. strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
  56. checkit(buf);
  57. strcpy(buf, "21474836470"); // INT32_MAX * 10
  58. checkit(buf);
  59. strcpy(buf, "31474836470"); // INT32_MAX * 10 + a bunch
  60. checkit(buf);
  61. strcpy(buf, "-2147483647"); // INT32_MIN + 1
  62. checkit(buf);
  63. strcpy(buf, "-2147483648"); // INT32_MIN
  64. checkit(buf);
  65. strcpy(buf, "-2147483649"); // INT32_MIN - 1
  66. checkit(buf);
  67. strcpy(buf, "-21474836480"); // INT32_MIN * 10
  68. checkit(buf);
  69. strcpy(buf, "9223372036854775806"); // INT64_MAX - 1
  70. checkit(buf);
  71. strcpy(buf, "9223372036854775807"); // INT64_MAX
  72. checkit(buf);
  73. strcpy(buf, "9223372036854775808"); // INT64_MAX + 1
  74. checkit(buf);
  75. strcpy(buf, "-9223372036854775808"); // INT64_MIN
  76. checkit(buf);
  77. strcpy(buf, "-9223372036854775809"); // INT64_MIN - 1
  78. checkit(buf);
  79. strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1
  80. checkit(buf);
  81. strcpy(buf, "18446744073709551615"); // UINT64_MAX
  82. checkit(buf);
  83. strcpy(buf, "18446744073709551616"); // UINT64_MAX + 1
  84. checkit(buf);
  85. strcpy(buf, "-18446744073709551616"); // -UINT64_MAX
  86. checkit(buf);
  87. // Ensure we can still parse valid numbers after parsing out of range ones.
  88. strcpy(buf, "123");
  89. checkit(buf);
  90. printf("\n==========json_parse_uint64() test===========\n");
  91. checkit_uint("x");
  92. checkit_uint("0");
  93. checkit_uint("-0");
  94. checkit_uint("00000000");
  95. checkit_uint("-00000000");
  96. checkit_uint("1");
  97. strcpy(buf, "2147483647"); // aka INT32_MAX
  98. checkit_uint(buf);
  99. strcpy(buf, "-1");
  100. checkit_uint(buf);
  101. strcpy(buf, "-9223372036854775808");
  102. checkit_uint(buf);
  103. strcpy(buf, " 1");
  104. checkit_uint(buf);
  105. strcpy(buf, "00001234");
  106. checkit_uint(buf);
  107. strcpy(buf, "0001234x");
  108. checkit_uint(buf);
  109. strcpy(buf, "4294967295"); // aka UINT32_MAX
  110. checkit_uint(buf);
  111. strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
  112. checkit_uint(buf);
  113. strcpy(buf, "21474836470"); // INT32_MAX * 10
  114. checkit_uint(buf);
  115. strcpy(buf, "31474836470"); // INT32_MAX * 10 + a bunch
  116. checkit_uint(buf);
  117. strcpy(buf, "9223372036854775806"); // INT64_MAX - 1
  118. checkit_uint(buf);
  119. strcpy(buf, "9223372036854775807"); // INT64_MAX
  120. checkit_uint(buf);
  121. strcpy(buf, "9223372036854775808"); // INT64_MAX + 1
  122. checkit_uint(buf);
  123. strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1
  124. checkit_uint(buf);
  125. strcpy(buf, "18446744073709551615"); // UINT64_MAX
  126. checkit_uint(buf);
  127. strcpy(buf, "18446744073709551616"); // UINT64_MAX + 1
  128. checkit_uint(buf);
  129. // Ensure we can still parse valid numbers after parsing out of range ones.
  130. strcpy(buf, "123");
  131. checkit_uint(buf);
  132. return 0;
  133. }