diff --git a/src/ebml.rs b/src/ebml.rs index ea7bf10..b23ee57 100644 --- a/src/ebml.rs +++ b/src/ebml.rs @@ -117,7 +117,7 @@ const EIGHT_MAX: u64 = EIGHT_FLAG - 2; pub fn encode_varint(varint: Varint, buffer: &mut [u8]) -> Result { let (size, number) = match varint { Varint::Unknown => (1, 0xFF), - Varint::Value(too_big) if too_big >= EIGHT_MAX => { + Varint::Value(too_big) if too_big > EIGHT_MAX => { return Err(WriteError::OutOfRange) }, Varint::Value(value) => { @@ -246,6 +246,33 @@ mod tests { assert_eq!(encode_varint(Varint::Value(128), &mut buffer), Ok(2)); assert_eq!(&buffer[0..2], &[0x40, 128]); assert_eq!(encode_varint(Varint::Value(128), &mut no_space), Err(WriteError::BufferTooSmall)); + + // 6 bytes + assert_eq!(encode_varint(Varint::Value(0x03FFFFFFFFFE), &mut buffer), Ok(6)); + assert_eq!(&buffer[0..6], &[0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]); + + // 7 bytes + assert_eq!(encode_varint(Varint::Value(0x03FFFFFFFFFF), &mut buffer), Ok(7)); + assert_eq!(&buffer[0..7], &[0x02, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]); + + assert_eq!(encode_varint(Varint::Value(0x01000000000000), &mut buffer), Ok(7)); + assert_eq!(&buffer[0..7], &[0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); + + assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut buffer), Ok(7)); + assert_eq!(&buffer[0..7], &[0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]); + + assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut no_space), Err(WriteError::BufferTooSmall)); + assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut buffer[0..6]), Err(WriteError::BufferTooSmall)); + + // 8 bytes + assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFF), &mut buffer), Ok(8)); + assert_eq!(&buffer[0..8], &[0x01, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]); + + assert_eq!(encode_varint(Varint::Value(0xFFFFFFFFFFFFFE), &mut buffer), Ok(8)); + assert_eq!(&buffer[0..8], &[0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]); + + assert_eq!(encode_varint(Varint::Value(0xFFFFFFFFFFFFFF), &mut buffer), Err(WriteError::OutOfRange)); + assert_eq!(encode_varint(Varint::Value(u64::max_value()), &mut buffer), Err(WriteError::OutOfRange)); } #[test]