summaryrefslogtreecommitdiff
path: root/tests/test_locale.c
blob: 55c9cc6e55b91dcfe329a17691593d10a40a0e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "json.h"
#include "json_tokener.h"
#include "snprintf_compat.h"

#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif /* HAVE_LOCALE_H */
#ifdef HAVE_XLOCALE_H
#include <xlocale.h>
#endif

int main(int argc, char **argv)
{
	json_object *new_obj;
#ifdef HAVE_SETLOCALE
	setlocale(LC_NUMERIC, "de_DE");
#else
	printf("No locale\n");
#endif

	char buf1[10], buf2[10];
	// Should result in "0,1", if the locale is installed.
	// Regardless of what it generates, we check that it's
	// consistent below.
	(void)snprintf(buf1, sizeof(buf1), "%f", 0.1);

	MC_SET_DEBUG(1);

	new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0,2.3e10]");

	(void)snprintf(buf2, sizeof(buf2), "%f", 0.1);
	if (strcmp(buf1, buf2) != 0)
		printf("ERROR: Original locale not restored \"%s\" != \"%s\"", buf1, buf2);

#ifdef HAVE_SETLOCALE
	setlocale(LC_NUMERIC, "C");
#endif

	// Explicitly print each value, to avoid having the "special"
	// serialization done for parsed doubles simply re-emit the original
	// string that was parsed.  (see json_object_new_double_s())
	printf("new_obj.to_string()=[");
	unsigned int ii;
	for (ii = 0; ii < json_object_array_length(new_obj); ii++)
	{
		json_object *val = json_object_array_get_idx(new_obj, ii);
		printf("%s%.2lf", (ii > 0) ? "," : "", json_object_get_double(val));
	}
	printf("]\n");

	printf("new_obj.to_string()=%s\n",
	       json_object_to_json_string_ext(new_obj, JSON_C_TO_STRING_NOZERO));
	json_object_put(new_obj);

	return 0;
}