Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_bigint.d

raw
1 pure @safe unittest
2 {
3 import std.bigint;
4
5 ubyte[] magnitude = [1, 2, 3, 4, 5, 6];
6 auto b1 = BigInt(false, magnitude);
7 assert(cast(long) b1 == 0x01_02_03_04_05_06L);
8 auto b2 = BigInt(true, magnitude);
9 assert(cast(long) b2 == -0x01_02_03_04_05_06L);
10
11 }
12
13 @safe unittest
14 {
15 import std.bigint;
16
17 ulong data = 1_000_000_000_000;
18 auto bigData = BigInt(data);
19 assert(bigData == BigInt("1_000_000_000_000"));
20
21 }
22
23 @safe unittest
24 {
25 import std.bigint;
26
27 const(BigInt) b1 = BigInt("1_234_567_890");
28 BigInt b2 = BigInt(b1);
29 assert(b2 == BigInt("1_234_567_890"));
30
31 }
32
33 @safe unittest
34 {
35 import std.bigint;
36
37 auto b = BigInt("123");
38 b = 456;
39 assert(b == BigInt("456"));
40
41 }
42
43 @safe unittest
44 {
45 import std.bigint;
46
47 auto b1 = BigInt("123");
48 auto b2 = BigInt("456");
49 b2 = b1;
50 assert(b2 == BigInt("123"));
51
52 }
53
54 @safe unittest
55 {
56 import std.bigint;
57
58 auto b = BigInt("1_000_000_000");
59
60 b += 12345;
61 assert(b == BigInt("1_000_012_345"));
62
63 b /= 5;
64 assert(b == BigInt("200_002_469"));
65
66 }
67
68 @safe unittest
69 {
70 import std.bigint;
71
72 auto x = BigInt("123");
73 auto y = BigInt("321");
74 x += y;
75 assert(x == BigInt("444"));
76
77 }
78
79 @safe unittest
80 {
81 import std.bigint;
82
83 auto x = BigInt("123");
84 auto y = BigInt("456");
85 BigInt z = x * y;
86 assert(z == BigInt("56088"));
87
88 }
89
90 @safe unittest
91 {
92 import std.bigint;
93
94 auto x = BigInt("123");
95 x *= 300;
96 assert(x == BigInt("36900"));
97
98 }
99
100 @safe unittest
101 {
102 import std.bigint;
103
104 auto x = BigInt("1_000_000_500");
105 long l = 1_000_000L;
106 ulong ul = 2_000_000UL;
107 int i = 500_000;
108 short s = 30_000;
109
110 assert(is(typeof(x % l) == long) && x % l == 500L);
111 assert(is(typeof(x % ul) == BigInt) && x % ul == BigInt(500));
112 assert(is(typeof(x % i) == int) && x % i == 500);
113 assert(is(typeof(x % s) == int) && x % s == 10500);
114
115 }
116
117 @safe unittest
118 {
119 import std.bigint;
120
121 auto x = BigInt("100");
122 BigInt y = 123 + x;
123 assert(y == BigInt("223"));
124
125 BigInt z = 123 - x;
126 assert(z == BigInt("23"));
127
128 // Dividing a built-in integer type by BigInt always results in
129 // something that fits in a built-in type, so the built-in type is
130 // returned, not BigInt.
131 assert(is(typeof(1000 / x) == int));
132 assert(1000 / x == 10);
133
134 }
135
136 @safe unittest
137 {
138 import std.bigint;
139
140 auto x = BigInt("1234");
141 assert(-x == BigInt("-1234"));
142
143 ++x;
144 assert(x == BigInt("1235"));
145
146 }
147
148 @safe unittest
149 {
150 import std.bigint;
151
152 // Note that when comparing a BigInt to a float or double the
153 // full precision of the BigInt is always considered, unlike
154 // when comparing an int to a float or a long to a double.
155 assert(BigInt(123456789) != cast(float) 123456789);
156
157 }
158
159 @safe unittest
160 {
161 import std.bigint;
162
163 // Non-zero values are regarded as true
164 auto x = BigInt("1");
165 auto y = BigInt("10");
166 assert(x);
167 assert(y);
168
169 // Zero value is regarded as false
170 auto z = BigInt("0");
171 assert(!z);
172
173 }
174
175 @safe unittest
176 {
177 import std.bigint;
178
179 import std.conv : to, ConvOverflowException;
180 import std.exception : assertThrown;
181
182 assert(BigInt("0").to!int == 0);
183
184 assert(BigInt("0").to!ubyte == 0);
185 assert(BigInt("255").to!ubyte == 255);
186 assertThrown!ConvOverflowException(BigInt("256").to!ubyte);
187 assertThrown!ConvOverflowException(BigInt("-1").to!ubyte);
188
189 }
190
191 @system unittest
192 {
193 import std.bigint;
194
195 assert(cast(float) BigInt("35540592535949172786332045140593475584")
196 == 35540592535949172786332045140593475584.0f);
197 assert(cast(double) BigInt("35540601499647381470685035515422441472")
198 == 35540601499647381470685035515422441472.0);
199 assert(cast(real) BigInt("35540601499647381470685035515422441472")
200 == 35540601499647381470685035515422441472.0L);
201
202 assert(cast(float) BigInt("-0x1345_6780_0000_0000_0000_0000_0000") == -0x1.3456_78p+108f );
203 assert(cast(double) BigInt("-0x1345_678a_bcde_f000_0000_0000_0000") == -0x1.3456_78ab_cdefp+108 );
204 assert(cast(real) BigInt("-0x1345_678a_bcde_f000_0000_0000_0000") == -0x1.3456_78ab_cdefp+108L);
205
206 }
207
208 @system unittest
209 {
210 import std.bigint;
211
212 // BigInts whose values cannot be exactly represented as float/double/real
213 // are rounded when cast to float/double/real. When cast to float or
214 // double or 64-bit real the rounding is strictly defined. When cast
215 // to extended-precision real the rounding rules vary by environment.
216
217 // BigInts that fall somewhere between two non-infinite floats/doubles
218 // are rounded to the closer value when cast to float/double.
219 assert(cast(float) BigInt(0x1aaa_aae7) == 0x1.aaa_aaep+28f);
220 assert(cast(float) BigInt(0x1aaa_aaff) == 0x1.aaa_ab0p+28f);
221 assert(cast(float) BigInt(-0x1aaa_aae7) == -0x1.aaaaaep+28f);
222 assert(cast(float) BigInt(-0x1aaa_aaff) == -0x1.aaaab0p+28f);
223
224 assert(cast(double) BigInt(0x1aaa_aaaa_aaaa_aa77) == 0x1.aaa_aaaa_aaaa_aa00p+60);
225 assert(cast(double) BigInt(0x1aaa_aaaa_aaaa_aaff) == 0x1.aaa_aaaa_aaaa_ab00p+60);
226 assert(cast(double) BigInt(-0x1aaa_aaaa_aaaa_aa77) == -0x1.aaa_aaaa_aaaa_aa00p+60);
227 assert(cast(double) BigInt(-0x1aaa_aaaa_aaaa_aaff) == -0x1.aaa_aaaa_aaaa_ab00p+60);
228
229 // BigInts that fall exactly between two non-infinite floats/doubles
230 // are rounded away from zero when cast to float/double. (Note that
231 // in most environments this is NOT the same rounding rule rule used
232 // when casting int/long to float/double.)
233 assert(cast(float) BigInt(0x1aaa_aaf0) == 0x1.aaa_ab0p+28f);
234 assert(cast(float) BigInt(-0x1aaa_aaf0) == -0x1.aaaab0p+28f);
235
236 assert(cast(double) BigInt(0x1aaa_aaaa_aaaa_aa80) == 0x1.aaa_aaaa_aaaa_ab00p+60);
237 assert(cast(double) BigInt(-0x1aaa_aaaa_aaaa_aa80) == -0x1.aaa_aaaa_aaaa_ab00p+60);
238
239 // BigInts that are bounded on one side by the largest positive or
240 // most negative finite float/double and on the other side by infinity
241 // or -infinity are rounded as if in place of infinity was the value
242 // `2^^(T.max_exp)` when cast to float/double.
243 assert(cast(float) BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999") == float.infinity);
244 assert(cast(float) BigInt("-999_999_999_999_999_999_999_999_999_999_999_999_999") == -float.infinity);
245
246 assert(cast(double) BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999") < double.infinity);
247 assert(cast(real) BigInt("999_999_999_999_999_999_999_999_999_999_999_999_999") < real.infinity);
248
249 }
250
251 @safe unittest
252 {
253 import std.bigint;
254
255 const(BigInt) x = BigInt("123");
256 BigInt y = cast() x; // cast away const
257 assert(y == x);
258
259 }
260
261 @safe unittest
262 {
263 import std.bigint;
264
265 auto x = BigInt("100");
266 auto y = BigInt("10");
267 int z = 50;
268 const int w = 200;
269
270 assert(y < x);
271 assert(x > z);
272 assert(z > y);
273 assert(x < w);
274
275 }
276
277 @safe unittest
278 {
279 import std.bigint;
280
281 auto x = BigInt("0x1abc_de80_0000_0000_0000_0000_0000_0000");
282 BigInt y = x - 1;
283 BigInt z = x + 1;
284
285 double d = 0x1.abcde8p124;
286 assert(y < d);
287 assert(z > d);
288 assert(x >= d && x <= d);
289
290 // Note that when comparing a BigInt to a float or double the
291 // full precision of the BigInt is always considered, unlike
292 // when comparing an int to a float or a long to a double.
293 assert(BigInt(123456789) < cast(float) 123456789);
294
295 }
296
297 @safe unittest
298 {
299 import std.bigint;
300
301 auto b = BigInt("12345");
302 long l = b.toLong();
303 assert(l == 12345);
304
305 }
306
307 @safe unittest
308 {
309 import std.bigint;
310
311 auto big = BigInt("5_000_000");
312 auto i = big.toInt();
313 assert(i == 5_000_000);
314
315 // Numbers that are too big to fit into an int will be clamped to int.max.
316 auto tooBig = BigInt("5_000_000_000");
317 i = tooBig.toInt();
318 assert(i == int.max);
319
320 }
321
322 @safe unittest
323 {
324 import std.bigint;
325
326 import std.format : format;
327
328 auto x = BigInt("1_000_000");
329 x *= 12345;
330
331 assert(format("%d", x) == "12345000000");
332 assert(format("%x", x) == "2_dfd1c040");
333 assert(format("%X", x) == "2_DFD1C040");
334 assert(format("%o", x) == "133764340100");
335
336 }
337
338 @safe pure unittest
339 {
340 import std.bigint;
341
342 string[BigInt] aa;
343 aa[BigInt(123)] = "abc";
344 aa[BigInt(456)] = "def";
345
346 assert(aa[BigInt(123)] == "abc");
347 assert(aa[BigInt(456)] == "def");
348
349 }
350
351 @safe pure unittest
352 {
353 import std.bigint;
354
355 auto a = BigInt("1000");
356 assert(a.ulongLength() == 1);
357 assert(a.getDigit(0) == 1000);
358
359 assert(a.uintLength() == 1);
360 assert(a.getDigit!uint(0) == 1000);
361
362 auto b = BigInt("2_000_000_000_000_000_000_000_000_000");
363 assert(b.ulongLength() == 2);
364 assert(b.getDigit(0) == 4584946418820579328);
365 assert(b.getDigit(1) == 108420217);
366
367 assert(b.uintLength() == 3);
368 assert(b.getDigit!uint(0) == 3489660928);
369 assert(b.getDigit!uint(1) == 1067516025);
370 assert(b.getDigit!uint(2) == 108420217);
371
372 }
373
374 @safe unittest
375 {
376 import std.bigint;
377
378 BigInt a = "9588669891916142";
379 BigInt b = "7452469135154800";
380 auto c = a * b;
381 assert(c == BigInt("71459266416693160362545788781600"));
382 auto d = b * a;
383 assert(d == BigInt("71459266416693160362545788781600"));
384 assert(d == c);
385 d = c * BigInt("794628672112");
386 assert(d == BigInt("56783581982794522489042432639320434378739200"));
387 auto e = c + d;
388 assert(e == BigInt("56783581982865981755459125799682980167520800"));
389 auto f = d + c;
390 assert(f == e);
391 auto g = f - c;
392 assert(g == d);
393 g = f - d;
394 assert(g == c);
395 e = 12345678;
396 g = c + e;
397 auto h = g / b;
398 auto i = g % b;
399 assert(h == a);
400 assert(i == e);
401 BigInt j = "-0x9A56_57f4_7B83_AB78";
402 BigInt k = j;
403 j ^^= 11;
404 assert(k ^^ 11 == j);
405 }
406
407 @safe pure unittest
408 {
409 import std.bigint;
410
411 auto x = BigInt("123");
412 x *= 1000;
413 x += 456;
414
415 auto xstr = x.toDecimalString();
416 assert(xstr == "123456");
417 }
418
419 @safe unittest
420 {
421 import std.bigint;
422
423 auto x = BigInt("123");
424 x *= 1000;
425 x += 456;
426
427 auto xstr = x.toHex();
428 assert(xstr == "1E240");
429 }
430
431 nothrow pure @safe unittest
432 {
433 import std.bigint;
434
435 assert((-1).absUnsign == 1);
436 assert(1.absUnsign == 1);
437 }
438
439 @safe pure nothrow unittest
440 {
441 import std.bigint;
442
443 auto a = BigInt(123);
444 auto b = BigInt(25);
445 BigInt q, r;
446
447 divMod(a, b, q, r);
448
449 assert(q == 4);
450 assert(r == 23);
451 assert(q * b + r == a);
452 }
453
454 @safe unittest
455 {
456 import std.bigint;
457
458 BigInt base = BigInt("123456789012345678901234567890");
459 BigInt exponent = BigInt("1234567890123456789012345678901234567");
460 BigInt modulus = BigInt("1234567");
461
462 BigInt result = powmod(base, exponent, modulus);
463 assert(result == 359079);
464 }
465