aboutsummaryrefslogtreecommitdiff
path: root/src/tests/base_types.rs
blob: a35f2439b17b7e3051cb7da9f7d4c3f5fb206369 (plain)
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
use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8};
use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8};

use crate::protocol::primitive::*;

#[test]
pub fn serialize_string() {
    let test_string: String = String::from("Configured");

    assert_eq!(test_string.serialize(), [0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100]);
}

#[test]
pub fn serialize_string_utf8() {
    let test_string: String = String::from("Configured");

    assert_eq!(test_string.serialize_utf8(), [0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114, 101, 100]);
}

#[test]
pub fn deserialize_string() {
    let test_bytes: &[u8] = &[0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1];
    let (len, res) = String::parse(test_bytes);
    assert_eq!(res, "Configured");
    assert_eq!(len, 24);
}

#[test]
pub fn deserialize_string_utf8() {
    let test_bytes: &[u8] = &[0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114, 101, 100, 0, 0, 0, 1];
    let (len, res) = String::parse_utf8(test_bytes);
    assert_eq!(len, 14);
    assert_eq!(res, "Configured");
}

#[test]
pub fn serialize_string_list() {
    let mut test_list = StringList::new();
    test_list.push("Configured".to_string());
    assert_eq!(
        test_list.serialize(),
        [0, 0, 0, 1, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100]
    )
}

#[test]
pub fn deserialize_string_list() {
    let test_bytes: &[u8] = &[0, 0, 0, 24, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1];
    let mut test_list = StringList::new();
    test_list.push("Configured".to_string());
    println!("aaaaa");
    let (len, res) = StringList::parse(test_bytes);
    assert_eq!(len, 28);
    assert_eq!(test_list, res);
}