aboutsummaryrefslogtreecommitdiffstats
path: root/skeletons/tests/check-REAL.c
blob: 3a868aafdea7162ddaa7fd73bd4647326d5f5e9c (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#define	EMIT_ASN_DEBUG	1
#include <REAL.c>
#include <asn_codecs_prim.c>
#include <ber_decoder.c>
#include <ber_tlv_length.c>
#include <ber_tlv_tag.c>
#include <der_encoder.c>
#include <xer_decoder.c>
#include <xer_support.c>
#include <xer_encoder.c>
#include <constraints.c>

static char reconstructed[2][512];
static int reconstr_lens[2];

static int
callback(const void *buffer, size_t size, void *app_key) {
	char *buf = reconstructed[app_key ? 1 : 0];
	int *len = &reconstr_lens[app_key ? 1 : 0];

	if(*len + size >= sizeof(reconstructed[0]))
		return -1;

	memcpy(buf + *len, buffer, size);
	*len += size;

	return 0;
}

static void
check_str_repr(double d, const char *sample, const char *canonical_sample) {
	ssize_t s1, s2;

	reconstr_lens[1] = reconstr_lens[0] = 0;

	s1 = REAL__dump(d, 0, callback, 0);
	assert(s1 < sizeof(reconstructed[0]));
	assert(s1 == reconstr_lens[0]);
	reconstructed[0][s1] = '\0';

	s2 = REAL__dump(d, 1, callback, (void *)1);
	assert(s2 < sizeof(reconstructed[1]));
	assert(s2 == reconstr_lens[1]);
	reconstructed[1][s2] = '\0';

	if(sample) {
		printf("Checking %f->[%s] against [%s]%s\n",
			d, reconstructed[0], sample,
			canonical_sample ? " (canonical follows...)" : ""
		);
		assert(!strcmp(reconstructed[0], sample));
	}
	if(canonical_sample) {
		printf("Checking %f->[%s] against [%s] (canonical)\n",
			d, reconstructed[1], canonical_sample);
		assert(!strcmp(reconstructed[1], canonical_sample));
	}
}

static void
check_impl(REAL_t *rn, double orig_dbl, const char *sample, const char *canonical_sample, int line) {
	double val;
	uint8_t *p, *end;
	int ret;

	printf("Line %d: double value %.12f [", line, orig_dbl);
	for(p = (uint8_t *)&orig_dbl, end = p + sizeof(double); p < end ; p++)
		printf("%02x", *p);
	printf("] (ilogb %d)\n", ilogb(orig_dbl));

	val = frexp(orig_dbl, &ret);
	printf("frexp(%f, %d): [", val, ret);
	for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
		printf("%02x", *p);
	printf("]\n");

	ret = asn_double2REAL(rn, orig_dbl);
	assert(ret == 0);

	printf("converted into [");
	for(p = rn->buf, end = p + rn->size; p < end; p++)
		printf("%02x", *p);
	printf("]: %d\n", rn->size);

	ret = asn_REAL2double(rn, &val);
	assert(ret == 0);

	printf("and back to double: [");
	for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
		printf("%02x", *p);
	printf("] (ilogb %d)\n", ilogb(val));

	printf("%.12f vs %.12f\n", val, orig_dbl);
	assert((isnan(orig_dbl) && isnan(val)) || val == orig_dbl);
	printf("OK\n");

	check_str_repr(val, sample, canonical_sample);
}

uint8_t buf_1_0[]  = { 0x80, 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t buf_1_1[]  = { 0x80, 0xcc, 0x11, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a };
uint8_t buf_3_14[] = { 0x80, 0xcd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f };
/* These ones are very interesting! They check mantissa overflow! */
uint8_t buf_mo1[]  = { 0xC0, 0xc5, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3};
uint8_t buf_mo2[]  = { 0x80, 0xbd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3,2};

static void
check_buf(uint8_t *buf, size_t bufsize, double verify, const char *sample, const char *canonical_sample) {
	REAL_t rn;
	double val;
	uint8_t *p, *end;
	int ret;

	printf("verify double value %.12f [", verify);
	for(p = (uint8_t *)&verify, end = p + sizeof(double); p < end ; p++)
		printf("%02x", *p);
	printf("] (ilogb %d)\n", ilogb(verify));

	rn.buf = 0;
	rn.size = 0;

	ret = asn_double2REAL(&rn, verify);
	assert(ret == 0);

	printf("canonical DER: [");
	for(p = rn.buf, end = p + rn.size; p < end; p++)
		printf("%02x", *p);
	printf("]\n");

	rn.buf = buf;
	rn.size = bufsize;

	printf("received as:   [");
	for(p = rn.buf, end = p + rn.size; p < end; p++)
		printf("%02x", *p);
	printf("]\n");

	ret = asn_REAL2double(&rn, &val);
	assert(ret == 0);

	printf("%.12f vs %.12f\n", verify, val);

	assert(val == verify);

	check_str_repr(val, sample, canonical_sample);
}

static void
check_xer(int fuzzy, double orig_value) {
	asn_enc_rval_t er;
	asn_dec_rval_t rc;
	REAL_t st;
	REAL_t *newst0 = 0;
	REAL_t *newst1 = 0;
	REAL_t **newst0p = &newst0;
	REAL_t **newst1p = &newst1;
	double value0, value1;
	int ret;

	memset(&st, 0, sizeof(st));
	ret = asn_double2REAL(&st, orig_value);
	assert(ret == 0);

	reconstr_lens[0] = 0;
	reconstr_lens[1] = 0;
	er = xer_encode(&asn_DEF_REAL, &st,
		XER_F_BASIC, callback, 0);
	assert(er.encoded == reconstr_lens[0]);
	er = xer_encode(&asn_DEF_REAL, &st,
		XER_F_CANONICAL, callback, (void *)1);
	assert(er.encoded == reconstr_lens[1]);
	reconstructed[0][reconstr_lens[0]] = 0;
	reconstructed[1][reconstr_lens[1]] = 0;

	printf("%f vs (%d)[%s] & (%d)%s",
		orig_value,
		reconstr_lens[1], reconstructed[1],
		reconstr_lens[0], reconstructed[0]
	);

	rc = xer_decode(0, &asn_DEF_REAL, (void **)newst0p,
		reconstructed[0], reconstr_lens[0]);
	assert(rc.code == RC_OK);
	assert(rc.consumed < reconstr_lens[0]);

	rc = xer_decode(0, &asn_DEF_REAL, (void **)newst1p,
		reconstructed[1], reconstr_lens[1]);
	assert(rc.code == RC_OK);
	assert(rc.consumed == reconstr_lens[1]);

	ret = asn_REAL2double(newst0, &value0);
	assert(ret == 0);
	ret = asn_REAL2double(newst1, &value1);
	assert(ret == 0);

	assert((isnan(value0) && isnan(orig_value))
		|| value0 == orig_value
		|| fuzzy);
	assert((isnan(value1) && isnan(orig_value))
		|| value1 == orig_value);

	assert(newst0->size == st.size || fuzzy);
	assert(newst1->size == st.size);
	assert(fuzzy || memcmp(newst0->buf, st.buf, st.size) == 0);
	assert(memcmp(newst1->buf, st.buf, st.size) == 0);
}

#define	check(rn, d, str1, str2)	\
	check_impl(rn, d, str1, str2, __LINE__)

int
main() {
	REAL_t rn;
	static const double zero = 0.0;

	memset(&rn, 0, sizeof(rn));

	check(&rn, 0.0, "0", "0");
	check(&rn, -0.0, "-0", "-0");	/* minus-zero */
	check(&rn, zero/zero, "<NOT-A-NUMBER/>", "<NOT-A-NUMBER/>");
	check(&rn, 1.0/zero, "<PLUS-INFINITY/>", "<PLUS-INFINITY/>");
	check(&rn, -1.0/zero, "<MINUS-INFINITY/>", "<MINUS-INFINITY/>");
	check(&rn, 1.0, "1.0", "1.0E0");
	check(&rn, -1.0, "-1.0", "-1.0E0");
	check(&rn, 0.1, "0.1", "1.0E-1");
	check(&rn, 0.01, "0.01", "1.0E-2");
	check(&rn, 0.02, "0.02", "2.0E-2");
	check(&rn, 0.09, "0.09", "9.0E-2");
	check(&rn, 1.5, "1.5", "1.5E0");
	check(&rn, 0.33333, "0.33333", "3.3333E-1");
	check(&rn, 2, "2.0", "2.0E0");
	check(&rn, 2.1, "2.1", "2.1E0");
	check(&rn, 3, "3.0", "3.0E0");
	check(&rn, 3.1, "3.1", "3.1E0");
	check(&rn, 3.14, "3.14", "3.14E0");
	check(&rn, 3.1415, "3.1415", "3.1415E0");
	check(&rn, 3.141592, "3.141592", "3.141592E0");
	check(&rn, 3.14159265, "3.14159265", "3.14159265E0");
	check(&rn, -3.14159265, "-3.14159265", "-3.14159265E0");
	check(&rn, 14159265.0, "14159265.0", "1.4159265E7");
	check(&rn, -123456789123456789.0, "-123456789123456784.0", "-1.234567891234568E17");
	check(&rn, 0.00000000001, "0.00000000001", "9.999999999999999E-12");
	check(&rn, 0.00000000002, "0.00000000002", "2.0E-11");
	check(&rn, 0.00000000009, "0.00000000009", "9.0E-11");
	check(&rn, 0.000000000002, "0.000000000002", "2.0E-12");
	check(&rn, 0.0000000000002, "0.0000000000002", "2.0E-13");
	check(&rn, 0.00000000000002, "0.00000000000002", "2.0E-14");
	check(&rn, 0.000000000000002, "0.000000000000002", "2.0E-15");
	check(&rn, 0.0000000000000002, "0.0", "2.0E-16");
	check(&rn, 0.0000000000000000000001, "0.0", "1.0E-22");
	check(&rn, 0.000000000000000000000000000001, "0.0", "1.0E-30"); /* proved 2B a problem */
	check(&rn,-0.000000000000000000000000000001, "-0.0", "-1.0E-30"); /* proved 2B a problem */
	check(&rn, 0.0000000000010000000001000000000001, 0, 0);
	check(&rn, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
	check(&rn, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
	check(&rn,-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
	check(&rn,-3.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
	check(&rn, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
	check(&rn, -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);

	check_buf(buf_1_0, sizeof(buf_1_0),	1.0, "1.0", "1.0E0");
	check_buf(buf_1_1, sizeof(buf_1_1),	1.1, "1.1", "1.1E0");
	check_buf(buf_3_14, sizeof(buf_3_14),	3.14, "3.14", "3.14E0");
	check_buf(buf_mo1, sizeof(buf_mo1),	-3.14, "-3.14", "-3.14E0");
	check_buf(buf_mo2, sizeof(buf_mo2),	3.14, "3.14", "3.14E0");


#ifdef	NAN
	check_xer(0, NAN);	/* "<NOT-A-NUMBER/>" */
#else
	check_xer(0, zero/zero);	/* "<NOT-A-NUMBER/>" */
#endif
#ifdef	INFINITY
	check_xer(0, INFINITY);		/* "<PLUS-INFINITY/>" */
	check_xer(0, -INFINITY);	/* "<MINUS-INFINITY/>" */
#else
	check_xer(0, 1.0/zero);		/* "<PLUS-INFINITY/>" */
	check_xer(0, -1.0/zero);	/* "<MINUS-INFINITY/>" */
#endif
	check_xer(0, 1.0);
	check_xer(0, -1.0);
	check_xer(0, 1.5);
	check_xer(0, 123);
	check_xer(1, 0.0000000000000000000001);
	check_xer(1, -0.0000000000000000000001);

	return 0;
}

asn_dec_rval_t OCTET_STRING_decode_uper(asn_codec_ctx_t *ctx, asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts, void **sptr, asn_per_data_t *pd) { asn_dec_rval_t rv = { 0, 0 }; (void)ctx; (void)td; (void)cts; (void)sptr; (void)pd; return rv; }

asn_enc_rval_t OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts, void *sptr, asn_per_outp_t *po) { asn_enc_rval_t er = { 0, 0, 0 }; (void)td; (void)cts; (void)sptr; (void)po; return er; }