| 1 |
mod utils; |
| 2 |
|
| 3 |
use bincode::error::DecodeError; |
| 4 |
use core::cell::{Cell, RefCell}; |
| 5 |
use core::cmp::Reverse; |
| 6 |
use core::ops::Bound; |
| 7 |
use core::time::Duration; |
| 8 |
use std::num::*; |
| 9 |
use utils::{the_same, the_same_with_comparer}; |
| 10 |
|
| 11 |
#[test] |
| 12 |
fn test_numbers() { |
| 13 |
// integer types |
| 14 |
the_same(5u8); |
| 15 |
the_same(5u16); |
| 16 |
the_same(5u32); |
| 17 |
the_same(5u64); |
| 18 |
the_same(5u128); |
| 19 |
the_same(5usize); |
| 20 |
|
| 21 |
the_same(5i8); |
| 22 |
the_same(5i16); |
| 23 |
the_same(5i32); |
| 24 |
the_same(5i64); |
| 25 |
the_same(5i128); |
| 26 |
the_same(5isize); |
| 27 |
|
| 28 |
println!("Test {:?}", 5.0f32); |
| 29 |
the_same_with_comparer(5.0f32, |a, b| (a - b).abs() <= f32::EPSILON); |
| 30 |
the_same_with_comparer(5.0f64, |a, b| (a - b).abs() <= f64::EPSILON); |
| 31 |
|
| 32 |
// bool |
| 33 |
the_same(true); |
| 34 |
the_same(false); |
| 35 |
|
| 36 |
// utf8 characters |
| 37 |
for char in "aÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö文😀😁😂😃".chars() |
| 38 |
{ |
| 39 |
the_same(char); |
| 40 |
} |
| 41 |
|
| 42 |
// tuples, up to 8 |
| 43 |
the_same((1u8,)); |
| 44 |
the_same((1u8, 2u8)); |
| 45 |
the_same((1u8, 2u8, 3u8)); |
| 46 |
the_same((1u8, 2u8, 3u8, 4u8)); |
| 47 |
the_same((1u8, 2u8, 3u8, 4u8, 5u8)); |
| 48 |
the_same((1u8, 2u8, 3u8, 4u8, 5u8, 6u8)); |
| 49 |
the_same((1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8)); |
| 50 |
the_same((1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8)); |
| 51 |
|
| 52 |
// arrays |
| 53 |
#[rustfmt::skip] |
| 54 |
#[cfg(not(feature = "serde"))] // serde doesn't support arrays this big |
| 55 |
the_same([ |
| 56 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
| 57 |
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, |
| 58 |
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 59 |
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, |
| 60 |
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, |
| 61 |
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, |
| 62 |
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, |
| 63 |
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, |
| 64 |
129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, |
| 65 |
145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, |
| 66 |
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, |
| 67 |
177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, |
| 68 |
193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, |
| 69 |
209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, |
| 70 |
225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, |
| 71 |
241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 |
| 72 |
]); |
| 73 |
|
| 74 |
// Common types |
| 75 |
the_same(Option::<u32>::None); |
| 76 |
the_same(Option::<u32>::Some(1234)); |
| 77 |
|
| 78 |
the_same(Result::<u32, u8>::Ok(1555)); |
| 79 |
the_same(Result::<u32, u8>::Err(15)); |
| 80 |
|
| 81 |
the_same(Cell::<u32>::new(15)); |
| 82 |
the_same(RefCell::<u32>::new(15)); |
| 83 |
|
| 84 |
the_same(Duration::new(5, 730023852)); |
| 85 |
the_same(5u8..10u8); |
| 86 |
the_same(5u8..=10u8); |
| 87 |
the_same(Bound::<usize>::Unbounded); |
| 88 |
the_same(Bound::<usize>::Included(105)); |
| 89 |
the_same(Bound::<usize>::Excluded(5)); |
| 90 |
|
| 91 |
// NonZero* types |
| 92 |
the_same(NonZeroU8::new(0)); |
| 93 |
the_same(NonZeroU8::new(123)); |
| 94 |
the_same(NonZeroU16::new(0)); |
| 95 |
the_same(NonZeroU16::new(12345)); |
| 96 |
the_same(NonZeroU32::new(0)); |
| 97 |
the_same(NonZeroU32::new(12345)); |
| 98 |
the_same(NonZeroU64::new(0)); |
| 99 |
the_same(NonZeroU64::new(12345)); |
| 100 |
the_same(NonZeroU128::new(0)); |
| 101 |
the_same(NonZeroU128::new(12345)); |
| 102 |
the_same(NonZeroUsize::new(0)); |
| 103 |
the_same(NonZeroUsize::new(12345)); |
| 104 |
|
| 105 |
the_same(NonZeroI8::new(0)); |
| 106 |
the_same(NonZeroI8::new(123)); |
| 107 |
the_same(NonZeroI16::new(0)); |
| 108 |
the_same(NonZeroI16::new(12345)); |
| 109 |
the_same(NonZeroI32::new(0)); |
| 110 |
the_same(NonZeroI32::new(12345)); |
| 111 |
the_same(NonZeroI64::new(0)); |
| 112 |
the_same(NonZeroI64::new(12345)); |
| 113 |
the_same(NonZeroI128::new(0)); |
| 114 |
the_same(NonZeroI128::new(12345)); |
| 115 |
the_same(NonZeroIsize::new(0)); |
| 116 |
the_same(NonZeroIsize::new(12345)); |
| 117 |
|
| 118 |
// Wrapping types |
| 119 |
the_same(Wrapping(5u8)); |
| 120 |
the_same(Wrapping(5u16)); |
| 121 |
the_same(Wrapping(5u32)); |
| 122 |
the_same(Wrapping(5u64)); |
| 123 |
the_same(Wrapping(5u128)); |
| 124 |
the_same(Wrapping(5usize)); |
| 125 |
|
| 126 |
the_same(Wrapping(5i8)); |
| 127 |
the_same(Wrapping(5i16)); |
| 128 |
the_same(Wrapping(5i32)); |
| 129 |
the_same(Wrapping(5i64)); |
| 130 |
the_same(Wrapping(5i128)); |
| 131 |
the_same(Wrapping(5isize)); |
| 132 |
|
| 133 |
// Reverse types |
| 134 |
the_same(Reverse(5u8)); |
| 135 |
the_same(Reverse(5u16)); |
| 136 |
the_same(Reverse(5u32)); |
| 137 |
the_same(Reverse(5u64)); |
| 138 |
the_same(Reverse(5u128)); |
| 139 |
the_same(Reverse(5usize)); |
| 140 |
|
| 141 |
the_same(Reverse(5i8)); |
| 142 |
the_same(Reverse(5i16)); |
| 143 |
the_same(Reverse(5i32)); |
| 144 |
the_same(Reverse(5i64)); |
| 145 |
the_same(Reverse(5i128)); |
| 146 |
the_same(Reverse(5isize)); |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn test_refcell_already_borrowed() { |
| 151 |
let cell = RefCell::new(5u32); |
| 152 |
// first get a mutable reference to the cell |
| 153 |
let _mutable_guard = cell.borrow_mut(); |
| 154 |
// now try to encode it |
| 155 |
let mut slice = [0u8; 10]; |
| 156 |
let result = bincode::encode_into_slice(&cell, &mut slice, bincode::config::standard()) |
| 157 |
.expect_err("Encoding a borrowed refcell should fail"); |
| 158 |
|
| 159 |
match result { |
| 160 |
bincode::error::EncodeError::RefCellAlreadyBorrowed { .. } => {} // ok |
| 161 |
x => panic!("Expected a RefCellAlreadyBorrowed error, found {:?}", x), |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn test_slice() { |
| 167 |
let mut buffer = [0u8; 32]; |
| 168 |
let input: &[u8] = &[1, 2, 3, 4, 5, 6, 7]; |
| 169 |
bincode::encode_into_slice(input, &mut buffer, bincode::config::standard()).unwrap(); |
| 170 |
assert_eq!(&buffer[..8], &[7, 1, 2, 3, 4, 5, 6, 7]); |
| 171 |
|
| 172 |
let (output, len): (&[u8], usize) = |
| 173 |
bincode::borrow_decode_from_slice(&buffer[..8], bincode::config::standard()).unwrap(); |
| 174 |
assert_eq!(input, output); |
| 175 |
assert_eq!(len, 8); |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn test_option_slice() { |
| 180 |
let mut buffer = [0u8; 32]; |
| 181 |
let input: Option<&[u8]> = Some(&[1, 2, 3, 4, 5, 6, 7]); |
| 182 |
let n = bincode::encode_into_slice(input, &mut buffer, bincode::config::standard()).unwrap(); |
| 183 |
assert_eq!(&buffer[..n], &[1, 7, 1, 2, 3, 4, 5, 6, 7]); |
| 184 |
|
| 185 |
let (output, len): (Option<&[u8]>, usize) = |
| 186 |
bincode::borrow_decode_from_slice(&buffer[..n], bincode::config::standard()).unwrap(); |
| 187 |
assert_eq!(input, output); |
| 188 |
assert_eq!(len, n); |
| 189 |
|
| 190 |
let mut buffer = [0u8; 32]; |
| 191 |
let input: Option<&[u8]> = None; |
| 192 |
let n = bincode::encode_into_slice(input, &mut buffer, bincode::config::standard()).unwrap(); |
| 193 |
assert_eq!(&buffer[..n], &[0]); |
| 194 |
|
| 195 |
let (output, len): (Option<&[u8]>, usize) = |
| 196 |
bincode::borrow_decode_from_slice(&buffer[..n], bincode::config::standard()).unwrap(); |
| 197 |
assert_eq!(input, output); |
| 198 |
assert_eq!(len, n); |
| 199 |
} |
| 200 |
|
| 201 |
#[test] |
| 202 |
fn test_str() { |
| 203 |
let mut buffer = [0u8; 32]; |
| 204 |
let input: &str = "Hello world"; |
| 205 |
bincode::encode_into_slice(input, &mut buffer, bincode::config::standard()).unwrap(); |
| 206 |
assert_eq!( |
| 207 |
&buffer[..12], |
| 208 |
&[11, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] |
| 209 |
); |
| 210 |
|
| 211 |
let (output, len): (&str, usize) = |
| 212 |
bincode::borrow_decode_from_slice(&buffer[..12], bincode::config::standard()).unwrap(); |
| 213 |
assert_eq!(input, output); |
| 214 |
assert_eq!(len, 12); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn test_option_str() { |
| 219 |
let mut buffer = [0u8; 32]; |
| 220 |
let input: Option<&str> = Some("Hello world"); |
| 221 |
let n = bincode::encode_into_slice(input, &mut buffer, bincode::config::standard()).unwrap(); |
| 222 |
assert_eq!( |
| 223 |
&buffer[..n], |
| 224 |
&[1, 11, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] |
| 225 |
); |
| 226 |
|
| 227 |
let (output, len): (Option<&str>, usize) = |
| 228 |
bincode::borrow_decode_from_slice(&buffer[..n], bincode::config::standard()).unwrap(); |
| 229 |
assert_eq!(input, output); |
| 230 |
assert_eq!(len, n); |
| 231 |
|
| 232 |
let mut buffer = [0u8; 32]; |
| 233 |
let input: Option<&str> = None; |
| 234 |
let n = bincode::encode_into_slice(input, &mut buffer, bincode::config::standard()).unwrap(); |
| 235 |
assert_eq!(&buffer[..n], &[0]); |
| 236 |
|
| 237 |
let (output, len): (Option<&str>, usize) = |
| 238 |
bincode::borrow_decode_from_slice(&buffer[..n], bincode::config::standard()).unwrap(); |
| 239 |
assert_eq!(input, output); |
| 240 |
assert_eq!(len, n); |
| 241 |
} |
| 242 |
|
| 243 |
#[test] |
| 244 |
fn test_array() { |
| 245 |
let mut buffer = [0u8; 32]; |
| 246 |
let input: [u8; 10] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]; |
| 247 |
bincode::encode_into_slice(input, &mut buffer, bincode::config::standard()).unwrap(); |
| 248 |
assert_eq!(&buffer[..10], &[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]); |
| 249 |
|
| 250 |
let (output, len): ([u8; 10], usize) = |
| 251 |
bincode::decode_from_slice(&buffer[..11], bincode::config::standard()).unwrap(); |
| 252 |
assert_eq!(input, output); |
| 253 |
assert_eq!(len, 10); |
| 254 |
|
| 255 |
let mut buffer = [0u8; 32]; |
| 256 |
let input: [u8; 1] = [1]; |
| 257 |
let config = bincode::config::standard() |
| 258 |
.with_fixed_int_encoding() |
| 259 |
.with_little_endian(); |
| 260 |
let len = bincode::encode_into_slice(input, &mut buffer, config).unwrap(); |
| 261 |
assert_eq!(len, 1); |
| 262 |
assert_eq!(&buffer[..1], &[1]); |
| 263 |
|
| 264 |
let (output, len): ([u8; 1], usize) = bincode::decode_from_slice(&buffer[..9], config).unwrap(); |
| 265 |
assert_eq!(len, 1); |
| 266 |
assert_eq!(input, output); |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn test_duration_out_of_range() { |
| 271 |
let mut input = [0u8; 14]; |
| 272 |
|
| 273 |
bincode::encode_into_slice( |
| 274 |
(u64::MAX, u32::MAX), |
| 275 |
&mut input, |
| 276 |
bincode::config::standard(), |
| 277 |
) |
| 278 |
.unwrap(); |
| 279 |
|
| 280 |
let result: Result<(std::time::Duration, usize), _> = |
| 281 |
bincode::decode_from_slice(&input, bincode::config::standard()); |
| 282 |
|
| 283 |
match result { |
| 284 |
Err(DecodeError::InvalidDuration { |
| 285 |
secs: u64::MAX, |
| 286 |
nanos: u32::MAX, |
| 287 |
}) => {} |
| 288 |
Err(e) => panic!("Expected InvalidDuration, got {:?}", e), |
| 289 |
Ok(_) => panic!("Expected the decode to fail"), |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
#[test] |
| 294 |
fn test_duration_wrapping() { |
| 295 |
let mut input = [0u8; 14]; |
| 296 |
|
| 297 |
bincode::encode_into_slice( |
| 298 |
(u64::MAX - 4, u32::MAX), |
| 299 |
&mut input, |
| 300 |
bincode::config::standard(), |
| 301 |
) |
| 302 |
.unwrap(); |
| 303 |
|
| 304 |
let (result, _): (std::time::Duration, _) = |
| 305 |
bincode::decode_from_slice(&input, bincode::config::standard()).unwrap(); |
| 306 |
|
| 307 |
assert_eq!(result.as_secs(), u64::MAX); |
| 308 |
|
| 309 |
assert_eq!(result.subsec_nanos(), 294967295); |
| 310 |
} |