forked from UCSD-ECE141/assignment-1-ekirimlioglu-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesting.hpp
More file actions
194 lines (162 loc) · 5.89 KB
/
Testing.hpp
File metadata and controls
194 lines (162 loc) · 5.89 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
//
// Created by Emin Tunc Kirimlioglu on 1/2/25.
//
#ifndef TESTING_HPP
#define TESTING_HPP
#include <iostream>
#include <vector>
#include <string>
#include "Sequence.hpp"
constexpr int kNoErrorsDetected = 0;
constexpr int kAddFailure = 100;
constexpr int kRemoveFailure = 200;
constexpr int kGetFailure = 300;
constexpr int kInsertFailure = 400;
constexpr int kSizeFailure = 500;
template<typename T>
inline bool matchesVector(Sequence<T>& sequence, const std::vector<T>& vec) {
if (sequence.size() != vec.size()) {
return false;
}
for (size_t i = 0; i < vec.size(); i++) {
if (sequence.get(i) != vec[i]) {
return false;
}
}
return true;
}
// Base test functions that work with any Sequence implementation
class SequenceTester {
public:
template<typename T>
static int doBasicTests(Sequence<T>& sequence) {
// Test add and get
sequence.add(T(100));
sequence.add(T(200));
sequence.add(T(300));
std::vector<T> expected = {T(100), T(200), T(300)};
if (!matchesVector(sequence, expected)) {
std::cout << "Basic add/get test failed\n";
return kAddFailure;
}
// Test negative indexing
if (sequence.get(-1) != T(300) || sequence.get(-2) != T(200)) {
std::cout << "Negative indexing test failed\n";
return kGetFailure;
}
// Test insert with negative index
sequence.insert(-2, T(150)); // Insert before second-to-last
expected = {T(100), T(200), T(150), T(300)};
if (!matchesVector(sequence, expected)) {
std::cout << "Insert with negative index test failed\n";
return kInsertFailure;
}
// Test remove with position
sequence.remove(1); // Remove second element
expected = {T(100), T(150), T(300)};
if (!matchesVector(sequence, expected)) {
std::cout << "Remove with position test failed\n";
return kRemoveFailure;
}
// Test remove with negative index
sequence.remove(-1); // Remove last element
expected = {T(100), T(150)};
if (!matchesVector(sequence, expected)) {
std::cout << "Remove with negative index test failed\n";
return kRemoveFailure;
}
// Test size
if (sequence.size() != 2) {
std::cout << "Size test failed\n";
return kSizeFailure;
}
return kNoErrorsDetected;
}
template<typename T>
static int doStressTest(Sequence<T>& sequence) {
std::vector<T> expected;
// Test adding many elements
for (int i = 0; i < 1000; i++) {
sequence.add(T(i));
expected.push_back(T(i));
}
if (!matchesVector(sequence, expected)) {
std::cout << "Stress test - adding elements failed\n";
return kAddFailure;
}
// Test random inserts with negative indices
for (int i = 0; i < 100; i++) {
int pos = (rand() % (sequence.size() * 2)) - sequence.size(); // Mix of positive and negative
T value = T(rand() % 1000);
sequence.insert(pos, value);
pos = pos < 0 ? sequence.size() + pos : pos; // Convert negative to positive for vector
expected.insert(expected.begin() + pos, value);
if (!matchesVector(sequence, expected)) {
std::cout << "Stress test - random inserts failed\n";
return kInsertFailure;
}
}
// Test random removes from various positions
for (int i = 0; i < 100; i++) {
int pos = (rand() % (sequence.size() * 2)) - sequence.size(); // Mix of positive and negative
sequence.remove(pos);
pos = pos < 0 ? sequence.size() + pos + 1: pos; // Convert negative to positive for vector
expected.erase(expected.begin() + pos);
if (!matchesVector(sequence, expected)) {
std::cout << "Stress test - random removes failed\n";
return kRemoveFailure;
}
}
return kNoErrorsDetected;
}
template<typename T>
static int doEdgeCaseTests(Sequence<T>& sequence) {
try {
// Test empty sequence operations
if (!sequence.is_empty()) {
std::cout << "Empty check failed\n";
return kSizeFailure;
}
// Test remove from empty
try {
sequence.remove();
std::cout << "Remove from empty should throw\n";
return kRemoveFailure;
} catch (...) {
// Expected
}
// Test get with invalid negative index
try {
sequence.get(-1);
std::cout << "Get with invalid negative index should throw\n";
return kGetFailure;
} catch (...) {
// Expected
}
// Test insert with out-of-bounds negative index
try {
sequence.insert(-2, T(100));
std::cout << "Insert with invalid negative index should throw\n";
return kInsertFailure;
} catch (...) {
// Expected
}
// Test operations with single element
sequence.add(T(100));
if (sequence.get(-1) != T(100)) {
std::cout << "Get last element with negative index failed\n";
return kGetFailure;
}
sequence.remove(-1);
if (!sequence.is_empty()) {
std::cout << "Remove with negative index failed\n";
return kRemoveFailure;
}
} catch (const std::exception& e) {
std::cout << "Unexpected exception in edge case tests\n";
return -1;
}
return kNoErrorsDetected;
}
};
#endif //TESTING_HPP