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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. 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", JSON_C_OPTION_THREAD) < 0)
  42. printf("ERROR: json_c_set_serialization_double_format() failed");
  43. printf("obj.to_string(long thread format)=%s\n", json_object_to_json_string(obj));
  44. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_THREAD) < 0)
  45. printf("ERROR: json_c_set_serialization_double_format() failed");
  46. printf("obj.to_string(back to global format)=%s\n", json_object_to_json_string(obj));
  47. #else
  48. // Just fake it up, so the output matches.
  49. printf("obj.to_string(with thread format)=%s\n", "T0.52X");
  50. printf("obj.to_string(back to global format)=%s\n", "x0.524y");
  51. #endif
  52. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
  53. printf("ERROR: json_c_set_serialization_double_format() failed");
  54. printf("obj.to_string(back to default format)=%s\n", json_object_to_json_string(obj));
  55. json_object_put(obj);
  56. obj = json_object_new_double(12.0);
  57. printf("obj(12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
  58. if (json_c_set_serialization_double_format("%.0f", JSON_C_OPTION_GLOBAL) < 0)
  59. printf("ERROR: json_c_set_serialization_double_format() failed");
  60. printf("obj(12.0).to_string(%%.0f)=%s\n", json_object_to_json_string(obj));
  61. if (json_c_set_serialization_double_format("%.0g", JSON_C_OPTION_GLOBAL) < 0)
  62. printf("ERROR: json_c_set_serialization_double_format() failed");
  63. printf("obj(12.0).to_string(%%.0g)=%s\n", json_object_to_json_string(obj));
  64. if (json_c_set_serialization_double_format("%.2g", JSON_C_OPTION_GLOBAL) < 0)
  65. printf("ERROR: json_c_set_serialization_double_format() failed");
  66. printf("obj(12.0).to_string(%%.1g)=%s\n", json_object_to_json_string(obj));
  67. // Reset to default to free memory
  68. if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
  69. printf("ERROR: json_c_set_serialization_double_format() failed");
  70. json_object_put(obj);
  71. obj = json_object_new_double(-12.0);
  72. printf("obj(-12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
  73. json_object_put(obj);
  74. /* Test NaN handling */
  75. obj = json_object_new_double(zero_dot_zero / zero_dot_zero);
  76. printf("obj(0.0/0.0)=%s\n", json_object_to_json_string(obj));
  77. json_object_put(obj);
  78. /* Test Infinity and -Infinity handling */
  79. obj = json_object_new_double(1.0/zero_dot_zero);
  80. printf("obj(1.0/0.0)=%s\n", json_object_to_json_string(obj));
  81. json_object_put(obj);
  82. obj = json_object_new_double(-1.0/zero_dot_zero);
  83. printf("obj(-1.0/0.0)=%s\n", json_object_to_json_string(obj));
  84. json_object_put(obj);
  85. return 0;
  86. }