use std::collections::BTreeMap;
use CborType;
#[ test]
fn test_nint() {
struct Testcase {
value: i64,
expected: Vec<u8>,
}
let testcases: Vec<Testcase> = vec![
Testcase {
value: -1 ,
expected: vec![0 x20],
},
Testcase {
value: -10 ,
expected: vec![0 x29],
},
Testcase {
value: -100 ,
expected: vec![0 x38, 0 x63],
},
Testcase {
value: -1000 ,
expected: vec![0 x39, 0 x03, 0 xe7],
},
Testcase {
value: -1000000 ,
expected: vec![0 x3a, 0 x00, 0 x0f, 0 x42, 0 x3f],
},
Testcase {
value: -4611686018427387903 ,
expected: vec![0 x3b, 0 x3f, 0 xff, 0 xff, 0 xff, 0 xff, 0 xff, 0 xff, 0 xfe],
},
];
for testcase in testcases {
let cbor = CborType::SignedInteger(testcase.value);
assert_eq!(testcase.expected, cbor.serialize());
}
}
#[ test]
fn test_bstr() {
struct Testcase {
value: Vec<u8>,
expected: Vec<u8>,
}
let testcases: Vec<Testcase> = vec![
Testcase {
value: vec![],
expected: vec![0 x40],
},
Testcase {
value: vec![0 x01, 0 x02, 0 x03, 0 x04],
expected: vec![0 x44, 0 x01, 0 x02, 0 x03, 0 x04],
},
Testcase {
value: vec![
0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf,
0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf,
],
expected: vec![
0 x58, 0 x19, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf,
0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf, 0 xaf,
],
},
];
for testcase in testcases {
let cbor = CborType::Bytes(testcase.value);
assert_eq!(testcase.expected, cbor.serialize());
}
}
#[ test]
fn test_tstr() {
struct Testcase {
value: String,
expected: Vec<u8>,
}
let testcases: Vec<Testcase> = vec![
Testcase {
value: String::new(),
expected: vec![0 x60],
},
Testcase {
value: String::from("a" ),
expected: vec![0 x61, 0 x61],
},
Testcase {
value: String::from("IETF" ),
expected: vec![0 x64, 0 x49, 0 x45, 0 x54, 0 x46],
},
Testcase {
value: String::from("\" \\"),
expected: vec![0 x62, 0 x22, 0 x5c],
},
Testcase {
value: String::from("水" ),
expected: vec![0 x63, 0 xe6, 0 xb0, 0 xb4],
},
];
for testcase in testcases {
let cbor = CborType::String(testcase.value);
assert_eq!(testcase.expected, cbor.serialize());
}
}
#[ test]
fn test_arr() {
struct Testcase {
value: Vec<CborType>,
expected: Vec<u8>,
}
let nested_arr_1 = vec![CborType::Integer(2 ), CborType::Integer(3 )];
let nested_arr_2 = vec![CborType::Integer(4 ), CborType::Integer(5 )];
let testcases: Vec<Testcase> = vec![
Testcase {
value: vec![],
expected: vec![0 x80],
},
Testcase {
value: vec![
CborType::Integer(1 ),
CborType::Integer(2 ),
CborType::Integer(3 ),
],
expected: vec![0 x83, 0 x01, 0 x02, 0 x03],
},
Testcase {
value: vec![
CborType::Integer(1 ),
CborType::Array(nested_arr_1),
CborType::Array(nested_arr_2),
],
expected: vec![0 x83, 0 x01, 0 x82, 0 x02, 0 x03, 0 x82, 0 x04, 0 x05],
},
Testcase {
value: vec![
CborType::Integer(1 ),
CborType::Integer(2 ),
CborType::Integer(3 ),
CborType::Integer(4 ),
CborType::Integer(5 ),
CborType::Integer(6 ),
CborType::Integer(7 ),
CborType::Integer(8 ),
CborType::Integer(9 ),
CborType::Integer(10 ),
CborType::Integer(11 ),
CborType::Integer(12 ),
CborType::Integer(13 ),
CborType::Integer(14 ),
CborType::Integer(15 ),
CborType::Integer(16 ),
CborType::Integer(17 ),
CborType::Integer(18 ),
CborType::Integer(19 ),
CborType::Integer(20 ),
CborType::Integer(21 ),
CborType::Integer(22 ),
CborType::Integer(23 ),
CborType::Integer(24 ),
CborType::Integer(25 ),
],
expected: vec![
0 x98, 0 x19, 0 x01, 0 x02, 0 x03, 0 x04, 0 x05, 0 x06, 0 x07, 0 x08, 0 x09, 0 x0a, 0 x0b, 0 x0c,
0 x0d, 0 x0e, 0 x0f, 0 x10, 0 x11, 0 x12, 0 x13, 0 x14, 0 x15, 0 x16, 0 x17, 0 x18, 0 x18, 0 x18,
0 x19,
],
},
];
for testcase in testcases {
let cbor = CborType::Array(testcase.value);
assert_eq!(testcase.expected, cbor.serialize());
}
}
#[ test]
fn test_map() {
let empty_map: BTreeMap<CborType, CborType> = BTreeMap::new();
assert_eq!(vec![0 xa0], CborType::Map(empty_map).serialize());
let mut positive_map: BTreeMap<CborType, CborType> = BTreeMap::new();
positive_map.insert(CborType::Integer(20 ), CborType::Integer(10 ));
positive_map.insert(CborType::Integer(10 ), CborType::Integer(20 ));
positive_map.insert(CborType::Integer(15 ), CborType::Integer(15 ));
assert_eq!(
vec![0 xa3, 0 x0a, 0 x14, 0 x0f, 0 x0f, 0 x14, 0 x0a],
CborType::Map(positive_map).serialize()
);
let mut negative_map: BTreeMap<CborType, CborType> = BTreeMap::new();
negative_map.insert(CborType::SignedInteger(-4 ), CborType::Integer(10 ));
negative_map.insert(CborType::SignedInteger(-1 ), CborType::Integer(20 ));
negative_map.insert(CborType::SignedInteger(-5 ), CborType::Integer(15 ));
negative_map.insert(CborType::SignedInteger(-6 ), CborType::Integer(10 ));
assert_eq!(
vec![0 xa4, 0 x20, 0 x14, 0 x23, 0 x0a, 0 x24, 0 x0f, 0 x25, 0 x0a],
CborType::Map(negative_map).serialize()
);
let mut mixed_map: BTreeMap<CborType, CborType> = BTreeMap::new();
mixed_map.insert(CborType::Integer(0 ), CborType::Integer(10 ));
mixed_map.insert(CborType::SignedInteger(-10 ), CborType::Integer(20 ));
mixed_map.insert(CborType::Integer(15 ), CborType::Integer(15 ));
assert_eq!(
vec![0 xa3, 0 x00, 0 x0a, 0 x0f, 0 x0f, 0 x29, 0 x14],
CborType::Map(mixed_map).serialize()
);
let mut very_mixed_map: BTreeMap<CborType, CborType> = BTreeMap::new();
very_mixed_map.insert(CborType::Integer(0 ), CborType::Integer(10 ));
very_mixed_map.insert(
CborType::SignedInteger(-10000 ),
CborType::String("low" .to_string()),
);
very_mixed_map.insert(CborType::SignedInteger(-10 ), CborType::Integer(20 ));
very_mixed_map.insert(
CborType::Integer(10001 ),
CborType::String("high" .to_string()),
);
very_mixed_map.insert(
CborType::Integer(10000 ),
CborType::String("high" .to_string()),
);
very_mixed_map.insert(CborType::Integer(15 ), CborType::Integer(15 ));
let expected = vec![
0 xa6, 0 x00, 0 x0a, 0 x0f, 0 x0f, 0 x29, 0 x14, 0 x19, 0 x27, 0 x10, 0 x64, 0 x68, 0 x69, 0 x67, 0 x68,
0 x19, 0 x27, 0 x11, 0 x64, 0 x68, 0 x69, 0 x67, 0 x68, 0 x39, 0 x27, 0 x0F, 0 x63, 0 x6C, 0 x6F, 0 x77,
];
assert_eq!(expected, CborType::Map(very_mixed_map).serialize());
}
#[ test]
#[ ignore]
// XXX: The string isn't put into the map at the moment, so we can't actually
// test this.
fn test_invalid_map() {
let mut invalid_map: BTreeMap<CborType, CborType> = BTreeMap::new();
invalid_map.insert(CborType::SignedInteger(-10 ), CborType::Integer(20 ));
invalid_map.insert(CborType::String("0" .to_string()), CborType::Integer(10 ));
invalid_map.insert(CborType::Integer(15 ), CborType::Integer(15 ));
let expected: Vec<u8> = vec![];
assert_eq!(expected, CborType::Map(invalid_map).serialize());
}
#[ test]
fn test_integer() {
struct Testcase {
value: u64,
expected: Vec<u8>,
}
let testcases: Vec<Testcase> = vec![
Testcase {
value: 0 ,
expected: vec![0 ],
},
Testcase {
value: 1 ,
expected: vec![1 ],
},
Testcase {
value: 10 ,
expected: vec![0 x0a],
},
Testcase {
value: 23 ,
expected: vec![0 x17],
},
Testcase {
value: 24 ,
expected: vec![0 x18, 0 x18],
},
Testcase {
value: 25 ,
expected: vec![0 x18, 0 x19],
},
Testcase {
value: 100 ,
expected: vec![0 x18, 0 x64],
},
Testcase {
value: 1000 ,
expected: vec![0 x19, 0 x03, 0 xe8],
},
Testcase {
value: 1000000 ,
expected: vec![0 x1a, 0 x00, 0 x0f, 0 x42, 0 x40],
},
Testcase {
value: 1000000000000 ,
expected: vec![0 x1b, 0 x00, 0 x00, 0 x00, 0 xe8, 0 xd4, 0 xa5, 0 x10, 0 x00],
},
Testcase {
value: 18446744073709551615 ,
expected: vec![0 x1b, 0 xff, 0 xff, 0 xff, 0 xff, 0 xff, 0 xff, 0 xff, 0 xff],
},
];
for testcase in testcases {
let cbor = CborType::Integer(testcase.value);
assert_eq!(testcase.expected, cbor.serialize());
}
}
#[ test]
fn test_tagged_item() {
let cbor = CborType::Tag(0 x12, Box ::new(CborType::Integer(2 ).clone()));
assert_eq!(vec![0 xD2, 0 x02], cbor.serialize());
let cbor = CborType::Tag(0 x62, Box ::new(CborType::Array(vec![]).clone()));
assert_eq!(vec![0 xD8, 0 x62, 0 x80], cbor.serialize());
}
#[ test]
fn test_null() {
let cbor = CborType::Null;
assert_eq!(vec![0 xf6], cbor.serialize());
}
#[ test]
fn test_null_in_array() {
let cbor = CborType::Array(vec![CborType::Null, CborType::Null]);
assert_eq!(vec![0 x82, 0 xf6, 0 xf6], cbor.serialize());
}
Messung V0.5 in Prozent C=100 H=100 G=100
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-22)
¤
*© Formatika GbR, Deutschland