Skip to content

Commit da9c1fb

Browse files
committed
libstd: Add tests for BigRational
1 parent 76ecce0 commit da9c1fb

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

src/libstd/num/rational.rs

+56-20
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl<T: FromStrRadix + Clone + Integer + Ord>
283283
#[cfg(test)]
284284
mod test {
285285
use super::*;
286-
use core::num::{Zero,One,FromStrRadix};
286+
use core::num::{Zero,One,FromStrRadix,IntConvertible};
287287
use core::from_str::FromStr;
288288
289289
pub static _0 : Rational = Ratio { numer: 0, denom: 1};
@@ -293,6 +293,12 @@ mod test {
293293
pub static _3_2: Rational = Ratio { numer: 3, denom: 2};
294294
pub static _neg1_2: Rational = Ratio { numer: -1, denom: 2};
295295
296+
pub fn to_big(n: Rational) -> BigRational {
297+
Ratio::new(
298+
IntConvertible::from_int(n.numer),
299+
IntConvertible::from_int(n.denom)
300+
)
301+
}
296302
297303
#[test]
298304
fn test_test_constants() {
@@ -340,45 +346,75 @@ mod test {
340346
341347
#[test]
342348
fn test_add() {
343-
assert_eq!(_1 + _1_2, _3_2);
344-
assert_eq!(_1 + _1, _2);
345-
assert_eq!(_1_2 + _3_2, _2);
346-
assert_eq!(_1_2 + _neg1_2, _0);
349+
fn test(a: Rational, b: Rational, c: Rational) {
350+
assert_eq!(a + b, c);
351+
assert_eq!(to_big(a) + to_big(b), to_big(c));
352+
}
353+
354+
test(_1, _1_2, _3_2);
355+
test(_1, _1, _2);
356+
test(_1_2, _3_2, _2);
357+
test(_1_2, _neg1_2, _0);
347358
}
348359
349360
#[test]
350361
fn test_sub() {
351-
assert_eq!(_1 - _1_2, _1_2);
352-
assert_eq!(_3_2 - _1_2, _1);
353-
assert_eq!(_1 - _neg1_2, _3_2);
362+
fn test(a: Rational, b: Rational, c: Rational) {
363+
assert_eq!(a - b, c);
364+
assert_eq!(to_big(a) - to_big(b), to_big(c))
365+
}
366+
367+
test(_1, _1_2, _1_2);
368+
test(_3_2, _1_2, _1);
369+
test(_1, _neg1_2, _3_2);
354370
}
355371
356372
#[test]
357373
fn test_mul() {
358-
assert_eq!(_1 * _1_2, _1_2);
359-
assert_eq!(_1_2 * _3_2, Ratio::new(3,4));
360-
assert_eq!(_1_2 * _neg1_2, Ratio::new(-1, 4));
374+
fn test(a: Rational, b: Rational, c: Rational) {
375+
assert_eq!(a * b, c);
376+
assert_eq!(to_big(a) * to_big(b), to_big(c))
377+
}
378+
379+
test(_1, _1_2, _1_2);
380+
test(_1_2, _3_2, Ratio::new(3,4));
381+
test(_1_2, _neg1_2, Ratio::new(-1, 4));
361382
}
362383
363384
#[test]
364385
fn test_div() {
365-
assert_eq!(_1 / _1_2, _2);
366-
assert_eq!(_3_2 / _1_2, _1 + _2);
367-
assert_eq!(_1 / _neg1_2, _neg1_2 + _neg1_2 + _neg1_2 + _neg1_2);
386+
fn test(a: Rational, b: Rational, c: Rational) {
387+
assert_eq!(a / b, c);
388+
assert_eq!(to_big(a) / to_big(b), to_big(c))
389+
}
390+
391+
test(_1, _1_2, _2);
392+
test(_3_2, _1_2, _1 + _2);
393+
test(_1, _neg1_2, _neg1_2 + _neg1_2 + _neg1_2 + _neg1_2);
368394
}
369395
370396
#[test]
371397
fn test_rem() {
372-
assert_eq!(_3_2 % _1, _1_2);
373-
assert_eq!(_2 % _neg1_2, _0);
374-
assert_eq!(_1_2 % _2, _1_2);
398+
fn test(a: Rational, b: Rational, c: Rational) {
399+
assert_eq!(a % b, c);
400+
assert_eq!(to_big(a) % to_big(b), to_big(c))
401+
}
402+
403+
test(_3_2, _1, _1_2);
404+
test(_2, _neg1_2, _0);
405+
test(_1_2, _2, _1_2);
375406
}
376407
377408
#[test]
378409
fn test_neg() {
379-
assert_eq!(-_0, _0);
380-
assert_eq!(-_1_2, _neg1_2);
381-
assert_eq!(-(-_1), _1);
410+
fn test(a: Rational, b: Rational) {
411+
assert_eq!(-a, b);
412+
assert_eq!(-to_big(a), to_big(b))
413+
}
414+
415+
test(_0, _0);
416+
test(_1_2, _neg1_2);
417+
test(-_1, _1);
382418
}
383419
#[test]
384420
fn test_zero() {

0 commit comments

Comments
 (0)