-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest12.cpp
252 lines (220 loc) · 7.41 KB
/
test12.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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <type_traits>
namespace MailProcessing
{
// Represents a letter in a 1950s office environment
struct Letter
{
std::string recipient;
std::string department;
std::string contents;
bool urgent;
};
// Different routing strategies for mail
template <typename LetterType>
struct InternalMailRoute
{
void route(const LetterType &letter, const std::string &message)
{
std::cout << "[Internal Mail] To: " << letter.recipient << " (" << letter.department << "), Message: " << message << std::endl;
}
};
template <typename LetterType>
struct ExternalMailRoute
{
void route(const LetterType &letter, const std::string &message)
{
std::cout << "[External Mail] To: " << letter.recipient << ", Message: " << message << std::endl;
}
};
// Different sorting actions based on priority
struct DepartmentSort
{
};
struct UrgencySort
{
};
// Default handling rule - no special handling
template <typename LetterType>
struct DefaultHandler
{
bool handle(const LetterType &letter)
{
return true;
}
};
// Rule to handle urgent mail specifically
template <typename LetterType>
struct UrgentMailHandler
{
bool handle(const LetterType &letter)
{
return letter.urgent;
}
};
// Main template for processing mail
template <typename LetterType,
template <typename> typename Router,
typename SortAction,
template <typename> typename Handler = DefaultHandler>
class MailProcessor
{
public:
MailProcessor() = default;
bool process(LetterType letter)
{
static_assert(std::is_same_v<SortAction, DepartmentSort> || std::is_same_v<SortAction, UrgencySort>,
"Invalid SortAction provided.");
Handler<LetterType> handler;
if (!handler.handle(letter))
{
router_.route(letter, "Handling rule not met.");
return false;
}
// The core sorting logic - this is your task
if constexpr (std::is_same_v<SortAction, DepartmentSort>)
{
// TODO: Implement the logic to sort mail by department.
// This might involve adding the letter to a department-specific pile.
// This is the HARD PART.
router_.route(letter, "Sorted by department.");
return true;
}
else if constexpr (std::is_same_v<SortAction, UrgencySort>)
{
// TODO: Implement the logic to sort mail by urgency.
// This might involve placing urgent mail in a priority tray.
// This is the HARD PART.
router_.route(letter, "Sorted by urgency.");
return true;
}
return false; // Should not reach here due to static_assert
}
private:
Router<LetterType> router_;
};
} // namespace MailProcessing
bool test_department_sort_internal()
{
using namespace MailProcessing;
MailProcessor<Letter, InternalMailRoute, DepartmentSort> processor;
Letter letter{"Bob Cratchit", "Accounting", "Payroll Report", false};
return processor.process(letter);
}
bool test_urgency_sort_external_urgent()
{
using namespace MailProcessing;
MailProcessor<Letter, ExternalMailRoute, UrgencySort> processor;
Letter letter{"Ebenezer Scrooge", "Management", "Urgent Business Proposal", true};
return processor.process(letter);
}
bool test_urgency_sort_internal_not_urgent()
{
using namespace MailProcessing;
MailProcessor<Letter, InternalMailRoute, UrgencySort> processor;
Letter letter{"Tiny Tim", "Logistics", "Christmas List", false};
return processor.process(letter);
}
bool test_department_sort_external()
{
using namespace MailProcessing;
MailProcessor<Letter, ExternalMailRoute, DepartmentSort> processor;
Letter letter{"Jacob Marley", "Consulting", "Audit Findings", false};
return processor.process(letter);
}
bool test_urgent_mail_handler_blocks()
{
using namespace MailProcessing;
MailProcessor<Letter, InternalMailRoute, DepartmentSort, UrgentMailHandler> processor;
Letter not_urgent{"Unknown", "Sales", "Flyer", false};
return !processor.process(not_urgent); // Expecting it to be blocked by the handler
}
bool test_urgent_mail_handler_allows()
{
using namespace MailProcessing;
MailProcessor<Letter, InternalMailRoute, DepartmentSort, UrgentMailHandler> processor;
Letter urgent{"Important Person", "HR", "Confidential Memo", true};
return processor.process(urgent);
}
template <typename T>
concept InvalidSortAction = !std::is_same_v<T, MailProcessing::DepartmentSort> && !std::is_same_v<T, MailProcessing::UrgencySort>;
void static_assert_test()
{
using namespace MailProcessing;
// This will cause a compilation error due to the static_assert
// using BadProcessor = MailProcessor<Letter, InternalMailRoute, int>; // Invalid SortAction
static_assert(true, "This test confirms the static_assert for invalid SortAction compiles.");
}
int main()
{
int failures = 0;
std::cout << "Running tests for Mail Processing System (1950s Edition):" << std::endl;
if (test_department_sort_internal())
{
std::cout << "[PASSED] Department Sort - Internal Mail" << std::endl;
}
else
{
std::cout << "[FAILED] Department Sort - Internal Mail" << std::endl;
failures++;
}
if (test_urgency_sort_external_urgent())
{
std::cout << "[PASSED] Urgency Sort - External Mail (Urgent)" << std::endl;
}
else
{
std::cout << "[FAILED] Urgency Sort - External Mail (Urgent)" << std::endl;
failures++;
}
if (test_urgency_sort_internal_not_urgent())
{
std::cout << "[PASSED] Urgency Sort - Internal Mail (Not Urgent)" << std::endl;
}
else
{
std::cout << "[FAILED] Urgency Sort - Internal Mail (Not Urgent)" << std::endl;
failures++;
}
if (test_department_sort_external())
{
std::cout << "[PASSED] Department Sort - External Mail" << std::endl;
}
else
{
std::cout << "[FAILED] Department Sort - External Mail" << std::endl;
failures++;
}
if (test_urgent_mail_handler_blocks())
{
std::cout << "[PASSED] Urgent Mail Handler - Blocks Non-Urgent" << std::endl;
}
else
{
std::cout << "[FAILED] Urgent Mail Handler - Blocks Non-Urgent" << std::endl;
failures++;
}
if (test_urgent_mail_handler_allows())
{
std::cout << "[PASSED] Urgent Mail Handler - Allows Urgent" << std::endl;
}
else
{
std::cout << "[FAILED] Urgent Mail Handler - Allows Urgent" << std::endl;
failures++;
}
std::cout << "\nStatic Assert Test (Compilation Check):" << std::endl;
static_assert_test(); // This test primarily checks for compilation
if (failures == 0)
{
std::cout << "\nAll tests passed. Proceed to fill in the TODO sections." << std::endl;
}
else
{
std::cout << "\nSome tests failed. Review the output and then fill in the TODO sections." << std::endl;
}
return failures;
}