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 #![cfg(feature = "full-syntax")]
12 
13 extern crate num as num_renamed;
14 #[macro_use]
15 extern crate num_derive;
16 
17 #[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)]
18 enum Color {
19     Red,
20     Blue = 5,
21     Green,
22     Alpha = (-3 - (-5isize)) - 10,
23 }
24 
25 #[test]
test_from_primitive_for_enum_with_custom_value()26 fn test_from_primitive_for_enum_with_custom_value() {
27     let v: [Option<Color>; 5] = [
28         num_renamed::FromPrimitive::from_u64(0),
29         num_renamed::FromPrimitive::from_u64(5),
30         num_renamed::FromPrimitive::from_u64(6),
31         num_renamed::FromPrimitive::from_u64(-8isize as u64),
32         num_renamed::FromPrimitive::from_u64(3),
33     ];
34 
35     assert_eq!(
36         v,
37         [
38             Some(Color::Red),
39             Some(Color::Blue),
40             Some(Color::Green),
41             Some(Color::Alpha),
42             None
43         ]
44     );
45 }
46 
47 #[test]
test_to_primitive_for_enum_with_custom_value()48 fn test_to_primitive_for_enum_with_custom_value() {
49     let v: [Option<u64>; 4] = [
50         num_renamed::ToPrimitive::to_u64(&Color::Red),
51         num_renamed::ToPrimitive::to_u64(&Color::Blue),
52         num_renamed::ToPrimitive::to_u64(&Color::Green),
53         num_renamed::ToPrimitive::to_u64(&Color::Alpha),
54     ];
55 
56     assert_eq!(v, [Some(0), Some(5), Some(6), Some(-8isize as u64)]);
57 }
58 
59 #[test]
test_reflexive_for_enum_with_custom_value()60 fn test_reflexive_for_enum_with_custom_value() {
61     let before: [u64; 3] = [0, 5, 6];
62     let after: Vec<Option<u64>> = before
63         .iter()
64         .map(|&x| -> Option<Color> { num_renamed::FromPrimitive::from_u64(x) })
65         .map(|x| x.and_then(|x| num_renamed::ToPrimitive::to_u64(&x)))
66         .collect();
67     let before = before.iter().cloned().map(Some).collect::<Vec<_>>();
68 
69     assert_eq!(before, after);
70 }
71