Debian Codegraph @

terps/tads/tads3/test/data/packfloats.t

raw
1 #charset "latin1"
2
3 /*
4 * File.packBytes floating point type tests
5 */
6
7 #include <tads.h>
8 #include <file.h>
9 #include <bignum.h>
10 #include <bytearr.h>
11 #include <strbuf.h>
12
13
14 main(args)
15 {
16 local fname = 'packfloats.bin';
17 writeTest(fname);
18 readTest(fname);
19 }
20
21 writeTest(fname)
22 {
23 local fp = File.openRawFile(fname, FileAccessWrite);
24
25 twrite(fp, 'd',
26 1.0 + 2.2204460492503131e-016,
27 222.22222222, 333.333333333333333, 444.444,
28 555.555, 666.666, 777.777, 888.888, 999.999,
29 2000.0002);
30
31 // sub-normal numbers
32 twrite(fp, 'd',
33 1.2250738585072011e-308, 6.329835701939e-309);
34
35 // FLT_EPSILON, FLT_MAX, FLT_MIN, 1+FLT_EPSILON, FLT_MIN minus epsilon
36 twrite(fp, 'f',
37 [1.192092896e-07, 3.402823466e+38, 1.175494351e-38,
38 1.0 + 1.192092896e-07, 1.075494347e-38]);
39
40 // DBL_EPSILON, DBL_MAX, DBL_MIN, 1+DBL_EPSILON, DBL_MIN minus epsilon
41 twrite(fp, 'd',
42 [2.2204460492503131e-016, 1.7976931348623158e+308,
43 2.2250738585072014e-308,
44 777.777,
45 888.888,
46 1.0 + 2.2204460492503131e-016,
47 1.2250738585072011e-308]);
48
49 fp.closeFile();
50 }
51
52 twrite(fp, fmt, [args])
53 {
54 fp.packBytes('C/a', '{{{<<fmt>>}}}');
55 fp.packBytes('C/<<fmt>>', args...);
56 }
57
58 readTest(fname)
59 {
60 local fp = File.openRawFile(fname, FileAccessRead);
61 local fileSize = fp.getFileSize();
62
63 while (fp.getPos() < fileSize)
64 {
65 try
66 {
67 tread(fp);
68 }
69 catch (Exception exc)
70 {
71 "Error: <<exc.displayException()>>\n";
72 break;
73 }
74 }
75
76 fp.setPos(0);
77 sayList(fp.unpackBytes('b*'));
78
79 fp.closeFile();
80 }
81
82 tread(fp)
83 {
84 local fmt = fp.unpackBytes('C/a')[1];
85 rexMatch('%{%{%{(.*)%}%}%}', fmt);
86 fmt = rexGroup(1)[3];
87 "Format code = <<fmt>> ";
88
89 fmt = 'C/(<<fmt>> X:<<fmt>> H:<<fmt>>!)';
90 local lst = fp.unpackBytes(fmt);
91 sayList(lst);
92 "\n";
93 }
94
95 tab(depth)
96 {
97 "<<makeString('\t', depth)>>";
98 }
99
100 sayList(lst, depth = 1)
101 {
102 "<<tab(depth)>>[\n";
103 foreach (local val in lst)
104 {
105 tab(depth);
106 switch (dataType(val))
107 {
108 case TypeList:
109 "\n";
110 sayList(val, depth + 1);
111 break;
112
113 case TypeSString:
114 " '<<rexReplace('[\'\\\n\t\b]', val, new function(m) {
115 return ['\'' -> '\\\'', '\\' -> '\\\\',
116 '\n' -> '\\n', '\t' -> '\\t', '\b' -> '\\b',
117 * -> '\\?'][m];
118 })>>'";
119 break;
120
121 case TypeInt:
122 " <<val>> (0x<<toString(val, 16)>>)";
123 break;
124
125 case TypeObject:
126 if (val.ofKind(BigNumber))
127 {
128 if (val.getScale() < -4 || val.getScale() > 6)
129 " <<val.formatString(-1, BignumExp | BignumExpSign)>>";
130 else
131 " <<val>>";
132
133 if (val.getWhole() == val && val.getScale() <= 20)
134 " (0x<<bignumToHex(val)>>)";
135 }
136 else if (val.ofKind(ByteArray))
137 {
138 " <<rexReplace('..', val.unpackBytes(1, 'H*')[1], '%* ')>>";
139 // " <<val.mapToString(new CharacterSet('latin1'))>>";
140 }
141 else
142 " (object)";
143 break;
144
145 default:
146 " <<val>>";
147 break;
148 }
149 "\n";
150 }
151 "<<tab(depth-1)>>]\n";
152 }
153
154 bignumToHex(val)
155 {
156 local s = new StringBuffer(30);
157 local neg = (val < 0);
158 if (neg)
159 val = -val;
160
161 while (val > 0)
162 {
163 local q = val.divideBy(16);
164 s.append(toString(toInteger(q[2]), 16));
165 val = q[1];
166 }
167
168 if (s.length() == 0)
169 s.append('0');
170 if (neg)
171 s.append('-');
172
173 for (local i = 1, local j = s.length() ; i < j ; ++i, --j)
174 {
175 local tmp = s[i];
176 s[i] = s[j];
177 s[j] = tmp;
178 }
179
180 return toString(s);
181 }