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

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