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.

130 lines
4.3KB

  1. extern crate byteorder;
  2. extern crate rosc;
  3. use byteorder::{BigEndian, ByteOrder};
  4. use std::mem;
  5. use rosc::{decoder, encoder, OscBundle, OscPacket, OscType};
  6. #[test]
  7. fn test_decode_no_args() {
  8. // message to build: /some/valid/address/4 ,
  9. let raw_addr = "/some/valid/address/4";
  10. let addr = encoder::encode_string(raw_addr);
  11. let type_tags = encoder::encode_string(",");
  12. let merged: Vec<u8> = addr.into_iter().chain(type_tags.into_iter()).collect();
  13. let osc_packet: Result<rosc::OscPacket, rosc::OscError> = decoder::decode(&merged);
  14. assert!(osc_packet.is_ok());
  15. match osc_packet {
  16. Ok(rosc::OscPacket::Message(msg)) => {
  17. assert_eq!(raw_addr, msg.addr);
  18. assert!(msg.args.is_empty());
  19. }
  20. Ok(_) => panic!("Expected an OscMessage!"),
  21. Err(e) => panic!(e),
  22. }
  23. }
  24. #[test]
  25. fn test_decode_empty_bundle() {
  26. let timetag = (4, 2);
  27. let content = vec![];
  28. let packet = encoder::encode(&OscPacket::Bundle(OscBundle { timetag, content })).unwrap();
  29. let osc_packet: Result<rosc::OscPacket, rosc::OscError> = decoder::decode(&packet);
  30. match osc_packet {
  31. Ok(rosc::OscPacket::Bundle(bundle)) => {
  32. assert_eq!(timetag, bundle.timetag);
  33. assert!(bundle.content.is_empty());
  34. }
  35. Ok(_) => panic!("Expected an OscBundle!"),
  36. Err(e) => panic!(e),
  37. }
  38. }
  39. #[test]
  40. fn test_decode_args() {
  41. // /another/valid/address/123 ,fdih 3.1415 3.14159265359 12345678i32
  42. // -1234567891011
  43. let addr = encoder::encode_string("/another/valid/address/123");
  44. // args
  45. let f = 3.1415f32;
  46. let mut f_bytes: [u8; 4] = [0u8; 4];
  47. BigEndian::write_f32(&mut f_bytes, f);
  48. assert_eq!(BigEndian::read_f32(&f_bytes), f);
  49. let d = 3.14159265359f64;
  50. let mut d_bytes: [u8; 8] = [0u8; 8];
  51. BigEndian::write_f64(&mut d_bytes, d);
  52. assert_eq!(BigEndian::read_f64(&d_bytes), d);
  53. let i = 12345678i32;
  54. let i_bytes: [u8; 4] = unsafe { mem::transmute(i.to_be()) };
  55. let l = -1234567891011i64;
  56. let h_bytes: [u8; 8] = unsafe { mem::transmute(l.to_be()) };
  57. let blob_size: [u8; 4] = unsafe { mem::transmute(6u32.to_be()) };
  58. let blob: Vec<u8> = vec![1u8, 2u8, 3u8, 4u8, 5u8, 6u8];
  59. let s = "I am an osc test string.";
  60. assert!(s.is_ascii());
  61. // Osc strings are null terminated like in C!
  62. let s_bytes: Vec<u8> = encoder::encode_string(s);
  63. let c = '$';
  64. let c_bytes: [u8; 4] = unsafe { mem::transmute((c as u32).to_be()) };
  65. let a = vec![OscType::Int(i), OscType::Float(f), OscType::Int(i)];
  66. let type_tags = encoder::encode_string(",fdsTFibhNIc[ifi]");
  67. let args: Vec<u8> = f_bytes
  68. .iter()
  69. .chain(d_bytes.iter())
  70. .chain(s_bytes.iter())
  71. .chain(i_bytes.iter())
  72. .chain(blob_size.iter())
  73. .chain(blob.iter())
  74. .chain(vec![0u8, 0u8].iter())
  75. .chain(h_bytes.iter())
  76. .chain(c_bytes.iter())
  77. // array content
  78. .chain(i_bytes.iter())
  79. .chain(f_bytes.iter())
  80. .chain(i_bytes.iter())
  81. .map(|x| *x)
  82. .collect::<Vec<u8>>();
  83. let merged: Vec<u8> = addr
  84. .into_iter()
  85. .chain(type_tags.into_iter())
  86. .chain(args)
  87. .collect::<Vec<u8>>();
  88. match decoder::decode(&merged).unwrap() {
  89. rosc::OscPacket::Message(msg) => {
  90. for arg in msg.args {
  91. match arg {
  92. rosc::OscType::Int(x) => assert_eq!(i, x),
  93. rosc::OscType::Long(x) => assert_eq!(l, x),
  94. rosc::OscType::Float(x) => assert_eq!(f, x),
  95. rosc::OscType::Double(x) => assert_eq!(d, x),
  96. rosc::OscType::String(x) => assert_eq!(s, x),
  97. rosc::OscType::Blob(x) => assert_eq!(blob, x),
  98. // cant assign bool args to type_tag
  99. // , so there is no real test wether the value is
  100. // correct or not
  101. rosc::OscType::Bool(_) => (),
  102. rosc::OscType::Inf => (),
  103. rosc::OscType::Nil => (),
  104. // test time-tags, midi-messages and chars
  105. rosc::OscType::Char(x) => assert_eq!(c, x),
  106. rosc::OscType::Array(x) => assert_eq!(a, x.content),
  107. _ => panic!(),
  108. }
  109. }
  110. }
  111. _ => panic!("Expected an OSC message!"),
  112. }
  113. }