]> git.lizzy.rs Git - rust.git/blob - library/core/tests/num/bignum.rs
Update rand in the stdlib tests, and remove the getrandom feature from it
[rust.git] / library / core / tests / num / bignum.rs
1 use core::num::bignum::tests::Big8x3 as Big;
2 use core::num::bignum::Big32x40;
3
4 #[test]
5 #[should_panic]
6 fn test_from_u64_overflow() {
7     Big::from_u64(0x1000000);
8 }
9
10 #[test]
11 fn test_add() {
12     assert_eq!(*Big::from_small(3).add(&Big::from_small(4)), Big::from_small(7));
13     assert_eq!(*Big::from_small(3).add(&Big::from_small(0)), Big::from_small(3));
14     assert_eq!(*Big::from_small(0).add(&Big::from_small(3)), Big::from_small(3));
15     assert_eq!(*Big::from_small(3).add(&Big::from_u64(0xfffe)), Big::from_u64(0x10001));
16     assert_eq!(*Big::from_u64(0xfedc).add(&Big::from_u64(0x789)), Big::from_u64(0x10665));
17     assert_eq!(*Big::from_u64(0x789).add(&Big::from_u64(0xfedc)), Big::from_u64(0x10665));
18 }
19
20 #[test]
21 #[should_panic]
22 fn test_add_overflow_1() {
23     Big::from_small(1).add(&Big::from_u64(0xffffff));
24 }
25
26 #[test]
27 #[should_panic]
28 fn test_add_overflow_2() {
29     Big::from_u64(0xffffff).add(&Big::from_small(1));
30 }
31
32 #[test]
33 fn test_add_small() {
34     assert_eq!(*Big::from_small(3).add_small(4), Big::from_small(7));
35     assert_eq!(*Big::from_small(3).add_small(0), Big::from_small(3));
36     assert_eq!(*Big::from_small(0).add_small(3), Big::from_small(3));
37     assert_eq!(*Big::from_small(7).add_small(250), Big::from_u64(257));
38     assert_eq!(*Big::from_u64(0x7fff).add_small(1), Big::from_u64(0x8000));
39     assert_eq!(*Big::from_u64(0x2ffe).add_small(0x35), Big::from_u64(0x3033));
40     assert_eq!(*Big::from_small(0xdc).add_small(0x89), Big::from_u64(0x165));
41 }
42
43 #[test]
44 #[should_panic]
45 fn test_add_small_overflow() {
46     Big::from_u64(0xffffff).add_small(1);
47 }
48
49 #[test]
50 fn test_sub() {
51     assert_eq!(*Big::from_small(7).sub(&Big::from_small(4)), Big::from_small(3));
52     assert_eq!(*Big::from_u64(0x10665).sub(&Big::from_u64(0x789)), Big::from_u64(0xfedc));
53     assert_eq!(*Big::from_u64(0x10665).sub(&Big::from_u64(0xfedc)), Big::from_u64(0x789));
54     assert_eq!(*Big::from_u64(0x10665).sub(&Big::from_u64(0x10664)), Big::from_small(1));
55     assert_eq!(*Big::from_u64(0x10665).sub(&Big::from_u64(0x10665)), Big::from_small(0));
56 }
57
58 #[test]
59 #[should_panic]
60 fn test_sub_underflow_1() {
61     Big::from_u64(0x10665).sub(&Big::from_u64(0x10666));
62 }
63
64 #[test]
65 #[should_panic]
66 fn test_sub_underflow_2() {
67     Big::from_small(0).sub(&Big::from_u64(0x123456));
68 }
69
70 #[test]
71 fn test_mul_small() {
72     assert_eq!(*Big::from_small(7).mul_small(5), Big::from_small(35));
73     assert_eq!(*Big::from_small(0xff).mul_small(0xff), Big::from_u64(0xfe01));
74     assert_eq!(*Big::from_u64(0xffffff / 13).mul_small(13), Big::from_u64(0xffffff));
75 }
76
77 #[test]
78 #[should_panic]
79 fn test_mul_small_overflow() {
80     Big::from_u64(0x800000).mul_small(2);
81 }
82
83 #[test]
84 fn test_mul_pow2() {
85     assert_eq!(*Big::from_small(0x7).mul_pow2(4), Big::from_small(0x70));
86     assert_eq!(*Big::from_small(0xff).mul_pow2(1), Big::from_u64(0x1fe));
87     assert_eq!(*Big::from_small(0xff).mul_pow2(12), Big::from_u64(0xff000));
88     assert_eq!(*Big::from_small(0x1).mul_pow2(23), Big::from_u64(0x800000));
89     assert_eq!(*Big::from_u64(0x123).mul_pow2(0), Big::from_u64(0x123));
90     assert_eq!(*Big::from_u64(0x123).mul_pow2(7), Big::from_u64(0x9180));
91     assert_eq!(*Big::from_u64(0x123).mul_pow2(15), Big::from_u64(0x918000));
92     assert_eq!(*Big::from_small(0).mul_pow2(23), Big::from_small(0));
93 }
94
95 #[test]
96 #[should_panic]
97 fn test_mul_pow2_overflow_1() {
98     Big::from_u64(0x1).mul_pow2(24);
99 }
100
101 #[test]
102 #[should_panic]
103 fn test_mul_pow2_overflow_2() {
104     Big::from_u64(0x123).mul_pow2(16);
105 }
106
107 #[test]
108 fn test_mul_pow5() {
109     assert_eq!(*Big::from_small(42).mul_pow5(0), Big::from_small(42));
110     assert_eq!(*Big::from_small(1).mul_pow5(2), Big::from_small(25));
111     assert_eq!(*Big::from_small(1).mul_pow5(4), Big::from_u64(25 * 25));
112     assert_eq!(*Big::from_small(4).mul_pow5(3), Big::from_u64(500));
113     assert_eq!(*Big::from_small(140).mul_pow5(2), Big::from_u64(25 * 140));
114     assert_eq!(*Big::from_small(25).mul_pow5(1), Big::from_small(125));
115     assert_eq!(*Big::from_small(125).mul_pow5(7), Big::from_u64(9765625));
116     assert_eq!(*Big::from_small(0).mul_pow5(127), Big::from_small(0));
117 }
118
119 #[test]
120 #[should_panic]
121 fn test_mul_pow5_overflow_1() {
122     Big::from_small(1).mul_pow5(12);
123 }
124
125 #[test]
126 #[should_panic]
127 fn test_mul_pow5_overflow_2() {
128     Big::from_small(230).mul_pow5(8);
129 }
130
131 #[test]
132 fn test_mul_digits() {
133     assert_eq!(*Big::from_small(3).mul_digits(&[5]), Big::from_small(15));
134     assert_eq!(*Big::from_small(0xff).mul_digits(&[0xff]), Big::from_u64(0xfe01));
135     assert_eq!(*Big::from_u64(0x123).mul_digits(&[0x56, 0x4]), Big::from_u64(0x4edc2));
136     assert_eq!(*Big::from_u64(0x12345).mul_digits(&[0x67]), Big::from_u64(0x7530c3));
137     assert_eq!(*Big::from_small(0x12).mul_digits(&[0x67, 0x45, 0x3]), Big::from_u64(0x3ae13e));
138     assert_eq!(*Big::from_u64(0xffffff / 13).mul_digits(&[13]), Big::from_u64(0xffffff));
139     assert_eq!(*Big::from_small(13).mul_digits(&[0x3b, 0xb1, 0x13]), Big::from_u64(0xffffff));
140 }
141
142 #[test]
143 #[should_panic]
144 fn test_mul_digits_overflow_1() {
145     Big::from_u64(0x800000).mul_digits(&[2]);
146 }
147
148 #[test]
149 #[should_panic]
150 fn test_mul_digits_overflow_2() {
151     Big::from_u64(0x1000).mul_digits(&[0, 0x10]);
152 }
153
154 #[test]
155 fn test_div_rem_small() {
156     let as_val = |(q, r): (&mut Big, u8)| (q.clone(), r);
157     assert_eq!(as_val(Big::from_small(0xff).div_rem_small(15)), (Big::from_small(17), 0));
158     assert_eq!(as_val(Big::from_small(0xff).div_rem_small(16)), (Big::from_small(15), 15));
159     assert_eq!(as_val(Big::from_small(3).div_rem_small(40)), (Big::from_small(0), 3));
160     assert_eq!(
161         as_val(Big::from_u64(0xffffff).div_rem_small(123)),
162         (Big::from_u64(0xffffff / 123), (0xffffffu64 % 123) as u8)
163     );
164     assert_eq!(
165         as_val(Big::from_u64(0x10000).div_rem_small(123)),
166         (Big::from_u64(0x10000 / 123), (0x10000u64 % 123) as u8)
167     );
168 }
169
170 #[test]
171 fn test_div_rem() {
172     fn div_rem(n: u64, d: u64) -> (Big, Big) {
173         let mut q = Big::from_small(42);
174         let mut r = Big::from_small(42);
175         Big::from_u64(n).div_rem(&Big::from_u64(d), &mut q, &mut r);
176         (q, r)
177     }
178     assert_eq!(div_rem(1, 1), (Big::from_small(1), Big::from_small(0)));
179     assert_eq!(div_rem(4, 3), (Big::from_small(1), Big::from_small(1)));
180     assert_eq!(div_rem(1, 7), (Big::from_small(0), Big::from_small(1)));
181     assert_eq!(div_rem(45, 9), (Big::from_small(5), Big::from_small(0)));
182     assert_eq!(div_rem(103, 9), (Big::from_small(11), Big::from_small(4)));
183     assert_eq!(div_rem(123456, 77), (Big::from_u64(1603), Big::from_small(25)));
184     assert_eq!(div_rem(0xffff, 1), (Big::from_u64(0xffff), Big::from_small(0)));
185     assert_eq!(div_rem(0xeeee, 0xffff), (Big::from_small(0), Big::from_u64(0xeeee)));
186     assert_eq!(div_rem(2_000_000, 2), (Big::from_u64(1_000_000), Big::from_u64(0)));
187 }
188
189 #[test]
190 fn test_is_zero() {
191     assert!(Big::from_small(0).is_zero());
192     assert!(!Big::from_small(3).is_zero());
193     assert!(!Big::from_u64(0x123).is_zero());
194     assert!(!Big::from_u64(0xffffff).sub(&Big::from_u64(0xfffffe)).is_zero());
195     assert!(Big::from_u64(0xffffff).sub(&Big::from_u64(0xffffff)).is_zero());
196 }
197
198 #[test]
199 fn test_get_bit() {
200     let x = Big::from_small(0b1101);
201     assert_eq!(x.get_bit(0), 1);
202     assert_eq!(x.get_bit(1), 0);
203     assert_eq!(x.get_bit(2), 1);
204     assert_eq!(x.get_bit(3), 1);
205     let y = Big::from_u64(1 << 15);
206     assert_eq!(y.get_bit(14), 0);
207     assert_eq!(y.get_bit(15), 1);
208     assert_eq!(y.get_bit(16), 0);
209 }
210
211 #[test]
212 #[should_panic]
213 fn test_get_bit_out_of_range() {
214     Big::from_small(42).get_bit(24);
215 }
216
217 #[test]
218 fn test_bit_length() {
219     for i in 0..8 * 3 {
220         // 010000...000
221         assert_eq!(Big::from_small(1).mul_pow2(i).bit_length(), i + 1);
222     }
223     for i in 1..8 * 3 - 1 {
224         // 010000...001
225         assert_eq!(Big::from_small(1).mul_pow2(i).add(&Big::from_small(1)).bit_length(), i + 1);
226         // 110000...000
227         assert_eq!(Big::from_small(3).mul_pow2(i).bit_length(), i + 2);
228     }
229     assert_eq!(Big::from_small(0).bit_length(), 0);
230     assert_eq!(Big::from_small(1).bit_length(), 1);
231     assert_eq!(Big::from_small(5).bit_length(), 3);
232     assert_eq!(Big::from_small(0x18).bit_length(), 5);
233     assert_eq!(Big::from_u64(0x4073).bit_length(), 15);
234     assert_eq!(Big::from_u64(0xffffff).bit_length(), 24);
235 }
236
237 #[test]
238 fn test_bit_length_32x40() {
239     for i in 0..32 * 40 {
240         // 010000...000
241         assert_eq!(Big32x40::from_small(1).mul_pow2(i).bit_length(), i + 1);
242     }
243     for i in 1..32 * 40 - 1 {
244         // 010000...001
245         assert_eq!(
246             Big32x40::from_small(1).mul_pow2(i).add(&Big32x40::from_small(1)).bit_length(),
247             i + 1
248         );
249         // 110000...000
250         assert_eq!(Big32x40::from_small(3).mul_pow2(i).bit_length(), i + 2);
251     }
252     assert_eq!(Big32x40::from_small(0).bit_length(), 0);
253     assert_eq!(Big32x40::from_small(1).bit_length(), 1);
254     assert_eq!(Big32x40::from_small(5).bit_length(), 3);
255     assert_eq!(Big32x40::from_small(0x18).bit_length(), 5);
256     assert_eq!(Big32x40::from_u64(0x4073).bit_length(), 15);
257     assert_eq!(Big32x40::from_u64(0xffffff).bit_length(), 24);
258     assert_eq!(Big32x40::from_u64(0xffffffffffffffff).bit_length(), 64);
259 }
260
261 #[test]
262 fn test_ord() {
263     assert!(Big::from_u64(0) < Big::from_u64(0xffffff));
264     assert!(Big::from_u64(0x102) < Big::from_u64(0x201));
265 }
266
267 #[test]
268 fn test_fmt() {
269     assert_eq!(format!("{:?}", Big::from_u64(0)), "0x0");
270     assert_eq!(format!("{:?}", Big::from_u64(0x1)), "0x1");
271     assert_eq!(format!("{:?}", Big::from_u64(0x12)), "0x12");
272     assert_eq!(format!("{:?}", Big::from_u64(0x123)), "0x1_23");
273     assert_eq!(format!("{:?}", Big::from_u64(0x1234)), "0x12_34");
274     assert_eq!(format!("{:?}", Big::from_u64(0x12345)), "0x1_23_45");
275     assert_eq!(format!("{:?}", Big::from_u64(0x123456)), "0x12_34_56");
276 }