summaryrefslogtreecommitdiff
path: root/src/unitnumber.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/unitnumber.cpp')
-rw-r--r--src/unitnumber.cpp236
1 files changed, 236 insertions, 0 deletions
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
7using namespace Bu;
8
5UnitNumber::UnitNumber() 9UnitNumber::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
14UnitNumber::~UnitNumber() 28UnitNumber::~UnitNumber()
15{ 29{
16} 30}
17 31
32void 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
18void UnitNumber::parse1() 56void 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
74void 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
129void 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
241void 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
256void 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