• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/json-c/tests/test_double_serializer.c

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt