Add tests for message decoding

This commit is contained in:
Patric Kanngießer 2020-12-30 00:39:27 +01:00
parent 0223763ed0
commit 3d3ff68299
5 changed files with 61 additions and 5 deletions

View file

@ -3,7 +3,7 @@ use core::convert::TryFrom;
/// The Channel is a value ranging from 0x0 to 0xF
/// This is a standard midi concept
/// Note Channel1 = 0 on the wire
#[derive(Debug,Copy,Clone)]
#[derive(Debug,Copy,Clone, Eq, PartialEq)]
#[repr(u8)]
pub enum Channel {
Channel1 = 0x0, Channel2 = 0x1, Channel3 = 0x2, Channel4 = 0x3,

View file

@ -14,7 +14,7 @@ type Velocity = U7;
/// Note: not current exhaustive and SysEx messages end up
/// being a confusing case. So are currently note implemented
/// they are sort-of unbounded
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub enum Message {
NoteOff(Channel,Note,Velocity),
NoteOn(Channel,Note,Velocity),

View file

@ -5,7 +5,7 @@ use num_enum::TryFromPrimitive;
/// note the flat versions are associated constants
/// but can be referenced like Note::Bb3
/// C1m is the C-1
#[derive(Debug,Copy,Clone,TryFromPrimitive)]
#[derive(Debug,Copy,Clone,TryFromPrimitive,Eq,PartialEq)]
#[repr(u8)]
pub enum Note {
C1m, Cs1m, D1m, Ds1m, E1m, F1m, Fs1m, G1m, Gs1m, A1m, As1m, B1m,

View file

@ -4,7 +4,7 @@ use crate::data::byte::u4::U4;
/// The Cable Number (CN) is a value ranging from 0x0 to 0xF
/// indicating the number assignment of the Embedded MIDI Jack associated
/// with the endpoint that is transferring the data
#[derive(Debug,Clone,Copy)]
#[derive(Debug,Clone,Copy,Eq, PartialEq)]
#[repr(u8)]
pub enum CableNumber {
Cable0 = 0x0, Cable1 = 0x1, Cable2 = 0x2, Cable3 = 0x3,

View file

@ -9,7 +9,7 @@ use core::convert::TryFrom;
/// A packet that communicates with the host
/// Currently supported is sending the specified normal midi
/// message over the supplied cable number
#[derive(Debug)]
#[derive(Debug,Eq, PartialEq)]
pub struct UsbMidiEventPacket {
pub cable_number : CableNumber,
pub message: Message
@ -85,3 +85,59 @@ impl UsbMidiEventPacket{
}
}
}
#[cfg(test)]
mod tests {
use core::convert::TryFrom;
use crate::data::usb_midi::usb_midi_event_packet::UsbMidiEventPacket;
use crate::data::midi::channel::Channel::{Channel1, Channel2};
use crate::data::midi::notes::Note;
use crate::data::byte::u7::U7;
use crate::data::midi::message::Message;
use crate::data::usb_midi::cable_number::CableNumber::{Cable0,Cable1};
use crate::data::midi::message::control_function::ControlFunction;
macro_rules! decode_message_test {
($($id:ident:$value:expr,)*) => {
$(
#[test]
fn $id() {
let (usb_midi_data_packet,expected) = $value;
let message = UsbMidiEventPacket::try_from(&usb_midi_data_packet[..]).unwrap();
assert_eq!(expected, message);
}
)*
}
}
decode_message_test! {
note_on: ([9, 144, 36, 127], UsbMidiEventPacket {
cable_number: Cable0,
message: Message::NoteOn(Channel1, Note::C2, U7(127))
}),
note_off: ([8, 128, 36, 0], UsbMidiEventPacket {
cable_number: Cable0,
message: Message::NoteOff(Channel1, Note::C2, U7(0))
}),
polyphonic_aftertouch: ([10, 160, 36, 64], UsbMidiEventPacket {
cable_number: Cable0,
message: Message::PolyphonicAftertouch(Channel1, Note::C2, U7(64))
}),
program_change: ([28, 192, 127, 0], UsbMidiEventPacket {
cable_number: Cable1,
message: Message::ProgramChange(Channel1, U7(127))
}),
channel_aftertouch: ([13, 208, 127, 0], UsbMidiEventPacket {
cable_number: Cable0,
message: Message::ChannelAftertouch(Channel1, U7(127))
}),
pitch_wheel: ([14, 224, 64, 32], UsbMidiEventPacket {
cable_number: Cable0,
message: Message::PitchWheelChange(Channel1, U7(64), U7(32))
}),
control_change: ([11, 177, 1, 32], UsbMidiEventPacket {
cable_number: Cable0,
message: Message::ControlChange(Channel2, ControlFunction::MOD_WHEEL_1, U7(32))
}),
}
}