gnu: kio: Search 'smbd' on $PATH.
[jackhill/guix/guix.git] / gnu / packages / patches / json-glib-fix-tests-32bit.patch
1 Fix floating point issues on 32-bit platforms:
2
3 https://gitlab.gnome.org/GNOME/json-glib/issues/27
4
5 This is an amalgamation of the following upstream commits:
6 https://gitlab.gnome.org/GNOME/json-glib/commit/70e2648e02232c1a439a7418388f18fee9afb3fe
7 https://gitlab.gnome.org/GNOME/json-glib/commit/675e27505776a1d77fa1ffd1974284890caec1f4
8
9 diff --git a/json-glib/tests/json-test-utils.h b/json-glib/tests/json-test-utils.h
10 new file mode 100644
11 index 0000000..83a02c6
12 --- /dev/null
13 +++ b/json-glib/tests/json-test-utils.h
14 @@ -0,0 +1,21 @@
15 +#include <string.h>
16 +#include <math.h>
17 +#include <float.h>
18 +#include <glib.h>
19 +#include <json-glib/json-glib.h>
20 +
21 +#define json_fuzzy_equals(n1,n2,epsilon) \
22 + (((n1) > (n2) ? ((n1) - (n2)) : ((n2) - (n1))) < (epsilon))
23 +
24 +#define json_assert_fuzzy_equals(n1,n2,epsilon) \
25 + G_STMT_START { \
26 + double __n1 = (n1), __n2 = (n2), __epsilon = (epsilon); \
27 + if (json_fuzzy_equals (__n1, __n2, __epsilon)) ; else { \
28 + g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
29 + #n1 " == " #n2 " (+/- " #epsilon ")", \
30 + __n1, "==", __n2, 'f'); \
31 + } \
32 + } G_STMT_END
33 +
34 +#define json_assert_almost_equals(n1,n2) \
35 + json_assert_fuzzy_equals (n1, n2, DBL_EPSILON)
36 diff --git a/json-glib/tests/array.c b/json-glib/tests/array.c
37 index 98afeab..426cd72 100644
38 --- a/json-glib/tests/array.c
39 +++ b/json-glib/tests/array.c
40 @@ -1,9 +1,4 @@
41 -#include <stdio.h>
42 -#include <stdlib.h>
43 -#include <string.h>
44 -
45 -#include <glib.h>
46 -#include <json-glib/json-glib.h>
47 +#include "json-test-utils.h"
48
49 static void
50 test_empty_array (void)
51 @@ -37,7 +32,7 @@ test_add_element (void)
52
53 json_array_add_double_element (array, 3.14);
54 g_assert_cmpint (json_array_get_length (array), ==, 3);
55 - g_assert_cmpfloat (json_array_get_double_element (array, 2), ==, 3.14);
56 + json_assert_fuzzy_equals (json_array_get_double_element (array, 2), 3.14, 0.001);
57
58 json_array_add_boolean_element (array, TRUE);
59 g_assert_cmpint (json_array_get_length (array), ==, 4);
60 diff --git a/json-glib/tests/node.c b/json-glib/tests/node.c
61 index 23bda63..80beb78 100644
62 --- a/json-glib/tests/node.c
63 +++ b/json-glib/tests/node.c
64 @@ -1,6 +1,4 @@
65 -#include <glib.h>
66 -#include <json-glib/json-glib.h>
67 -#include <string.h>
68 +#include "json-test-utils.h"
69
70 static void
71 test_init_int (void)
72 @@ -19,7 +17,7 @@ test_init_double (void)
73 JsonNode *node = json_node_new (JSON_NODE_VALUE);
74
75 json_node_set_double (node, 3.14159);
76 - g_assert_cmpfloat (json_node_get_double (node), ==, 3.14159);
77 + json_assert_fuzzy_equals (json_node_get_double (node), 3.14159, 0.00001);
78
79 json_node_free (node);
80 }
81 @@ -119,13 +117,13 @@ test_get_int (void)
82
83 json_node_set_int (node, 0);
84 g_assert_cmpint (json_node_get_int (node), ==, 0);
85 - g_assert_cmpfloat (json_node_get_double (node), ==, 0.0);
86 + json_assert_almost_equals (json_node_get_double (node), 0.0);
87 g_assert (!json_node_get_boolean (node));
88 g_assert (!json_node_is_null (node));
89
90 json_node_set_int (node, 42);
91 g_assert_cmpint (json_node_get_int (node), ==, 42);
92 - g_assert_cmpfloat (json_node_get_double (node), ==, 42.0);
93 + json_assert_almost_equals (json_node_get_double (node), 42.0);
94 g_assert (json_node_get_boolean (node));
95 g_assert (!json_node_is_null (node));
96
97 @@ -138,7 +136,7 @@ test_get_double (void)
98 JsonNode *node = json_node_new (JSON_NODE_VALUE);
99
100 json_node_set_double (node, 3.14);
101 - g_assert_cmpfloat (json_node_get_double (node), ==, 3.14);
102 + json_assert_fuzzy_equals (json_node_get_double (node), 3.14, 0.001);
103 g_assert_cmpint (json_node_get_int (node), ==, 3);
104 g_assert (json_node_get_boolean (node));
105
106 @@ -232,9 +230,9 @@ test_gvalue_autopromotion (void)
107 g_print ("Expecting a gdouble, got a %s\n", g_type_name (G_VALUE_TYPE (&check)));
108
109 g_assert_cmpint (G_VALUE_TYPE (&check), ==, G_TYPE_DOUBLE);
110 - g_assert_cmpfloat ((float) g_value_get_double (&check), ==, 3.14159f);
111 + json_assert_fuzzy_equals (g_value_get_double (&check), 3.14159, 0.00001);
112 g_assert_cmpint (G_VALUE_TYPE (&value), !=, G_VALUE_TYPE (&check));
113 - g_assert_cmpfloat ((gdouble) g_value_get_float (&value), ==, g_value_get_double (&check));
114 + json_assert_almost_equals (g_value_get_float (&value), g_value_get_double (&check));
115
116 g_value_unset (&value);
117 g_value_unset (&check);
118 diff --git a/json-glib/tests/parser.c b/json-glib/tests/parser.c
119 index f71584a..8c52a1d 100644
120 --- a/json-glib/tests/parser.c
121 +++ b/json-glib/tests/parser.c
122 @@ -1,11 +1,5 @@
123 -#include "config.h"
124 -
125 +#include "json-test-utils.h"
126 #include <stdlib.h>
127 -#include <stdio.h>
128 -
129 -#include <glib.h>
130 -
131 -#include <json-glib/json-glib.h>
132
133 static const gchar *test_empty_string = "";
134 static const gchar *test_empty_array_string = "[ ]";
135 @@ -38,13 +32,13 @@ verify_string_value (JsonNode *node)
136 static void
137 verify_double_value (JsonNode *node)
138 {
139 - g_assert_cmpfloat (10.2e3, ==, json_node_get_double (node));
140 + json_assert_fuzzy_equals (10.2e3, json_node_get_double (node), 0.1);
141 }
142
143 static void
144 verify_negative_double_value (JsonNode *node)
145 {
146 - g_assert_cmpfloat (-3.14, ==, json_node_get_double (node));
147 + json_assert_fuzzy_equals (-3.14, json_node_get_double (node), 0.01);
148 }
149
150 static const struct {
151 diff --git a/json-glib/tests/reader.c b/json-glib/tests/reader.c
152 index 43a6aac..9bab312 100644
153 --- a/json-glib/tests/reader.c
154 +++ b/json-glib/tests/reader.c
155 @@ -1,9 +1,4 @@
156 -#include <stdlib.h>
157 -#include <stdio.h>
158 -
159 -#include <glib.h>
160 -
161 -#include <json-glib/json-glib.h>
162 +#include "json-test-utils.h"
163
164 static const gchar *test_base_array_data =
165 "[ 0, true, null, \"foo\", 3.14, [ false ], { \"bar\" : 42 } ]";
166 @@ -78,7 +73,7 @@ test_base_object (void)
167 g_assert (json_reader_get_error (reader) == NULL);
168
169 json_reader_read_member (reader, "double");
170 - g_assert_cmpfloat (json_reader_get_double_value (reader), ==, 42.47);
171 + json_assert_fuzzy_equals (json_reader_get_double_value (reader), 42.47, 0.01);
172 json_reader_end_element (reader);
173
174 g_object_unref (reader);