blob: 3a868aafdea7162ddaa7fd73bd4647326d5f5e9c [file] [log] [blame]
vlm785435b2004-09-14 12:46:35 +00001#define EMIT_ASN_DEBUG 1
vlm785435b2004-09-14 12:46:35 +00002#include <REAL.c>
vlm69be6fa2004-10-21 12:23:47 +00003#include <asn_codecs_prim.c>
vlm785435b2004-09-14 12:46:35 +00004#include <ber_decoder.c>
5#include <ber_tlv_length.c>
6#include <ber_tlv_tag.c>
7#include <der_encoder.c>
vlm69be6fa2004-10-21 12:23:47 +00008#include <xer_decoder.c>
9#include <xer_support.c>
vlmcce2ddb2004-10-21 13:37:57 +000010#include <xer_encoder.c>
vlm785435b2004-09-14 12:46:35 +000011#include <constraints.c>
12
vlm09a4aa52004-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
vlm785435b2004-09-14 12:46:35 +000030static void
vlm09a4aa52004-09-27 20:52:36 +000031check_str_repr(double d, const char *sample, const char *canonical_sample) {
32 ssize_t s1, s2;
33
34 reconstr_lens[1] = reconstr_lens[0] = 0;
35
36 s1 = REAL__dump(d, 0, callback, 0);
37 assert(s1 < sizeof(reconstructed[0]));
38 assert(s1 == reconstr_lens[0]);
39 reconstructed[0][s1] = '\0';
40
41 s2 = REAL__dump(d, 1, callback, (void *)1);
42 assert(s2 < sizeof(reconstructed[1]));
43 assert(s2 == reconstr_lens[1]);
44 reconstructed[1][s2] = '\0';
45
46 if(sample) {
vlm0919fcc2005-04-25 21:08:25 +000047 printf("Checking %f->[%s] against [%s]%s\n",
48 d, reconstructed[0], sample,
49 canonical_sample ? " (canonical follows...)" : ""
50 );
vlm09a4aa52004-09-27 20:52:36 +000051 assert(!strcmp(reconstructed[0], sample));
52 }
53 if(canonical_sample) {
vlm0919fcc2005-04-25 21:08:25 +000054 printf("Checking %f->[%s] against [%s] (canonical)\n",
55 d, reconstructed[1], canonical_sample);
vlm09a4aa52004-09-27 20:52:36 +000056 assert(!strcmp(reconstructed[1], canonical_sample));
57 }
58}
59
60static void
vlm0919fcc2005-04-25 21:08:25 +000061check_impl(REAL_t *rn, double orig_dbl, const char *sample, const char *canonical_sample, int line) {
vlm785435b2004-09-14 12:46:35 +000062 double val;
63 uint8_t *p, *end;
64 int ret;
65
vlm0919fcc2005-04-25 21:08:25 +000066 printf("Line %d: double value %.12f [", line, orig_dbl);
vlm785435b2004-09-14 12:46:35 +000067 for(p = (uint8_t *)&orig_dbl, end = p + sizeof(double); p < end ; p++)
68 printf("%02x", *p);
69 printf("] (ilogb %d)\n", ilogb(orig_dbl));
70
71 val = frexp(orig_dbl, &ret);
72 printf("frexp(%f, %d): [", val, ret);
73 for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
74 printf("%02x", *p);
75 printf("]\n");
76
vlmef6355b2004-09-29 13:26:15 +000077 ret = asn_double2REAL(rn, orig_dbl);
vlm785435b2004-09-14 12:46:35 +000078 assert(ret == 0);
79
80 printf("converted into [");
81 for(p = rn->buf, end = p + rn->size; p < end; p++)
82 printf("%02x", *p);
vlm8f6ded02004-09-27 21:36:59 +000083 printf("]: %d\n", rn->size);
vlm785435b2004-09-14 12:46:35 +000084
vlmef6355b2004-09-29 13:26:15 +000085 ret = asn_REAL2double(rn, &val);
vlm785435b2004-09-14 12:46:35 +000086 assert(ret == 0);
87
88 printf("and back to double: [");
89 for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
90 printf("%02x", *p);
91 printf("] (ilogb %d)\n", ilogb(val));
92
vlmb8ce6f02004-10-26 09:35:25 +000093 printf("%.12f vs %.12f\n", val, orig_dbl);
94 assert((isnan(orig_dbl) && isnan(val)) || val == orig_dbl);
vlm785435b2004-09-14 12:46:35 +000095 printf("OK\n");
vlm09a4aa52004-09-27 20:52:36 +000096
97 check_str_repr(val, sample, canonical_sample);
vlm785435b2004-09-14 12:46:35 +000098}
99
100uint8_t buf_1_0[] = { 0x80, 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
101uint8_t buf_1_1[] = { 0x80, 0xcc, 0x11, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a };
102uint8_t buf_3_14[] = { 0x80, 0xcd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f };
vlmb8ce6f02004-10-26 09:35:25 +0000103/* These ones are very interesting! They check mantissa overflow! */
vlm09a4aa52004-09-27 20:52:36 +0000104uint8_t buf_mo1[] = { 0xC0, 0xc5, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3};
vlm785435b2004-09-14 12:46:35 +0000105uint8_t buf_mo2[] = { 0x80, 0xbd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3,2};
106
107static void
vlm09a4aa52004-09-27 20:52:36 +0000108check_buf(uint8_t *buf, size_t bufsize, double verify, const char *sample, const char *canonical_sample) {
vlm785435b2004-09-14 12:46:35 +0000109 REAL_t rn;
110 double val;
111 uint8_t *p, *end;
112 int ret;
113
114 printf("verify double value %.12f [", verify);
115 for(p = (uint8_t *)&verify, end = p + sizeof(double); p < end ; p++)
116 printf("%02x", *p);
117 printf("] (ilogb %d)\n", ilogb(verify));
118
119 rn.buf = 0;
120 rn.size = 0;
121
vlmef6355b2004-09-29 13:26:15 +0000122 ret = asn_double2REAL(&rn, verify);
vlm785435b2004-09-14 12:46:35 +0000123 assert(ret == 0);
124
125 printf("canonical DER: [");
126 for(p = rn.buf, end = p + rn.size; p < end; p++)
127 printf("%02x", *p);
128 printf("]\n");
129
130 rn.buf = buf;
131 rn.size = bufsize;
132
133 printf("received as: [");
134 for(p = rn.buf, end = p + rn.size; p < end; p++)
135 printf("%02x", *p);
136 printf("]\n");
137
vlmef6355b2004-09-29 13:26:15 +0000138 ret = asn_REAL2double(&rn, &val);
vlm785435b2004-09-14 12:46:35 +0000139 assert(ret == 0);
140
141 printf("%.12f vs %.12f\n", verify, val);
142
143 assert(val == verify);
vlm09a4aa52004-09-27 20:52:36 +0000144
145 check_str_repr(val, sample, canonical_sample);
vlm785435b2004-09-14 12:46:35 +0000146}
147
vlmcce2ddb2004-10-21 13:37:57 +0000148static void
149check_xer(int fuzzy, double orig_value) {
150 asn_enc_rval_t er;
151 asn_dec_rval_t rc;
152 REAL_t st;
153 REAL_t *newst0 = 0;
154 REAL_t *newst1 = 0;
vlm170e42c2006-07-27 11:46:25 +0000155 REAL_t **newst0p = &newst0;
156 REAL_t **newst1p = &newst1;
vlmcce2ddb2004-10-21 13:37:57 +0000157 double value0, value1;
158 int ret;
159
160 memset(&st, 0, sizeof(st));
161 ret = asn_double2REAL(&st, orig_value);
162 assert(ret == 0);
163
164 reconstr_lens[0] = 0;
165 reconstr_lens[1] = 0;
166 er = xer_encode(&asn_DEF_REAL, &st,
167 XER_F_BASIC, callback, 0);
168 assert(er.encoded == reconstr_lens[0]);
169 er = xer_encode(&asn_DEF_REAL, &st,
170 XER_F_CANONICAL, callback, (void *)1);
171 assert(er.encoded == reconstr_lens[1]);
172 reconstructed[0][reconstr_lens[0]] = 0;
173 reconstructed[1][reconstr_lens[1]] = 0;
174
175 printf("%f vs (%d)[%s] & (%d)%s",
176 orig_value,
177 reconstr_lens[1], reconstructed[1],
178 reconstr_lens[0], reconstructed[0]
179 );
180
vlm170e42c2006-07-27 11:46:25 +0000181 rc = xer_decode(0, &asn_DEF_REAL, (void **)newst0p,
vlmcce2ddb2004-10-21 13:37:57 +0000182 reconstructed[0], reconstr_lens[0]);
183 assert(rc.code == RC_OK);
184 assert(rc.consumed < reconstr_lens[0]);
185
vlm170e42c2006-07-27 11:46:25 +0000186 rc = xer_decode(0, &asn_DEF_REAL, (void **)newst1p,
vlmcce2ddb2004-10-21 13:37:57 +0000187 reconstructed[1], reconstr_lens[1]);
188 assert(rc.code == RC_OK);
189 assert(rc.consumed == reconstr_lens[1]);
190
191 ret = asn_REAL2double(newst0, &value0);
192 assert(ret == 0);
193 ret = asn_REAL2double(newst1, &value1);
194 assert(ret == 0);
195
vlmb8ce6f02004-10-26 09:35:25 +0000196 assert((isnan(value0) && isnan(orig_value))
197 || value0 == orig_value
vlmcce2ddb2004-10-21 13:37:57 +0000198 || fuzzy);
vlmb8ce6f02004-10-26 09:35:25 +0000199 assert((isnan(value1) && isnan(orig_value))
200 || value1 == orig_value);
vlmcce2ddb2004-10-21 13:37:57 +0000201
202 assert(newst0->size == st.size || fuzzy);
203 assert(newst1->size == st.size);
204 assert(fuzzy || memcmp(newst0->buf, st.buf, st.size) == 0);
205 assert(memcmp(newst1->buf, st.buf, st.size) == 0);
206}
207
vlm0919fcc2005-04-25 21:08:25 +0000208#define check(rn, d, str1, str2) \
209 check_impl(rn, d, str1, str2, __LINE__)
210
vlm785435b2004-09-14 12:46:35 +0000211int
212main() {
213 REAL_t rn;
vlmb1f8d612004-09-27 22:16:18 +0000214 static const double zero = 0.0;
vlm785435b2004-09-14 12:46:35 +0000215
216 memset(&rn, 0, sizeof(rn));
217
vlm09a4aa52004-09-27 20:52:36 +0000218 check(&rn, 0.0, "0", "0");
219 check(&rn, -0.0, "-0", "-0"); /* minus-zero */
vlmb1f8d612004-09-27 22:16:18 +0000220 check(&rn, zero/zero, "<NOT-A-NUMBER/>", "<NOT-A-NUMBER/>");
221 check(&rn, 1.0/zero, "<PLUS-INFINITY/>", "<PLUS-INFINITY/>");
222 check(&rn, -1.0/zero, "<MINUS-INFINITY/>", "<MINUS-INFINITY/>");
vlm09a4aa52004-09-27 20:52:36 +0000223 check(&rn, 1.0, "1.0", "1.0E0");
224 check(&rn, -1.0, "-1.0", "-1.0E0");
vlm09a4aa52004-09-27 20:52:36 +0000225 check(&rn, 0.1, "0.1", "1.0E-1");
vlm0919fcc2005-04-25 21:08:25 +0000226 check(&rn, 0.01, "0.01", "1.0E-2");
227 check(&rn, 0.02, "0.02", "2.0E-2");
228 check(&rn, 0.09, "0.09", "9.0E-2");
229 check(&rn, 1.5, "1.5", "1.5E0");
vlm09a4aa52004-09-27 20:52:36 +0000230 check(&rn, 0.33333, "0.33333", "3.3333E-1");
231 check(&rn, 2, "2.0", "2.0E0");
232 check(&rn, 2.1, "2.1", "2.1E0");
233 check(&rn, 3, "3.0", "3.0E0");
234 check(&rn, 3.1, "3.1", "3.1E0");
235 check(&rn, 3.14, "3.14", "3.14E0");
236 check(&rn, 3.1415, "3.1415", "3.1415E0");
237 check(&rn, 3.141592, "3.141592", "3.141592E0");
238 check(&rn, 3.14159265, "3.14159265", "3.14159265E0");
239 check(&rn, -3.14159265, "-3.14159265", "-3.14159265E0");
240 check(&rn, 14159265.0, "14159265.0", "1.4159265E7");
241 check(&rn, -123456789123456789.0, "-123456789123456784.0", "-1.234567891234568E17");
vlm0919fcc2005-04-25 21:08:25 +0000242 check(&rn, 0.00000000001, "0.00000000001", "9.999999999999999E-12");
243 check(&rn, 0.00000000002, "0.00000000002", "2.0E-11");
244 check(&rn, 0.00000000009, "0.00000000009", "9.0E-11");
245 check(&rn, 0.000000000002, "0.000000000002", "2.0E-12");
246 check(&rn, 0.0000000000002, "0.0000000000002", "2.0E-13");
247 check(&rn, 0.00000000000002, "0.00000000000002", "2.0E-14");
248 check(&rn, 0.000000000000002, "0.000000000000002", "2.0E-15");
249 check(&rn, 0.0000000000000002, "0.0", "2.0E-16");
vlm09a4aa52004-09-27 20:52:36 +0000250 check(&rn, 0.0000000000000000000001, "0.0", "1.0E-22");
251 check(&rn, 0.000000000000000000000000000001, "0.0", "1.0E-30"); /* proved 2B a problem */
252 check(&rn,-0.000000000000000000000000000001, "-0.0", "-1.0E-30"); /* proved 2B a problem */
253 check(&rn, 0.0000000000010000000001000000000001, 0, 0);
254 check(&rn, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
255 check(&rn, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
256 check(&rn,-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
257 check(&rn,-3.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
258 check(&rn, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
259 check(&rn, -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
vlm785435b2004-09-14 12:46:35 +0000260
vlm09a4aa52004-09-27 20:52:36 +0000261 check_buf(buf_1_0, sizeof(buf_1_0), 1.0, "1.0", "1.0E0");
262 check_buf(buf_1_1, sizeof(buf_1_1), 1.1, "1.1", "1.1E0");
263 check_buf(buf_3_14, sizeof(buf_3_14), 3.14, "3.14", "3.14E0");
264 check_buf(buf_mo1, sizeof(buf_mo1), -3.14, "-3.14", "-3.14E0");
265 check_buf(buf_mo2, sizeof(buf_mo2), 3.14, "3.14", "3.14E0");
vlm785435b2004-09-14 12:46:35 +0000266
vlmcce2ddb2004-10-21 13:37:57 +0000267
vlmb8ce6f02004-10-26 09:35:25 +0000268#ifdef NAN
269 check_xer(0, NAN); /* "<NOT-A-NUMBER/>" */
270#else
vlmcce2ddb2004-10-21 13:37:57 +0000271 check_xer(0, zero/zero); /* "<NOT-A-NUMBER/>" */
vlmb8ce6f02004-10-26 09:35:25 +0000272#endif
273#ifdef INFINITY
274 check_xer(0, INFINITY); /* "<PLUS-INFINITY/>" */
275 check_xer(0, -INFINITY); /* "<MINUS-INFINITY/>" */
276#else
vlmcce2ddb2004-10-21 13:37:57 +0000277 check_xer(0, 1.0/zero); /* "<PLUS-INFINITY/>" */
278 check_xer(0, -1.0/zero); /* "<MINUS-INFINITY/>" */
vlmb8ce6f02004-10-26 09:35:25 +0000279#endif
vlmcce2ddb2004-10-21 13:37:57 +0000280 check_xer(0, 1.0);
281 check_xer(0, -1.0);
282 check_xer(0, 1.5);
283 check_xer(0, 123);
284 check_xer(1, 0.0000000000000000000001);
285 check_xer(1, -0.0000000000000000000001);
286
vlm785435b2004-09-14 12:46:35 +0000287 return 0;
288}
vlm86380d32006-10-09 12:07:58 +0000289
290asn_dec_rval_t OCTET_STRING_decode_uper(asn_codec_ctx_t *ctx, asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts, void **sptr, asn_per_data_t *pd) { asn_dec_rval_t rv = { 0, 0 }; (void)ctx; (void)td; (void)cts; (void)sptr; (void)pd; return rv; }
291
292asn_enc_rval_t OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts, void *sptr, asn_per_outp_t *po) { asn_enc_rval_t er = { 0, 0, 0 }; (void)td; (void)cts; (void)sptr; (void)po; return er; }