@@ -283,7 +283,7 @@ impl<T: FromStrRadix + Clone + Integer + Ord>
283
283
#[cfg(test)]
284
284
mod test {
285
285
use super::*;
286
- use core::num::{Zero,One,FromStrRadix};
286
+ use core::num::{Zero,One,FromStrRadix,IntConvertible };
287
287
use core::from_str::FromStr;
288
288
289
289
pub static _0 : Rational = Ratio { numer: 0, denom: 1};
@@ -293,6 +293,12 @@ mod test {
293
293
pub static _3_2: Rational = Ratio { numer: 3, denom: 2};
294
294
pub static _neg1_2: Rational = Ratio { numer: -1, denom: 2};
295
295
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
+ }
296
302
297
303
#[test]
298
304
fn test_test_constants() {
@@ -340,45 +346,75 @@ mod test {
340
346
341
347
#[test]
342
348
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);
347
358
}
348
359
349
360
#[test]
350
361
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);
354
370
}
355
371
356
372
#[test]
357
373
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));
361
382
}
362
383
363
384
#[test]
364
385
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);
368
394
}
369
395
370
396
#[test]
371
397
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);
375
406
}
376
407
377
408
#[test]
378
409
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);
382
418
}
383
419
#[test]
384
420
fn test_zero() {
0 commit comments