diff options
author | Mike Buland <mike@xagasoft.com> | 2013-05-10 08:36:15 -0600 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2013-05-10 08:36:15 -0600 |
commit | dc0139c1880d1500b3e275b09781116613862f5f (patch) | |
tree | 479c22d0003d14a227040aa939de622067278c55 | |
parent | 0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1 (diff) | |
download | clic-dc0139c1880d1500b3e275b09781116613862f5f.tar.gz clic-dc0139c1880d1500b3e275b09781116613862f5f.tar.bz2 clic-dc0139c1880d1500b3e275b09781116613862f5f.tar.xz clic-dc0139c1880d1500b3e275b09781116613862f5f.zip |
Added more checking & unit-tests.
Diffstat (limited to '')
-rw-r--r-- | src/main.cpp | 376 | ||||
-rw-r--r-- | src/number.cpp | 5 | ||||
-rw-r--r-- | src/options.cpp | 33 | ||||
-rw-r--r-- | src/options.h | 1 | ||||
-rw-r--r-- | src/unitnumber.cpp | 236 | ||||
-rw-r--r-- | src/unitnumber.h | 5 |
6 files changed, 293 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 | } |
diff --git a/src/number.cpp b/src/number.cpp index cab7f46..cda829e 100644 --- a/src/number.cpp +++ b/src/number.cpp | |||
@@ -12,6 +12,8 @@ Number::Number( int iScale, int iRadix ) : | |||
12 | aInt( RadixToBits( iRadix ) ), | 12 | aInt( RadixToBits( iRadix ) ), |
13 | aFrac( aInt.getBitWidth(), iScale ) | 13 | aFrac( aInt.getBitWidth(), iScale ) |
14 | { | 14 | { |
15 | if( iRadix <= 1 ) | ||
16 | throw Bu::ExceptionBase("Invalid radix. Radix must be > 1."); | ||
15 | } | 17 | } |
16 | 18 | ||
17 | Number::Number( const Bu::String &sData, int iScale, int iRadix ) : | 19 | Number::Number( const Bu::String &sData, int iScale, int iRadix ) : |
@@ -21,6 +23,9 @@ Number::Number( const Bu::String &sData, int iScale, int iRadix ) : | |||
21 | aInt( RadixToBits( iRadix ) ), | 23 | aInt( RadixToBits( iRadix ) ), |
22 | aFrac( aInt.getBitWidth(), iScale ) | 24 | aFrac( aInt.getBitWidth(), iScale ) |
23 | { | 25 | { |
26 | if( iRadix <= 1 ) | ||
27 | throw Bu::ExceptionBase("Invalid radix. Radix must be > 1."); | ||
28 | |||
24 | set( sData ); | 29 | set( sData ); |
25 | } | 30 | } |
26 | 31 | ||
diff --git a/src/options.cpp b/src/options.cpp index 59dd2b4..9ade56a 100644 --- a/src/options.cpp +++ b/src/options.cpp | |||
@@ -21,6 +21,10 @@ Options::Options( int argc, char *argv[] ) : | |||
21 | addOption( Bu::slot(this, &Options::isPrime), 'p', "is-prime", | 21 | addOption( Bu::slot(this, &Options::isPrime), 'p', "is-prime", |
22 | "Tests every parameter after to see if it is prime then prints out " | 22 | "Tests every parameter after to see if it is prime then prints out " |
23 | "the ones that are prime. Set radix first."); | 23 | "the ones that are prime. Set radix first."); |
24 | addOption( Bu::slot(this, &Options::convert), 'c', "convert", | ||
25 | "Convert the provided number to the given radix in the format " | ||
26 | "number:to-radix or from-radix:number:to-radix. The radix should " | ||
27 | "be provided in base-10"); | ||
24 | addHelpOption('h', "help", "This help"); | 28 | addHelpOption('h', "help", "This help"); |
25 | 29 | ||
26 | parse( argc, argv ); | 30 | parse( argc, argv ); |
@@ -118,3 +122,32 @@ int Options::isPrime( Bu::StringArray aArgs ) | |||
118 | return aArgs.getSize(); | 122 | return aArgs.getSize(); |
119 | } | 123 | } |
120 | 124 | ||
125 | int Options::convert( Bu::StringArray aArgs ) | ||
126 | { | ||
127 | for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ ) | ||
128 | { | ||
129 | Bu::StringList lBits = (*i).split(':'); | ||
130 | if( lBits.getSize() < 2 || lBits.getSize() > 3 ) | ||
131 | throw Bu::ExceptionBase("Invalid format"); | ||
132 | |||
133 | int iFromRadix = iRadix; | ||
134 | int iToRadix; | ||
135 | Number n; | ||
136 | if( lBits.getSize() == 2 ) | ||
137 | { | ||
138 | iToRadix = strtol( lBits.last().getStr(), 0, 10 ); | ||
139 | n = Number( lBits.first(), 0, iFromRadix ); | ||
140 | } | ||
141 | else if( lBits.getSize() == 3 ) | ||
142 | { | ||
143 | iFromRadix = strtol( lBits.first().getStr(), 0, 10 ); | ||
144 | iToRadix = strtol( lBits.last().getStr(), 0, 10 ); | ||
145 | n = Number( *(lBits.begin()+1), 0, iFromRadix ); | ||
146 | } | ||
147 | Bu::println("%1").arg( n.toRadix( iToRadix ).toString() ); | ||
148 | } | ||
149 | |||
150 | exit( 0 ); | ||
151 | return 0; | ||
152 | } | ||
153 | |||
diff --git a/src/options.h b/src/options.h index 1085e1e..e0b7f33 100644 --- a/src/options.h +++ b/src/options.h | |||
@@ -16,6 +16,7 @@ private: | |||
16 | int selfTest( Bu::StringArray aArgs ); | 16 | int selfTest( Bu::StringArray aArgs ); |
17 | int textPrimes( Bu::StringArray aArgs ); | 17 | int textPrimes( Bu::StringArray aArgs ); |
18 | int isPrime( Bu::StringArray aArgs ); | 18 | int isPrime( Bu::StringArray aArgs ); |
19 | int convert( Bu::StringArray aArgs ); | ||
19 | 20 | ||
20 | int iScale; | 21 | int iScale; |
21 | int iRadix; | 22 | int iRadix; |
diff --git a/src/unitnumber.cpp b/src/unitnumber.cpp index 79797c9..92cf1b7 100644 --- a/src/unitnumber.cpp +++ b/src/unitnumber.cpp | |||
@@ -2,19 +2,57 @@ | |||
2 | 2 | ||
3 | #include "number.h" | 3 | #include "number.h" |
4 | 4 | ||
5 | #include <bu/sio.h> | ||
6 | |||
7 | using namespace Bu; | ||
8 | |||
5 | UnitNumber::UnitNumber() | 9 | UnitNumber::UnitNumber() |
6 | { | 10 | { |
7 | setName("Number"); | 11 | setName("Number"); |
12 | add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::packed1), | ||
13 | "packed1", Bu::UnitSuite::expectPass ); | ||
8 | add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::parse1), | 14 | add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::parse1), |
9 | "parse1", Bu::UnitSuite::expectPass ); | 15 | "parse1", Bu::UnitSuite::expectPass ); |
10 | add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::multiply1), | 16 | add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::multiply1), |
11 | "multiply1", Bu::UnitSuite::expectPass ); | 17 | "multiply1", Bu::UnitSuite::expectPass ); |
18 | add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::number1), | ||
19 | "number1", Bu::UnitSuite::expectPass ); | ||
20 | add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::compare1), | ||
21 | "compare1", Bu::UnitSuite::expectPass ); | ||
22 | add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::radix1), | ||
23 | "radix1", Bu::UnitSuite::expectPass ); | ||
24 | add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::fraction1), | ||
25 | "fraction1", Bu::UnitSuite::expectPass ); | ||
12 | } | 26 | } |
13 | 27 | ||
14 | UnitNumber::~UnitNumber() | 28 | UnitNumber::~UnitNumber() |
15 | { | 29 | { |
16 | } | 30 | } |
17 | 31 | ||
32 | void UnitNumber::packed1() | ||
33 | { | ||
34 | PackedIntArray a(4); | ||
35 | a.append( 3 ); | ||
36 | a.append( 9 ); | ||
37 | a.append( 5 ); | ||
38 | |||
39 | unitTest( a.toString() == "593" ); | ||
40 | unitTest( a.toBitString() == "0101 1001 0011" ); | ||
41 | unitTest( PackedIntArray( 4, 10 ).toString() == "0000000000" ); | ||
42 | |||
43 | PackedIntArray b(5); | ||
44 | for( int j = 0; j < 16; j++ ) | ||
45 | { | ||
46 | b.append( 21 ); | ||
47 | if( b[j] != 21 ) | ||
48 | { | ||
49 | throw Bu::ExceptionBase( | ||
50 | Bu::String("Error at position %1").arg( j ).end().getStr() | ||
51 | ); | ||
52 | } | ||
53 | } | ||
54 | } | ||
55 | |||
18 | void UnitNumber::parse1() | 56 | void UnitNumber::parse1() |
19 | { | 57 | { |
20 | unitTest( Number("121932631356500531347203169112635269").toString() == | 58 | unitTest( Number("121932631356500531347203169112635269").toString() == |
@@ -30,3 +68,201 @@ void UnitNumber::multiply1() | |||
30 | ); | 68 | ); |
31 | } | 69 | } |
32 | 70 | ||
71 | #define mathTest( anum, op, bnum, answ ) \ | ||
72 | unitTest( (Number(anum) op Number(bnum)).toString() == answ ) | ||
73 | |||
74 | void UnitNumber::number1() | ||
75 | { | ||
76 | mathTest("1000902491523000321", +, "3004392012498000700", | ||
77 | "4005294504021001021"); | ||
78 | mathTest("1000902491523000321", *, "3004392012498000700", | ||
79 | "3007103450821050020096034077958224700"); | ||
80 | |||
81 | mathTest("-872", +, "123", "-749"); | ||
82 | mathTest("728", +, "-51", "677"); | ||
83 | mathTest("44", +, "-55", "-11"); | ||
84 | mathTest("44", +, "-66", "-22"); | ||
85 | mathTest("44", -, "-66", "110"); | ||
86 | mathTest("44", -, "66", "-22"); | ||
87 | mathTest("7814", *, "24", "187536"); | ||
88 | mathTest("12345", *, "678", "8369910"); | ||
89 | mathTest("3592846", *, "944634757", "3393927208148422"); | ||
90 | mathTest("3592846", *, "", "0"); | ||
91 | mathTest("123", *, "-45", "-5535"); | ||
92 | mathTest("-123", *, "45", "-5535"); | ||
93 | mathTest("-123", *, "-45", "5535"); | ||
94 | mathTest("123", /, "45", "2"); | ||
95 | mathTest("12345", /, "45", "274"); | ||
96 | mathTest("3007103450821050020096034077958224700", +, "898239467", | ||
97 | "3007103450821050020096034078856464167"); | ||
98 | mathTest("3007103450821050020096034077958224700", -, "898239467", | ||
99 | "3007103450821050020096034077059985233"); | ||
100 | mathTest("3007103450821050020096034077958224700", *, "898239467", | ||
101 | "2701099000879360682431400938999032202794234900"); | ||
102 | mathTest("3007103450821050020096034077958224700", /, "898239467", | ||
103 | "3347774798700812397164501432"); | ||
104 | mathTest("3007103450821050020096034077958224700", %, "898239467", | ||
105 | "357807956"); | ||
106 | |||
107 | mathTest("983429807324875233421784598754987439873472349875329853298732", +, | ||
108 | "18446744073709551615", | ||
109 | "983429807324875233421784598754987439873490796619403562850347"); | ||
110 | mathTest("983429807324875233421784598754987439873472349875329853298732", -, | ||
111 | "18446744073709551615", | ||
112 | "983429807324875233421784598754987439873453903131256143747117"); | ||
113 | mathTest("983429807324875233421784598754987439873472349875329853298732", *, | ||
114 | "18446744073709551615", | ||
115 | "1814107797017946840561430060771417697390414826725906096177022" | ||
116 | "9365481264368052180"); | ||
117 | mathTest("983429807324875233421784598754987439873472349875329853298732", /, | ||
118 | "18446744073709551615", | ||
119 | "53311836679431538487701428703997840383479"); | ||
120 | mathTest("983429807324875233421784598754987439873472349875329853298732", %, | ||
121 | "18446744073709551615", | ||
122 | "2034904753109530147"); | ||
123 | } | ||
124 | |||
125 | #define compcheck( anum, op, bnum ) \ | ||
126 | a = #anum; b = #bnum; \ | ||
127 | unitTest( ((a op b) == (anum op bnum)) ) | ||
128 | |||
129 | void UnitNumber::compare1() | ||
130 | { | ||
131 | Number a, b; | ||
132 | |||
133 | compcheck( 5, >, 10 ); | ||
134 | compcheck( 10, >, 5 ); | ||
135 | compcheck( 5, >, 5 ); | ||
136 | compcheck( 7, >, 5 ); | ||
137 | compcheck( 5, >, 7 ); | ||
138 | compcheck( 123, >, 122 ); | ||
139 | compcheck( 123, >, 123 ); | ||
140 | compcheck( 123, >, 124 ); | ||
141 | compcheck( -123, >, 122 ); | ||
142 | compcheck( -123, >, -122 ); | ||
143 | compcheck( -122, >, -123 ); | ||
144 | compcheck( 123, >, -122 ); | ||
145 | |||
146 | compcheck( 5, <, 10 ); | ||
147 | compcheck( 10, <, 5 ); | ||
148 | compcheck( 5, <, 5 ); | ||
149 | compcheck( 7, <, 5 ); | ||
150 | compcheck( 5, <, 7 ); | ||
151 | compcheck( 123, <, 122 ); | ||
152 | compcheck( 123, <, 123 ); | ||
153 | compcheck( 123, <, 124 ); | ||
154 | compcheck( -123, <, 122 ); | ||
155 | compcheck( -123, <, -122 ); | ||
156 | compcheck( -122, <, -123 ); | ||
157 | compcheck( 123, <, -122 ); | ||
158 | |||
159 | compcheck( 5, >=, 10 ); | ||
160 | compcheck( 10, >=, 5 ); | ||
161 | compcheck( 5, >=, 5 ); | ||
162 | compcheck( 7, >=, 5 ); | ||
163 | compcheck( 5, >=, 7 ); | ||
164 | compcheck( 123, >=, 122 ); | ||
165 | compcheck( 123, >=, 123 ); | ||
166 | compcheck( 123, >=, 124 ); | ||
167 | compcheck( -123, >=, 122 ); | ||
168 | compcheck( -123, >=, -122 ); | ||
169 | compcheck( -122, >=, -123 ); | ||
170 | compcheck( 123, >=, -122 ); | ||
171 | |||
172 | compcheck( 5, <=, 10 ); | ||
173 | compcheck( 10, <=, 5 ); | ||
174 | compcheck( 5, <=, 5 ); | ||
175 | compcheck( 7, <=, 5 ); | ||
176 | compcheck( 5, <=, 7 ); | ||
177 | compcheck( 123, <=, 122 ); | ||
178 | compcheck( 123, <=, 123 ); | ||
179 | compcheck( 123, <=, 124 ); | ||
180 | compcheck( -123, <=, 122 ); | ||
181 | compcheck( -123, <=, -122 ); | ||
182 | compcheck( -122, <=, -123 ); | ||
183 | compcheck( 123, <=, -122 ); | ||
184 | |||
185 | |||
186 | a.setScale( 8 ); | ||
187 | b.setScale( 8 ); | ||
188 | compcheck( 10.1, >, 10.4 ); | ||
189 | compcheck( 10.1, >, 10.1 ); | ||
190 | compcheck( 10.4, >, 10.1 ); | ||
191 | compcheck( 10.413, >, 10.413 ); | ||
192 | compcheck( 10.41329135, >, 10.41329134 ); | ||
193 | compcheck( 10.41329134, >, 10.41329135 ); | ||
194 | compcheck( 10.41329135, >, 10.41329135 ); | ||
195 | compcheck( -123.3, >, 123.2 ); | ||
196 | compcheck( -123.3, >, -123.2 ); | ||
197 | compcheck( -123.3, >, -123.3 ); | ||
198 | compcheck( -123.3, >, -123.2 ); | ||
199 | compcheck( 123.3, >, -123.2 ); | ||
200 | |||
201 | compcheck( 10.1, <, 10.4 ); | ||
202 | compcheck( 10.1, <, 10.1 ); | ||
203 | compcheck( 10.4, <, 10.1 ); | ||
204 | compcheck( 10.413, <, 10.413 ); | ||
205 | compcheck( 10.41329135, <, 10.41329134 ); | ||
206 | compcheck( 10.41329134, <, 10.41329135 ); | ||
207 | compcheck( 10.41329135, <, 10.41329135 ); | ||
208 | compcheck( -123.3, <, 123.2 ); | ||
209 | compcheck( -123.3, <, -123.2 ); | ||
210 | compcheck( -123.3, <, -123.3 ); | ||
211 | compcheck( -123.3, <, -123.2 ); | ||
212 | compcheck( 123.3, <, -123.2 ); | ||
213 | |||
214 | compcheck( 10.1, >=, 10.4 ); | ||
215 | compcheck( 10.1, >=, 10.1 ); | ||
216 | compcheck( 10.4, >=, 10.1 ); | ||
217 | compcheck( 10.413, >=, 10.413 ); | ||
218 | compcheck( 10.41329135, >=, 10.41329134 ); | ||
219 | compcheck( 10.41329134, >=, 10.41329135 ); | ||
220 | compcheck( 10.41329135, >=, 10.41329135 ); | ||
221 | compcheck( -123.3, >=, 123.2 ); | ||
222 | compcheck( -123.3, >=, -123.2 ); | ||
223 | compcheck( -123.3, >=, -123.3 ); | ||
224 | compcheck( -123.3, >=, -123.2 ); | ||
225 | compcheck( 123.3, >=, -123.2 ); | ||
226 | |||
227 | compcheck( 10.1, <=, 10.4 ); | ||
228 | compcheck( 10.1, <=, 10.1 ); | ||
229 | compcheck( 10.4, <=, 10.1 ); | ||
230 | compcheck( 10.413, <=, 10.413 ); | ||
231 | compcheck( 10.41329135, <=, 10.41329134 ); | ||
232 | compcheck( 10.41329134, <=, 10.41329135 ); | ||
233 | compcheck( 10.41329135, <=, 10.41329135 ); | ||
234 | compcheck( -123.3, <=, 123.2 ); | ||
235 | compcheck( -123.3, <=, -123.2 ); | ||
236 | compcheck( -123.3, <=, -123.3 ); | ||
237 | compcheck( -123.3, <=, -123.2 ); | ||
238 | compcheck( 123.3, <=, -123.2 ); | ||
239 | } | ||
240 | |||
241 | void UnitNumber::radix1() | ||
242 | { | ||
243 | Number a( 10, 16 ), b( 10, 16 ); | ||
244 | |||
245 | a = "1f8a72bbce3"; | ||
246 | b = "9ea8cb3"; | ||
247 | unitTest( (a+b).toString() == "1f8b1164996" ); | ||
248 | unitTest( (a-b).toString() == "1f89d413030" ); | ||
249 | unitTest( (a/b).toString() == "32e4.4826b6b542" ); | ||
250 | unitTest( (a*b).toString() == "138c3eb3e7715f36b9" ); | ||
251 | } | ||
252 | |||
253 | #define mathTestS( sc, anum, op, bnum, answ ) \ | ||
254 | unitTest( (Number(anum, sc) op Number(bnum, sc)).toString() == answ ) | ||
255 | |||
256 | void UnitNumber::fraction1() | ||
257 | { | ||
258 | Number a( 8 ), b( 8 ); | ||
259 | |||
260 | mathTestS( 8, "123.456", +, "0.987", "124.443" ); | ||
261 | mathTestS( 8, "123.456", -, "0.987", "122.469" ); | ||
262 | mathTestS( 8, "123.456", *, "0.987", "121.851072" ); | ||
263 | mathTestS( 8, "123.456", /, "0.987", "125.08206686" ); | ||
264 | |||
265 | mathTestS( 8, "12", /, "4", "3" ); | ||
266 | mathTestS( 100, "9", /, "1.9", "4.7368421052631578947368421052631578947368421052631578947368421052631578947368421052631578947368421052" ); | ||
267 | } | ||
268 | |||
diff --git a/src/unitnumber.h b/src/unitnumber.h index 76496b9..83da27f 100644 --- a/src/unitnumber.h +++ b/src/unitnumber.h | |||
@@ -9,8 +9,13 @@ public: | |||
9 | UnitNumber(); | 9 | UnitNumber(); |
10 | virtual ~UnitNumber(); | 10 | virtual ~UnitNumber(); |
11 | 11 | ||
12 | void packed1(); | ||
12 | void parse1(); | 13 | void parse1(); |
13 | void multiply1(); | 14 | void multiply1(); |
15 | void number1(); | ||
16 | void compare1(); | ||
17 | void radix1(); | ||
18 | void fraction1(); | ||
14 | }; | 19 | }; |
15 | 20 | ||
16 | #endif | 21 | #endif |