| 1 | extern crate suppositions; |
| 2 | extern crate hex_slice; |
| 3 | use hex_slice::AsHex; |
| 4 | |
| 5 | use suppositions::*; |
| 6 | use suppositions::generators::*; |
| 7 | |
| 8 | |
| 9 | #[test] |
| 10 | fn should_start_and_end_with_square_brackets() { |
| 11 | property(vecs(u8s())) |
| 12 | .check(|data| { |
| 13 | let s = format!("{:x}", data.as_hex()); |
| 14 | let len = s.len(); |
| 15 | let first = s.get(..1); |
| 16 | let last = s.get(len-1..); |
| 17 | first == Some("[") && last == Some("]") |
| 18 | }); |
| 19 | } |
| 20 | |
| 21 | #[test] |
| 22 | fn should_group_u8_as_blocks_of_two() { |
| 23 | property(vecs(u8s()).map(|data| (data.clone(), format!("{:02x}", data.as_hex())))) |
| 24 | .check(|(_, s)| { |
| 25 | let len = s.len(); |
| 26 | let body = match s.get(1..len-1) { |
| 27 | Some(slice) => slice, |
| 28 | // Should always have a middle. |
| 29 | None => return false, |
| 30 | }; |
| 31 | |
| 32 | body.split_whitespace().all(|chunk| chunk.len() == 2) |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn should_group_u32_as_blocks_of_eight_when_specified() { |
| 38 | property(vecs(u32s()).map(|data| (data.clone(), format!("{:08x}", data.as_hex())))) |
| 39 | .check(|(_, s)| { |
| 40 | let len = s.len(); |
| 41 | let body = match s.get(1..len-1) { |
| 42 | Some(slice) => slice, |
| 43 | // Should always have a middle. |
| 44 | None => return false, |
| 45 | }; |
| 46 | |
| 47 | body.split_whitespace().all(|chunk| chunk.len() == 8) |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn should_be_decodable_as_original_data() { |
| 53 | property(vecs(u32s()).map(|data| (data.clone(), format!("{:x}", data.as_hex())))) |
| 54 | .check(|(orig, s)| -> std::result::Result<bool, _> { |
| 55 | let len = s.len(); |
| 56 | let body = match s.get(1..len-1) { |
| 57 | Some(slice) => slice, |
| 58 | // Should always have a middle. |
| 59 | None => return Ok(false), |
| 60 | }; |
| 61 | |
| 62 | body.split_whitespace() |
| 63 | .map(|chunk| u32::from_str_radix(chunk, 16)) |
| 64 | .collect::<Result<Vec<u32>, _>>() |
| 65 | .map(|d| d == orig) |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn plain_hex_should_group_u8_as_blocks_of_two() { |
| 71 | property(vecs(u8s()).map(|data| (data.clone(), format!("{:02x}", data.plain_hex(true))))) |
| 72 | .check(|(_, s)| { |
| 73 | s.split_whitespace().all(|chunk| chunk.len() == 2) |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn plain_hex_should_group_u32_as_blocks_of_eight_when_specified() { |
| 79 | property(vecs(u32s()).map(|data| (data.clone(), format!("{:08x}", data.plain_hex(true))))) |
| 80 | .check(|(_, s)| { |
| 81 | s.split_whitespace().all(|chunk| chunk.len() == 8) |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn plain_hex_should_be_decodable_as_original_data() { |
| 87 | property(vecs(u32s()).map(|data| (data.clone(), format!("{:x}", data.plain_hex(true))))) |
| 88 | .check(|(orig, s)| -> std::result::Result<bool, _> { |
| 89 | s.split_whitespace() |
| 90 | .map(|chunk| u32::from_str_radix(chunk, 16)) |
| 91 | .collect::<Result<Vec<u32>, _>>() |
| 92 | .map(|d| d == orig) |
| 93 | }); |
| 94 | } |