nanvm_lib/vm/bigint/
div.rs1use core::ops::Div;
2
3use crate::vm::{Any, BigInt, IVm};
4
5impl<A: IVm> Div for BigInt<A> {
6 type Output = Result<Self, Any<A>>;
7
8 fn div(self, rhs: Self) -> Self::Output {
10 self.div_mod(rhs).map(|(quotient, _)| quotient)
11 }
12}
13
14#[cfg(test)]
15mod tests {
16 use crate::vm::bigint::test_util::{T, int, pos};
17
18 #[test]
19 fn truncates_toward_zero() {
20 assert_eq!((int(10) / int(3)).unwrap(), int(3));
21 assert_eq!((int(-10) / int(3)).unwrap(), int(-3));
22 assert_eq!((int(10) / int(-3)).unwrap(), int(-3));
23 assert_eq!((int(-10) / int(-3)).unwrap(), int(3));
24 }
25
26 #[test]
27 fn exact_division() {
28 assert_eq!((int(12) / int(4)).unwrap(), int(3));
29 }
30
31 #[test]
32 fn zero_dividend() {
33 assert_eq!((int(0) / int(5)).unwrap(), T::default());
34 }
35
36 #[test]
37 fn dividend_smaller_than_divisor_truncates_to_zero() {
38 assert_eq!((int(3) / int(10)).unwrap(), T::default());
39 }
40
41 #[test]
42 fn zero_divisor_throws() {
43 assert!((int(5) / int(0)).is_err());
44 }
45
46 #[test]
47 fn multi_word_dividend() {
48 let a = pos([0, 1]);
50 let b: T = 2u64.into();
51 assert_eq!((a / b).unwrap(), (1u64 << 63).into());
52 }
53
54 #[test]
55 fn multi_word_result() {
56 let a = pos([0, 3]);
58 let b: T = 3u64.into();
59 assert_eq!((a / b).unwrap(), pos([0, 1]));
60 }
61}