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

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