Last active
February 22, 2023 13:24
-
-
Save nboutin/d8f442ba6eadadd179f436563dd2ee98 to your computer and use it in GitHub Desktop.
Revisions
-
nboutin revised this gist
Feb 22, 2023 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,10 @@ TEST(alarm, checkAlarmAfter10Minutes) { time_fake t; // implements the time_interface output_fake o; // implements the output interface alarm_clock alarm(t, o); auto ring_time = t.now() + 10min; clock.set_alarm(ring_time); t = t.now() + 10min; // We can increase our time EXPECT_EQ(o.value(), "Alarm\n"); // We can check the output } -
nboutin revised this gist
Feb 22, 2023 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,7 @@ #include "time_interface.h" #include "output_interface.h" class alarm_clock { public: alarm_clock(const time_interface&, output_interface&) -
nboutin created this gist
Feb 22, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ // https://www.youtube.com/watch?v=zYPb7oBU5_E #include "time_interface.h" #include "output_interface.h" class alarm_clock { public: alarm_clock(const time_interface&, output_interface&) // ... private: // ... const time_interface& time_; output_interface& output_; }; void alarm_clock::check_alarm() { if (!is_active_) return; if (now_is_approx_alarm_time()) { ring_alarm(); } } bool alarm_clock::now_is_approx_alarm_time() const { auto diff = abs(time_.now() - alarm_time_); return (diff < 1s); } void alarm_clock::ring_alarm() { output_ << "Alarm\n"; } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ int main() { time_impl t; // implements the time_interface output_console o; // implements the output_interface alarm_clock alarm(t, o); // ... return 0; } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ class output_interface { public: virtual ~output_interface() = default; virtual output_interface& operator<<(const string&) = 0; }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ TEST(alarm, checkAlarmAfter10Minutes) { time_fake t; // implements the time_interface output_fake o; // implements the output interface alarm_clock alarm(t, o); auto ring_time = t.now() + 10min; clock.set_alarm(ring_time); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ #include <chrono> class time_interface { public: using time_point = std::chrono::time_point<std::chrono::system_clock>; virtual ~time_interface() = default; virtual time_point now() const = 0; };