-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserTester.cpp
More file actions
287 lines (238 loc) · 7.25 KB
/
Copy pathUserTester.cpp
File metadata and controls
287 lines (238 loc) · 7.25 KB
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
#include <iostream>
#include <random>
#include "User.h"
using std::cout;
using std::endl;
// ANSI escape codes for colors
#define GREEN "\033[32m"
#define DARK_RED "\033[31m"
#define PURPLE "\033[35m"
#define YELLOW "\033[33m"
#define LIGHT_BLUE "\033[34m"
#define LIGHT_GREEN "\033[92m"
#define TEAL "\033[36m"
#define RED "\033[91m"
#define LIGHT_YELLOW "\033[93m"
#define WHITE "\033[0m"
// Function to set console color using ANSI escape codes
void set_console_color(const char *color)
{
std::cout << color;
}
std::string getDeviceTypeString(const DeviceType type)
{
if (type == PC)
return "PC";
else if (type == LAPTOP)
return "Laptop";
else if (type == TABLET)
return "Tablet";
else if (type == PHONE)
return "Phone";
return "Unknown";
}
std::string getDeviceString(const Device& device)
{
return
"[ID: " + std::to_string(device.getID()) +
", Type: " + getDeviceTypeString(device.getType()) +
", OS: " + device.getOS() +
", Activated: " + (device.isActive() ? "Yes" : "No") + "]";
}
std::string getUserString(const User& user)
{
return
"[ID: " + std::to_string(user.getID()) +
", Username: " + user.getUserName() +
", Age: " + std::to_string(user.getAge()) +
", All devices On: " + (user.checkIfDevicesAreOn() ? "Yes" : "No") + "]";
}
std::string getUserDevicesString(User& user)
{
std::string result = "";
DevicesList userDevices = user.getDevices();
DeviceNode* device = userDevices.get_first();
for (; device != nullptr; device = device->get_next())
{
result += getDeviceString(device->get_data());
result += "\n";
}
if (result != "")
{
// removes the '\n' at the end of the string
result = result.substr(0, result.length() - 1);
}
return result;
}
bool test2User()
{
bool result = false;
try
{
// Tests Ex2 part 2 - User
set_console_color(PURPLE);
cout <<
"********************\n" <<
"Test 2 - User \n" <<
"********************\n" << endl;
set_console_color(WHITE);
cout <<
"Initializing 4 Devices: ... \n" << endl;
Device device1;
device1.init(2123, LAPTOP, WINDOWS11);
Device device2;
device2.init(3212, PC, UbuntuOS);
Device device3;
device3.init(1121, TABLET, WINDOWS10);
Device device4;
device4.init(4134, PHONE, ANDROID);
std::string expected = "[ID: 2123, Type: Laptop, OS: Windows11, Activated: Yes]\n";
expected += "[ID: 3212, Type: PC, OS: UbuntuLinux, Activated: Yes]\n";
expected += "[ID: 1121, Type: Tablet, OS: Windows10, Activated: Yes]\n";
expected += "[ID: 4134, Type: Phone, OS: Android, Activated: Yes]";
std::string got =
getDeviceString(device1) + "\n" +
getDeviceString(device2) + "\n" +
getDeviceString(device3) + "\n" +
getDeviceString(device4);
cout << "Expected:\n" << expected << endl;
cout << "Got:\n" << got << endl;
if (got != expected)
{
set_console_color(RED);
std::cout << "FAILED: Device information is not as expected\n" <<
"check functions Device::init(), Device::getID(), \n" <<
"Device::getOS(), Device::getType()\n";
return false;
set_console_color(WHITE);
}
cout <<
"\nInitializing 2 Users: ... \n" << endl;
User user1;
user1.init(123456789, "blinkybill", 17);
User user2;
user2.init(987654321, "HatichEshMiGilShesh", 15);
expected = "[ID: 123456789, Username: blinkybill, Age: 17, All devices On: Yes]\n";
expected += "[ID: 987654321, Username: HatichEshMiGilShesh, Age: 15, All devices On: Yes]";
got =
getUserString(user1) + "\n" +
getUserString(user2);
cout << "Expected:\n" << expected << endl;
cout << "Got:\n" << got << endl;
if (got != expected)
{
set_console_color(RED);
std::cout << "FAILED: User information is not as expected\n" <<
"check functions User::init(), User::getID, User::getUsername(), \n" <<
"Device::getAge()\n";
return false;
set_console_color(WHITE);
}
cout <<
"\nAdding 2 devices to user1: ... \n" << endl;
user1.addDevice(device1);
user1.addDevice(device2);
expected = "[ID: 2123, Type: Laptop, OS: Windows11, Activated: Yes]\n";
expected += "[ID: 3212, Type: PC, OS: UbuntuLinux, Activated: Yes]";
got = getUserDevicesString(user1);
cout << "Expected:\n" << expected << endl;
cout << "Got:\n" << got << endl;
if (got != expected)
{
set_console_color(RED);
std::cout << "FAILED: User1 information is not as expected\n" <<
"check function User::getDevices()\n";
return false;
set_console_color(WHITE);
}
cout <<
"\nAdding 2 devices to user2: ... \n" << endl;
user2.addDevice(device3);
user2.addDevice(device4);
expected = "[ID: 1121, Type: Tablet, OS: Windows10, Activated: Yes]\n";
expected += "[ID: 4134, Type: Phone, OS: Android, Activated: Yes]";
got = getUserDevicesString(user2);
cout << "Expected:\n" << expected << endl;
cout << "Got:\n" << got << endl;
if (got != expected)
{
set_console_color(RED);
std::cout << "FAILED: User2 information is not as expected\n" <<
"check function User::getDevices()\n";
return false;
set_console_color(WHITE);
}
cout <<
"\nChecking if devices are on for user1: ... \n" << endl;
expected = "true";
got = user1.checkIfDevicesAreOn() ? "true" : "false";
cout << "Expected:" << expected << endl;
cout << "Got :" << got << endl;
if (got != expected)
{
set_console_color(RED);
std::cout << "FAILED: User1 devices should all be ON\n" <<
"check function User::init()\n";
return false;
set_console_color(WHITE);
}
cout <<
"\nDeactivating a device for user2: ... \n" << endl;
user2.getDevices().get_first()->get_data().deactivate();
cout <<
"Checking if devices are on for user2: ... \n" << endl;
expected = "false";
got = user2.checkIfDevicesAreOn() ? "true" : "false";
cout << "Expected:" << expected << endl;
cout << "Got :" << got << endl;
if (got != expected)
{
set_console_color(RED);
std::cout << "FAILED: All of User2 devices should NOT be ON\n" <<
"check function Device::deactivate()\n";
return false;
set_console_color(WHITE);
}
cout <<
"\nClears user objects: ... \n" << endl;
user1.clear();
user2.clear();
}
catch (...)
{
set_console_color(RED);
std::cerr << "Test crashed" << endl;
std::cout << "FAILED: The program crashed, check the following things:\n" <<
"1. Did you delete a pointer twice?\n2. Did you accesse index out of bounds?\n" <<
"3. Did you remember to initialize the lists before using them?";
return false;
}
set_console_color(LIGHT_GREEN);
std::cout << "\n########## User - TEST Passed!!! ##########\n\n";
set_console_color(WHITE);
return true;
}
int main()
{
set_console_color(LIGHT_YELLOW);
std::cout <<
"###########################\n" <<
"Exercise 2 - Social Network\n" <<
"Part 2 - User\n" <<
"###########################\n" << std::endl;
set_console_color(WHITE);
bool testResult = test2User();
if (testResult)
{
set_console_color(GREEN);
std::cout << "\n########## Ex2 Part2 Tests Passed!!! ##########" << "\n\n";
set_console_color(WHITE);
}
else
{
set_console_color(RED);
std::cout << "\n########## TEST Failed :( ##########\n";
set_console_color(WHITE);
}
return 0;
}