use super ::Cursor;
use rmp::decode::*;
use rmp::Marker;
#[ test]
fn from_fixstr_min_read_str_len() {
let buf: &[u8] = &[0 xa0];
let mut cur = Cursor::new(buf);
assert_eq!(0 , read_str_len(&mut cur).unwrap());
assert_eq!(1 , cur.position());
}
#[ test]
fn from_fixstr_rnd_read_str_len() {
let buf: &[u8] = &[0 xaa];
let mut cur = Cursor::new(buf);
assert_eq!(10 , read_str_len(&mut cur).unwrap());
assert_eq!(1 , cur.position());
}
#[ test]
fn from_fixstr_max_read_str_len() {
let buf: &[u8] = &[0 xbf];
let mut cur = Cursor::new(buf);
assert_eq!(31 , read_str_len(&mut cur).unwrap());
assert_eq!(1 , cur.position());
}
#[ test]
fn from_str8_min_read_str_len() {
let buf: &[u8] = &[0 xd9, 0 x00];
let mut cur = Cursor::new(buf);
assert_eq!(0 , read_str_len(&mut cur).unwrap());
assert_eq!(2 , cur.position());
}
#[ test]
fn from_str8_rnd_read_str_len() {
let buf: &[u8] = &[0 xd9, 0 x0a];
let mut cur = Cursor::new(buf);
assert_eq!(10 , read_str_len(&mut cur).unwrap());
assert_eq!(2 , cur.position());
}
#[ test]
fn from_str8_read_str_len_eof() {
let buf: &[u8] = &[0 xd9];
let mut cur = Cursor::new(buf);
read_str_len(&mut cur).err().unwrap();
assert_eq!(1 , cur.position());
}
#[ test]
fn from_str8_max_read_str_len() {
let buf: &[u8] = &[0 xd9, 0 xff];
let mut cur = Cursor::new(buf);
assert_eq!(255 , read_str_len(&mut cur).unwrap());
assert_eq!(2 , cur.position());
}
#[ test]
fn from_str16_min_read_str_len() {
let buf: &[u8] = &[0 xda, 0 x00, 0 x00];
let mut cur = Cursor::new(buf);
assert_eq!(0 , read_str_len(&mut cur).unwrap());
assert_eq!(3 , cur.position());
}
#[ test]
fn from_str16_max_read_str_len() {
let buf: &[u8] = &[0 xda, 0 xff, 0 xff];
let mut cur = Cursor::new(buf);
assert_eq!(65535 , read_str_len(&mut cur).unwrap());
assert_eq!(3 , cur.position());
}
#[ test]
fn from_str16_read_str_len_eof() {
let buf: &[u8] = &[0 xda, 0 x00];
let mut cur = Cursor::new(buf);
read_str_len(&mut cur).err().unwrap();
assert!(cur.position() >= 1 );
}
#[ test]
fn from_str32_min_read_str_len() {
let buf: &[u8] = &[0 xdb, 0 x00, 0 x00, 0 x00, 0 x00];
let mut cur = Cursor::new(buf);
assert_eq!(0 , read_str_len(&mut cur).unwrap());
assert_eq!(5 , cur.position());
}
#[ test]
fn from_str32_max_read_str_len() {
let buf: &[u8] = &[0 xdb, 0 xff, 0 xff, 0 xff, 0 xff];
let mut cur = Cursor::new(buf);
assert_eq!(4294967295 , read_str_len(&mut cur).unwrap());
assert_eq!(5 , cur.position());
}
#[ test]
fn from_str32_read_str_len_eof() {
let buf: &[u8] = &[0 xdb, 0 x00, 0 x00, 0 x00];
let mut cur = Cursor::new(buf);
read_str_len(&mut cur).err().unwrap();
assert!(cur.position() >= 1 );
}
#[ test]
fn from_null_read_str_len() {
let buf: &[u8] = &[0 xc0];
let mut cur = Cursor::new(buf);
match read_str_len(&mut cur) {
Err(ValueReadError::TypeMismatch(Marker::Null)) => (),
other => panic!("unexpected result: {other:?}" ),
}
assert_eq!(1 , cur.position());
}
#[ test]
fn from_str_strfix() {
let buf: &[u8] = &[0 xaa, 0 x6c, 0 x65, 0 x20, 0 x6d, 0 x65, 0 x73, 0 x73, 0 x61, 0 x67, 0 x65];
let mut cur = Cursor::new(buf);
let out: &mut [u8] = &mut [0 u8; 16 ];
assert_eq!("le message" , read_str(&mut cur, out).unwrap());
assert_eq!(11 , cur.position());
}
#[ test]
fn from_str_strfix_extra_data() {
let buf: &[u8] = &[0 xaa, 0 x6c, 0 x65, 0 x20, 0 x6d, 0 x65, 0 x73, 0 x73, 0 x61, 0 x67, 0 x65, 0 x00];
let mut cur = Cursor::new(buf);
let out: &mut [u8] = &mut [0 u8; 16 ];
assert_eq!("le message" , read_str(&mut cur, out).unwrap());
assert_eq!(11 , cur.position());
}
#[ test]
fn from_str_strfix_exact_buffer() {
let buf: &[u8] = &[0 xaa, 0 x6c, 0 x65, 0 x20, 0 x6d, 0 x65, 0 x73, 0 x73, 0 x61, 0 x67, 0 x65];
let mut cur = Cursor::new(buf);
let out: &mut [u8] = &mut [0 u8; 10 ];
assert_eq!("le message" , read_str(&mut cur, out).unwrap());
assert_eq!(11 , cur.position());
}
#[ test]
fn from_str_strfix_invalid_utf8() {
// Invalid 2 Octet Sequence.
let buf: &[u8] = &[0 xa2, 0 xc3, 0 x28];
let mut cur = Cursor::new(buf);
let out: &mut [u8] = &mut [0 u8; 16 ];
match read_str(&mut cur, out) {
Err(DecodeStringError::InvalidUtf8(raw, _)) => {
assert_eq!(&[0 xc3, 0 x28], raw);
}
other => panic!("unexpected result: {other:?}" ),
}
assert_eq!(3 , cur.position());
}
#[ test]
fn from_str_strfix_buffer_too_small() {
let buf: &[u8] = &[0 xaa, 0 x6c, 0 x65, 0 x20, 0 x6d, 0 x65, 0 x73, 0 x73, 0 x61, 0 x67, 0 x65];
let mut cur = Cursor::new(buf);
let out: &mut [u8] = &mut [0 u8; 9 ];
match read_str(&mut cur, out) {
Err(DecodeStringError::BufferSizeTooSmall(10 )) => (),
other => panic!("unexpected result: {other:?}" ),
}
assert_eq!(1 , cur.position());
}
#[ test]
fn from_str_strfix_decode_from_slice() {
// Wrap an incomplete buffer into the Cursor to see how many bytes were consumed.
let mut buf = vec![0 xaa, 0 x6c, 0 x65, 0 x20, 0 x6d, 0 x65, 0 x73];
assert!(read_str_from_slice(&buf).is_err());
// ... complete the buffer and try to parse again.
buf.append(&mut vec![0 x73, 0 x61, 0 x67, 0 x65]);
assert_eq!(("le message" , &[][..]), read_str_from_slice(&buf).unwrap());
}
#[ test]
fn from_str_strfix_decode_from_slice_with_trailing_bytes() {
let buf = vec![
0 xaa, 0 x6c, 0 x65, 0 x20, 0 x6d, 0 x65, 0 x73, 0 x73, 0 x61, 0 x67, 0 x65, 0 x01, 0 x02, 0 x03,
];
assert_eq!(("le message" , &[0 x01, 0 x02, 0 x03][..]), read_str_from_slice(&buf).unwrap());
}
#[ test]
fn example_process_sequence_of_strings() {
// Encoded: 'Unpacking', 'multiple', 'strings'.
let vec = [
0 xa9, 0 x55, 0 x6e, 0 x70, 0 x61, 0 x63, 0 x6b, 0 x69, 0 x6e, 0 x67,
0 xa8, 0 x6d, 0 x75, 0 x6c, 0 x74, 0 x69, 0 x70, 0 x6c, 0 x65, 0 xa7,
0 x73, 0 x74, 0 x72, 0 x69, 0 x6e, 0 x67, 0 x73
];
let mut chunks = Vec::new();
let mut unparsed = &vec[..];
loop {
match read_str_from_slice(unparsed) {
Ok((chunk, tail)) => {
chunks.push(chunk);
unparsed = tail;
}
Err(..) => break ,
}
}
assert_eq!(["Unpacking" , "multiple" , "strings" ], chunks[..]);
}
Messung V0.5 in Prozent C=96 H=99 G=97
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-06-27)
¤
*© Formatika GbR, Deutschland