| 1 |
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution and at |
| 3 |
// http://rust-lang.org/COPYRIGHT. |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 |
// option. This file may not be copied, modified, or distributed |
| 9 |
// except according to those terms. |
| 10 |
|
| 11 |
extern crate num as num_renamed; |
| 12 |
#[macro_use] |
| 13 |
extern crate num_derive; |
| 14 |
|
| 15 |
#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)] |
| 16 |
enum Color { |
| 17 |
Red, |
| 18 |
Blue, |
| 19 |
Green, |
| 20 |
} |
| 21 |
|
| 22 |
#[test] |
| 23 |
fn test_from_primitive_for_trivial_case() { |
| 24 |
let v: [Option<Color>; 4] = [ |
| 25 |
num_renamed::FromPrimitive::from_u64(0), |
| 26 |
num_renamed::FromPrimitive::from_u64(1), |
| 27 |
num_renamed::FromPrimitive::from_u64(2), |
| 28 |
num_renamed::FromPrimitive::from_u64(3), |
| 29 |
]; |
| 30 |
|
| 31 |
assert_eq!( |
| 32 |
v, |
| 33 |
[ |
| 34 |
Some(Color::Red), |
| 35 |
Some(Color::Blue), |
| 36 |
Some(Color::Green), |
| 37 |
None |
| 38 |
] |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn test_to_primitive_for_trivial_case() { |
| 44 |
let v: [Option<u64>; 3] = [ |
| 45 |
num_renamed::ToPrimitive::to_u64(&Color::Red), |
| 46 |
num_renamed::ToPrimitive::to_u64(&Color::Blue), |
| 47 |
num_renamed::ToPrimitive::to_u64(&Color::Green), |
| 48 |
]; |
| 49 |
|
| 50 |
assert_eq!(v, [Some(0), Some(1), Some(2)]); |
| 51 |
} |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn test_reflexive_for_trivial_case() { |
| 55 |
let before: [u64; 3] = [0, 1, 2]; |
| 56 |
let after: Vec<Option<u64>> = before |
| 57 |
.iter() |
| 58 |
.map(|&x| -> Option<Color> { num_renamed::FromPrimitive::from_u64(x) }) |
| 59 |
.map(|x| x.and_then(|x| num_renamed::ToPrimitive::to_u64(&x))) |
| 60 |
.collect(); |
| 61 |
let before = before.iter().cloned().map(Some).collect::<Vec<_>>(); |
| 62 |
|
| 63 |
assert_eq!(before, after); |
| 64 |
} |