123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #define CATCH_CONFIG_MAIN
- #include "catch.hpp"
-
- #include <iostream>
- #include <string>
-
- #include "time.h"
-
- using namespace std;
-
- TEST_CASE("Testing declaration and basic functions"){
- Time t{};
- Time t1{15, 45, 56};
-
- REQUIRE(to_string(t, false) == "00:00:00");
- REQUIRE(to_string(t1, true) == "03:45:56 pm");
- }
-
- TEST_CASE("Testing is_am()"){
- Time t1{5, 45, 56};
- Time t2{18, 34, 51};
-
- REQUIRE(is_am(t1));
- REQUIRE_FALSE(is_am(t2));
- }
-
- TEST_CASE("Testing is_valid()"){
- Time t1{5, 45, 56};
- Time t2{65, 34, 51};
- Time t3{3, -34, 51};
-
- REQUIRE(is_valid(t1));
- REQUIRE_FALSE(is_valid(t2));
- REQUIRE_FALSE(is_valid(t3));
- }
-
- TEST_CASE("Testing equality operator == and != "){
- Time t1{04, 42, 06};
- Time t2{18, 34, 01};
- Time t3{18, 34, 1};
-
- REQUIRE(t1 == t1);
- REQUIRE(t2 == t3);
- REQUIRE_FALSE(t1 == t2);
-
- REQUIRE(t1 != t2);
- REQUIRE_FALSE(t2 != t3);
- }
-
- /*************** Complementary work ***************/
- /* TODO: You also need to test if the objects passed to operator+ has
- * been modified or not*/
- // --> done
- /**************************************************/
-
- // Not required for this lab
- TEST_CASE("Testing + and - operators (between Time)"){
- Time t1{5, 45, 56};
- Time t1_copy{5, 45, 56};
- Time t3{11, 31, 52};
- Time t4{00, 01, 01};
- Time t5{11, 32, 53};
-
- REQUIRE(t1 + t1 == t3);
- REQUIRE(t1 == t1_copy);
- REQUIRE(t3 + t4 == t5);
- }
-
- TEST_CASE("Testing + and - operators (between Time and int)"){
- Time t1{5, 45, 56};
- Time t1_copy{5, 45, 56};
- Time t3{6, 45, 57};
-
- REQUIRE(t1 + 0 == t1);
- REQUIRE(t1 + 3601 == t3);
- REQUIRE(t1 == t1_copy);
- REQUIRE(t1 + 86400 == t1);
- }
-
- TEST_CASE("Testing incrementation, ++ and -- operators"){
- Time t1{5, 45, 56};
- Time t2{5, 45, 57};
- Time t1_copy{5, 45, 56};
-
- CHECK(t1++ == t1_copy);
- CHECK(t1 == t2);
- t2++;
- CHECK(++t1 == t2);
- CHECK(t1 == t2);
-
- CHECK(t2-- == t1);
- CHECK(t2 == --t1);
- }
-
- TEST_CASE("Testing comparison operators: >, <, >=, <="){
- Time t1{11, 31, 52};
- Time t2{11, 31, 52};
- Time t3{6, 45, 57};
-
- REQUIRE(t3 < t2);
- REQUIRE_FALSE(t1 < t2);
-
- REQUIRE(t1 <= t1);
- REQUIRE(t1 >= t1);
- REQUIRE(t3 <= t1);
- REQUIRE(t1 >= t3);
- REQUIRE_FALSE(t1 <= t3);
- }
-
- TEST_CASE("Testing stream operators << and >>"){
- Time t{6, 45, 57};
- Time t_check{12, 34, 23};
-
- ostringstream oss{};
- oss << t;
- REQUIRE(oss.str() == to_string(t));
-
- istringstream iss{to_string(t_check)};
- iss >> t;
- REQUIRE(t == t_check);
- REQUIRE(iss.eof());
-
- /*************** Complementary work ***************/
- /* TODO: You also need to check if the time object has been modified
- * or not, add this after fixing the issue in time.cc*/
- // --> done
- /**************************************************/
-
- t_check = t;
- istringstream iss_fail{"16:-3:18"};
- iss_fail >> t;
-
- REQUIRE(iss_fail.fail());
- REQUIRE(t == t_check);
- iss_fail.clear();
- }
|