Skip to content

Commit

Permalink
Support nostd for tests.
Browse files Browse the repository at this point in the history
Some tests relied on std features which broke when running with
nostd. Resolved by adding a nostd alternative to `TryFrom` and tagging
vec reliant code to not run on nostd.
  • Loading branch information
wmedrano committed Dec 18, 2023
1 parent aa50543 commit d790400
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
run: cargo clippy --all-targets --all-features -- -D clippy::all
- name: Test
run: cargo test --verbose
- name: Test nostd
run: cargo test --no-default-features
release:
runs-on: ubuntu-latest
needs: [test]
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT"
name = "wmidi"
readme = "README.md"
repository = "https://github.com/RustAudio/wmidi"
version = "4.0.8"
version = "4.0.9"

[lib]
# Required to pass flags to criterion benchmark.
Expand Down
20 changes: 13 additions & 7 deletions src/byte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ impl U7 {
/// The maximum value for a u7 data byte.
pub const MAX: U7 = U7(0x80 - 0x01);

/// Create a new `U7` or return an error if it is out of range.
#[inline(always)]
pub fn new(data: u8) -> Result<U7, Error> {
if data > u8::from(U7::MAX) {
Err(Error::DataByteOutOfRange)
} else {
Ok(U7(data))
}
}

/// Convert a `u8` into a `U7` without bounds checking.
///
/// # Safety
Expand Down Expand Up @@ -65,11 +75,7 @@ impl TryFrom<u8> for U7 {

#[inline(always)]
fn try_from(data: u8) -> Result<U7, Error> {
if data > u8::from(U7::MAX) {
Err(Error::DataByteOutOfRange)
} else {
Ok(U7(data))
}
U7::new(data)
}
}

Expand Down Expand Up @@ -158,7 +164,7 @@ mod tests {

#[test]
fn try_from_out_of_range_fails() {
for n in 0x80..=std::u8::MAX {
for n in 0x80..=u8::MAX {
assert_eq!(U7::try_from(n), Err(Error::DataByteOutOfRange));
}
}
Expand Down Expand Up @@ -204,7 +210,7 @@ mod tests {

#[test]
fn try_from_out_of_range_16_fails() {
for n in 0x4000..=std::u16::MAX {
for n in 0x4000..=u16::MAX {
assert_eq!(U14::try_from(n), Err(Error::U14OutOfRange));
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/cc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,11 @@ impl From<ControlFunction> for u8 {
mod test {
use super::*;
use crate::U7;
use std::convert::TryFrom;

#[test]
fn from_u7() {
for value in 0..128 {
let data = U7::try_from(value).unwrap();
let data = U7::new(value).unwrap();
let cc = ControlFunction::from(data);
assert_eq!(value, cc.into());
}
Expand Down
16 changes: 15 additions & 1 deletion src/midi_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,9 @@ mod test {
assert_eq!(b, [0xF0, 10, 20, 30, 40, 50, 0xF7, 0]);
}

#[cfg(feature = "std")]
#[test]
fn drop_unowned_sysex() {
fn drop_unowned_sysex_with_std() {
assert_eq!(
MidiMessage::SysEx(U7::try_from_bytes(&[1, 2, 3]).unwrap()).drop_unowned_sysex(),
None
Expand All @@ -655,6 +656,19 @@ mod test {
);
}

#[test]
fn drop_unowned_sysex_with_nostd() {
assert_eq!(
MidiMessage::SysEx(U7::try_from_bytes(&[1, 2, 3]).unwrap()).drop_unowned_sysex(),
None
);
assert_eq!(
MidiMessage::TuneRequest.drop_unowned_sysex(),
Some(MidiMessage::TuneRequest)
);
}

#[cfg(feature = "std")]
#[test]
fn to_owned() {
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ impl fmt::Display for Note {
mod test {
use super::*;

#[cfg(feature = "std")]
#[test]
fn note_to_frequency() {
let a440_f64 = Note::A4.to_freq_f64();
Expand Down

0 comments on commit d790400

Please sign in to comment.