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