Debian Codegraph @

.cargo/vendor/num-derive/tests/newtype.rs

raw
1 extern crate num as num_renamed;
2 #[macro_use]
3 extern crate num_derive;
4
5 use crate::num_renamed::{
6 Float, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Unsigned, Zero,
7 };
8 use std::ops::Neg;
9
10 #[derive(PartialEq, Zero, One, NumOps, Num, Unsigned)]
11 struct MyNum(u32);
12
13 #[test]
14 fn test_derive_unsigned_works() {
15 fn do_nothing_on_unsigned(_input: impl Unsigned) {}
16
17 let x = MyNum(42);
18 do_nothing_on_unsigned(x);
19 }
20
21 #[derive(
22 Debug,
23 Clone,
24 Copy,
25 PartialEq,
26 PartialOrd,
27 ToPrimitive,
28 FromPrimitive,
29 NumOps,
30 NumCast,
31 One,
32 Zero,
33 Num,
34 Float,
35 Signed,
36 )]
37 struct MyFloat(f64);
38
39 impl Neg for MyFloat {
40 type Output = MyFloat;
41 fn neg(self) -> Self {
42 MyFloat(self.0.neg())
43 }
44 }
45
46 #[test]
47 fn test_from_primitive() {
48 assert_eq!(MyFloat::from_u32(25), Some(MyFloat(25.0)));
49 }
50
51 #[test]
52 fn test_from_primitive_128() {
53 assert_eq!(
54 MyFloat::from_i128(std::i128::MIN),
55 Some(MyFloat((-2.0).powi(127)))
56 );
57 }
58
59 #[test]
60 fn test_to_primitive() {
61 assert_eq!(MyFloat(25.0).to_u32(), Some(25));
62 }
63
64 #[test]
65 fn test_to_primitive_128() {
66 let f = MyFloat::from_f32(std::f32::MAX).unwrap();
67 assert_eq!(f.to_i128(), None);
68 assert_eq!(f.to_u128(), Some(0xffff_ff00_0000_0000_0000_0000_0000_0000));
69 }
70
71 #[test]
72 fn test_num_ops() {
73 assert_eq!(MyFloat(25.0) + MyFloat(10.0), MyFloat(35.0));
74 assert_eq!(MyFloat(25.0) - MyFloat(10.0), MyFloat(15.0));
75 assert_eq!(MyFloat(25.0) * MyFloat(2.0), MyFloat(50.0));
76 assert_eq!(MyFloat(25.0) / MyFloat(10.0), MyFloat(2.5));
77 assert_eq!(MyFloat(25.0) % MyFloat(10.0), MyFloat(5.0));
78 }
79
80 #[test]
81 fn test_num_cast() {
82 assert_eq!(<MyFloat as NumCast>::from(25u8), Some(MyFloat(25.0)));
83 }
84
85 #[test]
86 fn test_zero() {
87 assert_eq!(MyFloat::zero(), MyFloat(0.0));
88 }
89
90 #[test]
91 fn test_one() {
92 assert_eq!(MyFloat::one(), MyFloat(1.0));
93 }
94
95 #[test]
96 fn test_num() {
97 assert_eq!(MyFloat::from_str_radix("25", 10).ok(), Some(MyFloat(25.0)));
98 }
99
100 #[test]
101 fn test_float() {
102 assert_eq!(MyFloat(4.0).log(MyFloat(2.0)), MyFloat(2.0));
103 }
104
105 #[test]
106 fn test_signed() {
107 assert!(MyFloat(-2.0).is_negative())
108 }