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

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