+fixed signed packs

This commit is contained in:
Sergey Chernov 2023-10-12 00:15:33 +01:00
parent 33a27d0124
commit 588446cd42
3 changed files with 64 additions and 0 deletions

View File

@ -89,6 +89,19 @@ pub trait BipackSink {
self.put_fixed_bytes(&result); self.put_fixed_bytes(&result);
} }
fn put_i64(self: &mut Self, value: i64) {
self.put_u64(value as u64)
}
fn put_i32(self: &mut Self, value: i32) {
self.put_u32(value as u32)
}
fn put_i16(self: &mut Self, value: i16) {
self.put_u16(value as u16)
}
fn put_i8(self: &mut Self, value: i8) {
self.put_u8(value as u8)
}
/// Put unsigned value to compressed variable-length format, `Smartint` in the bipack /// Put unsigned value to compressed variable-length format, `Smartint` in the bipack
/// terms. This format is used to store size of variable-length binaries and strings. /// terms. This format is used to store size of variable-length binaries and strings.
/// Use [crate::bipack_source::BipackSource::get_unsigned] to unpack it. /// Use [crate::bipack_source::BipackSource::get_unsigned] to unpack it.

View File

@ -61,6 +61,19 @@ pub trait BipackSource {
Ok(((self.get_u32()? as u64) << 32) | (self.get_u32()? as u64)) Ok(((self.get_u32()? as u64) << 32) | (self.get_u32()? as u64))
} }
fn get_i64(self: &mut Self) -> Result<i64> {
Ok(self.get_u64()? as i64)
}
fn get_i32(self: &mut Self) -> Result<i32> {
Ok(self.get_u32()? as i32)
}
fn get_i16(self: &mut Self) -> Result<i16> {
Ok(self.get_u16()? as i16)
}
fn get_i8(self: &mut Self) -> Result<i8> {
Ok(self.get_u8()? as i8)
}
/// Unpack variable-length packed unsigned value, used aslo internally to store size /// Unpack variable-length packed unsigned value, used aslo internally to store size
/// of arrays, binary data, strings, etc. To pack use /// of arrays, binary data, strings, etc. To pack use
/// [crate::bipack_sink::BipackSink::put_unsigned()]. /// [crate::bipack_sink::BipackSink::put_unsigned()].

View File

@ -189,6 +189,44 @@ mod tests {
assert_eq!("Hello, rupack!", src.get_str().unwrap()); assert_eq!("Hello, rupack!", src.get_str().unwrap());
} }
#[test]
fn test_signed() -> Result<()> {
fn test64(value: i64) -> Result<()> {
let mut x = Vec::new();
x.put_i64(value);
assert_eq!(value, SliceSource::from(&x).get_i64()?);
Ok(())
}
test64(0)?;
test64(1)?;
test64(-1)?;
test64(9223372036854775807)?;
test64(-9223372036854775808)?;
fn test32(value: i32) -> Result<()> {
let mut x = Vec::new();
x.put_i32(value);
assert_eq!(value, SliceSource::from(&x).get_i32()?);
Ok(())
}
test32(0)?;
test32(1)?;
test32(-1)?;
test32(2147483647)?;
test32(-2147483648)?;
fn test16(value: i16) -> Result<()> {
let mut x = Vec::new();
x.put_i16(value);
assert_eq!(value, SliceSource::from(&x).get_i16()?);
Ok(())
}
test16(0)?;
test16(1)?;
test16(-1)?;
test16(32767)?;
test16(-32768)?;
Ok(())
}
#[test] #[test]
fn test_dump() { fn test_dump() {
for l in 0..64 { for l in 0..64 {