Skip to content

Commit cac72de

Browse files
committed
Add some tests for integer binprot
1 parent ff53e53 commit cac72de

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

mina-p2p-messages/src/number.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,65 @@ where
159159
self.0.binprot_write(w)
160160
}
161161
}
162+
163+
#[cfg(test)]
164+
mod tests {
165+
use binprot::{BinProtRead, BinProtWrite};
166+
167+
#[test]
168+
fn u32_roundtrip() {
169+
for u32 in [
170+
0,
171+
1,
172+
u8::MAX as u32,
173+
u16::MAX as u32,
174+
u32::MAX,
175+
i8::MAX as u32,
176+
i16::MAX as u32,
177+
i32::MAX as u32,
178+
] {
179+
let mut buf = Vec::new();
180+
u32.binprot_write(&mut buf).unwrap();
181+
let mut r = buf.as_slice();
182+
if u32 <= 0x7f {
183+
assert_eq!(r[0], u32 as u8);
184+
} else {
185+
assert!(matches!(r[0], 0xfe | 0xfd | 0xfc));
186+
}
187+
let u32_ = u32::binprot_read(&mut r).unwrap();
188+
assert_eq!(r.len(), 0);
189+
assert_eq!(u32, u32_);
190+
}
191+
}
192+
193+
#[test]
194+
fn i32_roundtrip() {
195+
for i32 in [
196+
0,
197+
1,
198+
u8::MAX as i32,
199+
u16::MAX as i32,
200+
u32::MAX as i32,
201+
i8::MAX as i32,
202+
i16::MAX as i32,
203+
i32::MAX as i32,
204+
i8::MIN as i32,
205+
i16::MIN as i32,
206+
i32::MIN as i32,
207+
] {
208+
let mut buf = Vec::new();
209+
i32.binprot_write(&mut buf).unwrap();
210+
let mut r = buf.as_slice();
211+
if -0x80 <= i32 && i32 < 0 {
212+
assert_eq!(r[0], 0xff);
213+
} else if 0 <= i32 && i32 <= 0x80 {
214+
assert_eq!(r[0], i32 as u8);
215+
} else {
216+
assert!(matches!(r[0], 0xfe | 0xfd | 0xfc));
217+
}
218+
let i32_ = i32::binprot_read(&mut r).unwrap();
219+
assert_eq!(r.len(), 0);
220+
assert_eq!(i32, i32_);
221+
}
222+
}
223+
}

0 commit comments

Comments
 (0)