blob: f9b9acce30348c42df62f55c376400c8aab3f275 [file] [log] [blame]
Lev Walkin4eceeba2007-07-23 06:48:26 +00001#include <stdio.h>
2#include <assert.h>
3#include <math.h>
Lev Walkin8ca13c82016-01-24 22:40:00 -08004#include <float.h>
Lev Walkin4eceeba2007-07-23 06:48:26 +00005
Lev Walkin4eceeba2007-07-23 06:48:26 +00006#include <REAL.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +00007
Lev Walkined465432004-09-27 20:52:36 +00008static char reconstructed[2][512];
9static int reconstr_lens[2];
10
11static int
12callback(const void *buffer, size_t size, void *app_key) {
13 char *buf = reconstructed[app_key ? 1 : 0];
14 int *len = &reconstr_lens[app_key ? 1 : 0];
15
16 if(*len + size >= sizeof(reconstructed[0]))
17 return -1;
18
19 memcpy(buf + *len, buffer, size);
20 *len += size;
21
22 return 0;
23}
24
Lev Walkin833d2752012-01-07 15:29:25 -080025static char *
Lev Walkin97363482016-01-24 19:23:02 -080026d2s(double d, int canonical) {
Lev Walkin833d2752012-01-07 15:29:25 -080027 ssize_t s;
28
29 reconstr_lens[canonical] = 0;
30 s = REAL__dump(d, canonical, callback, (void *)(ptrdiff_t)canonical);
Lev Walkin97363482016-01-24 19:23:02 -080031 assert(s > 0 && (size_t)s < sizeof(reconstructed[canonical]));
Lev Walkin833d2752012-01-07 15:29:25 -080032 assert(s == reconstr_lens[canonical]);
33 reconstructed[canonical][s] = '\0'; // ASCIIZ
34 return reconstructed[canonical];
35}
36
37/*
38 * Verify that a string representation of a given floating point value
39 * is as given in the (sample) and (canonical_sample) arguments.
40 */
Lev Walkin41ba1f22004-09-14 12:46:35 +000041static void
Lev Walkin5fda7d52012-01-09 03:20:19 -080042check_str_representation(double d, const char *sample, const char *canonical_sample, int lineno) {
Lev Walkin833d2752012-01-07 15:29:25 -080043 char *s0, *s1;
Lev Walkined465432004-09-27 20:52:36 +000044
Lev Walkin97363482016-01-24 19:23:02 -080045 s0 = d2s(d, 0);
46 s1 = d2s(d, 1);
Lev Walkined465432004-09-27 20:52:36 +000047
48 if(sample) {
Lev Walkin8ca13c82016-01-24 22:40:00 -080049 printf("%03d: Checking %g->[\"%s\"] against [\"%s\"]%s\n",
Lev Walkin5fda7d52012-01-09 03:20:19 -080050 lineno, d, s0, sample,
Lev Walkinf0b808d2005-04-25 21:08:25 +000051 canonical_sample ? " (canonical follows...)" : ""
52 );
Lev Walkin833d2752012-01-07 15:29:25 -080053 assert(!strcmp(s0, sample));
Lev Walkined465432004-09-27 20:52:36 +000054 }
55 if(canonical_sample) {
Lev Walkin8ca13c82016-01-24 22:40:00 -080056 printf("%03d: Checking %g->[\"%s\"] against [\"%s\"] (canonical)\n",
Lev Walkin5fda7d52012-01-09 03:20:19 -080057 lineno, d, s1, canonical_sample);
Lev Walkin833d2752012-01-07 15:29:25 -080058 assert(!strcmp(s1, canonical_sample));
Lev Walkined465432004-09-27 20:52:36 +000059 }
60}
61
Lev Walkin5fe72e92012-01-07 17:00:29 -080062#define check(rn, d, str1, str2) \
63 check_impl(rn, d, str1, str2, __LINE__)
64
Lev Walkined465432004-09-27 20:52:36 +000065static void
Lev Walkin5fda7d52012-01-09 03:20:19 -080066check_impl(REAL_t *rn, double orig_dbl, const char *sample, const char *canonical_sample, int lineno) {
Lev Walkin41ba1f22004-09-14 12:46:35 +000067 double val;
68 uint8_t *p, *end;
69 int ret;
70
Lev Walkin5fda7d52012-01-09 03:20:19 -080071 printf("Line %d: double value %.12f [", lineno, orig_dbl);
Lev Walkin41ba1f22004-09-14 12:46:35 +000072 for(p = (uint8_t *)&orig_dbl, end = p + sizeof(double); p < end ; p++)
73 printf("%02x", *p);
74 printf("] (ilogb %d)\n", ilogb(orig_dbl));
75
76 val = frexp(orig_dbl, &ret);
77 printf("frexp(%f, %d): [", val, ret);
78 for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
79 printf("%02x", *p);
80 printf("]\n");
81
Lev Walkin5e033762004-09-29 13:26:15 +000082 ret = asn_double2REAL(rn, orig_dbl);
Lev Walkin41ba1f22004-09-14 12:46:35 +000083 assert(ret == 0);
84
85 printf("converted into [");
86 for(p = rn->buf, end = p + rn->size; p < end; p++)
87 printf("%02x", *p);
Lev Walkin2a789d92004-09-27 21:36:59 +000088 printf("]: %d\n", rn->size);
Lev Walkin41ba1f22004-09-14 12:46:35 +000089
Lev Walkin5e033762004-09-29 13:26:15 +000090 ret = asn_REAL2double(rn, &val);
Lev Walkin41ba1f22004-09-14 12:46:35 +000091 assert(ret == 0);
92
93 printf("and back to double: [");
94 for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
95 printf("%02x", *p);
96 printf("] (ilogb %d)\n", ilogb(val));
97
Lev Walkin1aea6982004-10-26 09:35:25 +000098 printf("%.12f vs %.12f\n", val, orig_dbl);
99 assert((isnan(orig_dbl) && isnan(val)) || val == orig_dbl);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000100 printf("OK\n");
Lev Walkined465432004-09-27 20:52:36 +0000101
Lev Walkin5fda7d52012-01-09 03:20:19 -0800102 check_str_representation(val, sample, canonical_sample, lineno);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000103}
Lev Walkin5f560912004-10-21 13:37:57 +0000104static void
105check_xer(int fuzzy, double orig_value) {
106 asn_enc_rval_t er;
107 asn_dec_rval_t rc;
108 REAL_t st;
109 REAL_t *newst0 = 0;
110 REAL_t *newst1 = 0;
Lev Walkinb1919382006-07-27 11:46:25 +0000111 REAL_t **newst0p = &newst0;
112 REAL_t **newst1p = &newst1;
Lev Walkin5f560912004-10-21 13:37:57 +0000113 double value0, value1;
114 int ret;
115
116 memset(&st, 0, sizeof(st));
117 ret = asn_double2REAL(&st, orig_value);
118 assert(ret == 0);
119
120 reconstr_lens[0] = 0;
121 reconstr_lens[1] = 0;
122 er = xer_encode(&asn_DEF_REAL, &st,
123 XER_F_BASIC, callback, 0);
124 assert(er.encoded == reconstr_lens[0]);
125 er = xer_encode(&asn_DEF_REAL, &st,
126 XER_F_CANONICAL, callback, (void *)1);
127 assert(er.encoded == reconstr_lens[1]);
128 reconstructed[0][reconstr_lens[0]] = 0;
129 reconstructed[1][reconstr_lens[1]] = 0;
130
131 printf("%f vs (%d)[%s] & (%d)%s",
132 orig_value,
133 reconstr_lens[1], reconstructed[1],
134 reconstr_lens[0], reconstructed[0]
135 );
136
Lev Walkinb1919382006-07-27 11:46:25 +0000137 rc = xer_decode(0, &asn_DEF_REAL, (void **)newst0p,
Lev Walkin5f560912004-10-21 13:37:57 +0000138 reconstructed[0], reconstr_lens[0]);
139 assert(rc.code == RC_OK);
Lev Walkin97363482016-01-24 19:23:02 -0800140 assert(reconstr_lens[0] > 0 && rc.consumed < (size_t)reconstr_lens[0]);
Lev Walkin5f560912004-10-21 13:37:57 +0000141
Lev Walkinb1919382006-07-27 11:46:25 +0000142 rc = xer_decode(0, &asn_DEF_REAL, (void **)newst1p,
Lev Walkin5f560912004-10-21 13:37:57 +0000143 reconstructed[1], reconstr_lens[1]);
144 assert(rc.code == RC_OK);
Lev Walkin97363482016-01-24 19:23:02 -0800145 assert(rc.consumed == (size_t)reconstr_lens[1]);
Lev Walkin5f560912004-10-21 13:37:57 +0000146
147 ret = asn_REAL2double(newst0, &value0);
148 assert(ret == 0);
149 ret = asn_REAL2double(newst1, &value1);
150 assert(ret == 0);
151
Lev Walkin1aea6982004-10-26 09:35:25 +0000152 assert((isnan(value0) && isnan(orig_value))
153 || value0 == orig_value
Lev Walkin5f560912004-10-21 13:37:57 +0000154 || fuzzy);
Lev Walkin1aea6982004-10-26 09:35:25 +0000155 assert((isnan(value1) && isnan(orig_value))
156 || value1 == orig_value);
Lev Walkin5f560912004-10-21 13:37:57 +0000157
158 assert(newst0->size == st.size || fuzzy);
159 assert(newst1->size == st.size);
160 assert(fuzzy || memcmp(newst0->buf, st.buf, st.size) == 0);
161 assert(memcmp(newst1->buf, st.buf, st.size) == 0);
162}
163
Lev Walkin5fe72e92012-01-07 17:00:29 -0800164static void
Lev Walkin5fda7d52012-01-09 03:20:19 -0800165check_ber_buffer_twoway(double d, const char *sample, const char *canonical_sample, uint8_t *inbuf, size_t insize, uint8_t *outbuf, size_t outsize, int lineno) {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800166 REAL_t rn;
167 double val;
168 int ret;
169
170 /*
171 * Decode our expected buffer and check that it matches the given (d).
172 */
173 rn.buf = inbuf;
174 rn.size = insize;
175 asn_REAL2double(&rn, &val);
176 if(isnan(val)) assert(isnan(d));
177 if(isnan(d)) assert(isnan(val));
178 if(!isnan(val) && !isnan(d)) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800179 assert(copysign(1.0, d) == copysign(1.0, val));
180 assert(d == val);
181 }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800182
183 /*
184 * Encode value and check that it matches our expected buffer.
185 */
186 memset(&rn, 0, sizeof(rn));
187 ret = asn_double2REAL(&rn, d);
188 assert(ret == 0);
Lev Walkin97363482016-01-24 19:23:02 -0800189 if((size_t)rn.size != outsize) {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800190 printf("Encoded %f into %d expected %ld\n",
191 d, (int)rn.size, outsize);
Lev Walkin97363482016-01-24 19:23:02 -0800192 assert((size_t)rn.size == outsize);
Lev Walkin5fe72e92012-01-07 17:00:29 -0800193 }
194 assert(memcmp(rn.buf, outbuf, rn.size) == 0);
195
Lev Walkin5fda7d52012-01-09 03:20:19 -0800196 check_str_representation(d, sample, canonical_sample, lineno);
Lev Walkin5fe72e92012-01-07 17:00:29 -0800197}
198
199static void
Lev Walkin5fda7d52012-01-09 03:20:19 -0800200check_ber_buffer_oneway(double d, const char *sample, const char *canonical_sample, uint8_t *buf, size_t bufsize, int lineno) {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800201 REAL_t rn;
202 double val;
203 uint8_t *p, *end;
204 int ret;
205
206 memset(&rn, 0, sizeof(rn));
207
208 printf("verify double value %.12f [", d);
209 for(p = (uint8_t *)&d, end = p + sizeof(double); p < end ; p++)
210 printf("%02x", *p);
211 printf("] (ilogb %d)\n", ilogb(d));
212
213
214 ret = asn_double2REAL(&rn, d);
215 assert(ret == 0);
216
217 printf("canonical DER: [");
218 for(p = rn.buf, end = p + rn.size; p < end; p++)
219 printf("%02x", *p);
220 printf("]\n");
221
222 rn.buf = buf;
223 rn.size = bufsize;
224
225 printf("received as: [");
226 for(p = rn.buf, end = p + rn.size; p < end; p++)
227 printf("%02x", *p);
228 printf("]\n");
229
230 ret = asn_REAL2double(&rn, &val);
231 assert(ret == 0);
232
233 printf("%.12f vs %.12f\n", d, val);
234
235 assert(val == d);
236
Lev Walkin5fda7d52012-01-09 03:20:19 -0800237 check_str_representation(val, sample, canonical_sample, lineno);
Lev Walkin5fe72e92012-01-07 17:00:29 -0800238}
239
Lev Walkine8727ec2012-01-09 05:46:16 -0800240/*
241 * 8.5.7 Verify binary encoding, two-way.
242 */
243static void
244check_ber_857_encoding(int base, int sign, int scaling_factor, int exponent, int mantissa) {
245 uint8_t buf[100];
246 uint8_t *b = buf;
247 int explen, mantlen;
248 REAL_t rn;
249 static REAL_t rn_check;
250 double d;
251 double verify;
252 int baseF = 0;
253 int ret;
254
255#define BIT(b) (1<<(b - 1))
256
257 switch(base) {
258 case 0: baseF = 1; break;
259 case 1: baseF = 3; break;
260 case 2: baseF = 4; break;
261 default: assert(base >= 0 && base <= 2);
262 }
263
264 if(exponent >= -128 && exponent <= 127) {
265 explen = 1;
266 } else {
267 assert(exponent > -60000 && exponent < 60000);
268 explen = 2;
269 }
270
271 if(mantissa == 0) {
272 mantlen = 0;
273 } else if(mantissa >= 0 && mantissa <= 255) {
274 mantlen = 1;
275 } else if(mantissa >= 0 && mantissa <= 65535) {
276 mantlen = 2;
277 } else {
278 assert(mantissa >= 0 && mantissa <= 256 * 65536);
279 mantlen = 3;
280 }
281
282 *b = BIT(8) | (sign ? BIT(7) : 0);
283 *b |= (base & 0x03) << 4; /* 8.5.7.2 */
284 *b |= (scaling_factor & 0x03) << 2; /* 8.5.7.3 */
285 *b |= ((explen - 1) & 0x03); /* 8.5.7.4 */
286 b++;
287 switch(explen) {
288 case 2: *b++ = (int8_t)(exponent >> 8);
289 case 1: *b++ = (int8_t)exponent;
290 }
291 switch(mantlen) {
292 case 3: *b++ = (mantissa >> 16) & 0xff;
293 case 2: *b++ = (mantissa >> 8) & 0xff;
294 case 1: *b++ = (mantissa & 0xff);
295 }
296
297 verify = (sign ? -1.0 : 1.0) * ldexp(mantissa, exponent * baseF + scaling_factor);
298
299 /* Verify than encoding of this double value round-trips */
300 if(!isinf(verify)) {
301 d = verify;
302 verify = 0.0;
303 ret = asn_double2REAL(&rn_check, d);
304 assert(ret == 0);
305 ret = asn_REAL2double(&rn_check, &verify);
306 assert(ret == 0);
307 assert(d == verify);
308
309 /* Verify with a slight non-friendly offset. Not too easy. */
310 d = verify - 0.13;
311 verify = 0.0;
312 ret = asn_double2REAL(&rn_check, d);
313 assert(ret == 0);
314 ret = asn_REAL2double(&rn_check, &verify);
315 assert(ret == 0);
316 assert(ret == 0);
317 assert(d == verify);
318 }
319
320 verify = (sign ? -1.0 : 1.0) * ldexp(mantissa, exponent * baseF + scaling_factor);
321
322 rn.buf = buf;
323 rn.size = b - buf;
324 ret = asn_REAL2double(&rn, &d);
325 if(!isinf(verify) && (ret != 0 || d != verify)) {
326 printf("Converting B=%d, S=%d, F=%d, E=%d/%d, M=%d/%d\n", (1 << baseF), sign, scaling_factor, exponent, explen, mantissa, mantlen);
327 printf("Verify: %e\n", verify);
328 uint8_t *p;
329 printf("received as: [");
330 for(p = buf; p < b; p++) printf("%02x", *p);
331 printf("]\n");
332 assert(ret == 0);
333 printf("Converted: %e\n", d);
334 assert(d == verify);
335 }
336}
Lev Walkin5fe72e92012-01-07 17:00:29 -0800337
338static void
339check_ber_encoding() {
340 static const double zero = 0.0;
341
Lev Walkin5fda7d52012-01-09 03:20:19 -0800342#define CHECK_BER_STRICT(v, nocan, can, inbuf, outbuf) \
343 check_ber_buffer_twoway(v, nocan, can, inbuf, sizeof(inbuf), \
344 outbuf, sizeof(outbuf), __LINE__)
Lev Walkin5fe72e92012-01-07 17:00:29 -0800345
346#define CHECK_BER_NONSTRICT(v, nocan, can, buf) \
Lev Walkin5fda7d52012-01-09 03:20:19 -0800347 check_ber_buffer_oneway(v, nocan, can, buf, sizeof(buf), __LINE__)
Lev Walkin5fe72e92012-01-07 17:00:29 -0800348
349 /*
350 * X.690 8.4 Encoding of an enumerated value.
351 */
352
353 /* 8.5.2 If the real value is the value plus zero,
354 * there shall be no contents octet in the encoding */
355 { uint8_t b_0[] = {};
Lev Walkin5fda7d52012-01-09 03:20:19 -0800356 CHECK_BER_STRICT(0.0, "0", "0", b_0, b_0); }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800357
358 /* 8.5.3 When -0 is to be encoded, there shall be only one contents octet */
359 { uint8_t b_m0[] = { 0x43 };
Lev Walkin5fda7d52012-01-09 03:20:19 -0800360 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0, b_m0); }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800361
362 /* Old way of encoding -0.0: 8.5.6 a) */
363 { uint8_t b_m0[] = { 0x43 };
364 uint8_t b_m0_856a[] = { 0xC0, 0x00 }; /* #8.5.6 a) */
365 uint8_t b_m0_856a_1[] = { 0xC0, 0x00, 0x00 };
366 uint8_t b_m0_856a_2[] = { 0xC0, 0x00, 0x00, 0x00 };
367 uint8_t b_m0_856a_3[] = { 0xC0, 0x00, 0x00, 0x00, 0x00 };
368 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a, b_m0);
369 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a_1, b_m0);
370 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a_2, b_m0);
Lev Walkin5fda7d52012-01-09 03:20:19 -0800371 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a_3, b_m0); }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800372
373 /* 8.5.6 c) => 8.5.9 SpecialRealValue */
374 { uint8_t b_pinf[] = { 0x40 };
375 uint8_t b_minf[] = { 0x41 };
376 uint8_t b_nan[] = { 0x42 };
377 CHECK_BER_STRICT(1.0/zero, "<PLUS-INFINITY/>", "<PLUS-INFINITY/>", b_pinf, b_pinf);
378 CHECK_BER_STRICT(-1.0/zero, "<MINUS-INFINITY/>", "<MINUS-INFINITY/>", b_minf, b_minf);
Lev Walkin5fda7d52012-01-09 03:20:19 -0800379 CHECK_BER_STRICT(zero/zero, "<NOT-A-NUMBER/>", "<NOT-A-NUMBER/>", b_nan, b_nan); }
380
381 /* 8.5.6 b) => 8.5.8 Decimal encoding is used; NR1 form */
382 { uint8_t b_0_nr1[] = { 0x01, '0' };
383 uint8_t b_0[] = { };
384 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr1, b_0); }
385 { uint8_t b_0_nr1[] = { 0x01, '0', '0' };
386 uint8_t b_0[] = { };
387 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr1, b_0); }
388 { uint8_t b_0_nr1[] = { 0x01, ' ', '0' };
389 uint8_t b_0[] = { };
390 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr1, b_0); }
391 { uint8_t b_p0_nr1[] = { 0x01, '+', '0' };
392 uint8_t b_0[] = { };
393 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr1, b_0); }
394 { uint8_t b_p0_nr1[] = { 0x01, ' ', '+', '0' };
395 uint8_t b_0[] = { };
396 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr1, b_0); }
397 { uint8_t b_m0_nr1[] = { 0x01, '-', '0' };
398 uint8_t b_m0[] = { 0x43 };
399 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr1, b_m0); }
400 { uint8_t b_m0_nr1[] = { 0x01, ' ', '-', '0' };
401 uint8_t b_m0[] = { 0x43 };
402 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr1, b_m0); }
403
404 { uint8_t b_1_nr1[] = { 0x01, '1' };
405 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
406 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr1, b_1); }
407 { uint8_t b_1_nr1[] = { 0x01, '0', '1' };
408 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
409 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr1, b_1); }
410 { uint8_t b_1_nr1[] = { 0x01, ' ', '1' };
411 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
412 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr1, b_1); }
413 { uint8_t b_p1_nr1[] = { 0x01, '+', '1' };
414 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
415 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr1, b_1); }
416 { uint8_t b_p1_nr1[] = { 0x01, ' ', '+', '1' };
417 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
418 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr1, b_1); }
419 { uint8_t b_m1_nr1[] = { 0x01, '-', '1' };
420 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
421 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr1, b_m1); }
422 { uint8_t b_m1_nr1[] = { 0x01, ' ', '-', '1' };
423 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
424 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr1, b_m1); }
425
426 {
427 uint8_t comma_symbol[] = { '.', ',' };
428 int csi;
429 for(csi = 0; csi < 2; csi++) {
430 uint8_t CS = comma_symbol[csi];
431
432 /* 8.5.6 b) => 8.5.8 Decimal encoding is used; NR2 form */
433 { uint8_t b_0_nr2[] = { 0x02, '0', CS, '0' };
434 uint8_t b_0[] = { };
435 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr2, b_0); }
436 { uint8_t b_0_nr2[] = { 0x02, '0', '0', CS, '0' };
437 uint8_t b_0[] = { };
438 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr2, b_0); }
439 { uint8_t b_0_nr2[] = { 0x02, ' ', '0', CS, '0' };
440 uint8_t b_0[] = { };
441 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr2, b_0); }
442 { uint8_t b_p0_nr2[] = { 0x02, '+', '0', CS, '0' };
443 uint8_t b_0[] = { };
444 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr2, b_0); }
445 { uint8_t b_p0_nr2[] = { 0x02, ' ', '+', '0', CS, '0' };
446 uint8_t b_0[] = { };
447 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr2, b_0); }
448 { uint8_t b_m0_nr2[] = { 0x02, '-', '0', CS, '0' };
449 uint8_t b_m0[] = { 0x43 };
450 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr2, b_m0); }
451 { uint8_t b_m0_nr2[] = { 0x02, ' ', '-', '0', CS, '0' };
452 uint8_t b_m0[] = { 0x43 };
453 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr2, b_m0); }
454
455 /* 8.5.6 b) => 8.5.8 NR2 "1." */
456 { uint8_t b_1_nr2[] = { 0x02, '1', CS };
457 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
458 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr2, b_1); }
459 { uint8_t b_1_nr2[] = { 0x02, '0', '1', CS };
460 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
461 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr2, b_1); }
462 { uint8_t b_1_nr2[] = { 0x02, ' ', '1', CS };
463 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
464 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr2, b_1); }
465 { uint8_t b_p1_nr2[] = { 0x02, '+', '1', CS };
466 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
467 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr2, b_1); }
468 { uint8_t b_p1_nr2[] = { 0x02, ' ', '+', '1', CS };
469 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
470 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr2, b_1); }
471 { uint8_t b_m1_nr2[] = { 0x02, '-', '1', CS };
472 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
473 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr2, b_m1); }
474 { uint8_t b_m1_nr2[] = { 0x02, ' ', '-', '1', CS };
475 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
476 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr2, b_m1); }
477
478 /* 8.5.6 b) => 8.5.8 NR2 ".5" */
479 { uint8_t b_05_nr2[] = { 0x02, CS, '5' };
480 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
481 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_05_nr2, b_05); }
482 { uint8_t b_05_nr2[] = { 0x02, '0', CS, '5' };
483 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
484 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_05_nr2, b_05); }
485 { uint8_t b_05_nr2[] = { 0x02, ' ', CS, '5' };
486 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
487 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_05_nr2, b_05); }
488 { uint8_t b_p1_nr2[] = { 0x02, '+', CS, '5' };
489 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
490 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p1_nr2, b_05); }
491 { uint8_t b_p1_nr2[] = { 0x02, ' ', '+', CS, '5' };
492 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
493 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p1_nr2, b_05); }
494 { uint8_t b_m05_nr2[] = { 0x02, '-', CS, '5' };
495 uint8_t b_m05[] = { 0xC0, 0xff, 0x01 };
496 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m05_nr2, b_m05); }
497 { uint8_t b_m05_nr2[] = { 0x02, ' ', '-', CS, '5' };
498 uint8_t b_m05[] = { 0xC0, 0xff, 0x01 };
499 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m05_nr2, b_m05); }
500
501 /* 8.5.6 b) => 8.5.8 Decimal encoding is used; NR3 form */
502 { uint8_t b_0_nr3[] = { 0x03, '0', CS, '0', 'e', '0' };
503 uint8_t b_0[] = { };
504 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr3, b_0); }
505 { uint8_t b_0_nr3[] = { 0x03, '0', '0', CS, '0', 'E', '0' };
506 uint8_t b_0[] = { };
507 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr3, b_0); }
508 { uint8_t b_0_nr3[] = { 0x03, ' ', '0', CS, '0', 'e', '0' };
509 uint8_t b_0[] = { };
510 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr3, b_0); }
511 { uint8_t b_p0_nr3[] = { 0x03, '+', '0', CS, '0', 'E', '+', '0' };
512 uint8_t b_0[] = { };
513 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr3, b_0); }
514 { uint8_t b_p0_nr3[] = { 0x03, ' ', '+', '0', CS, '0', 'e', '+', '0' };
515 uint8_t b_0[] = { };
516 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr3, b_0); }
517 { uint8_t b_m0_nr3[] = { 0x03, '-', '0', CS, '0', 'E', '-', '0' };
518 uint8_t b_m0[] = { 0x43 };
519 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr3, b_m0); }
520 { uint8_t b_m0_nr3[] = { 0x03, ' ', '-', '0', CS, '0', 'e', '-', '0' };
521 uint8_t b_m0[] = { 0x43 };
522 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr3, b_m0); }
523
524 /* 8.5.6 b) => 8.5.8 NR3 "5.e-1" */
525 { uint8_t b_5_nr3[] = { 0x03, '5', CS, 'e', '-', '1' };
526 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
527 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_5_nr3, b_5); }
528 { uint8_t b_5_nr3[] = { 0x03, '0', '5', CS, 'E', '-', '1' };
529 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
530 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_5_nr3, b_5); }
531 { uint8_t b_5_nr3[] = { 0x03, ' ', '5', CS, 'e', '-', '1' };
532 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
533 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_5_nr3, b_5); }
534 { uint8_t b_p5_nr3[] = { 0x03, '+', '5', CS, 'E', '-', '1' };
535 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
536 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p5_nr3, b_5); }
537 { uint8_t b_p5_nr3[] = { 0x03, ' ', '+', '5', CS, 'e', '-', '1' };
538 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
539 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p5_nr3, b_5); }
540 { uint8_t b_m5_nr3[] = { 0x03, '-', '5', CS, 'E', '-', '1' };
541 uint8_t b_m5[] = { 0xC0, 0xff, 0x01 };
542 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m5_nr3, b_m5); }
543 { uint8_t b_m5_nr3[] = { 0x03, ' ', '-', '5', CS, 'e', '-', '1' };
544 uint8_t b_m5[] = { 0xC0, 0xff, 0x01 };
545 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m5_nr3, b_m5); }
546
547 /* 8.5.6 b) => 8.5.8 NR3 ".5e1" */
548 { uint8_t b_05_nr3[] = { 0x03, CS, '5', 'e', '+', '1' };
549 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
550 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_05_nr3, b_05); }
551 { uint8_t b_05_nr3[] = { 0x03, '0', CS, '5', 'E', '+', '1'};
552 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
553 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_05_nr3, b_05); }
554 { uint8_t b_05_nr3[] = { 0x03, ' ', CS, '5', 'e', '1'};
555 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
556 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_05_nr3, b_05); }
557 { uint8_t b_p1_nr3[] = { 0x03, '+', CS, '5', 'E', '1' };
558 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
559 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_p1_nr3, b_05); }
560 { uint8_t b_p1_nr3[] = { 0x03, ' ', '+', CS, '5', 'e', '+', '1' };
561 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
562 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_p1_nr3, b_05); }
563 { uint8_t b_m05_nr3[] = { 0x03, '-', CS, '5', 'E', '+', '1' };
564 uint8_t b_m05[] = { 0xC0, 0x00, 0x05 };
565 CHECK_BER_STRICT(-5.0, "-5.0", "-5.0E0", b_m05_nr3, b_m05); }
566 { uint8_t b_m05_nr3[] = { 0x03, ' ', '-', CS, '5', 'e', '1' };
567 uint8_t b_m05[] = { 0xC0, 0x00, 0x05 };
568 CHECK_BER_STRICT(-5.0, "-5.0", "-5.0E0", b_m05_nr3, b_m05); }
569 } /* for(comma symbol) */
570 }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800571
Lev Walkine8727ec2012-01-09 05:46:16 -0800572 /* Scan through the range of bits, construct the valid base-2 numbers, and
573 * try two-way conversion with them */
574 {
575 int base, sign, scaling_factor, exponent, mantissa;
576 for(base = 0; base <= 2; base++) {
577 for(sign = 0; sign <= 1; sign++) {
578 for(scaling_factor = 0; scaling_factor <= 3; scaling_factor++) {
579 for(exponent = -1000; exponent < 1000; exponent += (exponent > -990 && exponent < 990) ? 100 : 1) {
580 for(mantissa = 0; mantissa < 66000; mantissa += (mantissa > 300 && mantissa < 65400) ? 100 : 1) {
581 check_ber_857_encoding(base, sign, scaling_factor, exponent, mantissa);
582 }
583 }
584 }
585 }
586 }
587 }
588
Lev Walkin5fe72e92012-01-07 17:00:29 -0800589 {
590 uint8_t b_1_0[] =
591 { 0x80, 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
592 uint8_t b_1_1[] =
593 { 0x80, 0xcc, 0x11, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a };
594 uint8_t b_3_14[] =
595 { 0x80, 0xcd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f };
596 uint8_t b_3_14_mo1[] =
597 { 0xC0, 0xc5, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3};
598 uint8_t b_3_14_mo2[] =
599 { 0x80, 0xbd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3,2};
600
601 CHECK_BER_NONSTRICT(1.0, "1.0", "1.0E0", b_1_0);
602 CHECK_BER_NONSTRICT(1.1, "1.1", "1.1E0", b_1_1);
603 CHECK_BER_NONSTRICT(3.14, "3.14", "3.14E0", b_3_14);
604 /* These two are very interesting! They check mantissa overflow! */
605 CHECK_BER_NONSTRICT(-3.14, "-3.14", "-3.14E0", b_3_14_mo1);
606 CHECK_BER_NONSTRICT(3.14, "3.14", "3.14E0", b_3_14_mo2);
607 }
608}
Lev Walkinf0b808d2005-04-25 21:08:25 +0000609
Lev Walkin41ba1f22004-09-14 12:46:35 +0000610int
611main() {
612 REAL_t rn;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000613 static const double zero = 0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000614 memset(&rn, 0, sizeof(rn));
615
Lev Walkin5fe72e92012-01-07 17:00:29 -0800616 check_ber_encoding();
617
Lev Walkined465432004-09-27 20:52:36 +0000618 check(&rn, 0.0, "0", "0");
619 check(&rn, -0.0, "-0", "-0"); /* minus-zero */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000620 check(&rn, zero/zero, "<NOT-A-NUMBER/>", "<NOT-A-NUMBER/>");
621 check(&rn, 1.0/zero, "<PLUS-INFINITY/>", "<PLUS-INFINITY/>");
622 check(&rn, -1.0/zero, "<MINUS-INFINITY/>", "<MINUS-INFINITY/>");
Lev Walkined465432004-09-27 20:52:36 +0000623 check(&rn, 1.0, "1.0", "1.0E0");
624 check(&rn, -1.0, "-1.0", "-1.0E0");
Lev Walkined465432004-09-27 20:52:36 +0000625 check(&rn, 0.1, "0.1", "1.0E-1");
Lev Walkinf0b808d2005-04-25 21:08:25 +0000626 check(&rn, 0.01, "0.01", "1.0E-2");
627 check(&rn, 0.02, "0.02", "2.0E-2");
628 check(&rn, 0.09, "0.09", "9.0E-2");
629 check(&rn, 1.5, "1.5", "1.5E0");
Lev Walkined465432004-09-27 20:52:36 +0000630 check(&rn, 0.33333, "0.33333", "3.3333E-1");
631 check(&rn, 2, "2.0", "2.0E0");
632 check(&rn, 2.1, "2.1", "2.1E0");
633 check(&rn, 3, "3.0", "3.0E0");
634 check(&rn, 3.1, "3.1", "3.1E0");
635 check(&rn, 3.14, "3.14", "3.14E0");
636 check(&rn, 3.1415, "3.1415", "3.1415E0");
637 check(&rn, 3.141592, "3.141592", "3.141592E0");
638 check(&rn, 3.14159265, "3.14159265", "3.14159265E0");
639 check(&rn, -3.14159265, "-3.14159265", "-3.14159265E0");
640 check(&rn, 14159265.0, "14159265.0", "1.4159265E7");
641 check(&rn, -123456789123456789.0, "-123456789123456784.0", "-1.234567891234568E17");
Lev Walkinf0b808d2005-04-25 21:08:25 +0000642 check(&rn, 0.00000000001, "0.00000000001", "9.999999999999999E-12");
643 check(&rn, 0.00000000002, "0.00000000002", "2.0E-11");
644 check(&rn, 0.00000000009, "0.00000000009", "9.0E-11");
645 check(&rn, 0.000000000002, "0.000000000002", "2.0E-12");
646 check(&rn, 0.0000000000002, "0.0000000000002", "2.0E-13");
647 check(&rn, 0.00000000000002, "0.00000000000002", "2.0E-14");
648 check(&rn, 0.000000000000002, "0.000000000000002", "2.0E-15");
649 check(&rn, 0.0000000000000002, "0.0", "2.0E-16");
Lev Walkined465432004-09-27 20:52:36 +0000650 check(&rn, 0.0000000000000000000001, "0.0", "1.0E-22");
651 check(&rn, 0.000000000000000000000000000001, "0.0", "1.0E-30"); /* proved 2B a problem */
652 check(&rn,-0.000000000000000000000000000001, "-0.0", "-1.0E-30"); /* proved 2B a problem */
653 check(&rn, 0.0000000000010000000001000000000001, 0, 0);
654 check(&rn, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
655 check(&rn, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
656 check(&rn,-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
657 check(&rn,-3.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
658 check(&rn, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
Lev Walkin8ca13c82016-01-24 22:40:00 -0800659 check(&rn, 0.25, "0.25", "2.5E-1");
660 check(&rn, -0.25, "-0.25", "-2.5E-1");
661 check(&rn, 0.03, "0.03", "3.0E-2");
662 check(&rn, -0.03, "-0.03", "-3.0E-2");
663
664 check(&rn, 4.01E-50, "0.0", "4.01E-50");
665 check(&rn, -4.01E-50, "-0.0", "-4.01E-50");
666 check(&rn, -4.9406564584124654E-324, "-0.0", "-4.940656458412465E-324"); /* MIN */
667 check(&rn, DBL_MIN, "0.0", "2.225073858507201E-308"); /* MIN */
668 check(&rn, -DBL_MIN, "-0.0", "-2.225073858507201E-308"); /* -MIN */
669 check(&rn, DBL_MAX, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0", "1.797693134862316E308"); /* MAX */
670 check(&rn, -DBL_MAX, "-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0", "-1.797693134862316E308"); /* MAX */
671 check(&rn, -DBL_TRUE_MIN, "-0.0", "-4.940656458412465E-324"); /* subnorm */
672 check(&rn, DBL_TRUE_MIN, "0.0", "4.940656458412465E-324"); /* subnorm */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000673
Lev Walkin5f560912004-10-21 13:37:57 +0000674
Lev Walkin1aea6982004-10-26 09:35:25 +0000675#ifdef NAN
676 check_xer(0, NAN); /* "<NOT-A-NUMBER/>" */
677#else
Lev Walkin5f560912004-10-21 13:37:57 +0000678 check_xer(0, zero/zero); /* "<NOT-A-NUMBER/>" */
Lev Walkin1aea6982004-10-26 09:35:25 +0000679#endif
680#ifdef INFINITY
681 check_xer(0, INFINITY); /* "<PLUS-INFINITY/>" */
682 check_xer(0, -INFINITY); /* "<MINUS-INFINITY/>" */
683#else
Lev Walkin5f560912004-10-21 13:37:57 +0000684 check_xer(0, 1.0/zero); /* "<PLUS-INFINITY/>" */
685 check_xer(0, -1.0/zero); /* "<MINUS-INFINITY/>" */
Lev Walkin1aea6982004-10-26 09:35:25 +0000686#endif
Lev Walkin5f560912004-10-21 13:37:57 +0000687 check_xer(0, 1.0);
688 check_xer(0, -1.0);
689 check_xer(0, 1.5);
690 check_xer(0, 123);
691 check_xer(1, 0.0000000000000000000001);
692 check_xer(1, -0.0000000000000000000001);
693
Lev Walkin41ba1f22004-09-14 12:46:35 +0000694 return 0;
695}