Untitled
unknown
plain_text
2 years ago
3.1 kB
1
Indexable
Never
#include "airline_ticket.h" #include "test_runner.h" #include <iostream> #include <map> #include <sstream> using namespace std; #define UPDATE_FIELD(ticket, field, values) \ { \ auto it = values.find(#field); \ if(it != values.end()) \ { \ istringstream is(it->second); \ is >> ticket.field; \ } \ } #define UPDATE_FIELD(ticket, field, values) \ { \ auto it = values.find(#field); \ if(it != values.end()) \ { \ ticket.field = it->second; \ } \ } istream& operator >> (istream& is, const Date& date) { is >> date.year; is.ignore(1); is >> date.month; is.ignore(1); is >> date.day; return is; } istream& operator >> (istream& is, const Time& time) { is >> time.hours; is.ignore(1); is >> time.minutes; return is; } ostream& operator << (ostream& out, const Time& time) { out << time.year << "-" << time.month << "-" << time.day; return out; } ostream& operator << (ostream& out, const Date& date) { out << date.hours << ":" << date.minutes; return out; } bool operator < (const Time& lhs, const Time& rhs) { return tie(lhs.year, lhs.month, lhs.day) < tie(rhs.year, rhs.month, rhs.day); } bool operator < (const Date& lhs, const Date& rhs) { return tie(lhs.hours, lhs.minutes) < tie(rhs.hours, rhs.minutes); } bool operator == (const Time& lhs, const Time& rhs) { return tie(lhs.year, lhs.month, lhs.day) == tie(rhs.year, rhs.month, rhs.day); } bool operator == (const Date& lhs, const Date& rhs) { return tie(lhs.hours, lhs.minutes) == tie(rhs.hours, rhs.minutes); } void TestUpdate() { AirlineTicket t; t.price = 0; const map<string, string> updates1 = { {"departure_date", "2018-2-28"}, {"departure_time", "17:40"}, }; UPDATE_FIELD(t, departure_date, updates1); UPDATE_FIELD(t, departure_time, updates1); UPDATE_FIELD(t, price, updates1); ASSERT_EQUAL(t.departure_date, (Date{ 2018, 2, 28 })); ASSERT_EQUAL(t.departure_time, (Time{ 17, 40 })); ASSERT_EQUAL(t.price, 0); const map<string, string> updates2 = { {"price", "12550"}, {"arrival_time", "20:33"}, }; UPDATE_FIELD(t, departure_date, updates2); UPDATE_FIELD(t, departure_time, updates2); UPDATE_FIELD(t, arrival_time, updates2); UPDATE_FIELD(t, price, updates2); ASSERT_EQUAL(t.departure_date, (Date{ 2018, 2, 28 })); ASSERT_EQUAL(t.departure_time, (Time{ 17, 40 })); ASSERT_EQUAL(t.price, 12550); ASSERT_EQUAL(t.arrival_time, (Time{ 20, 33 })); } void TestUpdates() { TestRunner tr; RUN_TEST(tr, TestUpdate); }