summaryrefslogtreecommitdiff
path: root/src/unitnumber.cpp
blob: fc56bf654600759f705925a739ae61562101568e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "unitnumber.h"

#include "number.h"

#include <bu/sio.h>

using namespace Bu;

UnitNumber::UnitNumber()
{
    setName("Number");
    add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::packed1),
            "packed1", Bu::UnitSuite::expectPass );
    add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::parse1),
            "parse1", Bu::UnitSuite::expectPass );
    add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::multiply1),
            "multiply1", Bu::UnitSuite::expectPass );
    add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::number1),
            "number1", Bu::UnitSuite::expectPass );
    add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::compare1),
            "compare1", Bu::UnitSuite::expectPass );
    add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::radix1),
            "radix1", Bu::UnitSuite::expectPass );
    add( static_cast<Bu::UnitSuite::Test>(&UnitNumber::fraction1),
            "fraction1", Bu::UnitSuite::expectPass );
}

UnitNumber::~UnitNumber()
{
}

void UnitNumber::packed1()
{
    PackedIntArray a(4);
    a.append( 3 );
    a.append( 9 );
    a.append( 5 );

    unitTest( a.toString() == "593" );
    unitTest( a.toBitString() == "0101 1001 0011" );
    unitTest( PackedIntArray( 4, 10 ).toString() == "0000000000" );

    PackedIntArray b(5);
    for( int j = 0; j < 16; j++ )
    {
        b.append( 21 );
        if( b[j] != 21 )
        {
            throw Bu::ExceptionBase(
                Bu::String("Error at position %1").arg( j ).end().getStr()
                );
        }
    }
}

void UnitNumber::parse1()
{
    unitTest( Number("121932631356500531347203169112635269").toString() ==
        "121932631356500531347203169112635269" );
}

void UnitNumber::multiply1()
{
    unitTest(Number("123456789") * Number("987654321") == "121932631112635269");
    unitTest(
        Number("123456789123456789") * Number("987654321987654321") ==
            "121932631356500531347203169112635269"
        );
}

#define mathTest( anum, op, bnum, answ ) \
    unitTest( (Number(anum) op Number(bnum)).toString() == answ )

void UnitNumber::number1()
{
    mathTest("1000902491523000321", +, "3004392012498000700",
            "4005294504021001021");
    mathTest("1000902491523000321", *, "3004392012498000700",
            "3007103450821050020096034077958224700");

    mathTest("-872", +, "123", "-749");
    mathTest("728", +, "-51", "677");
    mathTest("44", +, "-55", "-11");
    mathTest("44", +, "-66", "-22");
    mathTest("44", -, "-66", "110");
    mathTest("44", -, "66", "-22");
    mathTest("7814", *, "24", "187536");
    mathTest("12345", *, "678", "8369910");
    mathTest("3592846", *, "944634757", "3393927208148422");
    mathTest("3592846", *, "", "0");
    mathTest("123", *, "-45", "-5535");
    mathTest("-123", *, "45", "-5535");
    mathTest("-123", *, "-45", "5535");
    mathTest("123", /, "45", "2");
    mathTest("12345", /, "45", "274");
    mathTest("3007103450821050020096034077958224700", +, "898239467",
            "3007103450821050020096034078856464167");
    mathTest("3007103450821050020096034077958224700", -, "898239467",
            "3007103450821050020096034077059985233");
    mathTest("3007103450821050020096034077958224700", *, "898239467",
            "2701099000879360682431400938999032202794234900");
    mathTest("3007103450821050020096034077958224700", /, "898239467",
            "3347774798700812397164501432");
    mathTest("3007103450821050020096034077958224700", %, "898239467",
            "357807956");

    mathTest("983429807324875233421784598754987439873472349875329853298732", +,
            "18446744073709551615",
            "983429807324875233421784598754987439873490796619403562850347");
    mathTest("983429807324875233421784598754987439873472349875329853298732", -,
            "18446744073709551615",
            "983429807324875233421784598754987439873453903131256143747117");
    mathTest("983429807324875233421784598754987439873472349875329853298732", *,
            "18446744073709551615",
            "1814107797017946840561430060771417697390414826725906096177022"
            "9365481264368052180");
    mathTest("983429807324875233421784598754987439873472349875329853298732", /,
            "18446744073709551615",
            "53311836679431538487701428703997840383479");
    mathTest("983429807324875233421784598754987439873472349875329853298732", %,
            "18446744073709551615",
            "2034904753109530147");
}

#define compcheck( anum, op, bnum ) \
    a = #anum; b = #bnum; \
    unitTest( ((a op b) == (anum op bnum)) )
    
void UnitNumber::compare1()
{
    Number a, b;

    compcheck( 5, >, 10 );
    compcheck( 10, >, 5 );
    compcheck( 5, >, 5 );
    compcheck( 7, >, 5 );
    compcheck( 5, >, 7 );
    compcheck( 123, >, 122 );
    compcheck( 123, >, 123 );
    compcheck( 123, >, 124 );
    compcheck( -123, >, 122 );
    compcheck( -123, >, -122 );
    compcheck( -122, >, -123 );
    compcheck( 123, >, -122 );
   
    compcheck( 5, <, 10 );
    compcheck( 10, <, 5 );
    compcheck( 5, <, 5 );
    compcheck( 7, <, 5 );
    compcheck( 5, <, 7 );
    compcheck( 123, <, 122 );
    compcheck( 123, <, 123 );
    compcheck( 123, <, 124 );
    compcheck( -123, <, 122 );
    compcheck( -123, <, -122 );
    compcheck( -122, <, -123 );
    compcheck( 123, <, -122 );
    
    compcheck( 5, >=, 10 );
    compcheck( 10, >=, 5 );
    compcheck( 5, >=, 5 );
    compcheck( 7, >=, 5 );
    compcheck( 5, >=, 7 );
    compcheck( 123, >=, 122 );
    compcheck( 123, >=, 123 );
    compcheck( 123, >=, 124 );
    compcheck( -123, >=, 122 );
    compcheck( -123, >=, -122 );
    compcheck( -122, >=, -123 );
    compcheck( 123, >=, -122 );
   
    compcheck( 5, <=, 10 );
    compcheck( 10, <=, 5 );
    compcheck( 5, <=, 5 );
    compcheck( 7, <=, 5 );
    compcheck( 5, <=, 7 );
    compcheck( 123, <=, 122 );
    compcheck( 123, <=, 123 );
    compcheck( 123, <=, 124 );
    compcheck( -123, <=, 122 );
    compcheck( -123, <=, -122 );
    compcheck( -122, <=, -123 );
    compcheck( 123, <=, -122 );

    
    a.setScale( 8 );
    b.setScale( 8 );
    compcheck( 10.1, >, 10.4 );
    compcheck( 10.1, >, 10.1 );
    compcheck( 10.4, >, 10.1 );
    compcheck( 10.413, >, 10.413 );
    compcheck( 10.41329135, >, 10.41329134 );
    compcheck( 10.41329134, >, 10.41329135 );
    compcheck( 10.41329135, >, 10.41329135 );
    compcheck( -123.3, >, 123.2 );
    compcheck( -123.3, >, -123.2 );
    compcheck( -123.3, >, -123.3 );
    compcheck( -123.3, >, -123.2 );
    compcheck( 123.3, >, -123.2 );
    
    compcheck( 10.1, <, 10.4 );
    compcheck( 10.1, <, 10.1 );
    compcheck( 10.4, <, 10.1 );
    compcheck( 10.413, <, 10.413 );
    compcheck( 10.41329135, <, 10.41329134 );
    compcheck( 10.41329134, <, 10.41329135 );
    compcheck( 10.41329135, <, 10.41329135 );
    compcheck( -123.3, <, 123.2 );
    compcheck( -123.3, <, -123.2 );
    compcheck( -123.3, <, -123.3 );
    compcheck( -123.3, <, -123.2 );
    compcheck( 123.3, <, -123.2 );
    
    compcheck( 10.1, >=, 10.4 );
    compcheck( 10.1, >=, 10.1 );
    compcheck( 10.4, >=, 10.1 );
    compcheck( 10.413, >=, 10.413 );
    compcheck( 10.41329135, >=, 10.41329134 );
    compcheck( 10.41329134, >=, 10.41329135 );
    compcheck( 10.41329135, >=, 10.41329135 );
    compcheck( -123.3, >=, 123.2 );
    compcheck( -123.3, >=, -123.2 );
    compcheck( -123.3, >=, -123.3 );
    compcheck( -123.3, >=, -123.2 );
    compcheck( 123.3, >=, -123.2 );
   
    compcheck( 10.1, <=, 10.4 );
    compcheck( 10.1, <=, 10.1 );
    compcheck( 10.4, <=, 10.1 );
    compcheck( 10.413, <=, 10.413 );
    compcheck( 10.41329135, <=, 10.41329134 );
    compcheck( 10.41329134, <=, 10.41329135 );
    compcheck( 10.41329135, <=, 10.41329135 );
    compcheck( -123.3, <=, 123.2 );
    compcheck( -123.3, <=, -123.2 );
    compcheck( -123.3, <=, -123.3 );
    compcheck( -123.3, <=, -123.2 );
    compcheck( 123.3, <=, -123.2 );
}

void UnitNumber::radix1()
{
    Number a( 10, 16 ), b( 10, 16 );

    a = "1f8a72bbce3";
    b = "9ea8cb3";
    unitTest( (a+b).toString() == "1f8b1164996" );
    unitTest( (a-b).toString() == "1f89d413030" );
    unitTest( (a/b).toString() == "32e4.4826b6b542" );
    unitTest( (a*b).toString() == "138c3eb3e7715f36b9" );
}

#define mathTestS( sc, anum, op, bnum, answ ) \
    unitTest( (Number(anum, sc) op Number(bnum, sc)).toString() == answ )
#define mathTestP( sc, anum, op, bnum, answ ) \
    Bu::println(">>%1<<").arg((Number(anum, sc) op Number(bnum, sc)).toString())

void UnitNumber::fraction1()
{
    Number a( 8 ), b( 8 );

    mathTestS( 8, "123.456", +, "0.987", "124.443" );
    mathTestS( 8, "123.456", -, "0.987", "122.469" );
    mathTestS( 8, "123.456", *, "0.987", "121.851072" );
    mathTestS( 8, "123.456", /, "0.987", "125.08206686" );

    mathTestP( 5, "63.6", /, "504", "0.12619" );
    mathTestS( 5, "63.6", /, "504", "0.12619" );
    mathTestS( 8, "12", /, "4", "3" );
    mathTestS( 100, "9", /, "1.9", "4.7368421052631578947368421052631578947368421052631578947368421052631578947368421052631578947368421052" );
}