From 5a4aefb072b5e0de213a48522010150d4067490b Mon Sep 17 00:00:00 2001 From: alrap417 <alrap417@tlvm-4-1-4.ad.liu.se> Date: Thu, 9 Mar 2023 20:41:11 +0100 Subject: [PATCH] messasasgvf --- .../tdiu20-given-files-master/Time.cc | 139 ++++++++++----- .../tdiu20-given-files-master/Time.h | 29 ++-- .../tdiu20-given-files-master/time_test.cc | 161 +++++++++++++++++- 3 files changed, 261 insertions(+), 68 deletions(-) diff --git a/Uppgift-1-Klockslag/tdiu20-given-files-master/Time.cc b/Uppgift-1-Klockslag/tdiu20-given-files-master/Time.cc index e49a2b5..dd22579 100644 --- a/Uppgift-1-Klockslag/tdiu20-given-files-master/Time.cc +++ b/Uppgift-1-Klockslag/tdiu20-given-files-master/Time.cc @@ -2,15 +2,15 @@ // som deklarerats i Time.h #include "Time.h" - +#include "Time.h" void Time::check_invalid_time(/*int const h, int const m, int const s*/){ if(hour > 23 || hour < 00 || minute > 59 || minute < 00 || second > 59 || second < 00) { - throw invalid_argument{"ogiltigt_varde"}; + throw std::invalid_argument{"ogiltigt_varde"}; } } -Time::Time(string text) : hour{0}, minute{0}, second{0} { - stringstream stream{text}; +Time::Time(std::string text) : hour{0}, minute{0}, second{0} { + std::stringstream stream{text}; char delimiter; // delimiter står för det extra talet man får när klockan räknar stream >> hour >> delimiter >> minute >> delimiter >> second; check_invalid_time(); @@ -33,11 +33,10 @@ int Time::get_hour() { //Gets current hour. return hour; } -/* -bool Time::operator==(Time const &t) const +bool Time::operator==(Time const &rhs) const { - if(hour == t.hour && minute == t.minute && second == t.second) - + if(hour == rhs.hour && minute == rhs.minute && second == rhs.second) + { return true; } else @@ -47,13 +46,13 @@ bool Time::operator==(Time const &t) const } -bool Time::operator!=(Time const &t) const +bool Time::operator!=(Time const &rhs) const { - return !(*this == t); + return !(*this == rhs); } -*/ + bool Time::operator<(Time const &rhs) const // 5 < 10 { if(hour < rhs.hour) @@ -86,10 +85,10 @@ bool Time::operator>(Time const &rhs) const return (rhs < *this); } -/* -bool Time::operator<=(Time const &t) const + +bool Time::operator<=(Time const &rhs) const { - if(*this < t || *this == t) + if(*this < rhs || *this == rhs) { return true; } @@ -100,9 +99,9 @@ bool Time::operator<=(Time const &t) const } -bool Time::operator>=(Time const &t) const +bool Time::operator>=(Time const &rhs) const { - if(*this > t || *this == t) + if(*this > rhs || *this == rhs) { return true; } @@ -113,61 +112,112 @@ bool Time::operator>=(Time const &t) const } +Time Time::operator+(int const& rhs) const +{ + Time time{*this}; + + for (int i{}; i < rhs; i++) { + time.second++; + if(time.second > 59) { + time.second = 0; + time.minute++; + if(time.minute > 59) { + time.minute = 0; + time.hour++; + if(time.hour > 23) { + time.hour = 0; + } + } + } + } + return time; +} -/*10:22:15 -Time& Time::operator++() { - second++; +Time operator+(int const& lhs, Time const& rhs) { + return rhs + lhs; +} - if (second >= 60) { +Time Time::operator++(int) { + + Time tmp {*this}; + second++; + + if (second >= 59) { minute++; second = 0; } - if (minute >= 60) { + if (minute >= 59) { hour++; minute = 0; } - if (hour >= 24) { + if (hour >= 23) { hour = 0; } + return tmp; + } -Time Time::operator++(int) { - second++; - if (second >= 60) { +Time& Time::operator++() { + ++second; + + if (second > 59) { minute++; second = 0; } - if (minute >= 60) { + if (minute > 59) { hour++; minute = 0; } - if (hour >= 24) { + if (hour > 23) { hour = 0; } + return *this; } + +Time Time::operator-(int const n) const +{ + Time temp{hour, minute, second}; + + if(n < 0) + { + temp = temp + (-n); + } + else + { + for(int i{}; i < n; i++) + { + temp--; + } + } + return temp; +} + + Time Time::operator--(int) { + Time tmp {*this}; second--; - if (second >= 60) { - minute++; - second = 0; + if (second <= 0) { + minute--; + second = 59; } - if (minute >= 60) { - hour++; - minute = 0; + if (minute <= 0) { + hour--; + minute = 59; } - if (hour >= 24) { - hour = 0; + if (hour <= 0) { + hour = 23; } + return tmp; } Time& Time::operator--() { //fixa return this? överallt @@ -186,10 +236,11 @@ Time& Time::operator--() { //fixa return this? if (hour < 0) { hour = 23; } + return *this; } -*/ + bool Time::is_am() const { int temphour{hour}; @@ -201,21 +252,21 @@ bool Time::is_am() const { } else { - throw invalid_argument{"ogiltigt_varde"}; + throw std::invalid_argument{"ogiltigt_varde"}; } } -string Time::to_string(bool const Twelve_H) const +std::string Time::to_string(bool const Twelve_H) const { - stringstream stream; - string result; + std::stringstream stream; + std::string result; int tmp{hour}; if (Twelve_H == true && is_am() == false) { tmp = tmp - 12; if (tmp < 10) { - stream << setfill('0') << setw(2) << fixed << tmp << ':'; // Setw 2 nollor och fix för att avrunda + stream << std::setfill('0') << std::setw(2) << std::fixed << tmp << ':'; // std::setw 2 nollor och fix för att avrunda } } else @@ -228,11 +279,11 @@ string Time::to_string(bool const Twelve_H) const { if (is_am() == true) { - stream << setfill(' ') << setw(3) << right << fixed << "am"; + stream << std::setfill(' ') << std::setw(3) << std::right << std::fixed << "am"; } else { - stream << setfill(' ') << setw(3) << right << fixed << "pm"; + stream << std::setfill(' ') << std::setw(3) << std::right << std::fixed << "pm"; } } return stream.str(); @@ -252,7 +303,7 @@ std::istream &operator>>(std::istream &lhs, Time &rhs) { rhs = Time{tmp_h, tmp_m, tmp_s}; } - catch (exception &e) + catch (std::exception &e) { lhs.setstate(std::ios_base::failbit); } diff --git a/Uppgift-1-Klockslag/tdiu20-given-files-master/Time.h b/Uppgift-1-Klockslag/tdiu20-given-files-master/Time.h index fc3a97f..85498e5 100644 --- a/Uppgift-1-Klockslag/tdiu20-given-files-master/Time.h +++ b/Uppgift-1-Klockslag/tdiu20-given-files-master/Time.h @@ -12,35 +12,33 @@ #include <stdexcept> #include <ostream> -using namespace std; - class Time { public: + Time(int h , int m, int s); Time() : hour{0}, minute{0}, second{0} {}; - Time(string text); - Time(string h, string m, string s); - //Time(std::string const &t); + Time(std::string text); + Time(std::string h, std::string m, std::string s); int get_hour(); int get_minute(); int get_second(); bool is_am() const; - string to_string(bool const Twelve_H = false) const; + std::string to_string(bool const Twelve_H = false) const; - Time operator+(int const n) const; + Time operator+(int const& rhs) const; Time operator++(int); Time& operator++(); Time operator-(int const n) const; Time operator--(int); Time& operator--(); - bool operator!=(Time const &t) const; - bool operator==(Time const &t) const; - bool operator<(Time const &t) const; - bool operator>(Time const &t) const; - bool operator<=(Time const &t) const; - bool operator>=(Time const &t) const; + bool operator!=(Time const &rhs) const; + bool operator==(Time const &rhs) const; + bool operator<(Time const &rhs) const; + bool operator>(Time const &rhs) const; + bool operator<=(Time const &rhs) const; + bool operator>=(Time const &rhs) const; @@ -49,7 +47,8 @@ private: void check_invalid_time(/*int const h, int const m, int const s*/); }; -std::ostream& operator <<(std::ostream & lhs, Time const& rhs); -std::istream& operator >>(std::istream & lhs, Time rhs); +Time operator+(int const& lhs, Time const& rhs); +std::ostream& operator<<(std::ostream & lhs, Time const& rhs); +std::istream& operator>>(std::istream & lhs, Time rhs); #endif diff --git a/Uppgift-1-Klockslag/tdiu20-given-files-master/time_test.cc b/Uppgift-1-Klockslag/tdiu20-given-files-master/time_test.cc index 5326d12..d85c569 100644 --- a/Uppgift-1-Klockslag/tdiu20-given-files-master/time_test.cc +++ b/Uppgift-1-Klockslag/tdiu20-given-files-master/time_test.cc @@ -86,13 +86,17 @@ TEST_CASE ("Constructors and getters") TEST_CASE ("is_am") { Time t0{"05:00:00"}; - Time t1{"14:00:00"}; + Time t1{"04:00:00"}; + Time t2{"14:00:00"}; + Time t3{"16:00:00"}; CHECK ( t0.is_am() ); - CHECK_FALSE ( t1.is_am() ); - // Fill with extra corner cases! + CHECK ( t1.is_am() ); + CHECK_FALSE ( t2.is_am() ); + CHECK_FALSE ( t3.is_am() ); } + TEST_CASE ("to_string") { @@ -101,24 +105,67 @@ TEST_CASE ("to_string") Time t2{12, 0, 0}; Time t3{13, 0, 0}; Time t4{23, 59, 59}; + + SECTION("24 hour format no argument") { - CHECK( t0.to_string() == "00:00:00" ); - // Fill with more tests! + CHECK( t0.to_string() == "00:00:00"); + CHECK(t1.to_string() == "11:59:59"); + CHECK(t2.to_string() == "12:00:00"); + CHECK(t3.to_string() == "13:00:00"); + CHECK(t4.to_string() == "23:59:59"); + } + SECTION("24 hour format with argument") { - // Fill with more tests! - } + std::string to_string(); // vår default värde + + CHECK(t0.to_string(true) == "00:00:00 am"); + CHECK(t0.to_string(false) == "00:00:00"); + } + + SECTION("12 hour format") { // Fill with more tests! + std::string to_stirng(); + CHECK(t0.to_string() == "00:00:00"); + CHECK(t1.to_string() == "11:59:59"); + CHECK(t2.to_string() == "12:00:00"); + CHECK(t3.to_string() == "13:00:00"); + CHECK(t4.to_string() == "23:59:59"); } } -// Fill with more tests of other functions and operators! + + +TEST_CASE(">=") + { + Time t0{00, 00, 00}; + Time t1{00, 59, 00}; + Time t2{23, 59, 00}; + Time t3{23, 59, 59}; + + CHECK(t1 >= t0); + CHECK(t2 >= t1); + CHECK(t3 >= t2); + } + +TEST_CASE("<=") + { + Time t0{00, 00, 00}; + Time t1{00, 59, 00}; + Time t2{23, 59, 00}; + Time t3{23, 59, 59}; + + CHECK(t0 <= t1); + CHECK(t1 <= t2); + CHECK(t2 <= t3); + } + TEST_CASE("<") { Time t0{00, 00, 00}; @@ -130,8 +177,104 @@ TEST_CASE("<") CHECK( t1 < t3); CHECK( t0 < t2); + } - +TEST_CASE(">") + { + Time t0{00, 00, 00}; + Time t1{00, 59, 00}; + Time t2{23, 59, 00}; + Time t3{23, 59, 59}; + + CHECK(t1 > t0); + CHECK(t2 > t1); + CHECK(t3 > t2); } + + + TEST_CASE("=="){ + Time t0 {00, 00, 00}; + Time t1 {00, 00, 00}; + Time t2 {23, 59, 59}; + CHECK(t0 == t1); + CHECK_FALSE(t2 == t0); +} + +TEST_CASE("!="){ + + Time t0 {01, 01, 01}; + Time t1 {00, 00, 00}; + Time t2 {00, 00, 00}; + CHECK(t0 != t1); + CHECK_FALSE (t2 != t1); + +} + +TEST_CASE("Addera Med n") + +{ + Time t0 {00, 00, 00}; + Time t1 {11, 59, 59}; + Time t2 {12, 59, 59}; + Time t3 {23, 59, 59}; + CHECK((t0 + 30).get_second()==30); + CHECK((t1 + 10).to_string() == "12:00:09"); + CHECK((t2 + 5).to_string() == "13:00:04"); + CHECK((t3 + 59).to_string() == "00:00:58"); +} + + +TEST_CASE("Kommutativ Addition") +{ + Time t3 {23, 59, 59}; + CHECK((59 + t3).to_string() == "00:00:58"); +} + + +TEST_CASE("Subtrahera Med n") +{ + Time t0 {0, 0, 40}; + CHECK((t0 - 30).get_second()==10); + CHECK(t0.get_second()==40); + + Time t1 {21,00,00}; + CHECK ((t1 - 1).to_string() == "20:59:59"); + CHECK (t1.to_string() == "21:00:00"); +} + + +TEST_CASE("Post Increment") +{ + Time t0 {21,59,59}; + CHECK ((t0++).to_string() == "21:59:59"); + CHECK (t0.to_string() == "22:00:00"); + +} + + +TEST_CASE("Pre Increment") +{ + Time t0 {21,59,59}; + CHECK ((++t0).to_string() == "22:00:00"); + +} + + + +TEST_CASE("Post Decrement") +{ + Time t0 {22,00,00}; + CHECK (t0--.to_string() == "22:00:00"); + CHECK (t0.to_string() == "21:59:59"); +} + + +TEST_CASE("Pre Decrement") +{ + Time t0 {22,00,00}; + CHECK ((--t0).to_string() == "21:59:59"); + + +} -- GitLab