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_locale.c 1.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifdef NDEBUG
  2. #undef NDEBUG
  3. #endif
  4. #include <assert.h>
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "config.h"
  10. #include "json.h"
  11. #include "json_tokener.h"
  12. #include "snprintf_compat.h"
  13. #ifdef HAVE_LOCALE_H
  14. #include <locale.h>
  15. #endif /* HAVE_LOCALE_H */
  16. #ifdef HAVE_XLOCALE_H
  17. #include <xlocale.h>
  18. #endif
  19. int main(int argc, char **argv)
  20. {
  21. json_object *new_obj;
  22. #ifdef HAVE_SETLOCALE
  23. setlocale(LC_NUMERIC, "de_DE");
  24. #endif
  25. char buf1[10], buf2[10];
  26. // Should result in "0,1", if the locale is installed.
  27. // Regardless of what it generates, we check that it's
  28. // consistent below.
  29. (void)snprintf(buf1, sizeof(buf1), "%f", 0.1);
  30. MC_SET_DEBUG(1);
  31. new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0,2.3e10]");
  32. (void)snprintf(buf2, sizeof(buf2), "%f", 0.1);
  33. if (strcmp(buf1, buf2) != 0)
  34. printf("ERROR: Original locale not restored \"%s\" != \"%s\"", buf1, buf2);
  35. #ifdef HAVE_SETLOCALE
  36. setlocale(LC_NUMERIC, "C");
  37. #endif
  38. // Explicitly print each value, to avoid having the "special"
  39. // serialization done for parsed doubles simply re-emit the original
  40. // string that was parsed. (see json_object_new_double_s())
  41. printf("new_obj.to_string()=[");
  42. unsigned int ii;
  43. for (ii = 0; ii < json_object_array_length(new_obj); ii++)
  44. {
  45. json_object *val = json_object_array_get_idx(new_obj, ii);
  46. printf("%s%.2lf", (ii > 0) ? "," : "", json_object_get_double(val));
  47. }
  48. printf("]\n");
  49. printf("new_obj.to_string()=%s\n",
  50. json_object_to_json_string_ext(new_obj, JSON_C_TO_STRING_NOZERO));
  51. json_object_put(new_obj);
  52. return 0;
  53. }