diff options
Diffstat (limited to '')
-rw-r--r-- | src/main.cpp | 376 |
1 files changed, 13 insertions, 363 deletions
diff --git a/src/main.cpp b/src/main.cpp index c128dd8..4c34453 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -11,376 +11,26 @@ | |||
11 | #include <bu/streamstack.h> | 11 | #include <bu/streamstack.h> |
12 | using namespace Bu; | 12 | using namespace Bu; |
13 | 13 | ||
14 | void packedtest1() | 14 | int main( int argc, char *argv[] ) |
15 | { | ||
16 | println("-==- Packed Int Test -==-"); | ||
17 | |||
18 | PackedIntArray a(4); | ||
19 | a.append( 3 ); | ||
20 | a.append( 9 ); | ||
21 | a.append( 5 ); | ||
22 | |||
23 | println("%1").arg( a.toString() ); | ||
24 | println("%1").arg( a.toBitString() ); | ||
25 | println("%1").arg( PackedIntArray(4, 10).toString() ); | ||
26 | |||
27 | PackedIntArray b(5); | ||
28 | for( int j = 0; j < 16; j++ ) | ||
29 | { | ||
30 | b.append( 21 ); | ||
31 | if( b[j] != 21 ) | ||
32 | { | ||
33 | println("Error at position %1").arg( j ); | ||
34 | println("Raw: %1 (%2)").arg( b.toBitString() ).arg( b.toString() ); | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | |||
39 | void numbertest1() | ||
40 | { | ||
41 | println("-==- Number test -==-"); | ||
42 | |||
43 | Number a("1000902491523000321"); | ||
44 | Number b("3004392012498000700"); | ||
45 | |||
46 | println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); | ||
47 | |||
48 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); | ||
49 | |||
50 | a = "-872"; | ||
51 | b = "123"; | ||
52 | |||
53 | println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); | ||
54 | |||
55 | a = "728"; | ||
56 | b = "-51"; | ||
57 | |||
58 | println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); | ||
59 | |||
60 | a = "44"; | ||
61 | b = "-55"; | ||
62 | |||
63 | println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); | ||
64 | |||
65 | a = "44"; | ||
66 | b = "-66"; | ||
67 | |||
68 | println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); | ||
69 | |||
70 | a = "44"; | ||
71 | b = "-66"; | ||
72 | |||
73 | println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b ); | ||
74 | |||
75 | a = "44"; | ||
76 | b = "66"; | ||
77 | |||
78 | println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b ); | ||
79 | |||
80 | a = "7814"; | ||
81 | b = "24"; | ||
82 | |||
83 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); | ||
84 | |||
85 | a = "12345"; | ||
86 | b = "678"; | ||
87 | |||
88 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); | ||
89 | |||
90 | a = "3592846"; | ||
91 | b = "944634757"; | ||
92 | |||
93 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); | ||
94 | |||
95 | a = "3592846"; | ||
96 | b = ""; | ||
97 | |||
98 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); | ||
99 | |||
100 | a = "123"; | ||
101 | b = "-45"; | ||
102 | |||
103 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); | ||
104 | |||
105 | a = "-123"; | ||
106 | b = "45"; | ||
107 | |||
108 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); | ||
109 | |||
110 | a = "-123"; | ||
111 | b = "-45"; | ||
112 | |||
113 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); | ||
114 | |||
115 | a = "123"; | ||
116 | b = "45"; | ||
117 | |||
118 | println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); | ||
119 | |||
120 | a = "12345"; | ||
121 | b = "45"; | ||
122 | |||
123 | println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); | ||
124 | |||
125 | a = "3007103450821050020096034077958224700"; | ||
126 | b = "898239467"; | ||
127 | |||
128 | println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); | ||
129 | println("%1 %% %2 = %3").arg( a ).arg( b ).arg( a % b ); | ||
130 | |||
131 | a = "983429807324875233421784598754987439873472349875329853298732"; | ||
132 | b = "18446744073709551615"; | ||
133 | |||
134 | println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); | ||
135 | println("%1 %% %2 = %3").arg( a ).arg( b ).arg( a % b ); | ||
136 | } | ||
137 | |||
138 | #define compcheck( anum, op, bnum ) \ | ||
139 | a = #anum; b = #bnum; \ | ||
140 | println("%4: %1 " #op " %2 = %3").arg( a ).arg( b ). \ | ||
141 | arg( a op b ).arg( ((a op b) == (anum op bnum)) ? "pass" : "fail" ) | ||
142 | |||
143 | void numbertestcomp() | ||
144 | { | ||
145 | Number a, b; | ||
146 | |||
147 | println("-==- Greater Than -==-"); | ||
148 | compcheck( 5, >, 10 ); | ||
149 | compcheck( 10, >, 5 ); | ||
150 | compcheck( 5, >, 5 ); | ||
151 | compcheck( 7, >, 5 ); | ||
152 | compcheck( 5, >, 7 ); | ||
153 | compcheck( 123, >, 122 ); | ||
154 | compcheck( 123, >, 123 ); | ||
155 | compcheck( 123, >, 124 ); | ||
156 | compcheck( -123, >, 122 ); | ||
157 | compcheck( -123, >, -122 ); | ||
158 | compcheck( -122, >, -123 ); | ||
159 | compcheck( 123, >, -122 ); | ||
160 | |||
161 | println("-==- Less Than -==-"); | ||
162 | compcheck( 5, <, 10 ); | ||
163 | compcheck( 10, <, 5 ); | ||
164 | compcheck( 5, <, 5 ); | ||
165 | compcheck( 7, <, 5 ); | ||
166 | compcheck( 5, <, 7 ); | ||
167 | compcheck( 123, <, 122 ); | ||
168 | compcheck( 123, <, 123 ); | ||
169 | compcheck( 123, <, 124 ); | ||
170 | compcheck( -123, <, 122 ); | ||
171 | compcheck( -123, <, -122 ); | ||
172 | compcheck( -122, <, -123 ); | ||
173 | compcheck( 123, <, -122 ); | ||
174 | |||
175 | println("-==- Greater Than or Equal To -==-"); | ||
176 | compcheck( 5, >=, 10 ); | ||
177 | compcheck( 10, >=, 5 ); | ||
178 | compcheck( 5, >=, 5 ); | ||
179 | compcheck( 7, >=, 5 ); | ||
180 | compcheck( 5, >=, 7 ); | ||
181 | compcheck( 123, >=, 122 ); | ||
182 | compcheck( 123, >=, 123 ); | ||
183 | compcheck( 123, >=, 124 ); | ||
184 | compcheck( -123, >=, 122 ); | ||
185 | compcheck( -123, >=, -122 ); | ||
186 | compcheck( -122, >=, -123 ); | ||
187 | compcheck( 123, >=, -122 ); | ||
188 | |||
189 | println("-==- Less Than or Equal To -==-"); | ||
190 | compcheck( 5, <=, 10 ); | ||
191 | compcheck( 10, <=, 5 ); | ||
192 | compcheck( 5, <=, 5 ); | ||
193 | compcheck( 7, <=, 5 ); | ||
194 | compcheck( 5, <=, 7 ); | ||
195 | compcheck( 123, <=, 122 ); | ||
196 | compcheck( 123, <=, 123 ); | ||
197 | compcheck( 123, <=, 124 ); | ||
198 | compcheck( -123, <=, 122 ); | ||
199 | compcheck( -123, <=, -122 ); | ||
200 | compcheck( -122, <=, -123 ); | ||
201 | compcheck( 123, <=, -122 ); | ||
202 | |||
203 | println("-==-==- Non-Integer Test -==-==-"); | ||
204 | |||
205 | a.setScale( 8 ); | ||
206 | b.setScale( 8 ); | ||
207 | println("-==- Greater Than -==-"); | ||
208 | compcheck( 10.1, >, 10.4 ); | ||
209 | compcheck( 10.1, >, 10.1 ); | ||
210 | compcheck( 10.4, >, 10.1 ); | ||
211 | compcheck( 10.413, >, 10.413 ); | ||
212 | compcheck( 10.41329135, >, 10.41329134 ); | ||
213 | compcheck( 10.41329134, >, 10.41329135 ); | ||
214 | compcheck( 10.41329135, >, 10.41329135 ); | ||
215 | compcheck( -123.3, >, 123.2 ); | ||
216 | compcheck( -123.3, >, -123.2 ); | ||
217 | compcheck( -123.3, >, -123.3 ); | ||
218 | compcheck( -123.3, >, -123.2 ); | ||
219 | compcheck( 123.3, >, -123.2 ); | ||
220 | |||
221 | println("-==- Less Than -==-"); | ||
222 | compcheck( 10.1, <, 10.4 ); | ||
223 | compcheck( 10.1, <, 10.1 ); | ||
224 | compcheck( 10.4, <, 10.1 ); | ||
225 | compcheck( 10.413, <, 10.413 ); | ||
226 | compcheck( 10.41329135, <, 10.41329134 ); | ||
227 | compcheck( 10.41329134, <, 10.41329135 ); | ||
228 | compcheck( 10.41329135, <, 10.41329135 ); | ||
229 | compcheck( -123.3, <, 123.2 ); | ||
230 | compcheck( -123.3, <, -123.2 ); | ||
231 | compcheck( -123.3, <, -123.3 ); | ||
232 | compcheck( -123.3, <, -123.2 ); | ||
233 | compcheck( 123.3, <, -123.2 ); | ||
234 | |||
235 | println("-==- Greater Than or Equal To -==-"); | ||
236 | compcheck( 10.1, >=, 10.4 ); | ||
237 | compcheck( 10.1, >=, 10.1 ); | ||
238 | compcheck( 10.4, >=, 10.1 ); | ||
239 | compcheck( 10.413, >=, 10.413 ); | ||
240 | compcheck( 10.41329135, >=, 10.41329134 ); | ||
241 | compcheck( 10.41329134, >=, 10.41329135 ); | ||
242 | compcheck( 10.41329135, >=, 10.41329135 ); | ||
243 | compcheck( -123.3, >=, 123.2 ); | ||
244 | compcheck( -123.3, >=, -123.2 ); | ||
245 | compcheck( -123.3, >=, -123.3 ); | ||
246 | compcheck( -123.3, >=, -123.2 ); | ||
247 | compcheck( 123.3, >=, -123.2 ); | ||
248 | |||
249 | println("-==- Less Than or Equal To -==-"); | ||
250 | compcheck( 10.1, <=, 10.4 ); | ||
251 | compcheck( 10.1, <=, 10.1 ); | ||
252 | compcheck( 10.4, <=, 10.1 ); | ||
253 | compcheck( 10.413, <=, 10.413 ); | ||
254 | compcheck( 10.41329135, <=, 10.41329134 ); | ||
255 | compcheck( 10.41329134, <=, 10.41329135 ); | ||
256 | compcheck( 10.41329135, <=, 10.41329135 ); | ||
257 | compcheck( -123.3, <=, 123.2 ); | ||
258 | compcheck( -123.3, <=, -123.2 ); | ||
259 | compcheck( -123.3, <=, -123.3 ); | ||
260 | compcheck( -123.3, <=, -123.2 ); | ||
261 | compcheck( 123.3, <=, -123.2 ); | ||
262 | } | ||
263 | |||
264 | int getHob( int x ) | ||
265 | { | ||
266 | for( int j = 31; j >= 0; j-- ) | ||
267 | { | ||
268 | if( x&(1<<j) ) | ||
269 | { | ||
270 | return j+1; | ||
271 | } | ||
272 | } | ||
273 | return 0; | ||
274 | } | ||
275 | |||
276 | int getOrd( int x ) | ||
277 | { | ||
278 | return (int)(log(x)*0xb.8aa3b295c17fp-3+1.0); | ||
279 | } | ||
280 | |||
281 | double getTime() | ||
282 | { | ||
283 | timeval t; | ||
284 | gettimeofday( &t, NULL ); | ||
285 | return (double)t.tv_sec + (double)(t.tv_usec*.000001); | ||
286 | } | ||
287 | |||
288 | void ordertest() | ||
289 | { | 15 | { |
290 | for( int j = 2; j < 64; j++ ) | 16 | try |
291 | { | 17 | { |
292 | if( getOrd( j ) != getHob(j-1) ) | 18 | Options opt( argc, argv ); |
293 | println("Failure on radix %1").arg( j ); | ||
294 | } | ||
295 | 19 | ||
296 | double dStart, dEnd; | 20 | println("CliC - 0.06"); |
21 | println("Try \\exit, \\help, \\scale, and \\radix commands."); | ||
22 | println(""); | ||
297 | 23 | ||
298 | dStart = getTime(); | 24 | Lexer lex( sioRaw ); |
299 | for( int k = 0; k < 100000; k++ ) | 25 | lex.setScale( opt.getScale() ); |
300 | { | 26 | lex.setRadix( opt.getRadix() ); |
301 | for( int j = 2; j < 32; j++ ) | 27 | Parser parser( lex, sioRaw ); |
302 | { | 28 | parser.parse(); |
303 | getOrd( j ); | ||
304 | } | ||
305 | } | 29 | } |
306 | dEnd = getTime(); | 30 | catch( std::exception &e ) |
307 | |||
308 | println("getOrd: %1 in %2 (%3 cps)").arg( 30*100000 ).arg( dEnd-dStart ). | ||
309 | arg( (30*100000)/(dEnd-dStart) ); | ||
310 | |||
311 | dStart = getTime(); | ||
312 | for( int k = 0; k < 100000; k++ ) | ||
313 | { | 31 | { |
314 | for( int j = 2; j < 32; j++ ) | 32 | Bu::println("Error: %1").arg( e.what() ); |
315 | { | ||
316 | getHob( j-1 ); | ||
317 | } | ||
318 | } | 33 | } |
319 | dEnd = getTime(); | ||
320 | |||
321 | println("getHob: %1 in %2 (%3 cps)").arg( 30*100000 ).arg( dEnd-dStart ). | ||
322 | arg( (30*100000)/(dEnd-dStart) ); | ||
323 | } | ||
324 | |||
325 | void radixtest() | ||
326 | { | ||
327 | Number a( 10, 16 ), b( 10, 16 ); | ||
328 | |||
329 | a = "1f8a72bbce3"; | ||
330 | b = "9ea8cb3"; | ||
331 | println("%1 + %2 = %3"). | ||
332 | arg( a ). | ||
333 | arg( b ). | ||
334 | arg( (a + b) ); | ||
335 | println("%1 - %2 = %3"). | ||
336 | arg( a ). | ||
337 | arg( b ). | ||
338 | arg( (a - b) ); | ||
339 | println("%1 / %2 = %3").arg( a ).arg( b ). | ||
340 | arg( (a / b) ); | ||
341 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( (a * b) ); | ||
342 | } | ||
343 | |||
344 | void fractest() | ||
345 | { | ||
346 | Number a( 8 ), b( 8 ); | ||
347 | |||
348 | a = "123.456"; | ||
349 | b = "0.987"; | ||
350 | println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); | ||
351 | println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b ); | ||
352 | println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); | ||
353 | println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); | ||
354 | |||
355 | a = "12"; | ||
356 | b = "4"; | ||
357 | println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); | ||
358 | |||
359 | a = Number( "9", 100 ); | ||
360 | b = Number( "1.9", 100 ); | ||
361 | println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); | ||
362 | } | ||
363 | |||
364 | int main( int argc, char *argv[] ) | ||
365 | { | ||
366 | Options opt( argc, argv ); | ||
367 | |||
368 | println("CliC - 0.06"); | ||
369 | println("Try \\exit, \\help, \\scale, and \\radix commands."); | ||
370 | println(""); | ||
371 | |||
372 | // packedtest1(); | ||
373 | // numbertest1(); | ||
374 | // numbertestcomp(); | ||
375 | // radixtest(); | ||
376 | // fractest(); | ||
377 | // ordertest(); | ||
378 | |||
379 | Lexer lex( sioRaw ); | ||
380 | lex.setScale( opt.getScale() ); | ||
381 | lex.setRadix( opt.getRadix() ); | ||
382 | Parser parser( lex, sioRaw ); | ||
383 | parser.parse(); | ||
384 | 34 | ||
385 | return 0; | 35 | return 0; |
386 | } | 36 | } |