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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Tests if the format string for double serialization is handled correctly
  3. */
  4. #include <stdio.h>
  5. #include "config.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. json_object_set_serializer(obj, json_object_double_to_json_string, "%.17g", NULL);
  29. printf("obj.to_string(reset)=%s\n", json_object_to_json_string_ext(obj, 4));
  30. json_object_put(obj);
  31. obj = json_object_new_double(0.52381);
  32. printf("obj.to_string(default format)=%s\n", json_object_to_json_string(obj));
  33. if (json_c_set_serialization_double_format("x%0.3fy", JSON_C_OPTION_GLOBAL) < 0)
  34. printf("ERROR: json_c_set_serialization_double_format() failed");
  35. printf("obj.to_string(with global format)=%s\n", json_object_to_json_string(obj));
  36. #ifdef HAVE___THREAD
  37. if (json_c_set_serialization_double_format("T%0.2fX", JSON_C_OPTION_THREAD) < 0)
  38. printf("ERROR: json_c_set_serialization_double_format() failed");
  39. printf("obj.to_string(with thread format)=%s\n", json_object_to_json_string(obj));
  40. if (json_c_set_serialization_double_format("Ttttttttttttt%0.2fxxxxxxxxxxxxxxxxxxX", JSON_C_OPTION_THREAD) < 0)
  41. printf("ERROR: json_c_set_serialization_double_format() failed");
  42. printf("obj.to_string(long thread format)=%s\n", json_object_to_json_string(obj));
  43. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_THREAD) < 0)
  44. printf("ERROR: json_c_set_serialization_double_format() failed");
  45. printf("obj.to_string(back to global format)=%s\n", json_object_to_json_string(obj));
  46. #else
  47. // Just fake it up, so the output matches.
  48. printf("obj.to_string(with thread format)=%s\n", "T0.52X");
  49. printf("obj.to_string(back to global format)=%s\n", "x0.524y");
  50. #endif
  51. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
  52. printf("ERROR: json_c_set_serialization_double_format() failed");
  53. printf("obj.to_string(back to default format)=%s\n", json_object_to_json_string(obj));
  54. json_object_put(obj);
  55. obj = json_object_new_double(12.0);
  56. printf("obj(12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
  57. if (json_c_set_serialization_double_format("%.0f", JSON_C_OPTION_GLOBAL) < 0)
  58. printf("ERROR: json_c_set_serialization_double_format() failed");
  59. printf("obj(12.0).to_string(%%.0f)=%s\n", json_object_to_json_string(obj));
  60. if (json_c_set_serialization_double_format("%.0g", JSON_C_OPTION_GLOBAL) < 0)
  61. printf("ERROR: json_c_set_serialization_double_format() failed");
  62. printf("obj(12.0).to_string(%%.0g)=%s\n", json_object_to_json_string(obj));
  63. if (json_c_set_serialization_double_format("%.2g", JSON_C_OPTION_GLOBAL) < 0)
  64. printf("ERROR: json_c_set_serialization_double_format() failed");
  65. printf("obj(12.0).to_string(%%.1g)=%s\n", json_object_to_json_string(obj));
  66. // Reset to default to free memory
  67. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
  68. printf("ERROR: json_c_set_serialization_double_format() failed");
  69. json_object_put(obj);
  70. obj = json_object_new_double(-12.0);
  71. printf("obj(-12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
  72. json_object_put(obj);
  73. /* Test NaN handling */
  74. obj = json_object_new_double(zero_dot_zero / zero_dot_zero);
  75. printf("obj(0.0/0.0)=%s\n", json_object_to_json_string(obj));
  76. json_object_put(obj);
  77. /* Test Infinity and -Infinity handling */
  78. obj = json_object_new_double(1.0/zero_dot_zero);
  79. printf("obj(1.0/0.0)=%s\n", json_object_to_json_string(obj));
  80. json_object_put(obj);
  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. return 0;
  85. }