-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
860 lines (789 loc) · 23.9 KB
/
Copy pathmain.cpp
File metadata and controls
860 lines (789 loc) · 23.9 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
#include <iterator>
#include <iostream>
#include <vector>
#include <functional>
#include <cassert>
#include <list>
#include <chrono>
#include <exception>
#include <string>
#include <algorithm>
//Напишите реализацию для класса BiDirectionalList и тесты к нему.
//
//Предусмотрите обработку ошибок (выход за границы массива, передача неверного
//итератора в метод и т. д.) с использованием механизма исключений.
//
//-----------------------------------------------------------------------------
template<typename T>
class BiDirectionalList {
protected:
struct Node;
public:
class Iterator : public std::iterator<std::bidirectional_iterator_tag, T> {
public:
T& operator*() const;
T* operator->() const;
Iterator& operator++();
const Iterator operator++(int);
Iterator& operator--();
const Iterator operator--(int);
bool operator==(const Iterator& other) const;
bool operator!=(const Iterator& other) const;
private:
friend class BiDirectionalList;
const BiDirectionalList* const list_;
Node* node_;
Iterator(const BiDirectionalList* const list, Node* node) : list_(list),
node_(node) {}
};
class ConstIterator :
public std::iterator<std::bidirectional_iterator_tag, T> {
public:
const T& operator*() const;
const T* operator->() const;
ConstIterator& operator++();
const ConstIterator operator++(int);
ConstIterator& operator--();
const ConstIterator operator--(int);
bool operator==(const ConstIterator& other) const;
bool operator!=(const ConstIterator& other) const;
private:
friend class BiDirectionalList;
const BiDirectionalList* const list_;
const Node* node_;
ConstIterator(const BiDirectionalList* const list, Node* node)
: list_(list), node_(node) {}
};
BiDirectionalList() : first_(nullptr), last_(nullptr) {}
~BiDirectionalList() { Clear(); }
bool IsEmpty() const;
void Clear();
Iterator begin();
Iterator end();
ConstIterator begin() const;
ConstIterator end() const;
std::vector<T> AsArray() const;
void InsertBefore(Iterator position, const T& value);
void InsertBefore(Iterator position, T&& value);
void InsertAfter(Iterator position, const T& value);
void InsertAfter(Iterator position, T&& value);
void PushBack(const T& value);
void PushBack(T&& value);
void PushFront(const T& value);
void PushFront(T&& value);
void Erase(Iterator position);
void PopFront();
void PopBack();
Iterator Find(const T& value);
ConstIterator Find(const T& value) const;
Iterator Find(std::function<bool(const T&)> predicate);
ConstIterator Find(std::function<bool(const T&)> predicate) const;
protected:
struct Node {
explicit Node(const T& value);
explicit Node(T&& value);
T value_;
Node* next_node_;
Node* previous_node_;
};
Node* first_;
Node* last_;
void InsertBefore(Node* existing_node, Node* new_node);
void InsertAfter(Node* existing_node, Node* new_node);
void Erase(Node* node);
};
template<typename T>
BiDirectionalList<T>::Node::Node(const T& value) : value_(value),
previous_node_(nullptr),
next_node_(nullptr) {}
template<typename T>
BiDirectionalList<T>::Node::Node(T&& value) : value_(value),
previous_node_(nullptr),
next_node_(nullptr) {
value = T();
}
template<typename T>
T& BiDirectionalList<T>::Iterator::operator*() const {
return node_->value_;
}
template<typename T>
T* BiDirectionalList<T>::Iterator::operator->() const {
return &node_->value_;
}
template<typename T>
typename BiDirectionalList<T>::Iterator& BiDirectionalList<T>::
Iterator::operator++() {
if (node_ == nullptr) {
throw std::runtime_error("Impossible to increase iterator");
}
node_ = node_->next_node_;
return *this;
}
template<typename T>
const typename BiDirectionalList<T>::Iterator BiDirectionalList<T>::
Iterator::operator++(int) {
if (node_ == nullptr) {
throw std::runtime_error("Impossible to increase iterator");
}
auto new_node = node_;
node_ = node_->next_node_;
Iterator new_iterator(list_, new_node);
return new_iterator;
}
template<typename T>
typename BiDirectionalList<T>::Iterator& BiDirectionalList<T>::
Iterator::operator--() {
if (node_ == list_->first_) {
throw std::runtime_error("Impossible to reduce iterator");
} else if (node_ == nullptr) {
node_ = list_->last_;
} else {
node_ = node_->previous_node_;
}
return *this;
}
template<typename T>
const typename BiDirectionalList<T>::Iterator BiDirectionalList<T>::
Iterator::operator--(int) {
if (node_ == list_->first_) {
throw std::runtime_error("Impossible to reduce iterator");
}
auto new_node = node_;
if (node_ == nullptr) {
node_ = list_->last_;
} else {
node_ = node_->previous_node_;
}
Iterator new_iterator(list_, new_node);
return new_iterator;
}
template<typename T>
bool BiDirectionalList<T>::Iterator::operator==
(const BiDirectionalList::Iterator& other) const {
return other.node_ == node_;
}
template<typename T>
bool BiDirectionalList<T>::Iterator::operator!=
(const BiDirectionalList::Iterator& other) const {
return other.node_ != node_;
}
template<typename T>
const T& BiDirectionalList<T>::ConstIterator::operator*() const {
return node_->value_;
}
template<typename T>
const T* BiDirectionalList<T>::ConstIterator::operator->() const {
return &node_->value_;
}
template<typename T>
typename BiDirectionalList<T>::ConstIterator& BiDirectionalList<T>::
ConstIterator::operator++() {
if (node_ == nullptr) {
throw std::runtime_error("Impossible to increase iterator");
}
node_ = node_->next_node_;
return *this;
}
template<typename T>
const typename BiDirectionalList<T>::ConstIterator BiDirectionalList<T>::
ConstIterator::operator++(int) {
if (node_ == nullptr) {
throw std::runtime_error("Impossible to increase iterator");
}
auto new_node = node_;
node_ = node_->next_node_;
ConstIterator new_iterator(list_, new_node);
return new_iterator;
}
template<typename T>
typename BiDirectionalList<T>::ConstIterator& BiDirectionalList<T>::
ConstIterator::operator--() {
if (node_ == list_->first_) {
throw std::runtime_error("Impossible to reduce iterator");
}
if (node_ == nullptr) {
node_ = list_->last_;
} else {
node_ = node_->previous_node_;
}
return *this;
}
template<typename T>
const typename BiDirectionalList<T>::ConstIterator BiDirectionalList<T>::
ConstIterator::operator--(int) {
if (node_ == list_->first_) {
throw std::runtime_error("Impossible to reduce iterator");
}
auto new_node = node_;
if (node_ == nullptr) {
node_ = list_->last_;
} else {
node_ = node_->previous_node_;
}
ConstIterator new_iterator(list_, new_node);
return new_iterator;
}
template<typename T>
bool BiDirectionalList<T>::ConstIterator::operator==
(const BiDirectionalList::ConstIterator& other) const {
return other.node_ == node_;
}
template<typename T>
bool BiDirectionalList<T>::ConstIterator::operator!=
(const BiDirectionalList::ConstIterator& other) const {
return other.node_ != node_;
}
template<typename T>
bool BiDirectionalList<T>::IsEmpty() const {
return last_ == first_ && last_ == nullptr;
}
template<typename T>
void BiDirectionalList<T>::Clear() {
while (!IsEmpty()) {
Erase(last_);
}
}
template<typename T>
typename BiDirectionalList<T>::Iterator BiDirectionalList<T>::begin() {
return BiDirectionalList::Iterator(this, first_);
}
template<typename T>
typename BiDirectionalList<T>::Iterator BiDirectionalList<T>::end() {
return BiDirectionalList::Iterator(this, nullptr);
}
template<typename T>
typename BiDirectionalList<T>::ConstIterator BiDirectionalList<T>::
begin() const {
return BiDirectionalList::ConstIterator(this, first_);
}
template<typename T>
typename BiDirectionalList<T>::ConstIterator BiDirectionalList<T>::
end() const {
return BiDirectionalList::ConstIterator(this, nullptr);
}
template<typename T>
std::vector<T> BiDirectionalList<T>::AsArray() const {
std::vector<T> new_vector;
for (auto i = begin(); i != end(); ++i) {
new_vector.push_back(*i);
}
return new_vector;
}
template<typename T>
void BiDirectionalList<T>::InsertBefore(BiDirectionalList::Iterator position,
const T& value) {
Node* new_node = new Node(value);
InsertBefore(position.node_, new_node);
}
template<typename T>
void BiDirectionalList<T>::InsertBefore(BiDirectionalList::Iterator position,
T&& value) {
Node* new_node = new Node(std::move(value));
InsertBefore(position.node_, new_node);
}
template<typename T>
void BiDirectionalList<T>::InsertAfter(BiDirectionalList::Iterator position,
const T& value) {
Node* new_node = new Node(value);
InsertAfter(position.node_, new_node);
}
template<typename T>
void BiDirectionalList<T>::InsertAfter(BiDirectionalList::Iterator position,
T&& value) {
Node* new_node = new Node(std::move(value));
InsertAfter(position.node_, new_node);
}
template<typename T>
void BiDirectionalList<T>::PushBack(const T& value) {
Node* new_node = new Node(value);
InsertAfter(last_, new_node);
}
template<typename T>
void BiDirectionalList<T>::PushBack(T&& value) {
Node* new_node = new Node(std::move(value));
InsertAfter(last_, new_node);
}
template<typename T>
void BiDirectionalList<T>::PushFront(const T& value) {
Node* new_node = new Node(value);
InsertBefore(first_, new_node);
}
template<typename T>
void BiDirectionalList<T>::PushFront(T&& value) {
Node* new_node = new Node(std::move(value));
InsertBefore(first_, new_node);
}
template<typename T>
void BiDirectionalList<T>::Erase(BiDirectionalList::Iterator position) {
if (IsEmpty()) {
throw std::runtime_error("Impossible to delete element from empty list");
}
if (position == end()) {
throw std::runtime_error("Impossible to delete end");
}
Erase(position.node_);
}
template<typename T>
void BiDirectionalList<T>::PopFront() {
if (IsEmpty()) {
throw std::runtime_error("Impossible to delete element from empty list");
}
Erase(begin().node_);
}
template<typename T>
void BiDirectionalList<T>::PopBack() {
if (IsEmpty()) {
throw std::runtime_error("Impossible to delete element from empty list");
}
Erase((--end()).node_);
}
template<typename T>
typename BiDirectionalList<T>::Iterator BiDirectionalList<T>::
Find(const T& value) {
for (Iterator i = begin(); i != end(); ++i) {
if (*i == value) {
return i;
}
}
return end();
}
template<typename T>
typename BiDirectionalList<T>::ConstIterator BiDirectionalList<T>::
Find(const T& value) const {
for (ConstIterator i = begin(); i != end(); ++i) {
if (*i == value) {
return i;
}
}
return end();
}
template<typename T>
typename BiDirectionalList<T>::Iterator BiDirectionalList<T>::
Find(std::function<bool(const T&)> predicate) {
for (Iterator i = begin(); i != end(); ++i) {
if (predicate(*i)) {
return i;
}
}
return end();
}
template<typename T>
typename BiDirectionalList<T>::ConstIterator BiDirectionalList<T>::
Find(std::function<bool(const T&)> predicate) const {
for (ConstIterator i = begin(); i != end(); ++i) {
if (predicate(*i)) {
return i;
}
}
return end();
}
template<typename T>
void BiDirectionalList<T>::InsertBefore(BiDirectionalList::Node* existing_node,
BiDirectionalList::Node* new_node) {
if (first_ == nullptr) {
first_ = last_ = new_node;
} else if (existing_node == first_) {
new_node->next_node_ = existing_node;
existing_node->previous_node_ = new_node;
first_ = new_node;
} else {
Node* existing_previous_node = existing_node->previous_node_;
existing_node->previous_node_ = new_node;
new_node->previous_node_ = existing_previous_node;
existing_previous_node->next_node_ = new_node;
new_node->next_node_ = existing_node;
}
}
template<typename T>
void BiDirectionalList<T>::InsertAfter(BiDirectionalList::Node* existing_node,
BiDirectionalList::Node* new_node) {
if (IsEmpty()) {
first_ = last_ = new_node;
} else if (existing_node == last_) {
existing_node->next_node_ = new_node;
new_node->previous_node_ = existing_node;
last_ = new_node;
} else {
Node* existing_next_node = existing_node->next_node_;
existing_node->next_node_ = new_node;
new_node->next_node_ = existing_next_node;
existing_next_node->previous_node_ = new_node;
new_node->previous_node_ = existing_node;
}
}
template<typename T>
void BiDirectionalList<T>::Erase(BiDirectionalList::Node* node) {
if (node->next_node_ == nullptr && node->previous_node_ == nullptr) {
delete node;
first_ = last_ = nullptr;
} else if (node->next_node_ == nullptr) {
Node* prev = node->previous_node_;
delete node;
last_ = prev;
last_->next_node_ = nullptr;
} else if (node->previous_node_ == nullptr) {
Node* next = node->next_node_;
delete node;
first_ = next;
first_->previous_node_ = nullptr;
} else {
Node* prev = node->previous_node_;
Node* next = node->next_node_;
prev->next_node_ = next;
next->previous_node_ = prev;
delete node;
}
}
// Для тестирования группы закомментируйте или удалите строчку
// "#define SKIP_XXXXX" для соответствующей группы тестов.
//
// #define SKIP_Actions_with_iterators
// #define SKIP_PushFront
// #define SKIP_PushBack
// #define SKIP_PopBack
// #define SKIP_PopFront
// #define SKIP_Find
// #define SKIP_Find_with_predicate
// #define SKIP_Insert
// #define SKIP_Erase
// #define SKIP_Combo_vombo
// #define SKIP_Exception
//
//===========================================================
int main() {
int const COUNT = 15;
srand(time(0));
#ifndef SKIP_Actions_with_iterators
{
std::list<int> true_list;
BiDirectionalList<int> my_list;
for (int i = 0; i < COUNT; i++) {
int temp = rand();
true_list.push_back(temp);
my_list.PushBack(temp);
}
BiDirectionalList<int>::Iterator iter = my_list.begin();
auto true_iter = true_list.begin();
BiDirectionalList<int>::Iterator iter2 = --my_list.end();
auto true_iter2 = --true_list.end();
assert(*true_iter2 == *iter2);
for (int i = 0; i < COUNT; i++) {
assert(*iter == *true_iter);
if (i != COUNT - 1) {
iter++;
true_iter++;
}
}
for (int i = 0; i < COUNT; i++) {
assert(*iter == *true_iter);
if (i != COUNT - 1) {
iter--;
true_iter--;
}
}
for (int i = 0; i < COUNT; i++) {
assert(*iter == *true_iter);
if (i != COUNT - 1) {
++iter;
++true_iter;
}
}
for (int i = 0; i < COUNT; i++) {
assert(*iter == *true_iter);
if (i != COUNT - 1) {
--iter;
--true_iter;
}
}
my_list.Clear();
assert(my_list.IsEmpty());
std::cout << "[PASS] Actions with iterators" << std::endl;
}
#else
std::cout << "[SKIPPED] Actions with iterators" << std::endl;
#endif // SKIP_Actions_with_iterators
#ifndef SKIP_PushFront
{
BiDirectionalList<int> my_list;
std::vector<int> checker;
assert(my_list.IsEmpty());
for (int i = 0; i < COUNT; i++) {
int temp = rand();
my_list.PushFront(temp);
checker.push_back(temp);
}
reverse(begin(checker), end(checker));
assert(!(my_list.IsEmpty()));
assert(checker == my_list.AsArray());
my_list.Clear();
assert(my_list.IsEmpty());
std::cout << "[PASS] PushFront" << std::endl;
}
#else
std::cout << "[SKIPPED] PushFront" << std::endl;
#endif // SKIP_PushFront
#ifndef SKIP_PushBack
{
BiDirectionalList<int> my_list;
std::vector<int> checker;
assert(my_list.IsEmpty());
for (int i = 0; i < COUNT; i++) {
int temp = rand();
my_list.PushBack(temp);
checker.push_back(temp);
}
assert(!(my_list.IsEmpty()));
assert(checker == my_list.AsArray());
my_list.Clear();
assert(my_list.IsEmpty());
std::cout << "[PASS] PushBack" << std::endl;
}
#else
std::cout << "[SKIPPED] PushBack" << std::endl;
#endif // SKIP_PushBack
#ifndef SKIP_PopBack
{
BiDirectionalList<int> my_list;
std::vector<int> checker;
assert(my_list.IsEmpty());
for (int i = 0; i < COUNT; i++) {
int temp = rand();
my_list.PushBack(temp);
checker.push_back(temp);
}
for (int i = 0; i < COUNT; i++) {
checker.pop_back();
my_list.PopBack();
assert(checker == my_list.AsArray());
}
assert(my_list.IsEmpty());
std::cout << "[PASS] PopBack" << std::endl;
}
#else
std::cout << "[SKIPPED PopBack]" << std::endl;
#endif // SKIP_PopBack
#ifndef SKIP_PopFront
{
BiDirectionalList<int> my_list;
std::vector<int> checker;
assert(my_list.IsEmpty());
for (int i = 0; i < COUNT; i++) {
int temp = rand();
my_list.PushBack(temp);
checker.push_back(temp);
}
for (int i = 0; i < COUNT; i++) {
checker.erase(checker.begin());
my_list.PopFront();
assert(checker == my_list.AsArray());
}
assert(my_list.IsEmpty());
std::cout << "[PASS] PopFront" << std::endl;
}
#else
std::cout << "[SKIPPED] PopFront" << std::endl;
#endif // SKIP_PopFront
#ifndef SKIP_Find
{
BiDirectionalList<int> my_list;
for (int i = 0; i < COUNT; i++) {
my_list.PushBack(i);
}
BiDirectionalList<int>::Iterator check1 = my_list.Find(17);
BiDirectionalList<int>::Iterator check2 = my_list.Find(2);
BiDirectionalList<int>::Iterator check3 = my_list.Find(10);
BiDirectionalList<int>::Iterator check4 = my_list.Find(7);
BiDirectionalList<int>::Iterator check5 = my_list.Find(25);
assert(check1 == my_list.end());
assert(*check2 == 2);
assert(*check4 == 7);
assert(*check3 == 10);
assert(check5 == my_list.end());
std::cout << "[PASS] Find" << std::endl;
my_list.Clear();
assert(my_list.IsEmpty());
}
#else
std:: cout << "[SKIPPED] Find" << std::endl;
#endif // SKIP_Find
#ifndef SKIP_Find_with_predicate
{
BiDirectionalList<int> my_list;
for (int i = 0; i < COUNT; i++) {
my_list.PushBack(i);
}
auto function1 = [](const int &value) {
return value == 5;
};
auto function2 = [](const int &value) {
return value == 10;
};
auto function3 = [](const int &value) {
return value == 0;
};
auto function4 = [](const int &value) {
return value == -1;
};
auto function5 = [](const int &value) {
return value == 18;
};
BiDirectionalList<int>::Iterator check1 = my_list.Find(function1);
BiDirectionalList<int>::Iterator check2 = my_list.Find(function2);
BiDirectionalList<int>::Iterator check3 = my_list.Find(function3);
BiDirectionalList<int>::Iterator check4 = my_list.Find(function4);
BiDirectionalList<int>::Iterator check5 = my_list.Find(function5);
assert(*check1 == 5);
assert(*check2 == 10);
assert(*check3 == 0);
assert(check4 == my_list.end());
assert(check5 == my_list.end());
my_list.Clear();
assert(my_list.IsEmpty());
std::cout << "[PASS] Find with predicate" << std::endl;
}
#else
std::cout << "[SKIPPED] Find with predicate" << std::endl;
#endif // SKIP_Find_with_predicate
#ifndef SKIP_Insert
{
BiDirectionalList<int> my_list;
std::vector<int> checker;
for (int i = 0; i < 10; i++) {
my_list.PushBack(i * 2 + 1);
checker.push_back(i * 2 + 1);
};
assert(checker == my_list.AsArray());
checker = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 17, 19};
for (int i = 0; i < 5; i++) {
BiDirectionalList<int>::Iterator check = my_list.Find(i * 2 + 1);
my_list.InsertBefore(check, 2 * i);
}
assert(checker == my_list.AsArray());
for (int i = 5; i < 10; i++) {
BiDirectionalList<int>::Iterator check = my_list.Find(i * 2 - 1);
my_list.InsertAfter(check, 2 * i);
}
checker = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19};
assert(checker == my_list.AsArray());
my_list.Clear();
std::cout << "[PASS] Insert" << std::endl;
}
#else
std::cout << "[SKIPPED] Insert" << std::endl;
#endif // SKIP_Insert
#ifndef SKIP_Erase
{
BiDirectionalList<int> my_list;
std::vector<int> checker;
for (int i = 0; i < 20; i++) {
my_list.PushBack(i);
checker.push_back(i);
}
assert(checker == my_list.AsArray());
for (int i = 0; i < 10; i++) {
BiDirectionalList<int>::Iterator check = my_list.Find(i * 2);
my_list.Erase(check);
}
checker = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
assert(checker == my_list.AsArray());
for (int i = 0; i < 5; i++) {
BiDirectionalList<int>::Iterator check = my_list.Find(i * 2 + 1);
my_list.Erase(check);
}
checker = {11, 13, 15, 17, 19};
assert(checker == my_list.AsArray());
my_list.Clear();
std::cout << "[PASS] Erase" << std::endl;
}
#else
std::cout << "[SKIPPED] Erase" << std::endl;
#endif // SKIP_Erase
#ifndef SKIP_Combo_vombo
{
BiDirectionalList<int> my_list;
std::vector<int> checker;
for (int i = 0; i < COUNT; i++) {
my_list.PushFront(i);
checker.push_back(i);
}
reverse(begin(checker), end(checker));
assert(checker == my_list.AsArray());
for (int i = 0; i < COUNT; i++) {
my_list.PopFront();
checker.erase(begin(checker));
assert(checker == my_list.AsArray());
}
for (int i = 0; i < COUNT; i++) {
my_list.PushBack(i);
checker.push_back(i);
}
std::vector<int> checker2;
for (int i = 0; i < COUNT; i++) {
my_list.InsertBefore(my_list.begin(), (-1) * i);
checker2.push_back((-1) * i);
}
reverse(begin(checker2), end(checker2));
checker2.insert(end(checker2), begin(checker), end(checker));
assert(checker2 == my_list.AsArray());
for (int i = 0; i < COUNT; i++) {
my_list.InsertAfter(--my_list.end(), i + COUNT);
checker2.push_back(i + COUNT);
}
assert(checker2 == my_list.AsArray());
std::cout << "[PASS] Combo_vombo" << std::endl;
}
#else
std::cout << "[SKIPPED] Combo_vombo" << std::endl;
#endif // SKIP_Combo_vombo
#ifndef SKIP_Exception
{
BiDirectionalList<double> my_list;
try {
my_list.Erase(my_list.begin());
} catch (std::runtime_error &ex) {
assert(static_cast<std::string>(ex.what()) ==
"Impossible to delete element from empty list");
}
my_list.PushBack(8.5);
BiDirectionalList<double>::Iterator iter = my_list.begin();
try {
my_list.Erase(my_list.end());
} catch (std::runtime_error &ex) {
assert(static_cast<std::string>(ex.what()) ==
"Impossible to delete end");
}
try {
iter--;
} catch (std::runtime_error &ex) {
assert(static_cast<std::string>(ex.what()) ==
"Impossible to reduce iterator");
}
BiDirectionalList<double>::Iterator iter2 = my_list.end();
try {
--iter2;
} catch (std::runtime_error &ex) {
assert(static_cast<std::string>(ex.what()) ==
"Impossible to reduce iterator");
}
BiDirectionalList<double>::Iterator iter3 = my_list.end();
try {
iter3++;
} catch (std::runtime_error &ex) {
assert(static_cast<std::string>(ex.what()) ==
"Impossible to increase iterator");
}
BiDirectionalList<double>::Iterator iter4 = my_list.end();
try {
++iter4;
} catch (std::runtime_error &ex) {
assert(static_cast<std::string>(ex.what()) ==
"Impossible to increase iterator");
}
std::cout << "[PASS] Exception" << std::endl;
}
#else
std::cout << "[SKIPPED] Exception" << std::endl;
#endif // SKIP_Exception
return 0;
}