From 4b0ed457dbadccfd85bb2efcdb87d12500642803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 6 Aug 2023 16:40:37 +0200 Subject: [PATCH] tests(src): add tests for calendar tick functions --- source/src/lib.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/source/src/lib.rs b/source/src/lib.rs index 128f86e70d0b6d13b5fb63682fae7f0e0384b198..6b000a1c43fc4735e7ad5ec092ad5b529ca64c61 100644 --- a/source/src/lib.rs +++ b/source/src/lib.rs @@ -1,4 +1,4 @@ -#![no_main] +#![cfg_attr(test, no_main)] #![no_std] use defmt_rtt as _; // global logger @@ -43,7 +43,10 @@ pub fn exit() -> ! { #[cfg(test)] #[defmt_test::tests] mod unit_tests { - use crate::linear_interpolation::{LinearInterpolation, Point}; + use crate::{ + calendar::Calendar, + linear_interpolation::{LinearInterpolation, Point}, + }; use defmt::assert_eq; #[init] @@ -66,4 +69,72 @@ mod unit_tests { assert_eq!(li.interpolate(90).unwrap(), 0xFFFF - 3000 - 900); assert_eq!(li.interpolate(100).unwrap(), 0xFFFF - 3000); } + + #[test] + fn calendar_to_leap_year() { + let base = Calendar::new(0, 0, 0, 1, 1, 2023); + let end_of_leap_year = Calendar::with_base_year(2023, 0, 0, 0, 31, 12, 2024); + let end_of_leap_year_ticks = end_of_leap_year.to_ticks(); + assert_eq!(end_of_leap_year_ticks, (365 + 365) * 24 * 60 * 60); + let end_of_leap_year_from_ticks = + Calendar::from_ticks(base.clone(), end_of_leap_year_ticks); + assert_eq!(end_of_leap_year_from_ticks.day(), 31); + assert_eq!(end_of_leap_year_from_ticks.month(), 12); + assert_eq!(end_of_leap_year_from_ticks.year(), 2024); + assert_eq!(end_of_leap_year_from_ticks.hours(), 0); + assert_eq!(end_of_leap_year_from_ticks.minutes(), 0); + assert_eq!(end_of_leap_year_from_ticks.seconds(), 0); + } + + #[test] + fn calendar_past_leap_year() { + let base = Calendar::new(0, 0, 0, 1, 1, 2023); + let end_of_leap_year = Calendar::with_base_year(2023, 0, 0, 0, 31, 12, 2025); + let end_of_leap_year_ticks = end_of_leap_year.to_ticks(); + assert_eq!(end_of_leap_year_ticks, (365 + 366 + 364) * 24 * 60 * 60); + let end_of_leap_year_from_ticks = + Calendar::from_ticks(base.clone(), end_of_leap_year_ticks); + assert_eq!(end_of_leap_year_from_ticks.day(), 31); + assert_eq!(end_of_leap_year_from_ticks.month(), 12); + assert_eq!(end_of_leap_year_from_ticks.year(), 2025); + assert_eq!(end_of_leap_year_from_ticks.hours(), 0); + assert_eq!(end_of_leap_year_from_ticks.minutes(), 0); + assert_eq!(end_of_leap_year_from_ticks.seconds(), 0); + } + + #[test] + fn calendar_basic_ticks() { + let base = Calendar::new(0, 0, 0, 1, 1, 2023); + assert_eq!(base.to_ticks(), 0); + assert_eq!(Calendar::from_ticks(base.clone(), 0).to_ticks(), 0); + assert_eq!(Calendar::from_ticks(base.clone(), 120).to_ticks(), 120); + + let two_days = Calendar::from_ticks(base.clone(), 2 * 24 * 60 * 60); + assert_eq!(two_days.month(), 1); + assert_eq!(two_days.year(), 2023); + assert_eq!(two_days.day(), 3); + assert_eq!(two_days.to_ticks(), 2 * 24 * 60 * 60); + + let one_month = Calendar::from_ticks(base.clone(), 31 * 24 * 60 * 60); + assert_eq!(one_month.day(), 1); + assert_eq!(one_month.month(), 2); + assert_eq!(one_month.year(), 2023); + assert_eq!(one_month.hours(), 0); + assert_eq!(one_month.minutes(), 0); + assert_eq!(one_month.seconds(), 0); + assert_eq!(one_month.to_ticks(), 31 * 24 * 60 * 60); + + assert_eq!( + Calendar::from_ticks(base.clone(), (31 + 28) * 24 * 60 * 60).to_ticks(), + (31 + 28) * 24 * 60 * 60 + ); + assert_eq!( + Calendar::from_ticks(base.clone(), 13423544).to_ticks(), + 13423544 + ); + assert_eq!( + Calendar::from_ticks(base.clone(), 315360000).to_ticks(), + 315360000 + ); + } }