#include "Time_hms.hpp" #include "Time_s.hpp" #include #include #include int main() { // Construct and display a time. Time_hms t1{ 2, 15, 30 }; Time_s t2{ 2, 15, 30 }; std::cout << "Time_hms: " << t1 << std::endl; std::cout << "Time_s: " << t2 << std::endl; assert( t1.to_string() == t2.to_string() ); // Access the components. std::cout << std::endl; std::cout << "Hours: " << t1.hours() << " " << t2.hours() << std::endl; std::cout << "Minutes: " << static_cast( t1.minutes() ) << " " << static_cast( t2.minutes() ) << std::endl; std::cout << "Seconds: " << static_cast( t1.seconds() ) << " " << static_cast( t2.seconds() ) << std::endl; std::cout << "Negative: " << std::boolalpha << t1.negative() << " " << t2.negative() << std::endl; assert( t1.hours() == t2.hours() ); assert( t1.minutes() == t2.minutes() ); assert( t1.seconds() == t2.seconds() ); assert( t1.negative() == t2.negative() ); // Demonstrate unary operators. std::cout << std::endl; std::cout << "+t: " << +t1 << " " << +t2 << std::endl; std::cout << "-t: " << -t1 << " " << -t2 << std::endl; assert( (+t1).to_string() == (+t2).to_string() ); assert( (-t1).to_string() == (-t2).to_string() ); // A negative time with zero hours. Time_hms short1{ 0, 3, 5 }; Time_s short2{ 0, 3, 5 }; std::cout << std::endl; std::cout << "Short time: " << short1 << " " << short2 << std::endl; std::cout << "Negated: " << -short1 << " " << -short2 << std::endl; assert( (-short1).to_string() == (-short2).to_string() ); // Add and subtract times, including carrying and borrowing. Time_hms u1{ 1, 50, 45 }; Time_s u2{ 1, 50, 45 }; std::cout << std::endl; std::cout << "t: " << t1 << " " << t2 << std::endl; std::cout << "u: " << u1 << " " << u2 << std::endl; std::cout << "t + u: " << t1 + u1 << " " << t2 + u2 << std::endl; std::cout << "t - u: " << t1 - u1 << " " << t2 - u2 << std::endl; std::cout << "u - t: " << u1 - t1 << " " << u2 - t2 << std::endl; assert( (t1 + u1).to_string() == (t2 + u2).to_string() ); assert( (t1 - u1).to_string() == (t2 - u2).to_string() ); assert( (u1 - t1).to_string() == (u2 - t2).to_string() ); // Add and subtract times with different signs. std::cout << std::endl; std::cout << "t + (-u): " << (t1 + (-u1)) << " " << t2 + (-u2) << std::endl; std::cout << "(-t) + u: " << ((-t1) + u1) << " " << (-t2) + u2 << std::endl; std::cout << "(-t) - u: " << ((-t1) - u1) << " " << (-t2) - u2 << std::endl; std::cout << "(-t) - (-u): " << ((-t1) - (-u1)) << " " << (-t2) - (-u2) << std::endl; assert( (t1 + (-u1)).to_string() == (t2 + (-u2)).to_string() ); assert( ((-t1) + u1).to_string() == ((-t2) + u2).to_string() ); assert( ((-t1) - u1).to_string() == ((-t2) - u2).to_string() ); assert( ((-t1) - (-u1)).to_string() == ((-t2) - (-u2)).to_string() ); // Demonstrate crossing zero. Time_hms small1{ 0, 0, 30 }; Time_s small2{ 0, 0, 30 }; std::cout << std::endl; std::cout << "Small: " << small1 << " " << small2 << std::endl; std::cout << "Small - 45s: " << small1 - Time_hms{ 0, 0, 45 } << " " << small2 - Time_s{ 0, 0, 45 } << std::endl; assert( (small1 - Time_hms{ 0, 0, 45 }).to_string() == (small2 - Time_s{ 0, 0, 45 }).to_string() ); // Demonstrate compound assignment. Time_hms w1{ 1, 59, 30 }; Time_s w2{ 1, 59, 30 }; std::cout << std::endl; std::cout << "Initial: " << w1 << " " << w2 << std::endl; w1 += Time_hms{ 0, 1, 30 }; w2 += Time_s{ 0, 1, 30 }; std::cout << "After += 1m30s: " << w1 << " " << w2 << std::endl; assert( w1.to_string() == w2.to_string() ); w1 -= Time_hms{ 0, 2, 0 }; w2 -= Time_s{ 0, 2, 0 }; std::cout << "After -= 2m: " << w1 << " " << w2 << std::endl; assert( w1.to_string() == w2.to_string() ); w1 += Time_hms{ 1, 30, 0 }; w2 += Time_s{ 1, 30, 0 }; std::cout << "After += 1h30m: " << w1 << " " << w2 << std::endl; assert( w1.to_string() == w2.to_string() ); w1 -= Time_hms{ 5, 0, 0 }; w2 -= Time_s{ 5, 0, 0 }; std::cout << "After -= 5h: " << w1 << " " << w2 << std::endl; assert( w1.to_string() == w2.to_string() ); // Demonstrate the comparison operators. std::cout << std::endl; std::cout << "Comparisons of t and u:" << std::endl; std::cout << "t == u: " << (t1 == u1) << " " << (t2 == u2) << std::endl; std::cout << "t != u: " << (t1 != u1) << " " << (t2 != u2) << std::endl; std::cout << "t < u: " << (t1 < u1) << " " << (t2 < u2) << std::endl; std::cout << "t <= u: " << (t1 <= u1) << " " << (t2 <= u2) << std::endl; std::cout << "t >= u: " << (t1 >= u1) << " " << (t2 >= u2) << std::endl; std::cout << "t > u: " << (t1 > u1) << " " << (t2 > u2) << std::endl; assert( (t1 == u1) == (t2 == u2) ); assert( (t1 != u1) == (t2 != u2) ); assert( (t1 < u1) == (t2 < u2) ); assert( (t1 <= u1) == (t2 <= u2) ); assert( (t1 >= u1) == (t2 >= u2) ); assert( (t1 > u1) == (t2 > u2) ); // Demonstrate prefix and postfix increment and decrement. Time_hms v1{ 0, 59, 59 }; Time_s v2{ 0, 59, 59 }; std::cout << std::endl; std::cout << "Initial: " << v1 << " " << v2 << std::endl; ++v1; ++v2; std::cout << "After prefix ++: " << v1 << " " << v2 << std::endl; assert( v1.to_string() == v2.to_string() ); Time_hms old1{ v1++ }; Time_s old2{ v2++ }; std::cout << "Postfix ++ returned: " << old1 << " " << old2 << std::endl; std::cout << "After postfix ++: " << v1 << " " << v2 << std::endl; assert( old1.to_string() == old2.to_string() ); assert( v1.to_string() == v2.to_string() ); --v1; --v2; std::cout << "After prefix --: " << v1 << " " << v2 << std::endl; assert( v1.to_string() == v2.to_string() ); old1 = v1--; old2 = v2--; std::cout << "Postfix -- returned: " << old1 << " " << old2 << std::endl; std::cout << "After postfix --: " << v1 << " " << v2 << std::endl; assert( old1.to_string() == old2.to_string() ); assert( v1.to_string() == v2.to_string() ); // Zero has no negative representation. Time_hms zero1{ 0, 0, 0 }; Time_s zero2{ 0, 0, 0 }; std::cout << std::endl; std::cout << "Zero: " << zero1 << " " << zero2 << std::endl; std::cout << "Negative zero:" << -zero1 << " " << -zero2 << std::endl; assert( zero1.to_string() == zero2.to_string() ); assert( (-zero1).to_string() == (-zero2).to_string() ); assert( !(-zero1).negative() ); assert( !(-zero2).negative() ); // Display the size of each object. std::cout << std::endl; std::cout << "sizeof(Time_hms) = " << sizeof( Time_hms ) << " bytes" << std::endl; std::cout << "sizeof(Time_s) = " << sizeof( Time_s ) << " bytes" << std::endl; return 0; }