1#[cfg(feature = "codec")]
10use impl_codec::impl_uint_codec;
11#[cfg(feature = "rlp")]
12use impl_rlp::impl_uint_rlp;
13#[cfg(feature = "serialize")]
14use impl_serde::impl_uint_serde;
15use uint_crate::*;
16
17pub use uint_crate::{FromDecStrErr, FromStrRadixErr, FromStrRadixErrKind};
18
19construct_uint! {
20 pub struct U64(1);
22}
23#[cfg(feature = "rlp")]
24impl_uint_rlp!(U64, 1);
25#[cfg(feature = "serialize")]
26impl_uint_serde!(U64, 1);
27#[cfg(feature = "codec")]
28impl_uint_codec!(U64, 1);
29
30pub use primitive_types::{U128, U256, U512};
31
32#[cfg(test)]
33mod tests {
34 use super::{U256, U512};
35 use serde_json as ser;
36 use std::u64::MAX;
37
38 macro_rules! test_serialize {
39 ($name: ident, $test_name: ident) => {
40 #[test]
41 fn $test_name() {
42 let tests = vec![
43 ($name::from(0), "0x0"),
44 ($name::from(1), "0x1"),
45 ($name::from(2), "0x2"),
46 ($name::from(10), "0xa"),
47 ($name::from(15), "0xf"),
48 ($name::from(15), "0xf"),
49 ($name::from(16), "0x10"),
50 ($name::from(1_000), "0x3e8"),
51 ($name::from(100_000), "0x186a0"),
52 ($name::from(u64::max_value()), "0xffffffffffffffff"),
53 ($name::from(u64::max_value()) + 1, "0x10000000000000000"),
54 ];
55
56 for (number, expected) in tests {
57 assert_eq!(
58 format!("{:?}", expected),
59 ser::to_string_pretty(&number).unwrap()
60 );
61 assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
62 }
63
64 let tests = vec![
65 ($name::from(0), "0"),
66 ($name::from(1), "1"),
67 ($name::from(2), "2"),
68 ($name::from(10), "a"),
69 ($name::from(15), "f"),
70 ($name::from(15), "f"),
71 ($name::from(16), "10"),
72 ($name::from(1_000), "3e8"),
73 ($name::from(100_000), "186a0"),
74 ($name::from(u64::max_value()), "ffffffffffffffff"),
75 ($name::from(u64::max_value()) + 1, "10000000000000000"),
76 ];
77
78 for (number, expected) in tests {
79 assert_eq!(
80 format!("{:?}", "0x".to_string() + expected),
81 ser::to_string_pretty(&number).unwrap()
82 );
83 assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
84 }
85
86 assert!(ser::from_str::<$name>("\"0x\"").unwrap_err().is_data());
88 assert!(ser::from_str::<$name>("\"0xg\"").unwrap_err().is_data());
89 assert!(ser::from_str::<$name>("\"\"").unwrap_err().is_data());
90 }
91 };
92 }
93
94 test_serialize!(U256, test_u256);
95 test_serialize!(U512, test_u512);
96
97 #[test]
98 fn test_serialize_large_values() {
99 assert_eq!(
100 ser::to_string_pretty(&!U256::zero()).unwrap(),
101 "\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\""
102 );
103 assert!(ser::from_str::<U256>(
104 "\"0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\""
105 )
106 .unwrap_err()
107 .is_data());
108 }
109
110 #[test]
111 fn fixed_arrays_roundtrip() {
112 let raw: U256 = "7094875209347850239487502394881".into();
113 let array: [u8; 32] = raw.into();
114 let new_raw = array.into();
115
116 assert_eq!(raw, new_raw);
117 }
118
119 #[test]
120 fn u256_multi_full_mul() {
121 let result = U256([0, 0, 0, 0]).full_mul(U256([0, 0, 0, 0]));
122 assert_eq!(U512([0, 0, 0, 0, 0, 0, 0, 0]), result);
123
124 let result = U256([1, 0, 0, 0]).full_mul(U256([1, 0, 0, 0]));
125 assert_eq!(U512([1, 0, 0, 0, 0, 0, 0, 0]), result);
126
127 let result = U256([5, 0, 0, 0]).full_mul(U256([5, 0, 0, 0]));
128 assert_eq!(U512([25, 0, 0, 0, 0, 0, 0, 0]), result);
129
130 let result = U256([0, 5, 0, 0]).full_mul(U256([0, 5, 0, 0]));
131 assert_eq!(U512([0, 0, 25, 0, 0, 0, 0, 0]), result);
132
133 let result = U256([0, 0, 0, 4]).full_mul(U256([4, 0, 0, 0]));
134 assert_eq!(U512([0, 0, 0, 16, 0, 0, 0, 0]), result);
135
136 let result = U256([0, 0, 0, 5]).full_mul(U256([2, 0, 0, 0]));
137 assert_eq!(U512([0, 0, 0, 10, 0, 0, 0, 0]), result);
138
139 let result = U256([0, 0, 2, 0]).full_mul(U256([0, 5, 0, 0]));
140 assert_eq!(U512([0, 0, 0, 10, 0, 0, 0, 0]), result);
141
142 let result = U256([0, 3, 0, 0]).full_mul(U256([0, 0, 3, 0]));
143 assert_eq!(U512([0, 0, 0, 9, 0, 0, 0, 0]), result);
144
145 let result = U256([0, 0, 8, 0]).full_mul(U256([0, 0, 6, 0]));
146 assert_eq!(U512([0, 0, 0, 0, 48, 0, 0, 0]), result);
147
148 let result = U256([9, 0, 0, 0]).full_mul(U256([0, 3, 0, 0]));
149 assert_eq!(U512([0, 27, 0, 0, 0, 0, 0, 0]), result);
150
151 let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, 0, 0, 0]));
152 assert_eq!(U512([1, MAX - 1, 0, 0, 0, 0, 0, 0]), result);
153
154 let result = U256([0, MAX, 0, 0]).full_mul(U256([MAX, 0, 0, 0]));
155 assert_eq!(U512([0, 1, MAX - 1, 0, 0, 0, 0, 0]), result);
156
157 let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, 0, 0, 0]));
158 assert_eq!(U512([1, MAX, MAX - 1, 0, 0, 0, 0, 0]), result);
159
160 let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, MAX, 0, 0]));
161 assert_eq!(U512([1, MAX, MAX - 1, 0, 0, 0, 0, 0]), result);
162
163 let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, MAX, 0, 0]));
164 assert_eq!(U512([1, 0, MAX - 1, MAX, 0, 0, 0, 0]), result);
165
166 let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, MAX, MAX, 0]));
167 assert_eq!(U512([1, MAX, MAX, MAX - 1, 0, 0, 0, 0]), result);
168
169 let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, 0, 0, 0]));
170 assert_eq!(U512([1, MAX, MAX, MAX - 1, 0, 0, 0, 0]), result);
171
172 let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, MAX, MAX, MAX]));
173 assert_eq!(U512([1, MAX, MAX, MAX, MAX - 1, 0, 0, 0]), result);
174
175 let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, 0, 0, 0]));
176 assert_eq!(U512([1, MAX, MAX, MAX, MAX - 1, 0, 0, 0]), result);
177
178 let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, MAX, 0, 0]));
179 assert_eq!(U512([1, 0, MAX, MAX - 1, MAX, 0, 0, 0]), result);
180
181 let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, MAX, MAX, 0]));
182 assert_eq!(U512([1, 0, MAX, MAX - 1, MAX, 0, 0, 0]), result);
183
184 let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, MAX, 0, 0]));
185 assert_eq!(U512([1, 0, MAX, MAX, MAX - 1, MAX, 0, 0]), result);
186
187 let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, MAX, MAX, MAX]));
188 assert_eq!(U512([1, 0, MAX, MAX, MAX - 1, MAX, 0, 0]), result);
189
190 let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, MAX, MAX, 0]));
191 assert_eq!(U512([1, 0, 0, MAX - 1, MAX, MAX, 0, 0]), result);
192
193 let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, MAX, MAX, MAX]));
194 assert_eq!(U512([1, 0, 0, MAX, MAX - 1, MAX, MAX, 0]), result);
195
196 let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, MAX, MAX, 0]));
197 assert_eq!(U512([1, 0, 0, MAX, MAX - 1, MAX, MAX, 0]), result);
198
199 let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, MAX, MAX, MAX]));
200 assert_eq!(U512([1, 0, 0, 0, MAX - 1, MAX, MAX, MAX]), result);
201
202 let result = U256([0, 0, 0, MAX]).full_mul(U256([0, 0, 0, MAX]));
203 assert_eq!(U512([0, 0, 0, 0, 0, 0, 1, MAX - 1]), result);
204
205 let result = U256([1, 0, 0, 0]).full_mul(U256([0, 0, 0, MAX]));
206 assert_eq!(U512([0, 0, 0, MAX, 0, 0, 0, 0]), result);
207
208 let result = U256([1, 2, 3, 4]).full_mul(U256([5, 0, 0, 0]));
209 assert_eq!(U512([5, 10, 15, 20, 0, 0, 0, 0]), result);
210
211 let result = U256([1, 2, 3, 4]).full_mul(U256([0, 6, 0, 0]));
212 assert_eq!(U512([0, 6, 12, 18, 24, 0, 0, 0]), result);
213
214 let result = U256([1, 2, 3, 4]).full_mul(U256([0, 0, 7, 0]));
215 assert_eq!(U512([0, 0, 7, 14, 21, 28, 0, 0]), result);
216
217 let result = U256([1, 2, 3, 4]).full_mul(U256([0, 0, 0, 8]));
218 assert_eq!(U512([0, 0, 0, 8, 16, 24, 32, 0]), result);
219
220 let result = U256([1, 2, 3, 4]).full_mul(U256([5, 6, 7, 8]));
221 assert_eq!(U512([5, 16, 34, 60, 61, 52, 32, 0]), result);
222 }
223}