blob: c7b8112521c365a80560d042bb4a48af0589f58a [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() {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800348#define CHECK_BER_STRICT(v, nocan, can, inbuf, outbuf) \
349 check_ber_buffer_twoway(v, nocan, can, inbuf, sizeof(inbuf), \
350 outbuf, sizeof(outbuf), __LINE__)
Lev Walkin5fe72e92012-01-07 17:00:29 -0800351
352#define CHECK_BER_NONSTRICT(v, nocan, can, buf) \
Lev Walkin5fda7d52012-01-09 03:20:19 -0800353 check_ber_buffer_oneway(v, nocan, can, buf, sizeof(buf), __LINE__)
Lev Walkin5fe72e92012-01-07 17:00:29 -0800354
355 /*
356 * X.690 8.4 Encoding of an enumerated value.
357 */
358
359 /* 8.5.2 If the real value is the value plus zero,
360 * there shall be no contents octet in the encoding */
361 { uint8_t b_0[] = {};
Lev Walkin5fda7d52012-01-09 03:20:19 -0800362 CHECK_BER_STRICT(0.0, "0", "0", b_0, b_0); }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800363
364 /* 8.5.3 When -0 is to be encoded, there shall be only one contents octet */
365 { uint8_t b_m0[] = { 0x43 };
Lev Walkin5fda7d52012-01-09 03:20:19 -0800366 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0, b_m0); }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800367
368 /* Old way of encoding -0.0: 8.5.6 a) */
369 { uint8_t b_m0[] = { 0x43 };
370 uint8_t b_m0_856a[] = { 0xC0, 0x00 }; /* #8.5.6 a) */
371 uint8_t b_m0_856a_1[] = { 0xC0, 0x00, 0x00 };
372 uint8_t b_m0_856a_2[] = { 0xC0, 0x00, 0x00, 0x00 };
373 uint8_t b_m0_856a_3[] = { 0xC0, 0x00, 0x00, 0x00, 0x00 };
374 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a, b_m0);
375 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a_1, b_m0);
376 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a_2, b_m0);
Lev Walkin5fda7d52012-01-09 03:20:19 -0800377 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_856a_3, b_m0); }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800378
379 /* 8.5.6 c) => 8.5.9 SpecialRealValue */
380 { uint8_t b_pinf[] = { 0x40 };
381 uint8_t b_minf[] = { 0x41 };
382 uint8_t b_nan[] = { 0x42 };
Lev Walkin9c57f332017-09-17 22:30:49 -0700383 CHECK_BER_STRICT(INFINITY, "<PLUS-INFINITY/>", "<PLUS-INFINITY/>", b_pinf, b_pinf);
384 CHECK_BER_STRICT(-INFINITY, "<MINUS-INFINITY/>", "<MINUS-INFINITY/>", b_minf, b_minf);
385 CHECK_BER_STRICT(NAN, "<NOT-A-NUMBER/>", "<NOT-A-NUMBER/>", b_nan, b_nan); }
Lev Walkin5fda7d52012-01-09 03:20:19 -0800386
387 /* 8.5.6 b) => 8.5.8 Decimal encoding is used; NR1 form */
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_0_nr1[] = { 0x01, '0', '0' };
392 uint8_t b_0[] = { };
393 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr1, b_0); }
394 { uint8_t b_0_nr1[] = { 0x01, ' ', '0' };
395 uint8_t b_0[] = { };
396 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr1, b_0); }
397 { uint8_t b_p0_nr1[] = { 0x01, '+', '0' };
398 uint8_t b_0[] = { };
399 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr1, b_0); }
400 { uint8_t b_p0_nr1[] = { 0x01, ' ', '+', '0' };
401 uint8_t b_0[] = { };
402 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr1, b_0); }
403 { uint8_t b_m0_nr1[] = { 0x01, '-', '0' };
404 uint8_t b_m0[] = { 0x43 };
405 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr1, b_m0); }
406 { uint8_t b_m0_nr1[] = { 0x01, ' ', '-', '0' };
407 uint8_t b_m0[] = { 0x43 };
408 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr1, b_m0); }
409
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_1_nr1[] = { 0x01, '0', '1' };
414 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
415 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr1, b_1); }
416 { uint8_t b_1_nr1[] = { 0x01, ' ', '1' };
417 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
418 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr1, b_1); }
419 { uint8_t b_p1_nr1[] = { 0x01, '+', '1' };
420 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
421 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr1, b_1); }
422 { uint8_t b_p1_nr1[] = { 0x01, ' ', '+', '1' };
423 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
424 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr1, b_1); }
425 { uint8_t b_m1_nr1[] = { 0x01, '-', '1' };
426 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
427 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr1, b_m1); }
428 { uint8_t b_m1_nr1[] = { 0x01, ' ', '-', '1' };
429 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
430 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr1, b_m1); }
431
432 {
433 uint8_t comma_symbol[] = { '.', ',' };
434 int csi;
435 for(csi = 0; csi < 2; csi++) {
436 uint8_t CS = comma_symbol[csi];
437
438 /* 8.5.6 b) => 8.5.8 Decimal encoding is used; NR2 form */
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_0_nr2[] = { 0x02, '0', '0', CS, '0' };
443 uint8_t b_0[] = { };
444 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr2, b_0); }
445 { uint8_t b_0_nr2[] = { 0x02, ' ', '0', CS, '0' };
446 uint8_t b_0[] = { };
447 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr2, b_0); }
448 { uint8_t b_p0_nr2[] = { 0x02, '+', '0', CS, '0' };
449 uint8_t b_0[] = { };
450 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr2, b_0); }
451 { uint8_t b_p0_nr2[] = { 0x02, ' ', '+', '0', CS, '0' };
452 uint8_t b_0[] = { };
453 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr2, b_0); }
454 { uint8_t b_m0_nr2[] = { 0x02, '-', '0', CS, '0' };
455 uint8_t b_m0[] = { 0x43 };
456 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr2, b_m0); }
457 { uint8_t b_m0_nr2[] = { 0x02, ' ', '-', '0', CS, '0' };
458 uint8_t b_m0[] = { 0x43 };
459 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr2, b_m0); }
460
461 /* 8.5.6 b) => 8.5.8 NR2 "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_1_nr2[] = { 0x02, '0', '1', CS };
466 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
467 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr2, b_1); }
468 { uint8_t b_1_nr2[] = { 0x02, ' ', '1', CS };
469 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
470 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_1_nr2, b_1); }
471 { uint8_t b_p1_nr2[] = { 0x02, '+', '1', CS };
472 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
473 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr2, b_1); }
474 { uint8_t b_p1_nr2[] = { 0x02, ' ', '+', '1', CS };
475 uint8_t b_1[] = { 0x80, 0x00, 0x01 };
476 CHECK_BER_STRICT(1.0, "1.0", "1.0E0", b_p1_nr2, b_1); }
477 { uint8_t b_m1_nr2[] = { 0x02, '-', '1', CS };
478 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
479 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr2, b_m1); }
480 { uint8_t b_m1_nr2[] = { 0x02, ' ', '-', '1', CS };
481 uint8_t b_m1[] = { 0xC0, 0x00, 0x01 };
482 CHECK_BER_STRICT(-1.0, "-1.0", "-1.0E0", b_m1_nr2, b_m1); }
483
484 /* 8.5.6 b) => 8.5.8 NR2 ".5" */
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_05_nr2[] = { 0x02, '0', CS, '5' };
489 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
490 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_05_nr2, b_05); }
491 { uint8_t b_05_nr2[] = { 0x02, ' ', CS, '5' };
492 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
493 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_05_nr2, b_05); }
494 { uint8_t b_p1_nr2[] = { 0x02, '+', CS, '5' };
495 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
496 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p1_nr2, b_05); }
497 { uint8_t b_p1_nr2[] = { 0x02, ' ', '+', CS, '5' };
498 uint8_t b_05[] = { 0x80, 0xff, 0x01 };
499 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p1_nr2, b_05); }
500 { uint8_t b_m05_nr2[] = { 0x02, '-', CS, '5' };
501 uint8_t b_m05[] = { 0xC0, 0xff, 0x01 };
502 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m05_nr2, b_m05); }
503 { uint8_t b_m05_nr2[] = { 0x02, ' ', '-', CS, '5' };
504 uint8_t b_m05[] = { 0xC0, 0xff, 0x01 };
505 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m05_nr2, b_m05); }
506
507 /* 8.5.6 b) => 8.5.8 Decimal encoding is used; NR3 form */
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_0_nr3[] = { 0x03, '0', '0', CS, '0', 'E', '0' };
512 uint8_t b_0[] = { };
513 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr3, b_0); }
514 { uint8_t b_0_nr3[] = { 0x03, ' ', '0', CS, '0', 'e', '0' };
515 uint8_t b_0[] = { };
516 CHECK_BER_STRICT(0.0, "0", "0", b_0_nr3, b_0); }
517 { uint8_t b_p0_nr3[] = { 0x03, '+', '0', CS, '0', 'E', '+', '0' };
518 uint8_t b_0[] = { };
519 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr3, b_0); }
520 { uint8_t b_p0_nr3[] = { 0x03, ' ', '+', '0', CS, '0', 'e', '+', '0' };
521 uint8_t b_0[] = { };
522 CHECK_BER_STRICT(0.0, "0", "0", b_p0_nr3, b_0); }
523 { uint8_t b_m0_nr3[] = { 0x03, '-', '0', CS, '0', 'E', '-', '0' };
524 uint8_t b_m0[] = { 0x43 };
525 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr3, b_m0); }
526 { uint8_t b_m0_nr3[] = { 0x03, ' ', '-', '0', CS, '0', 'e', '-', '0' };
527 uint8_t b_m0[] = { 0x43 };
528 CHECK_BER_STRICT(-0.0, "-0", "-0", b_m0_nr3, b_m0); }
529
530 /* 8.5.6 b) => 8.5.8 NR3 "5.e-1" */
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_5_nr3[] = { 0x03, '0', '5', CS, 'E', '-', '1' };
535 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
536 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_5_nr3, b_5); }
537 { uint8_t b_5_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_5_nr3, b_5); }
540 { uint8_t b_p5_nr3[] = { 0x03, '+', '5', CS, 'E', '-', '1' };
541 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
542 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p5_nr3, b_5); }
543 { uint8_t b_p5_nr3[] = { 0x03, ' ', '+', '5', CS, 'e', '-', '1' };
544 uint8_t b_5[] = { 0x80, 0xff, 0x01 };
545 CHECK_BER_STRICT(0.5, "0.5", "5.0E-1", b_p5_nr3, b_5); }
546 { uint8_t b_m5_nr3[] = { 0x03, '-', '5', CS, 'E', '-', '1' };
547 uint8_t b_m5[] = { 0xC0, 0xff, 0x01 };
548 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m5_nr3, b_m5); }
549 { uint8_t b_m5_nr3[] = { 0x03, ' ', '-', '5', CS, 'e', '-', '1' };
550 uint8_t b_m5[] = { 0xC0, 0xff, 0x01 };
551 CHECK_BER_STRICT(-0.5, "-0.5", "-5.0E-1", b_m5_nr3, b_m5); }
552
553 /* 8.5.6 b) => 8.5.8 NR3 ".5e1" */
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_05_nr3[] = { 0x03, '0', CS, '5', 'E', '+', '1'};
558 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
559 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_05_nr3, b_05); }
560 { uint8_t b_05_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_05_nr3, b_05); }
563 { uint8_t b_p1_nr3[] = { 0x03, '+', CS, '5', 'E', '1' };
564 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
565 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_p1_nr3, b_05); }
566 { uint8_t b_p1_nr3[] = { 0x03, ' ', '+', CS, '5', 'e', '+', '1' };
567 uint8_t b_05[] = { 0x80, 0x00, 0x05 };
568 CHECK_BER_STRICT(5.0, "5.0", "5.0E0", b_p1_nr3, b_05); }
569 { uint8_t b_m05_nr3[] = { 0x03, '-', CS, '5', 'E', '+', '1' };
570 uint8_t b_m05[] = { 0xC0, 0x00, 0x05 };
571 CHECK_BER_STRICT(-5.0, "-5.0", "-5.0E0", b_m05_nr3, b_m05); }
572 { uint8_t b_m05_nr3[] = { 0x03, ' ', '-', CS, '5', 'e', '1' };
573 uint8_t b_m05[] = { 0xC0, 0x00, 0x05 };
574 CHECK_BER_STRICT(-5.0, "-5.0", "-5.0E0", b_m05_nr3, b_m05); }
575 } /* for(comma symbol) */
576 }
Lev Walkin5fe72e92012-01-07 17:00:29 -0800577
Lev Walkine8727ec2012-01-09 05:46:16 -0800578 /* Scan through the range of bits, construct the valid base-2 numbers, and
579 * try two-way conversion with them */
580 {
581 int base, sign, scaling_factor, exponent, mantissa;
582 for(base = 0; base <= 2; base++) {
583 for(sign = 0; sign <= 1; sign++) {
584 for(scaling_factor = 0; scaling_factor <= 3; scaling_factor++) {
585 for(exponent = -1000; exponent < 1000; exponent += (exponent > -990 && exponent < 990) ? 100 : 1) {
586 for(mantissa = 0; mantissa < 66000; mantissa += (mantissa > 300 && mantissa < 65400) ? 100 : 1) {
587 check_ber_857_encoding(base, sign, scaling_factor, exponent, mantissa);
588 }
589 }
590 }
591 }
592 }
593 }
594
Lev Walkin5fe72e92012-01-07 17:00:29 -0800595 {
596 uint8_t b_1_0[] =
597 { 0x80, 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
598 uint8_t b_1_1[] =
599 { 0x80, 0xcc, 0x11, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a };
600 uint8_t b_3_14[] =
601 { 0x80, 0xcd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f };
602 uint8_t b_3_14_mo1[] =
603 { 0xC0, 0xc5, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3};
604 uint8_t b_3_14_mo2[] =
605 { 0x80, 0xbd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3,2};
606
607 CHECK_BER_NONSTRICT(1.0, "1.0", "1.0E0", b_1_0);
608 CHECK_BER_NONSTRICT(1.1, "1.1", "1.1E0", b_1_1);
609 CHECK_BER_NONSTRICT(3.14, "3.14", "3.14E0", b_3_14);
610 /* These two are very interesting! They check mantissa overflow! */
611 CHECK_BER_NONSTRICT(-3.14, "-3.14", "-3.14E0", b_3_14_mo1);
612 CHECK_BER_NONSTRICT(3.14, "3.14", "3.14E0", b_3_14_mo2);
613 }
614}
Lev Walkinf0b808d2005-04-25 21:08:25 +0000615
Lev Walkin41ba1f22004-09-14 12:46:35 +0000616int
617main() {
618 REAL_t rn;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000619 memset(&rn, 0, sizeof(rn));
620
Lev Walkin5fe72e92012-01-07 17:00:29 -0800621 check_ber_encoding();
622
Lev Walkined465432004-09-27 20:52:36 +0000623 check(&rn, 0.0, "0", "0");
624 check(&rn, -0.0, "-0", "-0"); /* minus-zero */
Lev Walkin9c57f332017-09-17 22:30:49 -0700625 check(&rn, NAN, "<NOT-A-NUMBER/>", "<NOT-A-NUMBER/>");
626 check(&rn, INFINITY, "<PLUS-INFINITY/>", "<PLUS-INFINITY/>");
627 check(&rn, -INFINITY, "<MINUS-INFINITY/>", "<MINUS-INFINITY/>");
Lev Walkined465432004-09-27 20:52:36 +0000628 check(&rn, 1.0, "1.0", "1.0E0");
629 check(&rn, -1.0, "-1.0", "-1.0E0");
Lev Walkined465432004-09-27 20:52:36 +0000630 check(&rn, 0.1, "0.1", "1.0E-1");
Lev Walkinf0b808d2005-04-25 21:08:25 +0000631 check(&rn, 0.01, "0.01", "1.0E-2");
632 check(&rn, 0.02, "0.02", "2.0E-2");
633 check(&rn, 0.09, "0.09", "9.0E-2");
634 check(&rn, 1.5, "1.5", "1.5E0");
Lev Walkined465432004-09-27 20:52:36 +0000635 check(&rn, 0.33333, "0.33333", "3.3333E-1");
636 check(&rn, 2, "2.0", "2.0E0");
637 check(&rn, 2.1, "2.1", "2.1E0");
638 check(&rn, 3, "3.0", "3.0E0");
639 check(&rn, 3.1, "3.1", "3.1E0");
640 check(&rn, 3.14, "3.14", "3.14E0");
641 check(&rn, 3.1415, "3.1415", "3.1415E0");
642 check(&rn, 3.141592, "3.141592", "3.141592E0");
643 check(&rn, 3.14159265, "3.14159265", "3.14159265E0");
644 check(&rn, -3.14159265, "-3.14159265", "-3.14159265E0");
645 check(&rn, 14159265.0, "14159265.0", "1.4159265E7");
646 check(&rn, -123456789123456789.0, "-123456789123456784.0", "-1.234567891234568E17");
Lev Walkinf0b808d2005-04-25 21:08:25 +0000647 check(&rn, 0.00000000001, "0.00000000001", "9.999999999999999E-12");
648 check(&rn, 0.00000000002, "0.00000000002", "2.0E-11");
649 check(&rn, 0.00000000009, "0.00000000009", "9.0E-11");
650 check(&rn, 0.000000000002, "0.000000000002", "2.0E-12");
651 check(&rn, 0.0000000000002, "0.0000000000002", "2.0E-13");
652 check(&rn, 0.00000000000002, "0.00000000000002", "2.0E-14");
653 check(&rn, 0.000000000000002, "0.000000000000002", "2.0E-15");
654 check(&rn, 0.0000000000000002, "0.0", "2.0E-16");
Lev Walkined465432004-09-27 20:52:36 +0000655 check(&rn, 0.0000000000000000000001, "0.0", "1.0E-22");
656 check(&rn, 0.000000000000000000000000000001, "0.0", "1.0E-30"); /* proved 2B a problem */
657 check(&rn,-0.000000000000000000000000000001, "-0.0", "-1.0E-30"); /* proved 2B a problem */
658 check(&rn, 0.0000000000010000000001000000000001, 0, 0);
659 check(&rn, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
660 check(&rn, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
661 check(&rn,-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
662 check(&rn,-3.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
663 check(&rn, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
Lev Walkin8ca13c82016-01-24 22:40:00 -0800664 check(&rn, 0.25, "0.25", "2.5E-1");
665 check(&rn, -0.25, "-0.25", "-2.5E-1");
666 check(&rn, 0.03, "0.03", "3.0E-2");
667 check(&rn, -0.03, "-0.03", "-3.0E-2");
668
669 check(&rn, 4.01E-50, "0.0", "4.01E-50");
670 check(&rn, -4.01E-50, "-0.0", "-4.01E-50");
671 check(&rn, -4.9406564584124654E-324, "-0.0", "-4.940656458412465E-324"); /* MIN */
672 check(&rn, DBL_MIN, "0.0", "2.225073858507201E-308"); /* MIN */
673 check(&rn, -DBL_MIN, "-0.0", "-2.225073858507201E-308"); /* -MIN */
674 check(&rn, DBL_MAX, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0", "1.797693134862316E308"); /* MAX */
675 check(&rn, -DBL_MAX, "-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0", "-1.797693134862316E308"); /* MAX */
676 check(&rn, -DBL_TRUE_MIN, "-0.0", "-4.940656458412465E-324"); /* subnorm */
677 check(&rn, DBL_TRUE_MIN, "0.0", "4.940656458412465E-324"); /* subnorm */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000678
Lev Walkin5f560912004-10-21 13:37:57 +0000679
Lev Walkin1aea6982004-10-26 09:35:25 +0000680#ifdef NAN
681 check_xer(0, NAN); /* "<NOT-A-NUMBER/>" */
682#else
Lev Walkin5f560912004-10-21 13:37:57 +0000683 check_xer(0, zero/zero); /* "<NOT-A-NUMBER/>" */
Lev Walkin1aea6982004-10-26 09:35:25 +0000684#endif
685#ifdef INFINITY
686 check_xer(0, INFINITY); /* "<PLUS-INFINITY/>" */
687 check_xer(0, -INFINITY); /* "<MINUS-INFINITY/>" */
688#else
Lev Walkin5f560912004-10-21 13:37:57 +0000689 check_xer(0, 1.0/zero); /* "<PLUS-INFINITY/>" */
690 check_xer(0, -1.0/zero); /* "<MINUS-INFINITY/>" */
Lev Walkin1aea6982004-10-26 09:35:25 +0000691#endif
Lev Walkin5f560912004-10-21 13:37:57 +0000692 check_xer(0, 1.0);
693 check_xer(0, -1.0);
694 check_xer(0, 1.5);
695 check_xer(0, 123);
696 check_xer(1, 0.0000000000000000000001);
697 check_xer(1, -0.0000000000000000000001);
698
Lev Walkin41ba1f22004-09-14 12:46:35 +0000699 return 0;
700}