// Copyright 2021 Datafuse Labs. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::fmt; use ethnum::I256; use ethnum::U256; use micromarshal::*; use rand::distributions::Distribution; use rand::distributions::Standard; use rand::random; mod statbuffer; use statbuffer::StatBuffer; trait Random { fn random() -> Self; } impl Random for U256 { fn random() -> Self { U256::from_words(random(), random()) } } impl Random for I256 { fn random() -> Self { I256::from_words(random(), random()) } } fn test_some() where T: Copy + fmt::Debug + StatBuffer + Marshal + Unmarshal + PartialEq, Standard: Distribution, { for _ in 0..100 { let mut buffer = T::buffer(); let v = random::(); v.marshal(buffer.as_mut()); let u = T::unmarshal(buffer.as_ref()); assert_eq!(v, u); } } fn test_some_large() where T: Copy + fmt::Debug + StatBuffer + Marshal + Unmarshal + PartialEq + Random, { for _ in 0..100 { let mut buffer = T::buffer(); let v = T::random(); v.marshal(buffer.as_mut()); let u = T::unmarshal(buffer.as_ref()); assert_eq!(v, u); } } #[test] fn test_u8() { test_some::() } #[test] fn test_u16() { test_some::() } #[test] fn test_u32() { test_some::() } #[test] fn test_u64() { test_some::() } #[test] fn test_u128() { test_some::() } #[test] fn test_u256() { test_some_large::(); } #[test] fn test_i8() { test_some::() } #[test] fn test_i16() { test_some::() } #[test] fn test_i32() { test_some::() } #[test] fn test_i64() { test_some::() } #[test] fn test_i128() { test_some::() } #[test] fn test_i256() { test_some_large::() } #[test] fn test_f32() { test_some::() } #[test] fn test_f64() { test_some::() } #[test] fn test_bool() { test_some::() }