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
mod small_rational;
pub use big_rational::{MutNumerDenom, ParseRationalError, ValidRational};
pub use rational::small_rational::SmallRational;
#[cfg(test)]
mod tests {
use Rational;
use gmp_mpfr_sys::gmp;
use ops::Pow;
use std::{i32, u32};
use std::cmp::Ordering;
use std::mem;
#[test]
fn check_ref_op() {
let lhs = Rational::from((-13, 27));
let rhs = Rational::from((15, 101));
let pu = 30_u32;
let pi = -15_i32;
assert_eq!(Rational::from(-&lhs), -lhs.clone());
assert_eq!(Rational::from(&lhs + &rhs), lhs.clone() + &rhs);
assert_eq!(Rational::from(&lhs - &rhs), lhs.clone() - &rhs);
assert_eq!(Rational::from(&lhs * &rhs), lhs.clone() * &rhs);
assert_eq!(Rational::from(&lhs / &rhs), lhs.clone() / &rhs);
assert_eq!(Rational::from(&lhs << pu), lhs.clone() << pu);
assert_eq!(Rational::from(&lhs >> pu), lhs.clone() >> pu);
assert_eq!(Rational::from((&lhs).pow(pu)), lhs.clone().pow(pu));
assert_eq!(Rational::from(&lhs << pi), lhs.clone() << pi);
assert_eq!(Rational::from(&lhs >> pi), lhs.clone() >> pi);
assert_eq!(Rational::from((&lhs).pow(pi)), lhs.clone().pow(pi));
}
#[test]
fn check_cmp_frac() {
let zero = Rational::new();
let u = [0, 1, 100, u32::MAX];
let s = [i32::MIN, -100, -1, 0, 1, 100, i32::MAX];
for &n in &u {
for &d in &u {
if d != 0 {
let ans = 0.partial_cmp(&n);
assert_eq!(zero.partial_cmp(&(n, d)), ans);
assert_eq!(zero.partial_cmp(&Rational::from((n, d))), ans);
}
}
for &d in &s {
if d != 0 {
let mut ans = 0.partial_cmp(&n);
if d < 0 {
ans = ans.map(Ordering::reverse);
}
assert_eq!(zero.partial_cmp(&(n, d)), ans);
assert_eq!(zero.partial_cmp(&Rational::from((n, d))), ans);
}
}
}
for &n in &s {
for &d in &u {
if d != 0 {
let ans = 0.partial_cmp(&n);
assert_eq!(zero.partial_cmp(&(n, d)), ans);
assert_eq!(zero.partial_cmp(&Rational::from((n, d))), ans);
}
}
for &d in &s {
if d != 0 {
let mut ans = 0.partial_cmp(&n);
if d < 0 {
ans = ans.map(Ordering::reverse);
}
assert_eq!(zero.partial_cmp(&(n, d)), ans);
assert_eq!(zero.partial_cmp(&Rational::from((n, d))), ans);
}
}
}
}
#[test]
fn check_from_str() {
let bad_strings = [
(" 1", None),
("1/-1", None),
("1/+3", None),
("1/0", None),
("1 / 1", None),
("2/", None),
("/2", None),
("++1", None),
("+-1", None),
("1/80", Some(8)),
("0xf", Some(16)),
("9", Some(9)),
];
for &(s, radix) in bad_strings.into_iter() {
assert!(Rational::valid_str_radix(s, radix.unwrap_or(10)).is_err());
}
let good_strings = [
("0", 10, 0, 1),
("+0/fC", 16, 0, 1),
("-0/10", 2, 0, 1),
("-99/3", 10, -33, 1),
("+Ce/fF", 16, 0xce, 0xff),
("-77/2", 8, -0o77, 2),
];
for &(s, radix, n, d) in good_strings.into_iter() {
let r = Rational::from_str_radix(s, radix).unwrap();
assert_eq!(*r.numer(), n);
assert_eq!(*r.denom(), d);
}
}
#[test]
fn check_formatting() {
let r = Rational::from((-11, 15));
assert_eq!(format!("{}", r), "-11/15");
assert_eq!(format!("{:?}", r), "-11/15");
assert_eq!(format!("{:b}", r), "-1011/1111");
assert_eq!(format!("{:#b}", r), "-0b1011/1111");
assert_eq!(format!("{:o}", r), "-13/17");
assert_eq!(format!("{:#o}", r), "-0o13/17");
assert_eq!(format!("{:x}", r), "-b/f");
assert_eq!(format!("{:X}", r), "-B/F");
assert_eq!(format!("{:8x}", r), " -b/f");
assert_eq!(format!("{:08X}", r), "-0000B/F");
assert_eq!(format!("{:#08x}", r), "-0x00b/f");
assert_eq!(format!("{:#8X}", r), " -0xB/F");
}
#[test]
fn check_no_nails() {
assert_eq!(gmp::NAIL_BITS, 0);
assert_eq!(gmp::NUMB_BITS, gmp::LIMB_BITS);
assert_eq!(gmp::NUMB_BITS as usize, 8 * mem::size_of::<gmp::limb_t>());
}
}