blob: 9e32c40aa96b1fb7cbbe2c100a3a826bdceb5df4 [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 Walkin681f0592016-03-14 04:21:26 -07006/* C11 specifies DBL_TRUE_MIN, might not be immediately available. */
7#ifndef DBL_TRUE_MIN
8#define DBL_TRUE_MIN 4.9406564584124654E-324
9#endif
10
Lev Walkin4eceeba2007-07-23 06:48:26 +000011#include <REAL.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000012
Lev Walkined465432004-09-27 20:52:36 +000013static char reconstructed[2][512];
14static int reconstr_lens[2];
15
16static int
17callback(const void *buffer, size_t size, void *app_key) {
18 char *buf = reconstructed[app_key ? 1 : 0];
19 int *len = &reconstr_lens[app_key ? 1 : 0];
20
21 if(*len + size >= sizeof(reconstructed[0]))
22 return -1;
23
24 memcpy(buf + *len, buffer, size);
25 *len += size;
26
27 return 0;
28}
29
Lev Walkin833d2752012-01-07 15:29:25 -080030static char *
Lev Walkin97363482016-01-24 19:23:02 -080031d2s(double d, int canonical) {
Lev Walkin833d2752012-01-07 15:29:25 -080032 ssize_t s;
33
34 reconstr_lens[canonical] = 0;
35 s = REAL__dump(d, canonical, callback, (void *)(ptrdiff_t)canonical);
Lev Walkin97363482016-01-24 19:23:02 -080036 assert(s > 0 && (size_t)s < sizeof(reconstructed[canonical]));
Lev Walkin833d2752012-01-07 15:29:25 -080037 assert(s == reconstr_lens[canonical]);
38 reconstructed[canonical][s] = '\0'; // ASCIIZ
39 return reconstructed[canonical];
40}
41
42/*
43 * Verify that a string representation of a given floating point value
44 * is as given in the (sample) and (canonical_sample) arguments.
45 */
Lev Walkin41ba1f22004-09-14 12:46:35 +000046static void
Lev Walkin5fda7d52012-01-09 03:20:19 -080047check_str_representation(double d, const char *sample, const char *canonical_sample, int lineno) {
Lev Walkin833d2752012-01-07 15:29:25 -080048 char *s0, *s1;
Lev Walkined465432004-09-27 20:52:36 +000049
Lev Walkin97363482016-01-24 19:23:02 -080050 s0 = d2s(d, 0);
51 s1 = d2s(d, 1);
Lev Walkined465432004-09-27 20:52:36 +000052
53 if(sample) {
Lev Walkin8ca13c82016-01-24 22:40:00 -080054 printf("%03d: Checking %g->[\"%s\"] against [\"%s\"]%s\n",
Lev Walkin5fda7d52012-01-09 03:20:19 -080055 lineno, d, s0, sample,
Lev Walkinf0b808d2005-04-25 21:08:25 +000056 canonical_sample ? " (canonical follows...)" : ""
57 );
Lev Walkin833d2752012-01-07 15:29:25 -080058 assert(!strcmp(s0, sample));
Lev Walkined465432004-09-27 20:52:36 +000059 }
60 if(canonical_sample) {
Lev Walkin8ca13c82016-01-24 22:40:00 -080061 printf("%03d: Checking %g->[\"%s\"] against [\"%s\"] (canonical)\n",
Lev Walkin5fda7d52012-01-09 03:20:19 -080062 lineno, d, s1, canonical_sample);
Lev Walkin833d2752012-01-07 15:29:25 -080063 assert(!strcmp(s1, canonical_sample));
Lev Walkined465432004-09-27 20:52:36 +000064 }
65}
66
Lev Walkin5fe72e92012-01-07 17:00:29 -080067#define check(rn, d, str1, str2) \
68 check_impl(rn, d, str1, str2, __LINE__)
69
Lev Walkined465432004-09-27 20:52:36 +000070static void
Lev Walkin5fda7d52012-01-09 03:20:19 -080071check_impl(REAL_t *rn, double orig_dbl, const char *sample, const char *canonical_sample, int lineno) {
Lev Walkin41ba1f22004-09-14 12:46:35 +000072 double val;
73 uint8_t *p, *end;
74 int ret;
75
Lev Walkin5fda7d52012-01-09 03:20:19 -080076 printf("Line %d: double value %.12f [", lineno, orig_dbl);
Lev Walkin41ba1f22004-09-14 12:46:35 +000077 for(p = (uint8_t *)&orig_dbl, end = p + sizeof(double); p < end ; p++)
78 printf("%02x", *p);
79 printf("] (ilogb %d)\n", ilogb(orig_dbl));
80
81 val = frexp(orig_dbl, &ret);
82 printf("frexp(%f, %d): [", val, ret);
83 for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
84 printf("%02x", *p);
85 printf("]\n");
86
Lev Walkin5e033762004-09-29 13:26:15 +000087 ret = asn_double2REAL(rn, orig_dbl);
Lev Walkin41ba1f22004-09-14 12:46:35 +000088 assert(ret == 0);
89
90 printf("converted into [");
91 for(p = rn->buf, end = p + rn->size; p < end; p++)
92 printf("%02x", *p);
Lev Walkin494fb702017-08-07 20:07:00 -070093 printf("]: %zu\n", rn->size);
Lev Walkin41ba1f22004-09-14 12:46:35 +000094
Lev Walkin5e033762004-09-29 13:26:15 +000095 ret = asn_REAL2double(rn, &val);
Lev Walkin41ba1f22004-09-14 12:46:35 +000096 assert(ret == 0);
97
98 printf("and back to double: [");
99 for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
100 printf("%02x", *p);
101 printf("] (ilogb %d)\n", ilogb(val));
102
Lev Walkin1aea6982004-10-26 09:35:25 +0000103 printf("%.12f vs %.12f\n", val, orig_dbl);
104 assert((isnan(orig_dbl) && isnan(val)) || val == orig_dbl);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000105 printf("OK\n");
Lev Walkined465432004-09-27 20:52:36 +0000106
Lev Walkin5fda7d52012-01-09 03:20:19 -0800107 check_str_representation(val, sample, canonical_sample, lineno);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000108}
Lev Walkin5f560912004-10-21 13:37:57 +0000109static void
110check_xer(int fuzzy, double orig_value) {
111 asn_enc_rval_t er;
112 asn_dec_rval_t rc;
113 REAL_t st;
114 REAL_t *newst0 = 0;
115 REAL_t *newst1 = 0;
Lev Walkinb1919382006-07-27 11:46:25 +0000116 REAL_t **newst0p = &newst0;
117 REAL_t **newst1p = &newst1;
Lev Walkin5f560912004-10-21 13:37:57 +0000118 double value0, value1;
119 int ret;
120
121 memset(&st, 0, sizeof(st));
122 ret = asn_double2REAL(&st, orig_value);
123 assert(ret == 0);
124
125 reconstr_lens[0] = 0;
126 reconstr_lens[1] = 0;
127 er = xer_encode(&asn_DEF_REAL, &st,
128 XER_F_BASIC, callback, 0);
129 assert(er.encoded == reconstr_lens[0]);
130 er = xer_encode(&asn_DEF_REAL, &st,
131 XER_F_CANONICAL, callback, (void *)1);
132 assert(er.encoded == reconstr_lens[1]);
133 reconstructed[0][reconstr_lens[0]] = 0;
134 reconstructed[1][reconstr_lens[1]] = 0;
135
136 printf("%f vs (%d)[%s] & (%d)%s",
137 orig_value,
138 reconstr_lens[1], reconstructed[1],
139 reconstr_lens[0], reconstructed[0]
140 );
141
Lev Walkinb1919382006-07-27 11:46:25 +0000142 rc = xer_decode(0, &asn_DEF_REAL, (void **)newst0p,
Lev Walkin5f560912004-10-21 13:37:57 +0000143 reconstructed[0], reconstr_lens[0]);
144 assert(rc.code == RC_OK);
Lev Walkin97363482016-01-24 19:23:02 -0800145 assert(reconstr_lens[0] > 0 && rc.consumed < (size_t)reconstr_lens[0]);
Lev Walkin5f560912004-10-21 13:37:57 +0000146
Lev Walkinb1919382006-07-27 11:46:25 +0000147 rc = xer_decode(0, &asn_DEF_REAL, (void **)newst1p,
Lev Walkin5f560912004-10-21 13:37:57 +0000148 reconstructed[1], reconstr_lens[1]);
149 assert(rc.code == RC_OK);
Lev Walkin97363482016-01-24 19:23:02 -0800150 assert(rc.consumed == (size_t)reconstr_lens[1]);
Lev Walkin5f560912004-10-21 13:37:57 +0000151
152 ret = asn_REAL2double(newst0, &value0);
153 assert(ret == 0);
154 ret = asn_REAL2double(newst1, &value1);
155 assert(ret == 0);
156
Lev Walkin1aea6982004-10-26 09:35:25 +0000157 assert((isnan(value0) && isnan(orig_value))
158 || value0 == orig_value
Lev Walkin5f560912004-10-21 13:37:57 +0000159 || fuzzy);
Lev Walkin1aea6982004-10-26 09:35:25 +0000160 assert((isnan(value1) && isnan(orig_value))
161 || value1 == orig_value);
Lev Walkin5f560912004-10-21 13:37:57 +0000162
163 assert(newst0->size == st.size || fuzzy);
164 assert(newst1->size == st.size);
165 assert(fuzzy || memcmp(newst0->buf, st.buf, st.size) == 0);
166 assert(memcmp(newst1->buf, st.buf, st.size) == 0);
167}
168
Lev Walkin5fe72e92012-01-07 17:00:29 -0800169static void
Lev Walkin53e5ae62017-08-23 10:56:19 -0700170check_ber_buffer_twoway(double d, const char *sample, const char *canonical_sample, const uint8_t *inbuf, size_t insize, uint8_t *outbuf, size_t outsize, int lineno) {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800171 REAL_t rn;
172 double val;
173 int ret;
174
175 /*
176 * Decode our expected buffer and check that it matches the given (d).
177 */
Lev Walkin53e5ae62017-08-23 10:56:19 -0700178 rn.buf = calloc(1, insize + 1); /* By convention, buffers have extra \0 */
179 assert(rn.buf);
180 memcpy(rn.buf, inbuf, insize);
Lev Walkin5fe72e92012-01-07 17:00:29 -0800181 rn.size = insize;
182 asn_REAL2double(&rn, &val);
183 if(isnan(val)) assert(isnan(d));
184 if(isnan(d)) assert(isnan(val));
185 if(!isnan(val) && !isnan(d)) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800186 assert(copysign(1.0, d) == copysign(1.0, val));
187 assert(d == val);
188 }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800189
190 /*
191 * Encode value and check that it matches our expected buffer.
192 */
Lev Walkin53e5ae62017-08-23 10:56:19 -0700193 free(rn.buf);
Lev Walkin5fe72e92012-01-07 17:00:29 -0800194 memset(&rn, 0, sizeof(rn));
195 ret = asn_double2REAL(&rn, d);
196 assert(ret == 0);
Lev Walkin97363482016-01-24 19:23:02 -0800197 if((size_t)rn.size != outsize) {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800198 printf("Encoded %f into %d expected %ld\n",
199 d, (int)rn.size, outsize);
Lev Walkin97363482016-01-24 19:23:02 -0800200 assert((size_t)rn.size == outsize);
Lev Walkin5fe72e92012-01-07 17:00:29 -0800201 }
202 assert(memcmp(rn.buf, outbuf, rn.size) == 0);
203
Lev Walkin5fda7d52012-01-09 03:20:19 -0800204 check_str_representation(d, sample, canonical_sample, lineno);
Lev Walkin5fe72e92012-01-07 17:00:29 -0800205}
206
207static void
Lev Walkin5fda7d52012-01-09 03:20:19 -0800208check_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 -0800209 REAL_t rn;
210 double val;
211 uint8_t *p, *end;
212 int ret;
213
214 memset(&rn, 0, sizeof(rn));
215
216 printf("verify double value %.12f [", d);
217 for(p = (uint8_t *)&d, end = p + sizeof(double); p < end ; p++)
218 printf("%02x", *p);
219 printf("] (ilogb %d)\n", ilogb(d));
220
221
222 ret = asn_double2REAL(&rn, d);
223 assert(ret == 0);
224
225 printf("canonical DER: [");
226 for(p = rn.buf, end = p + rn.size; p < end; p++)
227 printf("%02x", *p);
228 printf("]\n");
229
230 rn.buf = buf;
231 rn.size = bufsize;
232
233 printf("received as: [");
234 for(p = rn.buf, end = p + rn.size; p < end; p++)
235 printf("%02x", *p);
236 printf("]\n");
237
238 ret = asn_REAL2double(&rn, &val);
239 assert(ret == 0);
240
241 printf("%.12f vs %.12f\n", d, val);
242
243 assert(val == d);
244
Lev Walkin5fda7d52012-01-09 03:20:19 -0800245 check_str_representation(val, sample, canonical_sample, lineno);
Lev Walkin5fe72e92012-01-07 17:00:29 -0800246}
247
Lev Walkine8727ec2012-01-09 05:46:16 -0800248/*
249 * 8.5.7 Verify binary encoding, two-way.
250 */
251static void
252check_ber_857_encoding(int base, int sign, int scaling_factor, int exponent, int mantissa) {
253 uint8_t buf[100];
254 uint8_t *b = buf;
255 int explen, mantlen;
256 REAL_t rn;
257 static REAL_t rn_check;
258 double d;
259 double verify;
260 int baseF = 0;
261 int ret;
262
263#define BIT(b) (1<<(b - 1))
264
265 switch(base) {
266 case 0: baseF = 1; break;
267 case 1: baseF = 3; break;
268 case 2: baseF = 4; break;
269 default: assert(base >= 0 && base <= 2);
270 }
271
272 if(exponent >= -128 && exponent <= 127) {
273 explen = 1;
274 } else {
275 assert(exponent > -60000 && exponent < 60000);
276 explen = 2;
277 }
278
279 if(mantissa == 0) {
280 mantlen = 0;
281 } else if(mantissa >= 0 && mantissa <= 255) {
282 mantlen = 1;
283 } else if(mantissa >= 0 && mantissa <= 65535) {
284 mantlen = 2;
285 } else {
286 assert(mantissa >= 0 && mantissa <= 256 * 65536);
287 mantlen = 3;
288 }
289
290 *b = BIT(8) | (sign ? BIT(7) : 0);
291 *b |= (base & 0x03) << 4; /* 8.5.7.2 */
292 *b |= (scaling_factor & 0x03) << 2; /* 8.5.7.3 */
293 *b |= ((explen - 1) & 0x03); /* 8.5.7.4 */
294 b++;
295 switch(explen) {
296 case 2: *b++ = (int8_t)(exponent >> 8);
297 case 1: *b++ = (int8_t)exponent;
298 }
299 switch(mantlen) {
300 case 3: *b++ = (mantissa >> 16) & 0xff;
301 case 2: *b++ = (mantissa >> 8) & 0xff;
302 case 1: *b++ = (mantissa & 0xff);
303 }
304
305 verify = (sign ? -1.0 : 1.0) * ldexp(mantissa, exponent * baseF + scaling_factor);
306
307 /* Verify than encoding of this double value round-trips */
308 if(!isinf(verify)) {
309 d = verify;
310 verify = 0.0;
311 ret = asn_double2REAL(&rn_check, d);
312 assert(ret == 0);
313 ret = asn_REAL2double(&rn_check, &verify);
314 assert(ret == 0);
315 assert(d == verify);
316
317 /* Verify with a slight non-friendly offset. Not too easy. */
318 d = verify - 0.13;
319 verify = 0.0;
320 ret = asn_double2REAL(&rn_check, d);
321 assert(ret == 0);
322 ret = asn_REAL2double(&rn_check, &verify);
323 assert(ret == 0);
324 assert(ret == 0);
325 assert(d == verify);
326 }
327
328 verify = (sign ? -1.0 : 1.0) * ldexp(mantissa, exponent * baseF + scaling_factor);
329
330 rn.buf = buf;
331 rn.size = b - buf;
332 ret = asn_REAL2double(&rn, &d);
333 if(!isinf(verify) && (ret != 0 || d != verify)) {
334 printf("Converting B=%d, S=%d, F=%d, E=%d/%d, M=%d/%d\n", (1 << baseF), sign, scaling_factor, exponent, explen, mantissa, mantlen);
335 printf("Verify: %e\n", verify);
336 uint8_t *p;
337 printf("received as: [");
338 for(p = buf; p < b; p++) printf("%02x", *p);
339 printf("]\n");
340 assert(ret == 0);
341 printf("Converted: %e\n", d);
342 assert(d == verify);
343 }
344}
Lev Walkin5fe72e92012-01-07 17:00:29 -0800345
346static void
347check_ber_encoding() {
348 static const double zero = 0.0;
349
Lev Walkin5fda7d52012-01-09 03:20:19 -0800350#define CHECK_BER_STRICT(v, nocan, can, inbuf, outbuf) \
351 check_ber_buffer_twoway(v, nocan, can, inbuf, sizeof(inbuf), \
352 outbuf, sizeof(outbuf), __LINE__)
Lev Walkin5fe72e92012-01-07 17:00:29 -0800353
354#define CHECK_BER_NONSTRICT(v, nocan, can, buf) \
Lev Walkin5fda7d52012-01-09 03:20:19 -0800355 check_ber_buffer_oneway(v, nocan, can, buf, sizeof(buf), __LINE__)
Lev Walkin5fe72e92012-01-07 17:00:29 -0800356
357 /*
358 * X.690 8.4 Encoding of an enumerated value.
359 */
360
361 /* 8.5.2 If the real value is the value plus zero,
362 * there shall be no contents octet in the encoding */
363 { uint8_t b_0[] = {};
Lev Walkin5fda7d52012-01-09 03:20:19 -0800364 CHECK_BER_STRICT(0.0, "0", "0", b_0, b_0); }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800365
366 /* 8.5.3 When -0 is to be encoded, there shall be only one contents octet */
367 { uint8_t b_m0[] = { 0x43 };
Lev Walkin5fda7d52012-01-09 03:20:19 -0800368 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0, b_m0); }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800369
370 /* Old way of encoding -0.0: 8.5.6 a) */
371 { uint8_t b_m0[] = { 0x43 };
372 uint8_t b_m0_856a[] = { 0xC0, 0x00 }; /* #8.5.6 a) */
373 uint8_t b_m0_856a_1[] = { 0xC0, 0x00, 0x00 };
374 uint8_t b_m0_856a_2[] = { 0xC0, 0x00, 0x00, 0x00 };
375 uint8_t b_m0_856a_3[] = { 0xC0, 0x00, 0x00, 0x00, 0x00 };
376 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a, b_m0);
377 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a_1, b_m0);
378 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a_2, b_m0);
Lev Walkin5fda7d52012-01-09 03:20:19 -0800379 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a_3, b_m0); }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800380
381 /* 8.5.6 c) => 8.5.9 SpecialRealValue */
382 { uint8_t b_pinf[] = { 0x40 };
383 uint8_t b_minf[] = { 0x41 };
384 uint8_t b_nan[] = { 0x42 };
385 CHECK_BER_STRICT(1.0/zero, "<PLUS-INFINITY/>", "<PLUS-INFINITY/>", b_pinf, b_pinf);
386 CHECK_BER_STRICT(-1.0/zero, "<MINUS-INFINITY/>", "<MINUS-INFINITY/>", b_minf, b_minf);
Lev Walkin5fda7d52012-01-09 03:20:19 -0800387 CHECK_BER_STRICT(zero/zero, "<NOT-A-NUMBER/>", "<NOT-A-NUMBER/>", b_nan, b_nan); }
388
389 /* 8.5.6 b) => 8.5.8 Decimal encoding is used; NR1 form */
390 { uint8_t b_0_nr1[] = { 0x01, '0' };
391 uint8_t b_0[] = { };
392 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr1, b_0); }
393 { uint8_t b_0_nr1[] = { 0x01, '0', '0' };
394 uint8_t b_0[] = { };
395 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr1, b_0); }
396 { uint8_t b_0_nr1[] = { 0x01, ' ', '0' };
397 uint8_t b_0[] = { };
398 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr1, b_0); }
399 { uint8_t b_p0_nr1[] = { 0x01, '+', '0' };
400 uint8_t b_0[] = { };
401 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr1, b_0); }
402 { uint8_t b_p0_nr1[] = { 0x01, ' ', '+', '0' };
403 uint8_t b_0[] = { };
404 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr1, b_0); }
405 { uint8_t b_m0_nr1[] = { 0x01, '-', '0' };
406 uint8_t b_m0[] = { 0x43 };
407 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr1, b_m0); }
408 { uint8_t b_m0_nr1[] = { 0x01, ' ', '-', '0' };
409 uint8_t b_m0[] = { 0x43 };
410 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr1, b_m0); }
411
412 { uint8_t b_1_nr1[] = { 0x01, '1' };
413 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
414 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr1, b_1); }
415 { uint8_t b_1_nr1[] = { 0x01, '0', '1' };
416 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
417 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr1, b_1); }
418 { uint8_t b_1_nr1[] = { 0x01, ' ', '1' };
419 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
420 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr1, b_1); }
421 { uint8_t b_p1_nr1[] = { 0x01, '+', '1' };
422 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
423 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr1, b_1); }
424 { uint8_t b_p1_nr1[] = { 0x01, ' ', '+', '1' };
425 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
426 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr1, b_1); }
427 { uint8_t b_m1_nr1[] = { 0x01, '-', '1' };
428 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
429 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr1, b_m1); }
430 { uint8_t b_m1_nr1[] = { 0x01, ' ', '-', '1' };
431 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
432 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr1, b_m1); }
433
434 {
435 uint8_t comma_symbol[] = { '.', ',' };
436 int csi;
437 for(csi = 0; csi < 2; csi++) {
438 uint8_t CS = comma_symbol[csi];
439
440 /* 8.5.6 b) => 8.5.8 Decimal encoding is used; NR2 form */
441 { uint8_t b_0_nr2[] = { 0x02, '0', CS, '0' };
442 uint8_t b_0[] = { };
443 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr2, b_0); }
444 { uint8_t b_0_nr2[] = { 0x02, '0', '0', CS, '0' };
445 uint8_t b_0[] = { };
446 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr2, b_0); }
447 { uint8_t b_0_nr2[] = { 0x02, ' ', '0', CS, '0' };
448 uint8_t b_0[] = { };
449 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr2, b_0); }
450 { uint8_t b_p0_nr2[] = { 0x02, '+', '0', CS, '0' };
451 uint8_t b_0[] = { };
452 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr2, b_0); }
453 { uint8_t b_p0_nr2[] = { 0x02, ' ', '+', '0', CS, '0' };
454 uint8_t b_0[] = { };
455 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr2, b_0); }
456 { uint8_t b_m0_nr2[] = { 0x02, '-', '0', CS, '0' };
457 uint8_t b_m0[] = { 0x43 };
458 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr2, b_m0); }
459 { uint8_t b_m0_nr2[] = { 0x02, ' ', '-', '0', CS, '0' };
460 uint8_t b_m0[] = { 0x43 };
461 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr2, b_m0); }
462
463 /* 8.5.6 b) => 8.5.8 NR2 "1." */
464 { uint8_t b_1_nr2[] = { 0x02, '1', CS };
465 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
466 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr2, b_1); }
467 { uint8_t b_1_nr2[] = { 0x02, '0', '1', CS };
468 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
469 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr2, b_1); }
470 { uint8_t b_1_nr2[] = { 0x02, ' ', '1', CS };
471 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
472 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr2, b_1); }
473 { uint8_t b_p1_nr2[] = { 0x02, '+', '1', CS };
474 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
475 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr2, b_1); }
476 { uint8_t b_p1_nr2[] = { 0x02, ' ', '+', '1', CS };
477 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
478 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr2, b_1); }
479 { uint8_t b_m1_nr2[] = { 0x02, '-', '1', CS };
480 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
481 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr2, b_m1); }
482 { uint8_t b_m1_nr2[] = { 0x02, ' ', '-', '1', CS };
483 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
484 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr2, b_m1); }
485
486 /* 8.5.6 b) => 8.5.8 NR2 ".5" */
487 { uint8_t b_05_nr2[] = { 0x02, CS, '5' };
488 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
489 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_05_nr2, b_05); }
490 { uint8_t b_05_nr2[] = { 0x02, '0', CS, '5' };
491 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
492 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_05_nr2, b_05); }
493 { uint8_t b_05_nr2[] = { 0x02, ' ', CS, '5' };
494 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
495 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_05_nr2, b_05); }
496 { uint8_t b_p1_nr2[] = { 0x02, '+', CS, '5' };
497 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
498 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p1_nr2, b_05); }
499 { uint8_t b_p1_nr2[] = { 0x02, ' ', '+', CS, '5' };
500 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
501 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p1_nr2, b_05); }
502 { uint8_t b_m05_nr2[] = { 0x02, '-', CS, '5' };
503 uint8_t b_m05[] = { 0xC0, 0xff, 0x01 };
504 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m05_nr2, b_m05); }
505 { uint8_t b_m05_nr2[] = { 0x02, ' ', '-', CS, '5' };
506 uint8_t b_m05[] = { 0xC0, 0xff, 0x01 };
507 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m05_nr2, b_m05); }
508
509 /* 8.5.6 b) => 8.5.8 Decimal encoding is used; NR3 form */
510 { uint8_t b_0_nr3[] = { 0x03, '0', CS, '0', 'e', '0' };
511 uint8_t b_0[] = { };
512 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr3, b_0); }
513 { uint8_t b_0_nr3[] = { 0x03, '0', '0', CS, '0', 'E', '0' };
514 uint8_t b_0[] = { };
515 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr3, b_0); }
516 { uint8_t b_0_nr3[] = { 0x03, ' ', '0', CS, '0', 'e', '0' };
517 uint8_t b_0[] = { };
518 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr3, b_0); }
519 { uint8_t b_p0_nr3[] = { 0x03, '+', '0', CS, '0', 'E', '+', '0' };
520 uint8_t b_0[] = { };
521 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr3, b_0); }
522 { uint8_t b_p0_nr3[] = { 0x03, ' ', '+', '0', CS, '0', 'e', '+', '0' };
523 uint8_t b_0[] = { };
524 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr3, b_0); }
525 { uint8_t b_m0_nr3[] = { 0x03, '-', '0', CS, '0', 'E', '-', '0' };
526 uint8_t b_m0[] = { 0x43 };
527 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr3, b_m0); }
528 { uint8_t b_m0_nr3[] = { 0x03, ' ', '-', '0', CS, '0', 'e', '-', '0' };
529 uint8_t b_m0[] = { 0x43 };
530 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr3, b_m0); }
531
532 /* 8.5.6 b) => 8.5.8 NR3 "5.e-1" */
533 { uint8_t b_5_nr3[] = { 0x03, '5', CS, 'e', '-', '1' };
534 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
535 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_5_nr3, b_5); }
536 { uint8_t b_5_nr3[] = { 0x03, '0', '5', CS, 'E', '-', '1' };
537 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
538 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_5_nr3, b_5); }
539 { uint8_t b_5_nr3[] = { 0x03, ' ', '5', CS, 'e', '-', '1' };
540 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
541 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_5_nr3, b_5); }
542 { uint8_t b_p5_nr3[] = { 0x03, '+', '5', CS, 'E', '-', '1' };
543 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
544 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p5_nr3, b_5); }
545 { uint8_t b_p5_nr3[] = { 0x03, ' ', '+', '5', CS, 'e', '-', '1' };
546 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
547 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p5_nr3, b_5); }
548 { uint8_t b_m5_nr3[] = { 0x03, '-', '5', CS, 'E', '-', '1' };
549 uint8_t b_m5[] = { 0xC0, 0xff, 0x01 };
550 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m5_nr3, b_m5); }
551 { uint8_t b_m5_nr3[] = { 0x03, ' ', '-', '5', CS, 'e', '-', '1' };
552 uint8_t b_m5[] = { 0xC0, 0xff, 0x01 };
553 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m5_nr3, b_m5); }
554
555 /* 8.5.6 b) => 8.5.8 NR3 ".5e1" */
556 { uint8_t b_05_nr3[] = { 0x03, CS, '5', 'e', '+', '1' };
557 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
558 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_05_nr3, b_05); }
559 { uint8_t b_05_nr3[] = { 0x03, '0', CS, '5', 'E', '+', '1'};
560 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
561 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_05_nr3, b_05); }
562 { uint8_t b_05_nr3[] = { 0x03, ' ', CS, '5', 'e', '1'};
563 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
564 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_05_nr3, b_05); }
565 { uint8_t b_p1_nr3[] = { 0x03, '+', CS, '5', 'E', '1' };
566 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
567 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_p1_nr3, b_05); }
568 { uint8_t b_p1_nr3[] = { 0x03, ' ', '+', CS, '5', 'e', '+', '1' };
569 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
570 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_p1_nr3, b_05); }
571 { uint8_t b_m05_nr3[] = { 0x03, '-', CS, '5', 'E', '+', '1' };
572 uint8_t b_m05[] = { 0xC0, 0x00, 0x05 };
573 CHECK_BER_STRICT(-5.0, "-5.0", "-5.0E0", b_m05_nr3, b_m05); }
574 { uint8_t b_m05_nr3[] = { 0x03, ' ', '-', CS, '5', 'e', '1' };
575 uint8_t b_m05[] = { 0xC0, 0x00, 0x05 };
576 CHECK_BER_STRICT(-5.0, "-5.0", "-5.0E0", b_m05_nr3, b_m05); }
577 } /* for(comma symbol) */
578 }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800579
Lev Walkine8727ec2012-01-09 05:46:16 -0800580 /* Scan through the range of bits, construct the valid base-2 numbers, and
581 * try two-way conversion with them */
582 {
583 int base, sign, scaling_factor, exponent, mantissa;
584 for(base = 0; base <= 2; base++) {
585 for(sign = 0; sign <= 1; sign++) {
586 for(scaling_factor = 0; scaling_factor <= 3; scaling_factor++) {
587 for(exponent = -1000; exponent < 1000; exponent += (exponent > -990 && exponent < 990) ? 100 : 1) {
588 for(mantissa = 0; mantissa < 66000; mantissa += (mantissa > 300 && mantissa < 65400) ? 100 : 1) {
589 check_ber_857_encoding(base, sign, scaling_factor, exponent, mantissa);
590 }
591 }
592 }
593 }
594 }
595 }
596
Lev Walkin5fe72e92012-01-07 17:00:29 -0800597 {
598 uint8_t b_1_0[] =
599 { 0x80, 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
600 uint8_t b_1_1[] =
601 { 0x80, 0xcc, 0x11, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a };
602 uint8_t b_3_14[] =
603 { 0x80, 0xcd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f };
604 uint8_t b_3_14_mo1[] =
605 { 0xC0, 0xc5, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3};
606 uint8_t b_3_14_mo2[] =
607 { 0x80, 0xbd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3,2};
608
609 CHECK_BER_NONSTRICT(1.0, "1.0", "1.0E0", b_1_0);
610 CHECK_BER_NONSTRICT(1.1, "1.1", "1.1E0", b_1_1);
611 CHECK_BER_NONSTRICT(3.14, "3.14", "3.14E0", b_3_14);
612 /* These two are very interesting! They check mantissa overflow! */
613 CHECK_BER_NONSTRICT(-3.14, "-3.14", "-3.14E0", b_3_14_mo1);
614 CHECK_BER_NONSTRICT(3.14, "3.14", "3.14E0", b_3_14_mo2);
615 }
616}
Lev Walkinf0b808d2005-04-25 21:08:25 +0000617
Lev Walkin41ba1f22004-09-14 12:46:35 +0000618int
619main() {
620 REAL_t rn;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000621 static const double zero = 0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000622 memset(&rn, 0, sizeof(rn));
623
Lev Walkin5fe72e92012-01-07 17:00:29 -0800624 check_ber_encoding();
625
Lev Walkined465432004-09-27 20:52:36 +0000626 check(&rn, 0.0, "0", "0");
627 check(&rn, -0.0, "-0", "-0"); /* minus-zero */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000628 check(&rn, zero/zero, "<NOT-A-NUMBER/>", "<NOT-A-NUMBER/>");
629 check(&rn, 1.0/zero, "<PLUS-INFINITY/>", "<PLUS-INFINITY/>");
630 check(&rn, -1.0/zero, "<MINUS-INFINITY/>", "<MINUS-INFINITY/>");
Lev Walkined465432004-09-27 20:52:36 +0000631 check(&rn, 1.0, "1.0", "1.0E0");
632 check(&rn, -1.0, "-1.0", "-1.0E0");
Lev Walkined465432004-09-27 20:52:36 +0000633 check(&rn, 0.1, "0.1", "1.0E-1");
Lev Walkinf0b808d2005-04-25 21:08:25 +0000634 check(&rn, 0.01, "0.01", "1.0E-2");
635 check(&rn, 0.02, "0.02", "2.0E-2");
636 check(&rn, 0.09, "0.09", "9.0E-2");
637 check(&rn, 1.5, "1.5", "1.5E0");
Lev Walkined465432004-09-27 20:52:36 +0000638 check(&rn, 0.33333, "0.33333", "3.3333E-1");
639 check(&rn, 2, "2.0", "2.0E0");
640 check(&rn, 2.1, "2.1", "2.1E0");
641 check(&rn, 3, "3.0", "3.0E0");
642 check(&rn, 3.1, "3.1", "3.1E0");
643 check(&rn, 3.14, "3.14", "3.14E0");
644 check(&rn, 3.1415, "3.1415", "3.1415E0");
645 check(&rn, 3.141592, "3.141592", "3.141592E0");
646 check(&rn, 3.14159265, "3.14159265", "3.14159265E0");
647 check(&rn, -3.14159265, "-3.14159265", "-3.14159265E0");
648 check(&rn, 14159265.0, "14159265.0", "1.4159265E7");
649 check(&rn, -123456789123456789.0, "-123456789123456784.0", "-1.234567891234568E17");
Lev Walkinf0b808d2005-04-25 21:08:25 +0000650 check(&rn, 0.00000000001, "0.00000000001", "9.999999999999999E-12");
651 check(&rn, 0.00000000002, "0.00000000002", "2.0E-11");
652 check(&rn, 0.00000000009, "0.00000000009", "9.0E-11");
653 check(&rn, 0.000000000002, "0.000000000002", "2.0E-12");
654 check(&rn, 0.0000000000002, "0.0000000000002", "2.0E-13");
655 check(&rn, 0.00000000000002, "0.00000000000002", "2.0E-14");
656 check(&rn, 0.000000000000002, "0.000000000000002", "2.0E-15");
657 check(&rn, 0.0000000000000002, "0.0", "2.0E-16");
Lev Walkined465432004-09-27 20:52:36 +0000658 check(&rn, 0.0000000000000000000001, "0.0", "1.0E-22");
659 check(&rn, 0.000000000000000000000000000001, "0.0", "1.0E-30"); /* proved 2B a problem */
660 check(&rn,-0.000000000000000000000000000001, "-0.0", "-1.0E-30"); /* proved 2B a problem */
661 check(&rn, 0.0000000000010000000001000000000001, 0, 0);
662 check(&rn, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
663 check(&rn, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
664 check(&rn,-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
665 check(&rn,-3.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
666 check(&rn, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
Lev Walkin8ca13c82016-01-24 22:40:00 -0800667 check(&rn, 0.25, "0.25", "2.5E-1");
668 check(&rn, -0.25, "-0.25", "-2.5E-1");
669 check(&rn, 0.03, "0.03", "3.0E-2");
670 check(&rn, -0.03, "-0.03", "-3.0E-2");
671
672 check(&rn, 4.01E-50, "0.0", "4.01E-50");
673 check(&rn, -4.01E-50, "-0.0", "-4.01E-50");
674 check(&rn, -4.9406564584124654E-324, "-0.0", "-4.940656458412465E-324"); /* MIN */
675 check(&rn, DBL_MIN, "0.0", "2.225073858507201E-308"); /* MIN */
676 check(&rn, -DBL_MIN, "-0.0", "-2.225073858507201E-308"); /* -MIN */
677 check(&rn, DBL_MAX, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0", "1.797693134862316E308"); /* MAX */
678 check(&rn, -DBL_MAX, "-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0", "-1.797693134862316E308"); /* MAX */
679 check(&rn, -DBL_TRUE_MIN, "-0.0", "-4.940656458412465E-324"); /* subnorm */
680 check(&rn, DBL_TRUE_MIN, "0.0", "4.940656458412465E-324"); /* subnorm */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000681
Lev Walkin5f560912004-10-21 13:37:57 +0000682
Lev Walkin1aea6982004-10-26 09:35:25 +0000683#ifdef NAN
684 check_xer(0, NAN); /* "<NOT-A-NUMBER/>" */
685#else
Lev Walkin5f560912004-10-21 13:37:57 +0000686 check_xer(0, zero/zero); /* "<NOT-A-NUMBER/>" */
Lev Walkin1aea6982004-10-26 09:35:25 +0000687#endif
688#ifdef INFINITY
689 check_xer(0, INFINITY); /* "<PLUS-INFINITY/>" */
690 check_xer(0, -INFINITY); /* "<MINUS-INFINITY/>" */
691#else
Lev Walkin5f560912004-10-21 13:37:57 +0000692 check_xer(0, 1.0/zero); /* "<PLUS-INFINITY/>" */
693 check_xer(0, -1.0/zero); /* "<MINUS-INFINITY/>" */
Lev Walkin1aea6982004-10-26 09:35:25 +0000694#endif
Lev Walkin5f560912004-10-21 13:37:57 +0000695 check_xer(0, 1.0);
696 check_xer(0, -1.0);
697 check_xer(0, 1.5);
698 check_xer(0, 123);
699 check_xer(1, 0.0000000000000000000001);
700 check_xer(1, -0.0000000000000000000001);
701
Lev Walkin41ba1f22004-09-14 12:46:35 +0000702 return 0;
703}