-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesting.cpp
More file actions
398 lines (312 loc) · 16.2 KB
/
Copy pathTesting.cpp
File metadata and controls
398 lines (312 loc) · 16.2 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "Testing.hpp"
#ifdef MEMTRACE
#include "memtrace.h"
#endif
#include "gtest_lite.h"
#include "IOUtils.hpp"
#include "UniquePointerLite.hpp"
#include "CaesarCipher.h"
#include "XORCipher.h"
const char *pretest_preface = "Pre-test condition failed: ";
using namespace testing;
void testing::indexing::call_tests(TestCaseResource &testcase_file) {
using namespace indexing;
TEST(indexing, valid_index) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_ptr = testcase_file.read_next<char[]>();
size_t index = *testcase_file.read_next<size_t>();
Cipher haystack = allocate_initialized_cipher<XORCipher>(key, cstr_ptr.get());
EXPECT_NO_THROW(haystack[index]);
EXPECT_EQ(haystack[index], cstr_ptr.get()[index]);
} END
TEST(indexing, out_of_bounds_index) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_ptr = testcase_file.read_next<char[]>();
Cipher haystack = allocate_initialized_cipher<XORCipher>(key, cstr_ptr.get());
size_t len = strlen(cstr_ptr.get());
#ifdef CPORTA
EXPECT_THROW(haystack[len], const std::out_of_range&);
#else
EXPECT_THROW(haystack[len], const std::out_of_range);
#endif
} END
}
void testing::assignation::call_tests(TestCaseResource &testcase_file) {
using namespace assignation;
TEST(assignation, empty_c_str_call) {
int key = *testcase_file.read_next<int>();
Cipher empty(new XORCipher(key));
EXPECT_NO_THROW(empty.c_str())
<< "Did not handle empty string without errors!";
EXPECT_STREQ(empty.c_str(), "")
<< "Queried cstring is not an empty string (or not NUL terminated)!";
} END
TEST(assignation, copy_constructor) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_ptr = testcase_file.read_next<char[]>();
Cipher to_copy = allocate_initialized_cipher<XORCipher>(key, cstr_ptr.get());
Cipher copy_constructed = to_copy;
EXPECT_STREQ(copy_constructed.c_str(), to_copy.c_str())
<< "Constructed object's stored text differs from source!";
} END
TEST(assignation, assign_cstr) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_ptr = testcase_file.read_next<char[]>();
check<XORCipher, const char*>(key, 0, cstr_ptr.get());
} END
TEST(assignation, assign_class) {
int xor_key_a = *testcase_file.read_next<int>();
int xor_key_b = *testcase_file.read_next<int>();
int caesar_key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
// same algo same key
check<XORCipher, XORCipher>(xor_key_a, xor_key_a, cstr.get());
// same algo diff key
check<XORCipher, XORCipher>(xor_key_a, xor_key_b, cstr.get());
// diff algo
check<XORCipher, CaesarCipher>(xor_key_a, caesar_key, cstr.get());
} END
TEST(assignation, recipher_and_assign_data) {
int xor_key = *testcase_file.read_next<int>();
int caesar_key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> original_cstr = testcase_file.read_next<char[]>();
unique_ptr_lite<char[]> overwriting_cstr = testcase_file.read_next<char[]>();
EXPECT_STRNE(original_cstr.get(), overwriting_cstr.get())
<< pretest_preface << "can not check for overwriting!";
Cipher target = allocate_initialized_cipher<XORCipher>(xor_key, original_cstr.get());
Cipher source = allocate_initialized_cipher<CaesarCipher>(caesar_key, overwriting_cstr.get());
EXPECT_STREQ(target.c_str(), original_cstr.get())
<< "Original value which would be overwritten did not get set!";
EXPECT_STREQ(source.c_str(), overwriting_cstr.get())
<< "Overwriting object's text did not get set!";
target.recipher_and_assign_data(source);
EXPECT_STREQ(target.c_str(), overwriting_cstr.get())
<< "Original Cipher object did not get overwritten!";
//
} END
}
void testing::concatenation::call_tests(TestCaseResource &testcase_file) {
using namespace concatenation;
TEST(concatenation, append_cstr) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_base = testcase_file.read_next<char[]>();
unique_ptr_lite<char[]> cstr_appendix = testcase_file.read_next<char[]>();
check<true, XORCipher, const char*>(cstr_base.get(), key, cstr_appendix.get(), 0);
} END
TEST(concatenation, concat_cstr) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_base = testcase_file.read_next<char[]>();
unique_ptr_lite<char[]> cstr_appendix = testcase_file.read_next<char[]>();
check<false, XORCipher, const char*>(cstr_base.get(), key, cstr_appendix.get(), 0);
check<false, const char*, XORCipher>(cstr_base.get(), 0, cstr_appendix.get(), key);
} END
TEST(concatenation, append_class) {
int key_xor_a = *testcase_file.read_next<int>();
int key_xor_b = *testcase_file.read_next<int>();
int key_caesar = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_base = testcase_file.read_next<char[]>();
unique_ptr_lite<char[]> cstr_appendix = testcase_file.read_next<char[]>();
// same algo same key
check<true, XORCipher, XORCipher>(cstr_base.get(), key_xor_a, cstr_appendix.get(), key_xor_a);
// same algo diff key
check<true, XORCipher, XORCipher>(cstr_base.get(), key_xor_a, cstr_appendix.get(), key_xor_b);
// diff algo
check<true, XORCipher, CaesarCipher>(cstr_base.get(), key_xor_a, cstr_appendix.get(), key_caesar);
} END
TEST(concatenation, concat_class) {
int key_xor_a = *testcase_file.read_next<int>();
int key_xor_b = *testcase_file.read_next<int>();
int key_caesar = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_base = testcase_file.read_next<char[]>();
unique_ptr_lite<char[]> cstr_appendix = testcase_file.read_next<char[]>();
// same algo same key
check<false, XORCipher, XORCipher>(cstr_base.get(), key_xor_a, cstr_appendix.get(), key_xor_a);
// same algo diff key
check<false, XORCipher, XORCipher>(cstr_base.get(), key_xor_a, cstr_appendix.get(), key_xor_b);
// diff algo
check<false, XORCipher, CaesarCipher>(cstr_base.get(), key_xor_a, cstr_appendix.get(), key_caesar);
} END
}
void testing::logical_operations::call_tests(TestCaseResource &testcase_file) {
using namespace logical_operations;
TEST(logical_operations, equality_cstr) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_base = testcase_file.read_next<char[]>();
unique_ptr_lite<char[]> cstr_diff = testcase_file.read_next<char[]>();
// check for true
check<true, XORCipher, const char*>(cstr_base.get(), key, cstr_base.get(), 0);
// check for false
check<true, XORCipher, const char*>(cstr_base.get(), key, cstr_diff.get(), 0);
// check global overload
check<true, const char*, XORCipher>(cstr_base.get(), 0, cstr_base.get(), key);
} END
TEST(logical_operations, inequality_cstr) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_base = testcase_file.read_next<char[]>();
unique_ptr_lite<char[]> cstr_diff = testcase_file.read_next<char[]>();
// check for true
check<false, XORCipher, const char*>(cstr_base.get(), key, cstr_base.get(), 0);
// check for false
check<false, XORCipher, const char*>(cstr_base.get(), key, cstr_diff.get(), 0);
// check global overload
check<false, const char*, XORCipher>(cstr_base.get(), 0, cstr_base.get(), key);
} END
TEST(logical_operations, equality_class) {
int xor_key_a = *testcase_file.read_next<int>();
int xor_key_b = *testcase_file.read_next<int>();
int caesar_key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
// same algo same key
check<true, XORCipher, XORCipher>(cstr.get(), xor_key_a, cstr.get(), xor_key_a);
// same algo diff key
check<true, XORCipher, XORCipher>(cstr.get(), xor_key_a, cstr.get(), xor_key_b);
// diff algo
check<true, XORCipher, CaesarCipher>(cstr.get(), xor_key_a, cstr.get(), caesar_key);
} END
TEST(logical_operations, inequality_class) {
int xor_key_a = *testcase_file.read_next<int>();
int xor_key_b = *testcase_file.read_next<int>();
int caesar_key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr_base = testcase_file.read_next<char[]>();
unique_ptr_lite<char[]> cstr_diff = testcase_file.read_next<char[]>();
// same algo same key
check<false, XORCipher, XORCipher>(cstr_base.get(), xor_key_a, cstr_diff.get(), xor_key_a);
// same algo diff key
check<false, XORCipher, XORCipher>(cstr_base.get(), xor_key_a, cstr_diff.get(), xor_key_b);
// diff algo
check<false, XORCipher, CaesarCipher>(cstr_base.get(), xor_key_a, cstr_diff.get(), caesar_key);
} END
}
void testing::iterator::call_tests(TestCaseResource &testcase_file) {
TEST(iterator, begin_end_no_throw) {
// no testdata read
Cipher cipher = allocate_initialized_cipher<XORCipher>(42, "Hello World!");
EXPECT_NO_THROW(cipher.begin())
<< ".begin() failed!";
EXPECT_NO_THROW(cipher.end())
<< ".end() failed!";
} END
TEST(iterator, dereference) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
EXPECT_TRUE(strlen(cstr.get()) > 0)
<< pretest_preface << "0 length string was given!";
Cipher cipher = allocate_initialized_cipher<XORCipher>(key, cstr.get());
Cipher::const_iterator it = cipher.begin();
EXPECT_NO_THROW(*it)
<< "Dereferencing operator failed!";
EXPECT_EQ(*it, cstr[0])
<< "Dereferencing differed from expected value!";
} END
TEST(iterator, add_sub_distance) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
size_t shift = *testcase_file.read_next<size_t>();
EXPECT_TRUE(shift < strlen(cstr.get()))
<< pretest_preface << "shift is not within the bounds of the supplied string!";
EXPECT_TRUE(cstr.get()[0] != cstr.get()[shift])
<< pretest_preface << "shift does not differ from the very first character, can not test shifting!";
Cipher cipher = allocate_initialized_cipher<XORCipher>(key, cstr.get());
Cipher::const_iterator it = cipher.begin();
Cipher::const_iterator it_shifted = it + shift;
// asserted first char differs from shifted
EXPECT_NE(*it, *it_shifted)
<< "Subtraction on an iterator modified original iterator!";
EXPECT_EQ(iterator_position_from_start(cipher, it_shifted), ptrdiff_t(shift))
<< "Addition on an iterator did not work correctly or distance was reported falsely!";
EXPECT_NE(iterator_position_from_start(cipher, it_shifted), iterator_position_from_start(cipher, it))
<< "Addition on an iterator modified original iterator!";
it_shifted = (it + shift) - shift;
EXPECT_EQ(iterator_position_from_start(cipher, it_shifted), 0)
<< "Addition then subtraction of the same value on the same iterator failed to return it to the beginning!";
} END
TEST(iterator, prefix) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
size_t shift = *testcase_file.read_next<size_t>();
EXPECT_TRUE(radius_of_index_within_cstr_contents(cstr.get(), shift, 1))
<< pretest_preface << "shift would not stay within the string!";
auto prefix_decrement_callback = [](Cipher::const_iterator &it) { return --it; };
check_offsetting<XORCipher>(prefix_decrement_callback, key, cstr.get(), IteratorOffsettingValues(shift, shift-1, shift-1));
auto prefix_increment_callback = [](Cipher::const_iterator &it) { return ++it; };
check_offsetting<XORCipher>(prefix_increment_callback, key, cstr.get(), IteratorOffsettingValues(shift, shift+1, shift+1));
} END
TEST(iterator, postfix) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
size_t shift = *testcase_file.read_next<size_t>();
EXPECT_TRUE(radius_of_index_within_cstr_contents(cstr.get(), shift, 1))
<< pretest_preface << "shift would not stay within the string!";
auto prefix_decrement_callback = [](Cipher::const_iterator &it) { return it--; };
check_offsetting<XORCipher>(prefix_decrement_callback, key, cstr.get(), IteratorOffsettingValues(shift, shift, shift-1));
auto prefix_increment_callback = [](Cipher::const_iterator &it) { return it++; };
check_offsetting<XORCipher>(prefix_increment_callback, key, cstr.get(), IteratorOffsettingValues(shift, shift, shift+1));
} END
TEST(iterator, compound_assignment) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
size_t initial_shift = *testcase_file.read_next<size_t>();
size_t shift = *testcase_file.read_next<size_t>();
EXPECT_TRUE(radius_of_index_within_cstr_contents(cstr.get(), initial_shift, shift))
<< pretest_preface << "shift would not stay within the string!";
auto compound_add = [shift](Cipher::const_iterator &it) {return it += shift;};
check_offsetting<XORCipher>(compound_add, key, cstr.get(), IteratorOffsettingValues(initial_shift, initial_shift+shift, initial_shift+shift));
auto compound_sub = [shift](Cipher::const_iterator &it) {return it -= shift;};
check_offsetting<XORCipher>(compound_sub, key, cstr.get(), IteratorOffsettingValues(initial_shift, initial_shift-shift, initial_shift-shift));
} END
TEST(iterator, indexing) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
size_t initial_shift = *testcase_file.read_next<size_t>();
size_t idx = *testcase_file.read_next<size_t>();
EXPECT_TRUE(radius_of_index_within_cstr_contents(cstr.get(), initial_shift, idx))
<< pretest_preface << "shift would not stay within the string!";
char *cstr_raw = cstr.get();
Cipher cipher = allocate_initialized_cipher<XORCipher>(key, cstr_raw);
auto index_test = [cipher, cstr_raw](int shift, int idx) {
Cipher::const_iterator it = cipher.begin() + shift;
EXPECT_EQ(it[idx], cstr_raw[shift + idx])
<< "Indexing operator did not yield expected result(" << (idx > 0 ? "positive" : "negative") << " index)!";
};
index_test(initial_shift, idx);
index_test(initial_shift, -idx);
} END
TEST(iterator, logic_equality_inequality) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
size_t idx_base = *testcase_file.read_next<size_t>();
size_t idx_diff = *testcase_file.read_next<size_t>();
EXPECT_TRUE(idx_base != idx_diff)
<< pretest_preface << "supplied indecies do not differ, can not check inequality!";
check_logic<OpType::eq, XORCipher>(key, cstr.get(), idx_base, idx_base);
check_logic<OpType::eq, XORCipher>(key, cstr.get(), idx_base, idx_diff);
check_logic<OpType::neq, XORCipher>(key, cstr.get(), idx_base, idx_base);
check_logic<OpType::neq, XORCipher>(key, cstr.get(), idx_base, idx_diff);
} END
TEST(iterator, logic_comparison) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
size_t smaller_idx = *testcase_file.read_next<size_t>();
size_t larger_idx = *testcase_file.read_next<size_t>();
EXPECT_TRUE(smaller_idx < larger_idx)
<< "one index was supposed to be smaller!";
// this will check equality by applying small index against itself
check_logic<OpType::lt, XORCipher>(key, cstr.get(), smaller_idx, larger_idx);
check_logic<OpType::lte, XORCipher>(key, cstr.get(), smaller_idx, larger_idx);
check_logic<OpType::gt, XORCipher>(key, cstr.get(), smaller_idx, larger_idx);
check_logic<OpType::gte, XORCipher>(key, cstr.get(), smaller_idx, larger_idx);
} END
TEST(iterator, foreach) {
int key = *testcase_file.read_next<int>();
unique_ptr_lite<char[]> cstr = testcase_file.read_next<char[]>();
Cipher cipher = allocate_initialized_cipher<XORCipher>(key,cstr.get());
char *deciphered_buf = new char[cipher.len() + 1]; // defer delete
size_t last_index = 0;
for (auto c : cipher)
deciphered_buf[last_index++] = c;
deciphered_buf[last_index] = '\0';
EXPECT_STREQ(deciphered_buf, cipher.c_str())
<< "c_str() and foreach deciphered strings differ!";
delete[] deciphered_buf;
} END
}