Skip to content

Commit

Permalink
Added unit tests for rkyv/rkyv-validation:
Browse files Browse the repository at this point in the history
  chrono month::tests::test_rkyv_validation
  chrono naive::date::tests::test_rkyv_validation
  chrono duration::tests::test_rkyv_validation
  chrono naive::datetime::tests::test_rkyv_validation
  chrono naive::isoweek::tests::test_rkyv_validation
  chrono naive::time::tests::test_rkyv_validation
  chrono offset::fixed::tests::test_rkyv_validation
  chrono offset::local::tests::test_rkyv_validation
  chrono weekday::tests::test_rkyv_validation
  • Loading branch information
mkatychev committed Oct 9, 2023
1 parent 57c2fbd commit f6f2edd
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,4 +797,12 @@ mod tests {
Err(OutOfRangeError(()))
);
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
let duration = Duration::seconds(1);
let bytes = rkyv::to_bytes::<_, 16>(&duration).unwrap();
assert_eq!(rkyv::from_bytes::<Duration>(&bytes).unwrap(), duration);
}
}
8 changes: 8 additions & 0 deletions src/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,12 @@ mod tests {
from_str::<Month>(string).unwrap_err();
}
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
let month = Month::January;
let bytes = rkyv::to_bytes::<_, 1>(&month).unwrap();
assert_eq!(rkyv::from_bytes::<Month>(&bytes).unwrap(), month);
}
}
12 changes: 12 additions & 0 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3326,6 +3326,18 @@ mod tests {
}
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
let date_min = NaiveDate::MIN;
let bytes = rkyv::to_bytes::<_, 4>(&date_min).unwrap();
assert_eq!(rkyv::from_bytes::<NaiveDate>(&bytes).unwrap(), date_min);

let date_max = NaiveDate::MAX;
let bytes = rkyv::to_bytes::<_, 4>(&date_max).unwrap();
assert_eq!(rkyv::from_bytes::<NaiveDate>(&bytes).unwrap(), date_max);
}

// MAX_YEAR-12-31 minus 0000-01-01
// = (MAX_YEAR-12-31 minus 0000-12-31) + (0000-12-31 - 0000-01-01)
// = MAX_YEAR * 365 + (# of leap years from 0001 to MAX_YEAR) + 365
Expand Down
12 changes: 12 additions & 0 deletions src/naive/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,15 @@ fn test_and_timezone_min_max_dates() {
}
}
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
let dt_min = NaiveDateTime::MIN;
let bytes = rkyv::to_bytes::<_, 12>(&dt_min).unwrap();
assert_eq!(rkyv::from_bytes::<NaiveDateTime>(&bytes).unwrap(), dt_min);

let dt_max = NaiveDateTime::MAX;
let bytes = rkyv::to_bytes::<_, 12>(&dt_max).unwrap();
assert_eq!(rkyv::from_bytes::<NaiveDateTime>(&bytes).unwrap(), dt_max);
}
14 changes: 14 additions & 0 deletions src/naive/isoweek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ impl fmt::Debug for IsoWeek {

#[cfg(test)]
mod tests {
#[cfg(feature = "rkyv-validation")]
use super::IsoWeek;
use crate::naive::{internals, NaiveDate};
use crate::Datelike;

Expand Down Expand Up @@ -207,4 +209,16 @@ mod tests {
assert!(monday.iso_week() >= friday.iso_week());
assert!(monday.iso_week() <= friday.iso_week());
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
let minweek = NaiveDate::MIN.iso_week();
let bytes = rkyv::to_bytes::<_, 4>(&minweek).unwrap();
assert_eq!(rkyv::from_bytes::<IsoWeek>(&bytes).unwrap(), minweek);

let maxweek = NaiveDate::MAX.iso_week();
let bytes = rkyv::to_bytes::<_, 4>(&maxweek).unwrap();
assert_eq!(rkyv::from_bytes::<IsoWeek>(&bytes).unwrap(), maxweek);
}
}
12 changes: 12 additions & 0 deletions src/naive/time/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,15 @@ fn test_overflowing_offset() {
assert_eq!(t.overflowing_add_offset(positive_offset).0, t + positive_offset);
assert_eq!(t.overflowing_sub_offset(positive_offset).0, t - positive_offset);
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
let t_min = NaiveTime::MIN;
let bytes = rkyv::to_bytes::<_, 8>(&t_min).unwrap();
assert_eq!(rkyv::from_bytes::<NaiveTime>(&bytes).unwrap(), t_min);

let t_max = NaiveTime::MAX;
let bytes = rkyv::to_bytes::<_, 8>(&t_max).unwrap();
assert_eq!(rkyv::from_bytes::<NaiveTime>(&bytes).unwrap(), t_max);
}
8 changes: 8 additions & 0 deletions src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,12 @@ mod tests {
let offset = FixedOffset::from_str("+06:30").unwrap();
assert_eq!(offset.local_minus_utc, (6 * 3600) + 1800);
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
let offset = FixedOffset::from_str("-0500").unwrap();
let bytes = rkyv::to_bytes::<_, 4>(&offset).unwrap();
assert_eq!(rkyv::from_bytes::<FixedOffset>(&bytes).unwrap(), offset);
}
}
13 changes: 13 additions & 0 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,17 @@ mod tests {
);
}
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
let local = Local;
// Local is a ZST and serializes to 0 bytes
let bytes = rkyv::to_bytes::<_, 0>(&local).unwrap();
assert_eq!(bytes.len(), 0);

// but is deserialized to an archived variant without a
// wrapping object
assert_eq!(rkyv::from_bytes::<Local>(&bytes).unwrap(), super::ArchivedLocal);
}
}
9 changes: 9 additions & 0 deletions src/weekday.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,13 @@ mod tests {
from_str::<Weekday>(str).unwrap_err();
}
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
let mon = Weekday::Mon;
let bytes = rkyv::to_bytes::<_, 1>(&mon).unwrap();

assert_eq!(rkyv::from_bytes::<Weekday>(&bytes).unwrap(), mon);
}
}

0 comments on commit f6f2edd

Please sign in to comment.