From 59a1cb14faad50d1737e8151655afb5fbafd6239 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Mon, 28 Oct 2024 22:18:30 -0400 Subject: [PATCH] Rust: completed Gigasecond again --- rust/gigasecond/.exercism/metadata.json | 2 +- rust/gigasecond/src/lib.rs | 4 +- rust/gigasecond/tests/gigasecond.rs | 95 +++++++++++++----------- rust/hello-world/.exercism/metadata.json | 2 +- 4 files changed, 57 insertions(+), 46 deletions(-) diff --git a/rust/gigasecond/.exercism/metadata.json b/rust/gigasecond/.exercism/metadata.json index 6b73bc2..8dd5c22 100644 --- a/rust/gigasecond/.exercism/metadata.json +++ b/rust/gigasecond/.exercism/metadata.json @@ -1 +1 @@ -{"track":"rust","exercise":"gigasecond","id":"46cf4dcc2c1d42f48213bc4054d72a04","url":"https://exercism.org/tracks/rust/exercises/gigasecond","handle":"Chomp1295","is_requester":true,"auto_approve":false} \ No newline at end of file +{"track":"rust","exercise":"gigasecond","id":"20e9d27ceb734a788023c5f555e0d88e","url":"https://exercism.org/tracks/rust/exercises/gigasecond","handle":"Chomp1295","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/rust/gigasecond/src/lib.rs b/rust/gigasecond/src/lib.rs index 4235212..1380e2f 100644 --- a/rust/gigasecond/src/lib.rs +++ b/rust/gigasecond/src/lib.rs @@ -1,6 +1,6 @@ -use time::{Duration, PrimitiveDateTime as DateTime}; +use time::PrimitiveDateTime as DateTime; // Returns a DateTime one billion seconds after start. pub fn after(start: DateTime) -> DateTime { - start + Duration::seconds(1_000_000_000) + start + time::Duration::seconds(1_000_000_000) } diff --git a/rust/gigasecond/tests/gigasecond.rs b/rust/gigasecond/tests/gigasecond.rs index a2b66c0..e29f5c1 100644 --- a/rust/gigasecond/tests/gigasecond.rs +++ b/rust/gigasecond/tests/gigasecond.rs @@ -1,48 +1,59 @@ -use time::PrimitiveDateTime as DateTime; +#[test] +fn date_only_specification_of_time() { + let start = datetime(2011, 4, 25, 0, 0, 0); + let actual = gigasecond::after(start); + let expected = datetime(2043, 1, 1, 1, 46, 40); + assert_eq!(actual, expected); +} -/// Create a datetime from the given numeric point in time. -/// -/// Panics if any field is invalid. -fn dt(year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> DateTime { - use time::{Date, Time}; +#[test] +#[ignore] +fn second_test_for_date_only_specification_of_time() { + let start = datetime(1977, 6, 13, 0, 0, 0); + let actual = gigasecond::after(start); + let expected = datetime(2009, 2, 19, 1, 46, 40); + assert_eq!(actual, expected); +} - DateTime::new( +#[test] +#[ignore] +fn third_test_for_date_only_specification_of_time() { + let start = datetime(1959, 7, 19, 0, 0, 0); + let actual = gigasecond::after(start); + let expected = datetime(1991, 3, 27, 1, 46, 40); + assert_eq!(actual, expected); +} + +#[test] +#[ignore] +fn full_time_specified() { + let start = datetime(2015, 1, 24, 22, 0, 0); + let actual = gigasecond::after(start); + let expected = datetime(2046, 10, 2, 23, 46, 40); + assert_eq!(actual, expected); +} + +#[test] +#[ignore] +fn full_time_with_day_roll_over() { + let start = datetime(2015, 1, 24, 23, 59, 59); + let actual = gigasecond::after(start); + let expected = datetime(2046, 10, 3, 1, 46, 39); + assert_eq!(actual, expected); +} + +fn datetime( + year: i32, + month: u8, + day: u8, + hour: u8, + minute: u8, + second: u8, +) -> time::PrimitiveDateTime { + use time::{Date, PrimitiveDateTime, Time}; + + PrimitiveDateTime::new( Date::from_calendar_date(year, month.try_into().unwrap(), day).unwrap(), Time::from_hms(hour, minute, second).unwrap(), ) } - -#[test] -fn date() { - let start_date = dt(2011, 4, 25, 0, 0, 0); - - assert_eq!(gigasecond::after(start_date), dt(2043, 1, 1, 1, 46, 40)); -} - -#[test] -fn another_date() { - let start_date = dt(1977, 6, 13, 0, 0, 0); - - assert_eq!(gigasecond::after(start_date), dt(2009, 2, 19, 1, 46, 40)); -} - -#[test] -fn third_date() { - let start_date = dt(1959, 7, 19, 0, 0, 0); - - assert_eq!(gigasecond::after(start_date), dt(1991, 3, 27, 1, 46, 40)); -} - -#[test] -fn datetime() { - let start_date = dt(2015, 1, 24, 22, 0, 0); - - assert_eq!(gigasecond::after(start_date), dt(2046, 10, 2, 23, 46, 40)); -} - -#[test] -fn another_datetime() { - let start_date = dt(2015, 1, 24, 23, 59, 59); - - assert_eq!(gigasecond::after(start_date), dt(2046, 10, 3, 1, 46, 39)); -} diff --git a/rust/hello-world/.exercism/metadata.json b/rust/hello-world/.exercism/metadata.json index 1250cd2..f559eff 100644 --- a/rust/hello-world/.exercism/metadata.json +++ b/rust/hello-world/.exercism/metadata.json @@ -1 +1 @@ -{"track":"rust","exercise":"hello-world","id":"8fc7d235b9714b939df049625c414d2c","url":"https://exercism.org/tracks/rust/exercises/hello-world","handle":"Chomp1295","is_requester":true,"auto_approve":false} \ No newline at end of file +{"track":"rust","exercise":"hello-world","id":"bf8f90455cd64bda84afecac949ecd0d","url":"https://exercism.org/tracks/rust/exercises/hello-world","handle":"Chomp1295","is_requester":true,"auto_approve":false} \ No newline at end of file