-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_type_hashmap.rs
86 lines (60 loc) · 2.23 KB
/
test_type_hashmap.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::collections::HashMap;
use jppe_derive::{BorrowByteEncode, BorrowByteDecode};
use jppe::{BorrowByteDecode, BorrowByteEncode};
#[derive(Debug, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
pub struct HashMapExample<'a> {
pub kv: HashMap<&'a str, &'a str>,
}
#[test]
fn test_type_hashmap() {
let mut hashmap_value = HashMap::new();
hashmap_value.insert("A1", "jkc1");
hashmap_value.insert("A2", "jkc2");
hashmap_value.insert("A3", "");
let hashmap_value = HashMapExample {
kv: hashmap_value,
};
let mut buf = vec![];
hashmap_value.encode(&mut buf, None, None);
let (input, value): (&[u8], HashMapExample) = BorrowByteDecode::decode(&buf, None, None).unwrap();
assert_eq!(input.is_empty(), true);
assert_eq!(value, hashmap_value);
}
#[derive(Debug, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
pub struct HashMapExample2<'a> {
#[jppe(split=b"=", linend=b"\r\n")]
pub kv: HashMap<&'a [u8], &'a [u8]>,
}
#[test]
fn test_type_hashmap_key_and_split() {
let input = b"A1=jkc1\r\nA2=jkc2\r\njkc";
let (input, value) = HashMapExample2::decode(input, None, None).unwrap();
let mut hashmap_value = HashMap::new();
hashmap_value.insert("A1".as_bytes(), "jkc1".as_bytes());
hashmap_value.insert("A2".as_bytes(), "jkc2".as_bytes());
let hashmap_value = HashMapExample2 {
kv: hashmap_value,
};
assert_eq!(value, hashmap_value);
assert_eq!(input, b"jkc");
let mut buf = vec![];
value.encode(&mut buf, None, None);
assert!(buf == b"A1=jkc1\r\nA2=jkc2\r\n" || buf == b"A2=jkc2\r\nA1=jkc1\r\n" );
}
#[derive(Debug, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
pub struct HashMapExample3<'a> {
#[jppe(split=b"=", linend=b"\r\n", count=1)]
pub kv: HashMap<&'a [u8], &'a [u8]>,
}
#[test]
fn test_type_hashmap_key_split_count() {
let input = b"A1=jkc1\r\nA2=jkc2\r\njkc";
let (input, value) = HashMapExample3::decode(input, None, None).unwrap();
let mut hashmap_value = HashMap::new();
hashmap_value.insert("A1".as_bytes(), "jkc1".as_bytes());
let hashmap_value = HashMapExample3 {
kv: hashmap_value,
};
assert_eq!(value, hashmap_value);
assert_eq!(input, b"A2=jkc2\r\njkc");
}