|
| 1 | +// SPDX-License-Identifier: CC0-1.0 |
| 2 | + |
| 3 | +//! Tests for encoder free functions. |
| 4 | +
|
| 5 | +#[cfg(feature = "std")] |
| 6 | +use std::io::{Cursor, Write}; |
| 7 | + |
| 8 | +use consensus_encoding::{ArrayEncoder, Encodable}; |
| 9 | + |
| 10 | +// Simple test type that implements Encodable. |
| 11 | +struct TestData(u32); |
| 12 | + |
| 13 | +impl Encodable for TestData { |
| 14 | + type Encoder<'s> |
| 15 | + = ArrayEncoder<4> |
| 16 | + where |
| 17 | + Self: 's; |
| 18 | + |
| 19 | + fn encoder(&self) -> Self::Encoder<'_> { |
| 20 | + ArrayEncoder::without_length_prefix(self.0.to_le_bytes()) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// Test with a type that creates an empty encoder. |
| 25 | +struct EmptyData; |
| 26 | + |
| 27 | +impl Encodable for EmptyData { |
| 28 | + type Encoder<'s> |
| 29 | + = ArrayEncoder<0> |
| 30 | + where |
| 31 | + Self: 's; |
| 32 | + |
| 33 | + fn encoder(&self) -> Self::Encoder<'_> { ArrayEncoder::without_length_prefix([]) } |
| 34 | +} |
| 35 | + |
| 36 | +#[test] |
| 37 | +#[cfg(feature = "std")] |
| 38 | +fn encode_std_writer() { |
| 39 | + let data = TestData(0x1234_5678); |
| 40 | + |
| 41 | + let mut cursor = Cursor::new(Vec::new()); |
| 42 | + consensus_encoding::encode_to_writer(&data, &mut cursor).unwrap(); |
| 43 | + |
| 44 | + let result = cursor.into_inner(); |
| 45 | + assert_eq!(result, vec![0x78, 0x56, 0x34, 0x12]); |
| 46 | +} |
| 47 | + |
| 48 | +#[test] |
| 49 | +#[cfg(feature = "alloc")] |
| 50 | +fn encode_vec() { |
| 51 | + let data = TestData(0xDEAD_BEEF); |
| 52 | + let vec = consensus_encoding::encode_to_vec(&data); |
| 53 | + assert_eq!(vec, vec![0xEF, 0xBE, 0xAD, 0xDE]); |
| 54 | +} |
| 55 | + |
| 56 | +#[test] |
| 57 | +#[cfg(feature = "alloc")] |
| 58 | +fn encode_vec_empty_data() { |
| 59 | + let data = EmptyData; |
| 60 | + let result = consensus_encoding::encode_to_vec(&data); |
| 61 | + assert!(result.is_empty()); |
| 62 | +} |
| 63 | + |
| 64 | +#[test] |
| 65 | +#[cfg(feature = "std")] |
| 66 | +fn encode_std_writer_empty_data() { |
| 67 | + let data = EmptyData; |
| 68 | + let mut cursor = Cursor::new(Vec::new()); |
| 69 | + consensus_encoding::encode_to_writer(&data, &mut cursor).unwrap(); |
| 70 | + |
| 71 | + let result = cursor.into_inner(); |
| 72 | + assert!(result.is_empty()); |
| 73 | +} |
| 74 | + |
| 75 | +#[test] |
| 76 | +#[cfg(feature = "std")] |
| 77 | +fn encode_std_writer_io_error() { |
| 78 | + // Test writer that always fails. |
| 79 | + struct FailingWriter; |
| 80 | + |
| 81 | + impl Write for FailingWriter { |
| 82 | + fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> { |
| 83 | + Err(std::io::Error::other("test error")) |
| 84 | + } |
| 85 | + |
| 86 | + fn flush(&mut self) -> std::io::Result<()> { Ok(()) } |
| 87 | + } |
| 88 | + |
| 89 | + let data = TestData(0x1234_5678); |
| 90 | + let mut writer = FailingWriter; |
| 91 | + |
| 92 | + let result = consensus_encoding::encode_to_writer(&data, &mut writer); |
| 93 | + |
| 94 | + assert!(result.is_err()); |
| 95 | + assert_eq!(result.unwrap_err().kind(), std::io::ErrorKind::Other); |
| 96 | +} |
0 commit comments