Skip to content

Commit

Permalink
Support nostd for tests. (#17)
Browse files Browse the repository at this point in the history
* Support nostd for tests.

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 authored Dec 18, 2023
1 parent aa50543 commit eaa5f4c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 23 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/benchmark_main.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
name: Benchmark Main

# Benchmark HEAD in the main branch against the previous commit.
name: benchmark_main
on:
push:
branches: [main]

env:
CARGO_TERM_COLOR: always

jobs:
build:
benchmark:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/benchmark_pr.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
name: Benchmark PR

# Benchmark the PR against the main branch.
name: benchmark_pr
on:
pull_request:
branches: [main]

env:
CARGO_TERM_COLOR: always

jobs:
benchmark:
runs-on: ubuntu-latest
Expand Down
19 changes: 14 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Build/Test/Release

# Runs general build and test logic.
name: test
on:
push:
branches: [main]
Expand All @@ -8,9 +8,16 @@ on:

env:
CARGO_TERM_COLOR: always

jobs:
test:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Lint
run: cargo clippy --all-targets --all-features -- -D clippy::all
continue-on-error: true
unit_tests:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
Expand All @@ -23,9 +30,11 @@ 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]
needs: [unit_tests]
if: contains('refs/heads/main', github.ref)
steps:
- name: Checkout Repository
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 eaa5f4c

Please sign in to comment.