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_double_serializer.c 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Tests if the format string for double serialization is handled correctly
  3. */
  4. #ifdef NDEBUG
  5. #undef NDEBUG
  6. #endif
  7. #include "config.h"
  8. #include <stdio.h>
  9. #include "json_object.h"
  10. #include "json_object_private.h"
  11. /* Avoid compiler warnings about diving by constant zero */
  12. double zero_dot_zero = 0.0;
  13. int main(int argc, char **argv)
  14. {
  15. struct json_object *obj = json_object_new_double(0.5);
  16. char udata[] = "test";
  17. printf("Test default serializer:\n");
  18. printf("obj.to_string(standard)=%s\n", json_object_to_json_string(obj));
  19. printf("Test default serializer with custom userdata:\n");
  20. obj->_userdata = udata;
  21. printf("obj.to_string(userdata)=%s\n", json_object_to_json_string(obj));
  22. printf("Test explicit serializer with custom userdata:\n");
  23. json_object_set_serializer(obj, json_object_double_to_json_string, udata, NULL);
  24. printf("obj.to_string(custom)=%s\n", json_object_to_json_string(obj));
  25. printf("Test reset serializer:\n");
  26. json_object_set_serializer(obj, NULL, NULL, NULL);
  27. printf("obj.to_string(reset)=%s\n", json_object_to_json_string(obj));
  28. json_object_put(obj);
  29. printf("Test no zero reset serializer:\n");
  30. obj = json_object_new_double(3.1415000);
  31. char data[] = "%.17g";
  32. json_object_set_serializer(obj, json_object_double_to_json_string, data, NULL);
  33. printf("obj.to_string(reset)=%s\n", json_object_to_json_string_ext(obj, 4));
  34. json_object_put(obj);
  35. obj = json_object_new_double(0.52381);
  36. printf("obj.to_string(default format)=%s\n", json_object_to_json_string(obj));
  37. if (json_c_set_serialization_double_format("x%0.3fy", JSON_C_OPTION_GLOBAL) < 0)
  38. printf("ERROR: json_c_set_serialization_double_format() failed");
  39. printf("obj.to_string(with global format)=%s\n", json_object_to_json_string(obj));
  40. #ifdef HAVE___THREAD
  41. if (json_c_set_serialization_double_format("T%0.2fX", JSON_C_OPTION_THREAD) < 0)
  42. printf("ERROR: json_c_set_serialization_double_format() failed");
  43. printf("obj.to_string(with thread format)=%s\n", json_object_to_json_string(obj));
  44. if (json_c_set_serialization_double_format("Ttttttttttttt%0.2fxxxxxxxxxxxxxxxxxxX",
  45. JSON_C_OPTION_THREAD) < 0)
  46. printf("ERROR: json_c_set_serialization_double_format() failed");
  47. printf("obj.to_string(long thread format)=%s\n", json_object_to_json_string(obj));
  48. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_THREAD) < 0)
  49. printf("ERROR: json_c_set_serialization_double_format() failed");
  50. printf("obj.to_string(back to global format)=%s\n", json_object_to_json_string(obj));
  51. #else
  52. // Just fake it up, so the output matches.
  53. printf("obj.to_string(with thread format)=%s\n", "T0.52X");
  54. printf("obj.to_string(long thread format)=%s\n", "Ttttttttttttt0.52xxxxxxxxxxxxxxxxxxX");
  55. printf("obj.to_string(back to global format)=%s\n", "x0.524y");
  56. #endif
  57. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
  58. printf("ERROR: json_c_set_serialization_double_format() failed");
  59. printf("obj.to_string(back to default format)=%s\n", json_object_to_json_string(obj));
  60. json_object_put(obj);
  61. obj = json_object_new_double(12.0);
  62. printf("obj(12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
  63. if (json_c_set_serialization_double_format("%.0f", JSON_C_OPTION_GLOBAL) < 0)
  64. printf("ERROR: json_c_set_serialization_double_format() failed");
  65. printf("obj(12.0).to_string(%%.0f)=%s\n", json_object_to_json_string(obj));
  66. if (json_c_set_serialization_double_format("%.0g", JSON_C_OPTION_GLOBAL) < 0)
  67. printf("ERROR: json_c_set_serialization_double_format() failed");
  68. printf("obj(12.0).to_string(%%.0g)=%s\n", json_object_to_json_string(obj));
  69. if (json_c_set_serialization_double_format("%.2g", JSON_C_OPTION_GLOBAL) < 0)
  70. printf("ERROR: json_c_set_serialization_double_format() failed");
  71. printf("obj(12.0).to_string(%%.1g)=%s\n", json_object_to_json_string(obj));
  72. // Reset to default to free memory
  73. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
  74. printf("ERROR: json_c_set_serialization_double_format() failed");
  75. json_object_put(obj);
  76. obj = json_object_new_double(-12.0);
  77. printf("obj(-12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
  78. json_object_put(obj);
  79. /* Test NaN handling */
  80. obj = json_object_new_double(zero_dot_zero / zero_dot_zero);
  81. printf("obj(0.0/0.0)=%s\n", json_object_to_json_string(obj));
  82. json_object_put(obj);
  83. /* Test Infinity and -Infinity handling */
  84. obj = json_object_new_double(1.0 / zero_dot_zero);
  85. printf("obj(1.0/0.0)=%s\n", json_object_to_json_string(obj));
  86. json_object_put(obj);
  87. obj = json_object_new_double(-1.0 / zero_dot_zero);
  88. printf("obj(-1.0/0.0)=%s\n", json_object_to_json_string(obj));
  89. json_object_put(obj);
  90. return 0;
  91. }