-
Notifications
You must be signed in to change notification settings - Fork 0
/
testCases.cpp
273 lines (218 loc) · 10.6 KB
/
testCases.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
#include <iostream>
#include <cassert>
#include "interface_template.cpp"
int failed_tests = 0;
void assertWithMessage(bool condition, const char* message) {
if (!condition) {
throw std::runtime_error(message);
}
}
void testLoadContactsFromFile() {
try {
// Create a PhoneBook and load contacts from "Details.txt"
PhoneBook phoneBook;
phoneBook.readRecordsFromFile("Details.txt"); // Assuming the file is in the same directory
// Fetch contacts and check if they are loaded correctly
vector<PhoneRecord*> fetchedContacts = phoneBook.fetchContacts(new std::string("Rahul Kumar Mishra"));
assertWithMessage(!fetchedContacts.empty(), "No contacts loaded from file");
assertWithMessage(fetchedContacts[0]->getName() == "Rahul Kumar Mishra", "Contact not loaded from file");
fetchedContacts = phoneBook.fetchContacts(new std::string("Vikram Rana"));
assertWithMessage(!fetchedContacts.empty(), "No contacts loaded from file");
// Either keyword Vikram or Rana should be present in the contact's name
assertWithMessage(
fetchedContacts[0]->getName().find("Vikram") != std::string::npos ||
fetchedContacts[0]->getName().find("Rana") != std::string::npos,
"Contact not loaded from file"
);
// Add more checks for other contacts as needed...
} catch (const std::exception& e) {
failed_tests++;
std::cout << "Unit test 'testLoadContactsFromFile' failed: " << e.what() << std::endl;
}
}
void testAddContact() {
try {
// Create a PhoneBook
PhoneBook phoneBook;
// Create some PhoneRecord objects
vector<string> phoneNumbers1 = {"1234567890", "4567891230"};
PhoneRecord record1("John Doe", "Company A", phoneNumbers1);
// Add a contact to the PhoneBook
phoneBook.addContact(&record1);
// Fetch the contact and assert its name
vector<PhoneRecord*> fetchedContacts = phoneBook.fetchContacts(new std::string("John Doe"));
assertWithMessage(!fetchedContacts.empty(), "Contact not found");
assertWithMessage(fetchedContacts[0]->getName() == "John Doe", "Contact not found");
} catch (const std::exception& e) {
failed_tests++;
std::cout << "Unit test 'testAddContact' failed: " << e.what() << std::endl;
}
}
void testDeleteContact() {
try {
// Create a PhoneBook
PhoneBook phoneBook;
// Create and add some PhoneRecord objects
vector<string> phoneNumbers2 = {"9998887777"};
PhoneRecord record2("Dwayne Johnson", "Company A", phoneNumbers2);
phoneBook.addContact(&record2);
// Delete the contact and check if it's removed
bool deleteResult = phoneBook.deleteContact(new std::string("Dwayne Johnson"));
assertWithMessage(deleteResult, "Contact deletion failed");
// Try to fetch the deleted contact
vector<PhoneRecord*> fetchedContacts = phoneBook.fetchContacts(new std::string("Dwayne Johnson"));
assertWithMessage(fetchedContacts.empty(), "Deleted contact still exists");
} catch (const std::exception& e) {
failed_tests++;
std::cout << "Unit test 'testDeleteContact' failed: " << e.what() << std::endl;
}
}
void testAddAndFetchContacts() {
try {
PhoneBook phoneBook;
// Create and add multiple PhoneRecord objects
vector<string> phoneNumbers1 = {"1234567890", "4567891230"};
PhoneRecord record1("John Doe", "Company A", phoneNumbers1);
phoneBook.addContact(&record1);
vector<string> phoneNumbers2 = {"9998887777"};
PhoneRecord record2("Dwayne Johnson", "Company B", phoneNumbers2);
phoneBook.addContact(&record2);
vector<string> phoneNumbers3 = {"5555555555"};
PhoneRecord record3("Dwayne John", "Company A", phoneNumbers3);
phoneBook.addContact(&record3);
vector<string> phoneNumbers4 = {"1111222233"};
PhoneRecord record4("Charlie Dwayne", "Company C", phoneNumbers4);
phoneBook.addContact(&record4);
// Fetch contacts by organization
vector<PhoneRecord*> Contacts = phoneBook.fetchContacts(new std::string("John"));
assertWithMessage(Contacts.size() == 2, "Incorrect number of John contacts");
vector<PhoneRecord*> Contacts2 = phoneBook.fetchContacts(new std::string("Dwayne"));
assertWithMessage(Contacts2.size() == 3, "Incorrect number of Dwayne contacts");
} catch (const std::exception& e) {
failed_tests++;
std::cout << "Unit test 'testAddAndFetchContacts' failed: " << e.what() << std::endl;
}
}
void testDeleteMultipleContacts() {
try {
PhoneBook phoneBook;
// Create and add multiple PhoneRecord objects
vector<string> phoneNumbers1 = {"1234567890", "4567891230"};
PhoneRecord record1("John Doe", "Company A", phoneNumbers1);
phoneBook.addContact(&record1);
vector<string> phoneNumbers2 = {"9998887777"};
PhoneRecord record2("Dwayne Johnson", "Company A", phoneNumbers2);
phoneBook.addContact(&record2);
vector<string> phoneNumbers3 = {"5555555555"};
PhoneRecord record3("Bob Smith", "Company A", phoneNumbers3);
phoneBook.addContact(&record3);
vector<string> phoneNumbers4 = {"1111222233"};
PhoneRecord record4("Charlie Brown", "Company C", phoneNumbers4);
phoneBook.addContact(&record4);
bool deleteResult = phoneBook.deleteContact(new std::string("Brown"));
assertWithMessage(deleteResult, "Contact deletion failed");
vector<PhoneRecord*> BrownContacts = phoneBook.fetchContacts(new std::string("Brown"));
assertWithMessage(BrownContacts.empty(), "Brown contact not deleted");
vector<PhoneRecord*> BobContacts = phoneBook.fetchContacts(new std::string("Bob"));
assertWithMessage(BobContacts.size() == 1, "Bob contact is deleted, where as it should not be deleted");
} catch (const std::exception& e) {
failed_tests++;
std::cout << "Unit test 'testDeleteMultipleContacts' failed: " << e.what() << std::endl;
}
}
void testFetchContactsByPartialQuery() {
try {
PhoneBook phoneBook;
// Create and add multiple PhoneRecord objects
vector<string> phoneNumbers1 = {"1234567890", "4567891230"};
PhoneRecord record1("John Doe", "Company A", phoneNumbers1);
phoneBook.addContact(&record1);
vector<string> phoneNumbers2 = {"9998887777"};
PhoneRecord record2("Dwayne Johnson", "Company B", phoneNumbers2);
phoneBook.addContact(&record2);
vector<string> phoneNumbers3 = {"5555555555"};
PhoneRecord record3("Bob Smith", "Company A", phoneNumbers3);
phoneBook.addContact(&record3);
// Fetch contacts by a partial query "John" (should match John Doe)
vector<PhoneRecord*> johnContacts = phoneBook.fetchContacts(new std::string("John"));
assertWithMessage(johnContacts.size() == 1, "Incorrect number of contacts for query 'John'");
} catch (const std::exception& e) {
failed_tests++;
std::cout << "Unit test 'testFetchContactsByPartialQuery' failed: " << e.what() << std::endl;
}
}
void testSearchByMiddleName() {
try {
PhoneBook phoneBook;
// Create and add multiple PhoneRecord objects
vector<string> phoneNumbers1 = {"1234567890", "4567891230"};
PhoneRecord record1("John Doe", "Company A", phoneNumbers1);
phoneBook.addContact(&record1);
vector<string> phoneNumbers2 = {"9998887777"};
PhoneRecord record2("Dwayne Johnson", "Company B", phoneNumbers2);
phoneBook.addContact(&record2);
vector<string> phoneNumbers3 = {"5555555555"};
PhoneRecord record3("Bob Smith", "Company A", phoneNumbers3);
phoneBook.addContact(&record3);
vector<string> phoneNumbers4 = {"1111222233"};
PhoneRecord record4("Charlie Dwayne Brown", "Company C", phoneNumbers4); // Added middle name
phoneBook.addContact(&record4);
// Search for contacts using middle names
vector<PhoneRecord*> middleNameContacts = phoneBook.fetchContacts(new std::string("Dwayne"));
assertWithMessage(middleNameContacts.size() == 2, "Incorrect number of contacts with middle name 'Dwayne'");
} catch (const std::exception& e) {
failed_tests++;
std::cout << "Unit test 'testSearchByMiddleName' failed: " << e.what() << std::endl;
}
}
void testObjectValues() {
try {
PhoneBook phoneBook;
// Create and add a PhoneRecord object
vector<string> phoneNumbers1 = {"1234567890", "4567891230"};
PhoneRecord record1("John Doe", "Company A", phoneNumbers1);
phoneBook.addContact(&record1);
// Fetch the contact
vector<PhoneRecord*> fetchedContacts = phoneBook.fetchContacts(new std::string("John Doe"));
assertWithMessage(!fetchedContacts.empty(), "Contact not found");
// Check if the fetched contact has the correct values
PhoneRecord* fetchedRecord = fetchedContacts[0];
assertWithMessage(fetchedRecord->getName() == "John Doe", "Incorrect name");
assertWithMessage(fetchedRecord->getOrganisation() == "Company A", "Incorrect organization");
vector<string> fetchedPhoneNumbers = fetchedRecord->getPhoneNumbers();
assertWithMessage(fetchedPhoneNumbers.size() == 2, "Incorrect number of phone numbers");
assertWithMessage(fetchedPhoneNumbers[0] == "1234567890", "Incorrect phone number");
assertWithMessage(fetchedPhoneNumbers[1] == "4567891230", "Incorrect phone number");
} catch (const std::exception& e) {
failed_tests++;
std::cout << "Unit test 'testObjectValues' failed: " << e.what() << std::endl;
}
}
int main() {
std::vector<void (*)()> unit_tests_list = {
testLoadContactsFromFile,
testAddContact,
testDeleteContact,
testAddAndFetchContacts,
testDeleteMultipleContacts,
testFetchContactsByPartialQuery,
testObjectValues,
testSearchByMiddleName
};
int total = unit_tests_list.size();
for (int i = 0; i < total; ++i) {
try {
unit_tests_list[i]();
} catch (const std::exception& e) {
failed_tests++;
std::cout << "Unit test #" << i + 1 << " failure: " << e.what() << std::endl;
}
}
if (failed_tests == 0) {
std::cout << "All tests have passed successfully!" << std::endl;
} else {
std::cout << failed_tests << " tests failed!" << std::endl;
}
std::cout << "Number of failed test cases are: " << failed_tests << std::endl;
return 0;
}