-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceTester.cpp
More file actions
160 lines (138 loc) · 4.11 KB
/
Copy pathDeviceTester.cpp
File metadata and controls
160 lines (138 loc) · 4.11 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
#include <iostream>
#include <random>
#include "Device.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") + "]";
}
bool test1Device()
{
bool result = false;
try
{
// Tests Ex2 part 1 - Device
set_console_color(TEAL);
cout << "*******************\n"
<< "Test 1 - Device \n"
<< "*******************\n"
<< endl;
set_console_color(WHITE);
cout << "Initializing Device1: ... \n"
<< endl;
Device device1;
device1.init(3343, PC, WINDOWS11);
std::string expected = "[ID: 3343, Type: PC, OS: Windows11, Activated: Yes]";
std::string got = getDeviceString(device1);
cout << "Expected: " << expected << endl;
cout << "Got : " << got << std::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 << "\nDeactivating Device1: ... \n"
<< endl;
device1.deactivate();
expected = "[ID: 3343, Type: PC, OS: Windows11, Activated: No]";
got = getDeviceString(device1);
cout << "Expected: " << expected << endl;
cout << "Got : " << got << std::endl;
if (got != expected)
{
set_console_color(RED);
std::cout << "FAILED: Device information is not as expected\n"
<< "check functions Device::init(), Device::deactivate()\n";
return false;
set_console_color(WHITE);
}
cout << "\nActivating Device1: ... \n"
<< endl;
device1.activate();
expected = "[ID: 3343, Type: PC, OS: Windows11, Activated: Yes]";
got = getDeviceString(device1);
cout << "Expected: " << expected << endl;
cout << "Got : " << got << std::endl;
if (got != expected)
{
set_console_color(RED);
std::cout << "FAILED: Device should be ON\n"
<< "check function Device::activate()\n";
return false;
set_console_color(WHITE);
}
}
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########## Device - 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 1 - Device\n"
<< "###########################\n"
<< std::endl;
set_console_color(WHITE);
bool testResult = test1Device();
if (testResult)
{
set_console_color(GREEN);
std::cout << "\n########## Ex2 Part1 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;
}