1 Star 0 Fork 4

连建锋 / jppe-rs

forked from JanKinCai / jppe-rs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
test_modifier_length.rs 2.25 KB
一键复制 编辑 原始数据 按行查看 历史
JanKinCai 提交于 2024-03-06 18:13 . :white_check_mark:test(upgrade): update unit test code.
#![feature(let_chains)]
use jppe::{BorrowByteEncode, BorrowByteDecode, FieldAttrModifiers};
// use jppe::prelude::*;
use jppe_derive::{BorrowByteDecode, BorrowByteEncode};
#[derive(Debug, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
pub struct StructLengthExample1<'a> {
#[jppe(length=3)]
pub a: u32,
#[jppe(length=1)]
pub b: &'a [u8],
#[jppe(length=1)]
pub c: Vec<u8,>
}
#[test]
fn test_struct_length_example1() {
let value = StructLengthExample1 { a: 1, b: &[1], c: vec![1]};
let mut buf = vec![];
value.encode(&mut buf, None, None);
assert_eq!(buf, vec![0x00, 0x00, 0x01, 0x01, 0x01]);
let (input, value2) = StructLengthExample1::decode(&[0x00, 0x00, 0x01, 0x01, 0x01], None, None).unwrap();
assert_eq!(value2, value);
assert_eq!(input.is_empty(), true);
}
#[derive(Debug, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
pub struct StructLengthExample2<'a> {
pub a: u8,
#[jppe(length="a")]
pub b: &'a [u8],
pub c: u8,
#[jppe(length="c - 1")]
pub d: &'a [u8],
}
#[test]
fn test_struct_length_example2() {
let input = vec![0x01, 0x01, 0x02, 0x01];
let value = StructLengthExample2 { a: 1, b: &[1], c: 2, d: &[1]};
let mut buf = vec![];
value.encode(&mut buf, None, None);
assert_eq!(buf, input);
let (input, value2) = StructLengthExample2::decode(&input, None, None).unwrap();
assert_eq!(value2, value);
assert_eq!(input.is_empty(), true);
}
#[derive(Debug, Default, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
pub enum EnumLengthExample1<'a> {
Jkc {
a: u8,
#[jppe(length="a")]
b: &'a [u8],
c: u8,
// #[jppe(length="c - 1")]
d: &'a [u8],
},
#[jppe(branch_default)]
#[default]
Unknown
}
#[test]
fn test_struct_length_example3() {
let input = vec![0x01, 0x01, 0x02, 0x01];
let value = EnumLengthExample1::Jkc { a: 1, b: &[1], c: 2, d: &[1]} ;
let mut buf = vec![];
value.encode(&mut buf, None, None);
assert_eq!(buf, input);
let fattr = FieldAttrModifiers { branch: Some(0), ..Default::default() };
let (input, value2) = EnumLengthExample1::decode(&input, None, Some(&fattr)).unwrap();
assert_eq!(value2, value);
assert_eq!(input.is_empty(), true);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Rust
1
https://gitee.com/lian_jianfeng/jppe-rs.git
git@gitee.com:lian_jianfeng/jppe-rs.git
lian_jianfeng
jppe-rs
jppe-rs
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891