You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.0KB

  1. extern crate rosc;
  2. use rosc::{decoder, encoder};
  3. use rosc::{OscArray, OscBundle, OscColor, OscMessage, OscMidiMessage, OscPacket, OscType};
  4. #[test]
  5. fn test_encode_message_wo_args() {
  6. let msg_packet = OscPacket::Message(OscMessage {
  7. addr: "/some/addr".to_string(),
  8. args: vec![],
  9. });
  10. let enc_msg = encoder::encode(&msg_packet).unwrap();
  11. assert_eq!(enc_msg.len() % 4, 0);
  12. let msg = match msg_packet {
  13. OscPacket::Message(ref msg) => msg,
  14. _ => panic!(),
  15. };
  16. let dec_msg = match decoder::decode(&enc_msg).unwrap() {
  17. OscPacket::Message(m) => m,
  18. _ => panic!("Expected OscMessage!"),
  19. };
  20. assert_eq!(*msg, dec_msg)
  21. }
  22. #[test]
  23. fn test_encode_empty_bundle() {
  24. let bundle_packet = OscPacket::Bundle(OscBundle {
  25. timetag: (4, 2),
  26. content: vec![],
  27. });
  28. let enc_bundle = encoder::encode(&bundle_packet).unwrap();
  29. assert_eq!(enc_bundle.len() % 4, 0);
  30. assert_eq!(enc_bundle.len(), 16);
  31. let dec_bundle = match decoder::decode(&enc_bundle).unwrap() {
  32. OscPacket::Bundle(m) => m,
  33. _ => panic!("Expected OscBundle!"),
  34. };
  35. let bundle = match bundle_packet {
  36. OscPacket::Bundle(ref bundle) => bundle,
  37. _ => panic!(),
  38. };
  39. assert_eq!(*bundle, dec_bundle)
  40. }
  41. #[test]
  42. fn test_encode_message_with_args() {
  43. let msg_packet = OscPacket::Message(OscMessage {
  44. addr: "/another/address/1".to_string(),
  45. args: vec![
  46. 4i32.into(),
  47. 42i64.into(),
  48. 3.1415926f32.into(),
  49. 3.14159265359f64.into(),
  50. "This is a string.".to_string().into(),
  51. "This is a string too.".into(),
  52. vec![1u8, 2u8, 3u8].into(),
  53. (123, 456).into(),
  54. 'c'.into(),
  55. false.into(),
  56. true.into(),
  57. OscType::Nil,
  58. OscType::Inf,
  59. OscMidiMessage {
  60. port: 4,
  61. status: 41,
  62. data1: 42,
  63. data2: 129,
  64. }
  65. .into(),
  66. OscColor {
  67. red: 255,
  68. green: 192,
  69. blue: 42,
  70. alpha: 13,
  71. }
  72. .into(),
  73. OscArray {
  74. content: vec![
  75. 42i32.into(),
  76. OscArray {
  77. content: vec![1.23.into(), 3.21.into()],
  78. }
  79. .into(),
  80. "Yay".into(),
  81. ],
  82. }
  83. .into(),
  84. ],
  85. });
  86. let enc_msg = encoder::encode(&msg_packet).unwrap();
  87. assert_eq!(enc_msg.len() % 4, 0);
  88. let dec_msg: OscMessage = match decoder::decode(&enc_msg).unwrap() {
  89. OscPacket::Message(m) => m,
  90. _ => panic!("Expected OscMessage!"),
  91. };
  92. let msg = match msg_packet {
  93. OscPacket::Message(ref msg) => msg,
  94. _ => panic!(),
  95. };
  96. assert_eq!(*msg, dec_msg);
  97. }
  98. #[test]
  99. fn test_encode_bundle() {
  100. let msg0 = OscMessage {
  101. addr: "/view/1".to_string(),
  102. args: vec![],
  103. };
  104. let msg1 = OscMessage {
  105. addr: "/mixer/channel/1/amp".to_string(),
  106. args: vec![0.9f32.into()],
  107. };
  108. let msg2 = OscMessage {
  109. addr: "/osc/1/freq".to_string(),
  110. args: vec![440i32.into()],
  111. };
  112. let msg3 = OscMessage {
  113. addr: "/osc/1/phase".to_string(),
  114. args: vec![(-0.4f32).into()],
  115. };
  116. let bundle1 = OscBundle {
  117. timetag: (5678, 8765).into(),
  118. content: vec![OscPacket::Message(msg2), OscPacket::Message(msg3)],
  119. };
  120. let root_bundle = OscPacket::Bundle(OscBundle {
  121. timetag: (1234, 4321).into(),
  122. content: vec![
  123. OscPacket::Message(msg0),
  124. OscPacket::Message(msg1),
  125. OscPacket::Bundle(bundle1),
  126. ],
  127. });
  128. let enc_bundle = encoder::encode(&root_bundle).unwrap();
  129. assert_eq!(enc_bundle.len() % 4, 0);
  130. let dec_bundle = decoder::decode(&enc_bundle).unwrap();
  131. assert_eq!(root_bundle, dec_bundle);
  132. }