blob: d0bf3fca42de6ce350c2353610324ea602933a97 [file] [log] [blame]
Lev Walkin41ba1f22004-09-14 12:46:35 +00001#define EMIT_ASN_DEBUG 1
Lev Walkin41ba1f22004-09-14 12:46:35 +00002#include <REAL.c>
Lev Walkinb2cb57c2004-10-21 12:23:47 +00003#include <asn_codecs_prim.c>
Lev Walkin41ba1f22004-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>
Lev Walkinb2cb57c2004-10-21 12:23:47 +00008#include <xer_decoder.c>
9#include <xer_support.c>
Lev Walkin41ba1f22004-09-14 12:46:35 +000010#include <constraints.c>
11
Lev Walkined465432004-09-27 20:52:36 +000012static char reconstructed[2][512];
13static int reconstr_lens[2];
14
15static int
16callback(const void *buffer, size_t size, void *app_key) {
17 char *buf = reconstructed[app_key ? 1 : 0];
18 int *len = &reconstr_lens[app_key ? 1 : 0];
19
20 if(*len + size >= sizeof(reconstructed[0]))
21 return -1;
22
23 memcpy(buf + *len, buffer, size);
24 *len += size;
25
26 return 0;
27}
28
Lev Walkin41ba1f22004-09-14 12:46:35 +000029static void
Lev Walkined465432004-09-27 20:52:36 +000030check_str_repr(double d, const char *sample, const char *canonical_sample) {
31 ssize_t s1, s2;
32
33 reconstr_lens[1] = reconstr_lens[0] = 0;
34
35 s1 = REAL__dump(d, 0, callback, 0);
36 assert(s1 < sizeof(reconstructed[0]));
37 assert(s1 == reconstr_lens[0]);
38 reconstructed[0][s1] = '\0';
39
40 s2 = REAL__dump(d, 1, callback, (void *)1);
41 assert(s2 < sizeof(reconstructed[1]));
42 assert(s2 == reconstr_lens[1]);
43 reconstructed[1][s2] = '\0';
44
45 if(sample) {
46 printf("Checking [%s] against [%s]\n",
47 reconstructed[0], sample);
48 assert(!strcmp(reconstructed[0], sample));
49 }
50 if(canonical_sample) {
51 printf("Checking [%s] against [%s] (canonical)\n",
52 reconstructed[1], canonical_sample);
53 assert(!strcmp(reconstructed[1], canonical_sample));
54 }
55}
56
57static void
58check(REAL_t *rn, double orig_dbl, const char *sample, const char *canonical_sample) {
Lev Walkin41ba1f22004-09-14 12:46:35 +000059 double val;
60 uint8_t *p, *end;
61 int ret;
62
63 printf("double value %.12f [", orig_dbl);
64 for(p = (uint8_t *)&orig_dbl, end = p + sizeof(double); p < end ; p++)
65 printf("%02x", *p);
66 printf("] (ilogb %d)\n", ilogb(orig_dbl));
67
68 val = frexp(orig_dbl, &ret);
69 printf("frexp(%f, %d): [", val, ret);
70 for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
71 printf("%02x", *p);
72 printf("]\n");
73
Lev Walkin5e033762004-09-29 13:26:15 +000074 ret = asn_double2REAL(rn, orig_dbl);
Lev Walkin41ba1f22004-09-14 12:46:35 +000075 assert(ret == 0);
76
77 printf("converted into [");
78 for(p = rn->buf, end = p + rn->size; p < end; p++)
79 printf("%02x", *p);
Lev Walkin2a789d92004-09-27 21:36:59 +000080 printf("]: %d\n", rn->size);
Lev Walkin41ba1f22004-09-14 12:46:35 +000081
Lev Walkin5e033762004-09-29 13:26:15 +000082 ret = asn_REAL2double(rn, &val);
Lev Walkin41ba1f22004-09-14 12:46:35 +000083 assert(ret == 0);
84
85 printf("and back to double: [");
86 for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
87 printf("%02x", *p);
88 printf("] (ilogb %d)\n", ilogb(val));
89
90 printf("%.12f vs %.12f\n", orig_dbl, val);
Lev Walkin2a789d92004-09-27 21:36:59 +000091 assert(orig_dbl == val || (isnan(orig_dbl) && isnan(val)));
Lev Walkin41ba1f22004-09-14 12:46:35 +000092 printf("OK\n");
Lev Walkined465432004-09-27 20:52:36 +000093
94 check_str_repr(val, sample, canonical_sample);
Lev Walkin41ba1f22004-09-14 12:46:35 +000095}
96
97uint8_t buf_1_0[] = { 0x80, 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
98uint8_t buf_1_1[] = { 0x80, 0xcc, 0x11, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a };
99uint8_t buf_3_14[] = { 0x80, 0xcd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f };
100/* These ones are very interesting! It checks mantissa overflow! */
Lev Walkined465432004-09-27 20:52:36 +0000101uint8_t buf_mo1[] = { 0xC0, 0xc5, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3};
Lev Walkin41ba1f22004-09-14 12:46:35 +0000102uint8_t buf_mo2[] = { 0x80, 0xbd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3,2};
103
104static void
Lev Walkined465432004-09-27 20:52:36 +0000105check_buf(uint8_t *buf, size_t bufsize, double verify, const char *sample, const char *canonical_sample) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000106 REAL_t rn;
107 double val;
108 uint8_t *p, *end;
109 int ret;
110
111 printf("verify double value %.12f [", verify);
112 for(p = (uint8_t *)&verify, end = p + sizeof(double); p < end ; p++)
113 printf("%02x", *p);
114 printf("] (ilogb %d)\n", ilogb(verify));
115
116 rn.buf = 0;
117 rn.size = 0;
118
Lev Walkin5e033762004-09-29 13:26:15 +0000119 ret = asn_double2REAL(&rn, verify);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000120 assert(ret == 0);
121
122 printf("canonical DER: [");
123 for(p = rn.buf, end = p + rn.size; p < end; p++)
124 printf("%02x", *p);
125 printf("]\n");
126
127 rn.buf = buf;
128 rn.size = bufsize;
129
130 printf("received as: [");
131 for(p = rn.buf, end = p + rn.size; p < end; p++)
132 printf("%02x", *p);
133 printf("]\n");
134
Lev Walkin5e033762004-09-29 13:26:15 +0000135 ret = asn_REAL2double(&rn, &val);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000136 assert(ret == 0);
137
138 printf("%.12f vs %.12f\n", verify, val);
139
140 assert(val == verify);
Lev Walkined465432004-09-27 20:52:36 +0000141
142 check_str_repr(val, sample, canonical_sample);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000143}
144
145int
146main() {
147 REAL_t rn;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000148 static const double zero = 0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000149
150 memset(&rn, 0, sizeof(rn));
151
Lev Walkined465432004-09-27 20:52:36 +0000152 check(&rn, 0.0, "0", "0");
153 check(&rn, -0.0, "-0", "-0"); /* minus-zero */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000154 check(&rn, zero/zero, "<NOT-A-NUMBER/>", "<NOT-A-NUMBER/>");
155 check(&rn, 1.0/zero, "<PLUS-INFINITY/>", "<PLUS-INFINITY/>");
156 check(&rn, -1.0/zero, "<MINUS-INFINITY/>", "<MINUS-INFINITY/>");
Lev Walkined465432004-09-27 20:52:36 +0000157 check(&rn, 1.0, "1.0", "1.0E0");
158 check(&rn, -1.0, "-1.0", "-1.0E0");
159 check(&rn, 1.5, "1.5", "1.5E0");
160 check(&rn, 0.1, "0.1", "1.0E-1");
161 check(&rn, 0.33333, "0.33333", "3.3333E-1");
162 check(&rn, 2, "2.0", "2.0E0");
163 check(&rn, 2.1, "2.1", "2.1E0");
164 check(&rn, 3, "3.0", "3.0E0");
165 check(&rn, 3.1, "3.1", "3.1E0");
166 check(&rn, 3.14, "3.14", "3.14E0");
167 check(&rn, 3.1415, "3.1415", "3.1415E0");
168 check(&rn, 3.141592, "3.141592", "3.141592E0");
169 check(&rn, 3.14159265, "3.14159265", "3.14159265E0");
170 check(&rn, -3.14159265, "-3.14159265", "-3.14159265E0");
171 check(&rn, 14159265.0, "14159265.0", "1.4159265E7");
172 check(&rn, -123456789123456789.0, "-123456789123456784.0", "-1.234567891234568E17");
173 check(&rn, 0.00000000001, "0.0", "9.999999999999999E-12");
174 check(&rn, 0.00000000002, "0.0", "2.0E-11");
175 check(&rn, 0.00000000009, "0.0", "9.0E-11");
176 check(&rn, 0.0000000000000000000001, "0.0", "1.0E-22");
177 check(&rn, 0.000000000000000000000000000001, "0.0", "1.0E-30"); /* proved 2B a problem */
178 check(&rn,-0.000000000000000000000000000001, "-0.0", "-1.0E-30"); /* proved 2B a problem */
179 check(&rn, 0.0000000000010000000001000000000001, 0, 0);
180 check(&rn, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
181 check(&rn, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
182 check(&rn,-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
183 check(&rn,-3.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
184 check(&rn, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333, 0, 0);
185 check(&rn, -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, 0, 0);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000186
Lev Walkined465432004-09-27 20:52:36 +0000187 check_buf(buf_1_0, sizeof(buf_1_0), 1.0, "1.0", "1.0E0");
188 check_buf(buf_1_1, sizeof(buf_1_1), 1.1, "1.1", "1.1E0");
189 check_buf(buf_3_14, sizeof(buf_3_14), 3.14, "3.14", "3.14E0");
190 check_buf(buf_mo1, sizeof(buf_mo1), -3.14, "-3.14", "-3.14E0");
191 check_buf(buf_mo2, sizeof(buf_mo2), 3.14, "3.14", "3.14E0");
Lev Walkin41ba1f22004-09-14 12:46:35 +0000192
193 return 0;
194}