fathom_web3/types/
uint.rs

1pub use ethereum_types::{BigEndianHash, Bloom as H2048, H128, H160, H256, H512, H520, H64, U128, U256, U64};
2
3#[cfg(test)]
4mod tests {
5    use super::*;
6    use wasm_bindgen_test::*;
7
8    type Res = Result<U256, serde_json::Error>;
9
10    #[test]
11    fn should_compare_correctly() {
12        let mut arr = [0u8; 32];
13        arr[31] = 0;
14        arr[30] = 15;
15        arr[29] = 1;
16        arr[28] = 0;
17        arr[27] = 10;
18        let a = U256::from(arr.as_ref());
19        arr[27] = 9;
20        let b = U256::from(arr.as_ref());
21        let c = U256::from(0);
22        let d = U256::from(10_000);
23
24        assert!(b < a);
25        assert!(d < a);
26        assert!(d < b);
27        assert!(c < a);
28        assert!(c < b);
29        assert!(c < d);
30    }
31
32    #[test]
33    fn should_display_correctly() {
34        let mut arr = [0u8; 32];
35        arr[31] = 0;
36        arr[30] = 15;
37        arr[29] = 1;
38        arr[28] = 0;
39        arr[27] = 10;
40        let a = U256::from(arr.as_ref());
41        let b = U256::from(1023);
42        let c = U256::from(0);
43        let d = U256::from(10000);
44
45        // Debug
46        assert_eq!(&format!("{:?}", a), "42949742336");
47        assert_eq!(&format!("{:?}", b), "1023");
48        assert_eq!(&format!("{:?}", c), "0");
49        assert_eq!(&format!("{:?}", d), "10000");
50
51        // Display
52        assert_eq!(&format!("{}", a), "42949742336");
53        assert_eq!(&format!("{}", b), "1023");
54        assert_eq!(&format!("{}", c), "0");
55        assert_eq!(&format!("{}", d), "10000");
56
57        // Lowerhex
58        assert_eq!(&format!("{:x}", a), "a00010f00");
59        assert_eq!(&format!("{:x}", b), "3ff");
60        assert_eq!(&format!("{:x}", c), "0");
61        assert_eq!(&format!("{:x}", d), "2710");
62
63        // Prefixed Lowerhex
64        assert_eq!(&format!("{:#x}", a), "0xa00010f00");
65        assert_eq!(&format!("{:#x}", b), "0x3ff");
66        assert_eq!(&format!("{:#x}", c), "0x0");
67        assert_eq!(&format!("{:#x}", d), "0x2710");
68    }
69
70    #[test]
71    fn should_display_hash_correctly() {
72        let mut arr = [0; 16];
73        arr[15] = 0;
74        arr[14] = 15;
75        arr[13] = 1;
76        arr[12] = 0;
77        arr[11] = 10;
78        let a = H128::from_uint(&arr.into());
79        let b = H128::from_uint(&1023.into());
80        let c = H128::from_uint(&0.into());
81        let d = H128::from_uint(&10000.into());
82
83        // Debug
84        assert_eq!(&format!("{:?}", a), "0x00000000000000000000000a00010f00");
85        assert_eq!(&format!("{:?}", b), "0x000000000000000000000000000003ff");
86        assert_eq!(&format!("{:?}", c), "0x00000000000000000000000000000000");
87        assert_eq!(&format!("{:?}", d), "0x00000000000000000000000000002710");
88
89        // Display
90        assert_eq!(&format!("{}", a), "0x0000…0f00");
91        assert_eq!(&format!("{}", b), "0x0000…03ff");
92        assert_eq!(&format!("{}", c), "0x0000…0000");
93        assert_eq!(&format!("{}", d), "0x0000…2710");
94
95        // Lowerhex
96        assert_eq!(&format!("{:x}", a), "00000000000000000000000a00010f00");
97        assert_eq!(&format!("{:x}", b), "000000000000000000000000000003ff");
98        assert_eq!(&format!("{:x}", c), "00000000000000000000000000000000");
99        assert_eq!(&format!("{:x}", d), "00000000000000000000000000002710");
100    }
101
102    #[test]
103    fn should_deserialize_hash_correctly() {
104        let deserialized1: H128 = serde_json::from_str(r#""0x00000000000000000000000a00010f00""#).unwrap();
105
106        assert_eq!(deserialized1, H128::from_low_u64_be(0xa00010f00));
107    }
108
109    #[test]
110    fn should_serialize_u256() {
111        let serialized1 = serde_json::to_string(&U256::from(0)).unwrap();
112        let serialized2 = serde_json::to_string(&U256::from(1)).unwrap();
113        let serialized3 = serde_json::to_string(&U256::from(16)).unwrap();
114        let serialized4 = serde_json::to_string(&U256::from(256)).unwrap();
115
116        assert_eq!(serialized1, r#""0x0""#);
117        assert_eq!(serialized2, r#""0x1""#);
118        assert_eq!(serialized3, r#""0x10""#);
119        assert_eq!(serialized4, r#""0x100""#);
120    }
121
122    #[test]
123    fn should_succesfully_deserialize_decimals() {
124        let deserialized1: Res = serde_json::from_str(r#""""#);
125        let deserialized2: Res = serde_json::from_str(r#""0""#);
126        let deserialized3: Res = serde_json::from_str(r#""10""#);
127        let deserialized4: Res = serde_json::from_str(r#""1000000""#);
128        let deserialized5: Res = serde_json::from_str(r#""1000000000000000000""#);
129
130        assert!(deserialized1.is_err());
131        assert!(deserialized2.is_ok());
132        assert!(deserialized3.is_ok());
133        assert!(deserialized4.is_ok());
134        assert!(deserialized5.is_ok());
135    }
136
137    #[test]
138    fn should_deserialize_u256() {
139        let deserialized1: Result<U256, _> = serde_json::from_str(r#""0x""#);
140        let deserialized2: U256 = serde_json::from_str(r#""0x0""#).unwrap();
141        let deserialized3: U256 = serde_json::from_str(r#""0x1""#).unwrap();
142        let deserialized4: U256 = serde_json::from_str(r#""0x01""#).unwrap();
143        let deserialized5: U256 = serde_json::from_str(r#""0x100""#).unwrap();
144
145        assert!(deserialized1.is_err());
146        assert_eq!(deserialized2, 0.into());
147        assert_eq!(deserialized3, 1.into());
148        assert_eq!(deserialized4, 1.into());
149        assert_eq!(deserialized5, 256.into());
150    }
151
152    #[test]
153    fn test_to_from_u64() {
154        assert_eq!(1u64, U256::from(1u64).low_u64());
155        assert_eq!(11u64, U256::from(11u64).low_u64());
156        assert_eq!(111u64, U256::from(111u64).low_u64());
157    }
158
159    // Getting random numbers uses a different code path in JS, so we sanity
160    // check it here.
161    #[wasm_bindgen_test]
162    fn random_doesnt_panic() {
163        H160::random();
164    }
165}