Sharing of my labs carried out during the TDDE18 course at Linköping University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_main.cc 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #define CATCH_CONFIG_MAIN
  2. #include "catch.hpp"
  3. #include <iostream>
  4. #include <string>
  5. #include "time.h"
  6. using namespace std;
  7. TEST_CASE("Testing declaration and basic functions"){
  8. Time t{};
  9. Time t1{15, 45, 56};
  10. REQUIRE(to_string(t, false) == "00:00:00");
  11. REQUIRE(to_string(t1, true) == "03:45:56 pm");
  12. }
  13. TEST_CASE("Testing is_am()"){
  14. Time t1{5, 45, 56};
  15. Time t2{18, 34, 51};
  16. REQUIRE(is_am(t1));
  17. REQUIRE_FALSE(is_am(t2));
  18. }
  19. TEST_CASE("Testing is_valid()"){
  20. Time t1{5, 45, 56};
  21. Time t2{65, 34, 51};
  22. Time t3{3, -34, 51};
  23. REQUIRE(is_valid(t1));
  24. REQUIRE_FALSE(is_valid(t2));
  25. REQUIRE_FALSE(is_valid(t3));
  26. }
  27. TEST_CASE("Testing equality operator == and != "){
  28. Time t1{04, 42, 06};
  29. Time t2{18, 34, 01};
  30. Time t3{18, 34, 1};
  31. REQUIRE(t1 == t1);
  32. REQUIRE(t2 == t3);
  33. REQUIRE_FALSE(t1 == t2);
  34. REQUIRE(t1 != t2);
  35. REQUIRE_FALSE(t2 != t3);
  36. }
  37. /*************** Complementary work ***************/
  38. /* TODO: You also need to test if the objects passed to operator+ has
  39. * been modified or not*/
  40. // --> done
  41. /**************************************************/
  42. // Not required for this lab
  43. TEST_CASE("Testing + and - operators (between Time)"){
  44. Time t1{5, 45, 56};
  45. Time t1_copy{5, 45, 56};
  46. Time t3{11, 31, 52};
  47. Time t4{00, 01, 01};
  48. Time t5{11, 32, 53};
  49. REQUIRE(t1 + t1 == t3);
  50. REQUIRE(t1 == t1_copy);
  51. REQUIRE(t3 + t4 == t5);
  52. }
  53. TEST_CASE("Testing + and - operators (between Time and int)"){
  54. Time t1{5, 45, 56};
  55. Time t1_copy{5, 45, 56};
  56. Time t3{6, 45, 57};
  57. REQUIRE(t1 + 0 == t1);
  58. REQUIRE(t1 + 3601 == t3);
  59. REQUIRE(t1 == t1_copy);
  60. REQUIRE(t1 + 86400 == t1);
  61. }
  62. TEST_CASE("Testing incrementation, ++ and -- operators"){
  63. Time t1{5, 45, 56};
  64. Time t2{5, 45, 57};
  65. Time t1_copy{5, 45, 56};
  66. CHECK(t1++ == t1_copy);
  67. CHECK(t1 == t2);
  68. t2++;
  69. CHECK(++t1 == t2);
  70. CHECK(t1 == t2);
  71. CHECK(t2-- == t1);
  72. CHECK(t2 == --t1);
  73. }
  74. TEST_CASE("Testing comparison operators: >, <, >=, <="){
  75. Time t1{11, 31, 52};
  76. Time t2{11, 31, 52};
  77. Time t3{6, 45, 57};
  78. REQUIRE(t3 < t2);
  79. REQUIRE_FALSE(t1 < t2);
  80. REQUIRE(t1 <= t1);
  81. REQUIRE(t1 >= t1);
  82. REQUIRE(t3 <= t1);
  83. REQUIRE(t1 >= t3);
  84. REQUIRE_FALSE(t1 <= t3);
  85. }
  86. TEST_CASE("Testing stream operators << and >>"){
  87. Time t{6, 45, 57};
  88. Time t_check{12, 34, 23};
  89. ostringstream oss{};
  90. oss << t;
  91. REQUIRE(oss.str() == to_string(t));
  92. istringstream iss{to_string(t_check)};
  93. iss >> t;
  94. REQUIRE(t == t_check);
  95. REQUIRE(iss.eof());
  96. /*************** Complementary work ***************/
  97. /* TODO: You also need to check if the time object has been modified
  98. * or not, add this after fixing the issue in time.cc*/
  99. // --> done
  100. /**************************************************/
  101. t_check = t;
  102. istringstream iss_fail{"16:-3:18"};
  103. iss_fail >> t;
  104. REQUIRE(iss_fail.fail());
  105. REQUIRE(t == t_check);
  106. iss_fail.clear();
  107. }