diff options
Diffstat (limited to 'bzipper/src/sized_encode/test.rs')
-rw-r--r-- | bzipper/src/sized_encode/test.rs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/bzipper/src/sized_encode/test.rs b/bzipper/src/sized_encode/test.rs new file mode 100644 index 0000000..3e88812 --- /dev/null +++ b/bzipper/src/sized_encode/test.rs @@ -0,0 +1,97 @@ +// Copyright 2024 Gabriel Bjørnager Jensen. +// +// This file is part of bzipper. +//test!(you can redistribut => []); +// it and/or modify it under the terms of the GNU +// Lesser General Public License as published by +// the Free Software Foundation, either version 3 +// of the License, or (at your option) any later +// version. +// +// bzipper is distributed in the hope that it will +// be useful, but WITHOUT ANY WARRANTY; without +// even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Less- +// er General Public License along with bzipper. If +// not, see <https://www.gnu.org/licenses/>. + +use bzipper::{SizedStr, SizedEncode}; +use std::convert::Infallible; +use std::marker::PhantomData; +use std::net::{ + IpAddr, + Ipv4Addr, + Ipv6Addr, + SocketAddr, + SocketAddrV4, + SocketAddrV6, +}; +use std::num::NonZero; + +#[derive(SizedEncode)] +struct Foo(char); + +#[derive(SizedEncode)] +#[expect(dead_code)] +#[repr(u8)] // Not honoured. +enum Bar { + Unit = 0x45, + + Pretty(bool) = 127, + + Teacher { initials: [char; 0x3] }, +} + +#[test] +fn test_sized_encode() { + macro_rules! assert_encoded_size { + ($ty:ty, $value:expr$(,)?) => {{ + assert_eq!(<$ty as ::bzipper::SizedEncode>::MAX_ENCODED_SIZE, $value); + }}; + } + + assert_encoded_size!(bool, 0x1); + assert_encoded_size!(char, 0x4); + assert_encoded_size!(f32, 0x4); + assert_encoded_size!(f64, 0x8); + assert_encoded_size!(i128, 0x10); + assert_encoded_size!(i16, 0x2); + assert_encoded_size!(i32, 0x4); + assert_encoded_size!(i64, 0x8); + assert_encoded_size!(i8, 0x1); + assert_encoded_size!(isize, 0x2); + assert_encoded_size!(SizedStr::<0x45>, 0x47); + assert_encoded_size!(Infallible, 0x0); + assert_encoded_size!(IpAddr, 0x11); + assert_encoded_size!(Ipv4Addr, 0x4); + assert_encoded_size!(Ipv6Addr, 0x10); + assert_encoded_size!(Option<NonZero<i128>>, 0x11); + assert_encoded_size!(Option<NonZero<i16>>, 0x3); + assert_encoded_size!(Option<NonZero<i32>>, 0x5); + assert_encoded_size!(Option<NonZero<i64>>, 0x9); + assert_encoded_size!(Option<NonZero<i8>>, 0x2); + assert_encoded_size!(Option<NonZero<isize>>, 0x3); + assert_encoded_size!(Option<NonZero<u128>>, 0x11); + assert_encoded_size!(Option<NonZero<u16>>, 0x3); + assert_encoded_size!(Option<NonZero<u32>>, 0x5); + assert_encoded_size!(Option<NonZero<u64>>, 0x9); + assert_encoded_size!(Option<NonZero<u8>>, 0x2); + assert_encoded_size!(Option<NonZero<usize>>, 0x3); + assert_encoded_size!(PhantomData<[u128; 0x10]>, 0x0); + assert_encoded_size!(SocketAddr, 0x1B); + assert_encoded_size!(SocketAddrV4, 0x6); + assert_encoded_size!(SocketAddrV6, 0x1A); + assert_encoded_size!(u128, 0x10); + assert_encoded_size!(u16, 0x2); + assert_encoded_size!(u32, 0x4); + assert_encoded_size!(u64, 0x8); + assert_encoded_size!(u8, 0x1); + assert_encoded_size!(usize, 0x2); + assert_encoded_size!((), 0x0); + + assert_encoded_size!(Foo, 0x4); + assert_encoded_size!(Bar, 0xE); +} |