LCOV - code coverage report
Current view: top level - libs/utils/tests - bin_ser_test.rs (source / functions) Coverage Total Hit
Test: 8ff8efadb0253cf618c612650348666c0c564111.info Lines: 100.0 % 22 22
Test Date: 2024-11-20 17:53:50 Functions: 36.4 % 11 4

            Line data    Source code
       1              : use bytes::{Buf, BytesMut};
       2              : use hex_literal::hex;
       3              : use serde::Deserialize;
       4              : use std::io::Read;
       5              : use utils::bin_ser::LeSer;
       6              : 
       7            1 : #[derive(Debug, PartialEq, Eq, Deserialize)]
       8              : pub struct HeaderData {
       9              :     magic: u16,
      10              :     info: u16,
      11              :     tli: u32,
      12              :     pageaddr: u64,
      13              :     len: u32,
      14              : }
      15              : 
      16              : // A manual implementation using BytesMut, just so we can
      17              : // verify that we decode the same way.
      18            1 : pub fn decode_header_data(buf: &mut BytesMut) -> HeaderData {
      19            1 :     HeaderData {
      20            1 :         magic: buf.get_u16_le(),
      21            1 :         info: buf.get_u16_le(),
      22            1 :         tli: buf.get_u32_le(),
      23            1 :         pageaddr: buf.get_u64_le(),
      24            1 :         len: buf.get_u32_le(),
      25            1 :     }
      26            1 : }
      27              : 
      28            1 : pub fn decode2<R: Read>(reader: &mut R) -> HeaderData {
      29            1 :     HeaderData::des_from(reader).unwrap()
      30            1 : }
      31              : 
      32              : #[test]
      33            1 : fn test1() {
      34            1 :     let raw1 = hex!("8940 7890 5534 7890  1289 5379 8378 7893  4207 8923 4712 3218");
      35            1 :     let mut buf1 = BytesMut::from(&raw1[..]);
      36            1 :     let mut buf2 = &raw1[..];
      37            1 :     let dec1 = decode_header_data(&mut buf1);
      38            1 :     let dec2 = decode2(&mut buf2);
      39            1 :     assert_eq!(dec1, dec2);
      40            1 :     assert_eq!(buf1, buf2);
      41            1 : }
        

Generated by: LCOV version 2.1-beta