test_parse_int64.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "config.h"
  4. #include "json_inttypes.h"
  5. #include "json_util.h"
  6. void checkit(const char *buf)
  7. {
  8. int64_t cint64 = -666;
  9. int retval = json_parse_int64(buf, &cint64);
  10. printf("buf=%s parseit=%d, value=%" PRId64 " \n", buf, retval, cint64);
  11. }
  12. /**
  13. * This test calls json_parse_int64 with a variety of different strings.
  14. * It's purpose is to ensure that the results are consistent across all
  15. * different environments that it might be executed in.
  16. *
  17. * This always exits with a 0 exit value. The output should be compared
  18. * against previously saved expected output.
  19. */
  20. int main()
  21. {
  22. char buf[100];
  23. checkit("x");
  24. checkit("0");
  25. checkit("-0");
  26. checkit("00000000");
  27. checkit("-00000000");
  28. checkit("1");
  29. strcpy(buf, "2147483647"); // aka INT32_MAX
  30. checkit(buf);
  31. strcpy(buf, "-1");
  32. checkit(buf);
  33. strcpy(buf, " -1");
  34. checkit(buf);
  35. strcpy(buf, "00001234");
  36. checkit(buf);
  37. strcpy(buf, "0001234x");
  38. checkit(buf);
  39. strcpy(buf, "-00001234");
  40. checkit(buf);
  41. strcpy(buf, "-00001234x");
  42. checkit(buf);
  43. strcpy(buf, "4294967295"); // aka UINT32_MAX
  44. sprintf(buf, "4294967296"); // aka UINT32_MAX + 1
  45. strcpy(buf, "21474836470"); // INT32_MAX * 10
  46. checkit(buf);
  47. strcpy(buf, "31474836470"); // INT32_MAX * 10 + a bunch
  48. checkit(buf);
  49. strcpy(buf, "-2147483647"); // INT32_MIN + 1
  50. checkit(buf);
  51. strcpy(buf, "-2147483648"); // INT32_MIN
  52. checkit(buf);
  53. strcpy(buf, "-2147483649"); // INT32_MIN - 1
  54. checkit(buf);
  55. strcpy(buf, "-21474836480"); // INT32_MIN * 10
  56. checkit(buf);
  57. strcpy(buf, "9223372036854775806"); // INT64_MAX - 1
  58. checkit(buf);
  59. strcpy(buf, "9223372036854775807"); // INT64_MAX
  60. checkit(buf);
  61. strcpy(buf, "9223372036854775808"); // INT64_MAX + 1
  62. checkit(buf);
  63. strcpy(buf, "-9223372036854775808"); // INT64_MIN
  64. checkit(buf);
  65. strcpy(buf, "-9223372036854775809"); // INT64_MIN - 1
  66. checkit(buf);
  67. strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1
  68. checkit(buf);
  69. strcpy(buf, "18446744073709551615"); // UINT64_MAX
  70. checkit(buf);
  71. strcpy(buf, "18446744073709551616"); // UINT64_MAX + 1
  72. checkit(buf);
  73. strcpy(buf, "-18446744073709551616"); // -UINT64_MAX
  74. checkit(buf);
  75. // Ensure we can still parse valid numbers after parsing out of range ones.
  76. strcpy(buf, "123");
  77. checkit(buf);
  78. return 0;
  79. }