diff options
| author | Finomnis <Finomnis@users.noreply.github.com> | 2023-12-04 15:53:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-04 14:53:02 +0000 |
| commit | c227a71d243db6d539f3c64e3b4bb1b3ab282693 (patch) | |
| tree | 6a1175f98d6c823b3e2beb1fbb9ed9e462f46811 /rtic-time/tests | |
| parent | 3de5f793f361d97e061999ce53130faae7bb0d2d (diff) | |
Refactor race condition free timer helper (#850)
* Implement half_period_counter in rtic-time
* Rename compute_now to calculate_now, use it in stm32 and imxrt
* Add more tests
* Add some docs
* Fix clippy warning, add imxrt timer to monotonics tests
* Bump dependency version to make sure monotonics will build properly
* Add changelog to rtic-monotonics
* Add more docs
* Add more docs
* Finish documentation
* Fix typos
* Switch from atomic-polyfill to portable-atomic
* Some more doc fixes
* More doc fixes
* Minor doc fix
* Minor doc fix
* Fix Atomics not existing
* Fix example
* Minor example improvement
* Revert back to atomic-polyfill
* Fix cargo.toml formatting
* Remove atomic-polyfill
* Attempt to fix unused macro warning
* Remove atomics completely from half period counter
* Minor doc fix
* Doc fixes
* Doc fixes
* Remove obsolete comment
* Fix ordering in monotonic initialization sequence
Diffstat (limited to 'rtic-time/tests')
| -rw-r--r-- | rtic-time/tests/half_period_time_counter.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/rtic-time/tests/half_period_time_counter.rs b/rtic-time/tests/half_period_time_counter.rs new file mode 100644 index 0000000..ceffbea --- /dev/null +++ b/rtic-time/tests/half_period_time_counter.rs @@ -0,0 +1,95 @@ +use rtic_time::half_period_counter::calculate_now; + +macro_rules! do_test_u8 { + ($periods:literal, $counter:literal, $expected:literal) => {{ + let periods: u32 = $periods; + let counter: u8 = $counter; + let expected: u32 = $expected; + let actual: u32 = calculate_now(periods, || counter); + assert_eq!( + actual, expected, + "Expected: ({} | {}) => {}, got: {}", + periods, counter, expected, actual + ); + }}; +} + +macro_rules! do_test_u16 { + ($periods:literal, $counter:literal, $expected:literal) => {{ + let periods: u16 = $periods; + let counter: u16 = $counter; + let expected: u32 = $expected; + let actual: u32 = calculate_now(periods, || counter); + assert_eq!( + actual, expected, + "Expected: ({} | {}) => {}, got: {}", + periods, counter, expected, actual + ); + }}; +} + +#[test] +fn half_period_time_counter_u8() { + do_test_u8!(0, 0, 0); + do_test_u8!(0, 1, 1); + + do_test_u8!(0, 126, 126); + do_test_u8!(1, 126, 382); // This is why it's important to load the periods before the counter + do_test_u8!(0, 127, 127); + do_test_u8!(1, 127, 383); + do_test_u8!(0, 128, 128); + do_test_u8!(1, 128, 128); + do_test_u8!(0, 129, 129); + do_test_u8!(1, 129, 129); + + do_test_u8!(1, 254, 254); + do_test_u8!(2, 254, 510); + do_test_u8!(1, 255, 255); + do_test_u8!(2, 255, 511); + do_test_u8!(1, 0, 256); + do_test_u8!(2, 0, 256); + do_test_u8!(1, 1, 257); + do_test_u8!(2, 1, 257); + + do_test_u8!(2, 126, 382); + do_test_u8!(3, 126, 638); + do_test_u8!(2, 127, 383); + do_test_u8!(3, 127, 639); + do_test_u8!(2, 128, 384); + do_test_u8!(3, 128, 384); + do_test_u8!(2, 129, 385); + do_test_u8!(3, 129, 385); +} + +#[test] +fn half_period_time_counter_u16() { + do_test_u16!(0, 0, 0); + do_test_u16!(0, 1, 1); + + do_test_u16!(0, 32766, 32766); + do_test_u16!(1, 32766, 98302); // This is why it's important to load the periods before the counter + do_test_u16!(0, 32767, 32767); + do_test_u16!(1, 32767, 98303); + do_test_u16!(0, 32768, 32768); + do_test_u16!(1, 32768, 32768); + do_test_u16!(0, 32769, 32769); + do_test_u16!(1, 32769, 32769); + + do_test_u16!(1, 65534, 65534); + do_test_u16!(2, 65534, 131070); + do_test_u16!(1, 65535, 65535); + do_test_u16!(2, 65535, 131071); + do_test_u16!(1, 0, 65536); + do_test_u16!(2, 0, 65536); + do_test_u16!(1, 1, 65537); + do_test_u16!(2, 1, 65537); + + do_test_u16!(2, 32766, 98302); + do_test_u16!(3, 32766, 163838); + do_test_u16!(2, 32767, 98303); + do_test_u16!(3, 32767, 163839); + do_test_u16!(2, 32768, 98304); + do_test_u16!(3, 32768, 98304); + do_test_u16!(2, 32769, 98305); + do_test_u16!(3, 32769, 98305); +} |
