-
Notifications
You must be signed in to change notification settings - Fork 0
/
progtest5.cpp
484 lines (467 loc) · 22.5 KB
/
progtest5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#ifndef __PROGTEST__
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <cctype>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <set>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <memory>
#include <functional>
#include <stdexcept>
#endif /* __PROGTEST */
using namespace std;
#ifndef __PROGTEST__
class CTimeStamp
{
public:
CTimeStamp ( int year,
int month,
int day,
int hour,
int minute,
int sec );
int Compare ( const CTimeStamp & x ) const;
friend ostream & operator << ( ostream & os,
const CTimeStamp & x );
private:
int m_Year;
int m_Month;
int m_Day;
int m_Hour;
int m_Min;
int m_Sec;
};
//=================================================================================================
CTimeStamp::CTimeStamp ( int year,
int month,
int day,
int hour,
int minute,
int sec )
:m_Year ( year ),
m_Month ( month ),
m_Day ( day ),
m_Hour ( hour ),
m_Min ( minute ),
m_Sec ( sec )
{}
//-------------------------------------------------------------------------------------------------
int CTimeStamp::Compare ( const CTimeStamp & x ) const
{
if ( m_Year != x . m_Year )
return m_Year - x . m_Year;
if ( m_Month != x . m_Month )
return m_Month - x . m_Month;
if ( m_Day != x . m_Day )
return m_Day - x . m_Day;
if ( m_Hour != x . m_Hour )
return m_Hour - x . m_Hour;
if ( m_Min != x . m_Min )
return m_Min - x . m_Min;
if ( m_Sec != x . m_Sec )
return m_Sec - x . m_Sec;
return 0;
}
//-------------------------------------------------------------------------------------------------
ostream & operator << ( ostream & os,
const CTimeStamp & x )
{
//YYYY-MM-DD HH24:MI:SS
os << setfill('0') << setw(4)
<< x . m_Year << "-"
<< setw(2)
<< x . m_Month << "-" << x . m_Day << " "
<< x . m_Hour << ":" << x . m_Min << ":" << x . m_Sec;
return os;
}
//=================================================================================================
class CMailBody
{
public:
// copy cons/op=/destructor is correctly implemented in the testing environment
CMailBody ( int size,
const char * data );
CMailBody ( const CMailBody & x );
~CMailBody ( void );
CMailBody & operator = ( const CMailBody & x );
friend ostream & operator << ( ostream & os,
const CMailBody & x );
private:
int m_Size;
char * m_Data;
};
//=================================================================================================
CMailBody::CMailBody ( int size,
const char * data )
:m_Size ( size ),
m_Data ( new char[ m_Size + 1 ] )
{
memcpy ( m_Data, data, m_Size + 1 );
}
//-------------------------------------------------------------------------------------------------
CMailBody::CMailBody ( const CMailBody & x )
:m_Size ( x . m_Size ),
m_Data ( new char[ m_Size + 1 ] )
{
memcpy ( m_Data, x . m_Data, m_Size + 1 );
}
//-------------------------------------------------------------------------------------------------
CMailBody::~CMailBody ( void )
{
delete [] m_Data;
}
//-------------------------------------------------------------------------------------------------
CMailBody & CMailBody::operator = ( const CMailBody & x )
{
if ( this == &x )
return *this;
m_Size = x . m_Size ;
delete [] m_Data;
m_Data = new char[ m_Size + 1 ];
memcpy ( m_Data, x . m_Data, m_Size + 1 );
return *this;
}
//-------------------------------------------------------------------------------------------------
ostream & operator << ( ostream & os,
const CMailBody & x )
{
return os << "mail body: " << x . m_Size << " B";
}
//=================================================================================================
class CAttach
{
public:
CAttach ( int x )
: m_X ( x ),
m_RefCnt ( 1 )
{
}
void AddRef ( void ) const
{
m_RefCnt ++;
}
void Release ( void ) const
{
if ( !--m_RefCnt )
delete this;
}
private:
int m_X;
mutable int m_RefCnt;
CAttach ( const CAttach & x );
CAttach & operator = ( const CAttach & x );
~CAttach ( void ) = default;
friend ostream & operator << ( ostream & os,
const CAttach & x )
{
return os << "attachment: " << x . m_X << " B";
}
};
//=================================================================================================
#endif /* __PROGTEST__, DO NOT remove */
class CMail
{
public:
CMail ( const CTimeStamp & timeStamp,
const string & from,
const CMailBody & body,
const CAttach * attach );
~CMail ( void );
CMail ( const CMail & x );
CMail & operator = ( const CMail & x );
const string & From ( void ) const;
const CMailBody & Body ( void ) const;
const CTimeStamp & TimeStamp ( void ) const;
const CAttach * Attachment ( void ) const;
friend bool operator < ( const CMail & lho,
const CMail & rho );
friend ostream & operator << ( ostream & os,
const CMail & x );
private:
CTimeStamp m_TimeStamp;
string m_From;
CMailBody m_Body;
const CAttach * m_Attach;
};
//=================================================================================================
CMail::CMail ( const CTimeStamp & timeStamp,
const string & from,
const CMailBody & body,
const CAttach * attach )
:m_TimeStamp ( timeStamp ),
m_From ( from ),
m_Body ( body ),
m_Attach ( attach )
{
if ( m_Attach != nullptr )
m_Attach -> AddRef ();
}
//-------------------------------------------------------------------------------------------------
CMail::~CMail ( void )
{
if ( m_Attach != nullptr )
m_Attach -> Release ();
}
//-------------------------------------------------------------------------------------------------
CMail::CMail ( const CMail & x )
:m_TimeStamp ( x . m_TimeStamp ),
m_From ( x . m_From ),
m_Body ( x . m_Body ),
m_Attach ( x . m_Attach )
{
if ( m_Attach != nullptr )
m_Attach -> AddRef ();
}
//-------------------------------------------------------------------------------------------------
CMail & CMail::operator = ( const CMail & x )
{
if ( this == &x )
return *this;
m_TimeStamp = x . m_TimeStamp;
m_From = x . m_From;
m_Body = x . m_Body;
if ( m_Attach != nullptr )
m_Attach -> Release ();
m_Attach = x . m_Attach;
if ( m_Attach != nullptr )
m_Attach -> AddRef ();
return *this;
}
//-------------------------------------------------------------------------------------------------
const string & CMail::From ( void ) const
{
return m_From;
}
//-------------------------------------------------------------------------------------------------
const CMailBody & CMail::Body ( void ) const
{
return m_Body;
}
//-------------------------------------------------------------------------------------------------
const CTimeStamp & CMail::TimeStamp ( void ) const
{
return m_TimeStamp;
}
//-------------------------------------------------------------------------------------------------
const CAttach * CMail::Attachment ( void ) const
{
return m_Attach;
}
//-------------------------------------------------------------------------------------------------
bool operator < ( const CMail & lho,
const CMail & rho )
{
return lho . m_TimeStamp . Compare ( rho . m_TimeStamp ) < 0;
}
//-------------------------------------------------------------------------------------------------
ostream & operator << ( ostream & os,
const CMail & x )
{
os << x . m_TimeStamp << " "
<< x . m_From << " "
<< x . m_Body;
if ( x . m_Attach != nullptr )
os << " + " << * ( x . m_Attach ) ;
return os;
}
//=================================================================================================
class CMailBox
{
public:
CMailBox ( void );
bool Delivery ( const CMail & mail );
bool NewFolder ( const string & folderName );
bool MoveMail ( const string & fromFolder,
const string & toFolder );
list<CMail> ListMail ( const string & folderName,
const CTimeStamp & from,
const CTimeStamp & to ) const;
set<string> ListAddr ( const CTimeStamp & from,
const CTimeStamp & to ) const;
private:
map<string, set<CMail>> m_Folders;
};
//=================================================================================================
CMailBox::CMailBox ( void )
{
NewFolder ( "inbox" );
}
//-------------------------------------------------------------------------------------------------
bool CMailBox::Delivery ( const CMail & mail )
{
return m_Folders . at ("inbox") . insert ( mail ) . second;
}
//-------------------------------------------------------------------------------------------------
bool CMailBox::NewFolder ( const string & folderName )
{
if ( m_Folders . count ( folderName ) == 1 )
return false;
m_Folders[folderName];
return true;
}
//-------------------------------------------------------------------------------------------------
bool CMailBox::MoveMail ( const string & fromFolder,
const string & toFolder )
{
if ( m_Folders . count ( fromFolder ) != 1 || m_Folders . count ( toFolder ) != 1 )
return false;
//m_Folders[toFolder] . merge ( move ( m_Folders[fromFolder] ) );
m_Folders[toFolder] . merge ( m_Folders[fromFolder] );
return true;
}
//-------------------------------------------------------------------------------------------------
list<CMail> CMailBox::ListMail ( const string & folderName,
const CTimeStamp & from,
const CTimeStamp & to ) const
{
list<CMail> tmp;
if ( m_Folders . count ( folderName ) != 1 )
return tmp;
auto i = m_Folders . at (folderName) . lower_bound ( CMail (from, "", CMailBody ( 0, "" ), nullptr ) );
auto upper = m_Folders . at (folderName) . upper_bound ( CMail (to , "", CMailBody ( 0, "" ), nullptr ) );
for ( ; i != upper ; i++ )
tmp . push_back ( *i );
return tmp;
}
//-------------------------------------------------------------------------------------------------
set<string> CMailBox::ListAddr ( const CTimeStamp & from,
const CTimeStamp & to ) const
{
set<string> tmp;
for ( const auto & seto : m_Folders )
{
auto i = seto . second . lower_bound ( CMail (from, "", CMailBody ( 0, "" ), nullptr ) );
auto upper = seto . second . upper_bound ( CMail (to , "", CMailBody ( 0, "" ), nullptr ) );
for ( ; i != upper ; i++ )
tmp . insert ( i-> From () );
}
return tmp;
}
//=================================================================================================
#ifndef __PROGTEST__
#if 1
static string showMail ( const list<CMail> & l )
{
ostringstream oss;
for ( const auto & x : l )
oss << x << endl;
return oss . str ();
}
static string showUsers ( const set<string> & s )
{
ostringstream oss;
for ( const auto & x : s )
oss << x << endl;
return oss . str ();
}
#endif
int main ( void )
{
list<CMail> mailList;
set<string> users;
CAttach * att;
CMailBox m0;
assert ( m0 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 15, 24, 13 ), "user1@fit.cvut.cz", CMailBody ( 14, "mail content 1" ), nullptr ) ) );
assert ( m0 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 15, 26, 23 ), "user2@fit.cvut.cz", CMailBody ( 22, "some different content" ), nullptr ) ) );
att = new CAttach ( 200 );
assert ( m0 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 11, 23, 43 ), "boss1@fit.cvut.cz", CMailBody ( 14, "urgent message" ), att ) ) );
assert ( m0 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 18, 52, 27 ), "user1@fit.cvut.cz", CMailBody ( 14, "mail content 2" ), att ) ) );
att -> Release ();
att = new CAttach ( 97 );
assert ( m0 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 16, 12, 48 ), "boss1@fit.cvut.cz", CMailBody ( 24, "even more urgent message" ), att ) ) );
att -> Release ();
#if 1
assert ( showMail ( m0 . ListMail ( "inbox",
CTimeStamp ( 2000, 1, 1, 0, 0, 0 ),
CTimeStamp ( 2050, 12, 31, 23, 59, 59 ) ) ) == "2014-03-31 11:23:43 boss1@fit.cvut.cz mail body: 14 B + attachment: 200 B\n"
"2014-03-31 15:24:13 user1@fit.cvut.cz mail body: 14 B\n"
"2014-03-31 15:26:23 user2@fit.cvut.cz mail body: 22 B\n"
"2014-03-31 16:12:48 boss1@fit.cvut.cz mail body: 24 B + attachment: 97 B\n"
"2014-03-31 18:52:27 user1@fit.cvut.cz mail body: 14 B + attachment: 200 B\n" );
assert ( showMail ( m0 . ListMail ( "inbox",
CTimeStamp ( 2014, 3, 31, 15, 26, 23 ),
CTimeStamp ( 2014, 3, 31, 16, 12, 48 ) ) ) == "2014-03-31 15:26:23 user2@fit.cvut.cz mail body: 22 B\n"
"2014-03-31 16:12:48 boss1@fit.cvut.cz mail body: 24 B + attachment: 97 B\n" );
assert ( showUsers ( m0 . ListAddr ( CTimeStamp ( 2000, 1, 1, 0, 0, 0 ),
CTimeStamp ( 2050, 12, 31, 23, 59, 59 ) ) ) == "boss1@fit.cvut.cz\n"
"user1@fit.cvut.cz\n"
"user2@fit.cvut.cz\n" );
assert ( showUsers ( m0 . ListAddr ( CTimeStamp ( 2014, 3, 31, 15, 26, 23 ),
CTimeStamp ( 2014, 3, 31, 16, 12, 48 ) ) ) == "boss1@fit.cvut.cz\n"
"user2@fit.cvut.cz\n" );
CMailBox m1;
assert ( m1 . NewFolder ( "work" ) );
assert ( m1 . NewFolder ( "spam" ) );
assert ( !m1 . NewFolder ( "spam" ) );
assert ( m1 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 15, 24, 13 ), "user1@fit.cvut.cz", CMailBody ( 14, "mail content 1" ), nullptr ) ) );
att = new CAttach ( 500 );
assert ( m1 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 15, 26, 23 ), "user2@fit.cvut.cz", CMailBody ( 22, "some different content" ), att ) ) );
att -> Release ();
assert ( m1 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 11, 23, 43 ), "boss1@fit.cvut.cz", CMailBody ( 14, "urgent message" ), nullptr ) ) );
att = new CAttach ( 468 );
assert ( m1 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 18, 52, 27 ), "user1@fit.cvut.cz", CMailBody ( 14, "mail content 2" ), att ) ) );
att -> Release ();
assert ( m1 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 16, 12, 48 ), "boss1@fit.cvut.cz", CMailBody ( 24, "even more urgent message" ), nullptr ) ) );
assert ( showMail ( m1 . ListMail ( "inbox",
CTimeStamp ( 2000, 1, 1, 0, 0, 0 ),
CTimeStamp ( 2050, 12, 31, 23, 59, 59 ) ) ) == "2014-03-31 11:23:43 boss1@fit.cvut.cz mail body: 14 B\n"
"2014-03-31 15:24:13 user1@fit.cvut.cz mail body: 14 B\n"
"2014-03-31 15:26:23 user2@fit.cvut.cz mail body: 22 B + attachment: 500 B\n"
"2014-03-31 16:12:48 boss1@fit.cvut.cz mail body: 24 B\n"
"2014-03-31 18:52:27 user1@fit.cvut.cz mail body: 14 B + attachment: 468 B\n" );
assert ( showMail ( m1 . ListMail ( "work",
CTimeStamp ( 2000, 1, 1, 0, 0, 0 ),
CTimeStamp ( 2050, 12, 31, 23, 59, 59 ) ) ) == "" );
assert ( m1 . MoveMail ( "inbox", "work" ) );
assert ( showMail ( m1 . ListMail ( "inbox",
CTimeStamp ( 2000, 1, 1, 0, 0, 0 ),
CTimeStamp ( 2050, 12, 31, 23, 59, 59 ) ) ) == "" );
assert ( showMail ( m1 . ListMail ( "work",
CTimeStamp ( 2000, 1, 1, 0, 0, 0 ),
CTimeStamp ( 2050, 12, 31, 23, 59, 59 ) ) ) == "2014-03-31 11:23:43 boss1@fit.cvut.cz mail body: 14 B\n"
"2014-03-31 15:24:13 user1@fit.cvut.cz mail body: 14 B\n"
"2014-03-31 15:26:23 user2@fit.cvut.cz mail body: 22 B + attachment: 500 B\n"
"2014-03-31 16:12:48 boss1@fit.cvut.cz mail body: 24 B\n"
"2014-03-31 18:52:27 user1@fit.cvut.cz mail body: 14 B + attachment: 468 B\n" );
assert ( m1 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 19, 24, 13 ), "user2@fit.cvut.cz", CMailBody ( 14, "mail content 4" ), nullptr ) ) );
att = new CAttach ( 234 );
assert ( m1 . Delivery ( CMail ( CTimeStamp ( 2014, 3, 31, 13, 26, 23 ), "user3@fit.cvut.cz", CMailBody ( 9, "complains" ), att ) ) );
att -> Release ();
assert ( showMail ( m1 . ListMail ( "inbox",
CTimeStamp ( 2000, 1, 1, 0, 0, 0 ),
CTimeStamp ( 2050, 12, 31, 23, 59, 59 ) ) ) == "2014-03-31 13:26:23 user3@fit.cvut.cz mail body: 9 B + attachment: 234 B\n"
"2014-03-31 19:24:13 user2@fit.cvut.cz mail body: 14 B\n" );
assert ( showMail ( m1 . ListMail ( "work",
CTimeStamp ( 2000, 1, 1, 0, 0, 0 ),
CTimeStamp ( 2050, 12, 31, 23, 59, 59 ) ) ) == "2014-03-31 11:23:43 boss1@fit.cvut.cz mail body: 14 B\n"
"2014-03-31 15:24:13 user1@fit.cvut.cz mail body: 14 B\n"
"2014-03-31 15:26:23 user2@fit.cvut.cz mail body: 22 B + attachment: 500 B\n"
"2014-03-31 16:12:48 boss1@fit.cvut.cz mail body: 24 B\n"
"2014-03-31 18:52:27 user1@fit.cvut.cz mail body: 14 B + attachment: 468 B\n" );
assert ( m1 . MoveMail ( "inbox", "work" ) );
assert ( showMail ( m1 . ListMail ( "inbox",
CTimeStamp ( 2000, 1, 1, 0, 0, 0 ),
CTimeStamp ( 2050, 12, 31, 23, 59, 59 ) ) ) == "" );
assert ( showMail ( m1 . ListMail ( "work",
CTimeStamp ( 2000, 1, 1, 0, 0, 0 ),
CTimeStamp ( 2050, 12, 31, 23, 59, 59 ) ) ) == "2014-03-31 11:23:43 boss1@fit.cvut.cz mail body: 14 B\n"
"2014-03-31 13:26:23 user3@fit.cvut.cz mail body: 9 B + attachment: 234 B\n"
"2014-03-31 15:24:13 user1@fit.cvut.cz mail body: 14 B\n"
"2014-03-31 15:26:23 user2@fit.cvut.cz mail body: 22 B + attachment: 500 B\n"
"2014-03-31 16:12:48 boss1@fit.cvut.cz mail body: 24 B\n"
"2014-03-31 18:52:27 user1@fit.cvut.cz mail body: 14 B + attachment: 468 B\n"
"2014-03-31 19:24:13 user2@fit.cvut.cz mail body: 14 B\n" );
#endif
return 0;
}
#endif /* __PROGTEST__ */