Skip to content

Commit eaa5f4c

Browse files
authored
Support nostd for tests. (#17)
* 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.
1 parent aa50543 commit eaa5f4c

File tree

8 files changed

+50
-23
lines changed

8 files changed

+50
-23
lines changed

.github/workflows/benchmark_main.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
name: Benchmark Main
2-
1+
# Benchmark HEAD in the main branch against the previous commit.
2+
name: benchmark_main
33
on:
44
push:
55
branches: [main]
66

77
env:
88
CARGO_TERM_COLOR: always
9-
109
jobs:
11-
build:
10+
benchmark:
1211
runs-on: ubuntu-latest
1312
steps:
1413
- name: Checkout repo

.github/workflows/benchmark_pr.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
name: Benchmark PR
2-
1+
# Benchmark the PR against the main branch.
2+
name: benchmark_pr
33
on:
44
pull_request:
55
branches: [main]
66

77
env:
88
CARGO_TERM_COLOR: always
9-
109
jobs:
1110
benchmark:
1211
runs-on: ubuntu-latest

.github/workflows/test.yml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name: Build/Test/Release
2-
1+
# Runs general build and test logic.
2+
name: test
33
on:
44
push:
55
branches: [main]
@@ -8,9 +8,16 @@ on:
88

99
env:
1010
CARGO_TERM_COLOR: always
11-
1211
jobs:
13-
test:
12+
lint:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout Repository
16+
uses: actions/checkout@v2
17+
- name: Lint
18+
run: cargo clippy --all-targets --all-features -- -D clippy::all
19+
continue-on-error: true
20+
unit_tests:
1421
runs-on: ubuntu-latest
1522
steps:
1623
- name: Checkout Repository
@@ -23,9 +30,11 @@ jobs:
2330
run: cargo clippy --all-targets --all-features -- -D clippy::all
2431
- name: Test
2532
run: cargo test --verbose
33+
- name: Test nostd
34+
run: cargo test --no-default-features
2635
release:
2736
runs-on: ubuntu-latest
28-
needs: [test]
37+
needs: [unit_tests]
2938
if: contains('refs/heads/main', github.ref)
3039
steps:
3140
- name: Checkout Repository

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license = "MIT"
77
name = "wmidi"
88
readme = "README.md"
99
repository = "https://github.com/RustAudio/wmidi"
10-
version = "4.0.8"
10+
version = "4.0.9"
1111

1212
[lib]
1313
# Required to pass flags to criterion benchmark.

src/byte.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ impl U7 {
1111
/// The maximum value for a u7 data byte.
1212
pub const MAX: U7 = U7(0x80 - 0x01);
1313

14+
/// Create a new `U7` or return an error if it is out of range.
15+
#[inline(always)]
16+
pub fn new(data: u8) -> Result<U7, Error> {
17+
if data > u8::from(U7::MAX) {
18+
Err(Error::DataByteOutOfRange)
19+
} else {
20+
Ok(U7(data))
21+
}
22+
}
23+
1424
/// Convert a `u8` into a `U7` without bounds checking.
1525
///
1626
/// # Safety
@@ -65,11 +75,7 @@ impl TryFrom<u8> for U7 {
6575

6676
#[inline(always)]
6777
fn try_from(data: u8) -> Result<U7, Error> {
68-
if data > u8::from(U7::MAX) {
69-
Err(Error::DataByteOutOfRange)
70-
} else {
71-
Ok(U7(data))
72-
}
78+
U7::new(data)
7379
}
7480
}
7581

@@ -158,7 +164,7 @@ mod tests {
158164

159165
#[test]
160166
fn try_from_out_of_range_fails() {
161-
for n in 0x80..=std::u8::MAX {
167+
for n in 0x80..=u8::MAX {
162168
assert_eq!(U7::try_from(n), Err(Error::DataByteOutOfRange));
163169
}
164170
}
@@ -204,7 +210,7 @@ mod tests {
204210

205211
#[test]
206212
fn try_from_out_of_range_16_fails() {
207-
for n in 0x4000..=std::u16::MAX {
213+
for n in 0x4000..=u16::MAX {
208214
assert_eq!(U14::try_from(n), Err(Error::U14OutOfRange));
209215
}
210216
}

src/cc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,11 @@ impl From<ControlFunction> for u8 {
393393
mod test {
394394
use super::*;
395395
use crate::U7;
396-
use std::convert::TryFrom;
397396

398397
#[test]
399398
fn from_u7() {
400399
for value in 0..128 {
401-
let data = U7::try_from(value).unwrap();
400+
let data = U7::new(value).unwrap();
402401
let cc = ControlFunction::from(data);
403402
assert_eq!(value, cc.into());
404403
}

src/midi_message.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,9 @@ mod test {
630630
assert_eq!(b, [0xF0, 10, 20, 30, 40, 50, 0xF7, 0]);
631631
}
632632

633+
#[cfg(feature = "std")]
633634
#[test]
634-
fn drop_unowned_sysex() {
635+
fn drop_unowned_sysex_with_std() {
635636
assert_eq!(
636637
MidiMessage::SysEx(U7::try_from_bytes(&[1, 2, 3]).unwrap()).drop_unowned_sysex(),
637638
None
@@ -655,6 +656,19 @@ mod test {
655656
);
656657
}
657658

659+
#[test]
660+
fn drop_unowned_sysex_with_nostd() {
661+
assert_eq!(
662+
MidiMessage::SysEx(U7::try_from_bytes(&[1, 2, 3]).unwrap()).drop_unowned_sysex(),
663+
None
664+
);
665+
assert_eq!(
666+
MidiMessage::TuneRequest.drop_unowned_sysex(),
667+
Some(MidiMessage::TuneRequest)
668+
);
669+
}
670+
671+
#[cfg(feature = "std")]
658672
#[test]
659673
fn to_owned() {
660674
assert_eq!(

src/note.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ impl fmt::Display for Note {
482482
mod test {
483483
use super::*;
484484

485+
#[cfg(feature = "std")]
485486
#[test]
486487
fn note_to_frequency() {
487488
let a440_f64 = Note::A4.to_freq_f64();

0 commit comments

Comments
 (0)