Skip to content

Commit

Permalink
fixup! Implement stream encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
klingtnet committed Jun 10, 2023
1 parent 84311d1 commit 54a0db5
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions tests/decode_encode_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const GOLDEN_MESSAGE_WITH_ALL_TYPES: &str = "2f616e6f746865722f616464726573732f3
const GOLDEN_EMPTY_BUNDLE: &str = "2362756e646c65000000000400000002";
const GOLDEN_BUNDLE: &str = "2362756e646c6500000004d2000010e10000000c2f766965772f31002c000000000000202f6d697865722f6368616e6e656c2f312f616d70000000002c6600003f666666000000442362756e646c65000000162e0000223d000000142f6f73632f312f66726571002c690000000001b8000000182f6f73632f312f7068617365000000002c660000becccccd";
const GOLDEN_MULTI_PACKET: &str = "000000102f736f6d652f6164647200002c0000000000008c2362756e646c6500000004d2000010e10000000c2f766965772f31002c000000000000202f6d697865722f6368616e6e656c2f312f616d70000000002c6600003f666666000000442362756e646c65000000162e0000223d000000142f6f73632f312f66726571002c690000000001b8000000182f6f73632f312f7068617365000000002c660000becccccd";
const GOLDEN_MULTI_PACKET_BUNDLE_FIRST: &str = "0000008c2362756e646c6500000004d2000010e10000000c2f766965772f31002c000000000000202f6d697865722f6368616e6e656c2f312f616d70000000002c6600003f666666000000442362756e646c65000000162e0000223d000000142f6f73632f312f66726571002c690000000001b8000000182f6f73632f312f7068617365000000002c660000becccccd000000102f736f6d652f6164647200002c000000";

fn prefix_with_size(size: u32, golden: &str) -> String {
[hex::encode(size.to_be_bytes()), golden.into()].concat()
Expand Down Expand Up @@ -226,6 +227,83 @@ fn test_multi_packet_message() {
assert_eq!(packets, decoded_packets);
}

#[test]
fn test_multi_packet_message_bundle_first() {
let packets = vec![
OscPacket::Bundle(OscBundle {
timetag: (1234, 4321).into(),
content: vec![
OscPacket::Message(OscMessage {
addr: "/view/1".to_string(),
args: vec![],
}),
OscPacket::Message(OscMessage {
addr: "/mixer/channel/1/amp".to_string(),
args: vec![0.9f32.into()],
}),
OscPacket::Bundle(OscBundle {
timetag: (5678, 8765).into(),
content: vec![
OscPacket::Message(OscMessage {
addr: "/osc/1/freq".to_string(),
args: vec![440i32.into()],
}),
OscPacket::Message(OscMessage {
addr: "/osc/1/phase".to_string(),
args: vec![(-0.4f32).into()],
}),
],
}),
],
}),
OscPacket::Message(OscMessage {
addr: "/some/addr".to_string(),
args: vec![],
}),
];

// Stream encoding and decoding.
let bytes = encoder::encode_tcp(&packets).expect("stream encode failed");
assert_eq!(GOLDEN_MULTI_PACKET_BUNDLE_FIRST, hex::encode(&bytes));

let (remainder, decoded_packets) =
decoder::decode_tcp_vec(&bytes).expect("stream decode failed");
assert!(remainder.is_empty());
assert_eq!(
vec![OscPacket::Bundle(OscBundle {
timetag: (1234, 4321).into(),
content: vec![
OscPacket::Message(OscMessage {
addr: "/view/1".to_string(),
args: vec![],
}),
OscPacket::Message(OscMessage {
addr: "/mixer/channel/1/amp".to_string(),
args: vec![0.9f32.into()],
}),
OscPacket::Bundle(OscBundle {
timetag: (5678, 8765).into(),
content: vec![
OscPacket::Message(OscMessage {
addr: "/osc/1/freq".to_string(),
args: vec![440i32.into()],
}),
OscPacket::Message(OscMessage {
addr: "/osc/1/phase".to_string(),
args: vec![(-0.4f32).into()],
}),
],
}),
OscPacket::Message(OscMessage {
addr: "/some/addr".to_string(),
args: vec![],
}),
],
}),],
decoded_packets
);
}

#[cfg(feature = "std")]
#[test]
fn test_bundle_cursor() {
Expand Down

0 comments on commit 54a0db5

Please sign in to comment.