2
2
#include < cassert>
3
3
#include " ../include/LinkedList.h"
4
4
5
- void test_empty () {
5
+ int main ( int argc, char * argv[]) {
6
6
LinkedList<int > list;
7
7
assert (list.empty () == true );
8
- list.push_back (11 );
9
- assert (list.empty () == false );
10
- list.pop_back ();
11
- assert (list.empty () == true );
12
- std::cout << " empty passed!" << std::endl;
13
- }
14
-
15
- void test_size (){
16
- LinkedList<int > list;
17
8
assert (list.size () == 0 );
18
- list.push_back (2 );
9
+ try {
10
+ list.pop_front ();
11
+ std::cerr << " pop_front() throw an exception FAILED!" << std::endl;
12
+ } catch (const std::underflow_error &e){
13
+ std::cout << " pop_front() throw an exception PASSED: " << e.what () << std::endl;
14
+ }
15
+ try {
16
+ list.pop_back ();
17
+ std::cerr << " pop_back() throw an exception FAILED!" << std::endl;
18
+ } catch (const std::underflow_error &e){
19
+ std::cout << " pop_back() throw an exception PASSED: " << e.what () << std::endl;
20
+ }
21
+ list.push_front (32 ); // 32
22
+ assert (list.empty () == false );
19
23
assert (list.size () == 1 );
20
- list.pop_back ();
21
- assert (list.size () == 0 );
22
- std::cout << " size passed!" << std::endl;
23
- }
24
-
25
- // Test add functions
26
- void test (){
27
- LinkedList<int > list;
28
- list.push_front (5 );
29
- list.push_front (4 );
30
- list.push_front (3 ); // 3 4 5
31
- assert (list.size () == 3 );
32
- assert (list.front () == 3 );
33
- list.pop_front (); // 4 5
24
+ assert (list.front () == 32 );
25
+ assert (list.back () == 32 );
26
+ assert (list.contains (32 ) == 0 );
27
+ assert (list.get_at (0 ) == 32 );
28
+ list.push_back (22 ); // 32 22
29
+ assert (list.empty () == false );
34
30
assert (list.size () == 2 );
35
- assert (list.front () == 4 );
36
- std::cout << " push_front passed!" << std::endl;
37
- std::cout << " pop_front passed!" << std::endl;
38
- list.push_back (2 ); // 4 5 2
31
+ assert (list.front () == 32 );
32
+ assert (list.back () == 22 );
33
+ assert (list.contains (22 ) == 1 );
34
+ assert (list.get_at (0 ) == 32 );
35
+ list.push_front (11 ); // 11 32 22
36
+ assert (list.empty () == false );
39
37
assert (list.size () == 3 );
38
+ assert (list.front () == 11 );
39
+ assert (list.back () == 22 );
40
+ assert (list.contains (32 ) == 1 );
41
+ assert (list.get_at (2 ) == 22 );
42
+ try {
43
+ list.push_at (4 , 99 );
44
+ std::cerr << " push_at() throw an exception FAILED!" << std::endl;
45
+ } catch (const std::out_of_range &e){
46
+ std::cout << " push_at() throw an exception PASSED: " << e.what () << std::endl;
47
+ }
48
+ try {
49
+ list.pop_at (3 );
50
+ std::cerr << " pop_at() throw an exception FAILED!" << std::endl;
51
+ } catch (const std::out_of_range &e){
52
+ std::cout << " pop_at() throw an exception PASSED: " << e.what () << std::endl;
53
+ }
54
+ try {
55
+ list.change_at (3 , 99 );
56
+ std::cerr << " change_at() throw an exception FAILED!" << std::endl;
57
+ } catch (const std::out_of_range &e){
58
+ std::cout << " change_at() throw an exception PASSED: " << e.what () << std::endl;
59
+ }
60
+ try {
61
+ list.get_at (3 );
62
+ std::cerr << " get_at() throw an exception FAILED!" << std::endl;
63
+ } catch (const std::out_of_range &e){
64
+ std::cout << " get_at() throw an exception PASSED: " << e.what () << std::endl;
65
+ }
66
+ list.push_front (31 ); // 31 11 32 22
40
67
assert (list.empty () == false );
41
- assert (list.front () == 4 );
42
- assert (list.back () == 2 );
43
- std::cout << " push_back passed!" << std::endl;
44
- list.push_at (1 , 1 ); // 4 1 5 2
45
68
assert (list.size () == 4 );
46
- assert (list.contains ( 1 ) == 1 );
47
- std::cout << " push_at passed! " << std::endl ;
48
- list.pop_back (); // 4 1 5
49
- assert (list.size ( ) == 3 );
50
- assert ( list.back () == 5 );
51
- std::cout << " pop_back passed! " << std::endl ;
52
- list.push_front ( 3 );
53
- list.push_back ( 6 );
54
- list.push_back ( 2 ); // 3 4 1 5 6 2
55
- list.push_at ( 3 , 9 ); // 3 4 1 9 5 6 2
56
- assert (list.size ( ) == 7 );
57
- assert ( list.get_at ( 3 ) == 9 );
58
- list.pop_at ( 1 ); // 3 1 9 5 6 2
69
+ assert (list.front ( ) == 31 );
70
+ assert (list. back () == 22 ) ;
71
+ assert ( list.contains ( 22 ) == 3 );
72
+ assert (list.get_at ( 2 ) == 32 );
73
+ list.push_back ( 29 ); // 31 11 32 22 29
74
+ assert (list. empty () == false ) ;
75
+ assert ( list.size () == 5 );
76
+ assert ( list.front () == 31 );
77
+ assert ( list.back () == 29 );
78
+ assert ( list.contains ( 11 ) == 1 );
79
+ assert (list.get_at ( 3 ) == 22 );
80
+ list.push_back ( 11 ); // 31 11 32 22 29 11
81
+ assert ( list.empty () == false );
59
82
assert (list.size () == 6 );
60
- assert (list.get_at (1 ) == 1 );
61
- std::cout << " pop_at passed!" << std::endl;
62
- list.change_at (3 , 4 ); // 3 1 9 4 6 2
83
+ assert (list.front () == 31 );
84
+ assert (list.back () == 11 );
85
+ assert (list.contains (11 ) == 1 );
86
+ assert (list.get_at (4 ) == 29 );
87
+ list.change_eq_first (32 , 75 ); // 31 11 75 22 29 11
88
+ assert (list.contains (75 ) == 2 );
89
+ assert (list.get_at (2 ) == 75 );
90
+ assert (list.contains (32 ) == std::numeric_limits<size_t >::max ());
91
+ list.change_eq_all (11 , 57 ); // 31 57 75 22 29 57
92
+ assert (list.contains (57 ) == 1 );
93
+ assert (list.get_at (5 ) == 57 );
94
+ assert (list.contains (11 ) == std::numeric_limits<size_t >::max ());
95
+ list.change_at (2 , 39 ); // 31 57 39 22 29 57
96
+ assert (list.contains (39 ) == 2 );
97
+ assert (list.get_at (2 ) == 39 );
98
+ assert (list.contains (75 ) == std::numeric_limits<size_t >::max ());
99
+ list.print ();
100
+ list.reverse (); // 57 29 22 39 57 31
101
+ assert (list.empty () == false );
63
102
assert (list.size () == 6 );
64
- assert (list.get_at (3 ) == 4 );
65
- std::cout << " change_at passed!" << std::endl;
66
- list.push_back (1 ); // 3 1 9 4 6 2 1
67
- list.change_at (4 , 5 ); // 3 1 9 4 5 2 1
68
- list.change_eq_all (1 , 6 ); // 3 6 9 4 5 2 6
69
- list.change_eq_first (6 , 7 ); // 3 7 9 4 5 2 6
70
- assert (list.size () == 7 );
71
- assert (list.get_at (4 ) == 5 );
72
- assert (list.contains (1 ) == -1 );
73
- std::cout << " change passed!" << std::endl;
74
- list.reverse (); // 6 2 5 4 9 7 3
75
- assert (list.size () == 7 );
76
- assert (list.front () == 6 );
77
- assert (list.get_at (1 ) == 2 );
78
- assert (list.get_at (2 ) == 5 );
79
- assert (list.get_at (3 ) == 4 );
80
- assert (list.get_at (4 ) == 9 );
81
- assert (list.get_at (5 ) == 7 );
82
- assert (list.get_at (6 ) == 3 );
83
- assert (list.back () == 3 );
103
+ assert (list.get_at (1 ) == 29 );
104
+ assert (list.front () == 57 );
105
+ assert (list.back () == 31 );
84
106
list.print ();
85
- std::cout << " reverse passed!" << std::endl;
86
- std::cout << " All test passed!" << std::endl;
87
- }
88
-
89
- int main (int argc, char * argv[]) {
90
- test_empty ();
91
- test_size ();
92
- test ();
107
+ list.pop_front (); // 29 22 39 57 31
108
+ assert (list.empty () == false );
109
+ assert (list.size () == 5 );
110
+ assert (list.get_at (1 ) == 22 );
111
+ assert (list.front () == 29 );
112
+ assert (list.back () == 31 );
113
+ list.pop_back (); // 29 22 39 57
114
+ assert (list.empty () == false );
115
+ assert (list.size () == 4 );
116
+ assert (list.get_at (2 ) == 39 );
117
+ assert (list.front () == 29 );
118
+ assert (list.back () == 57 );
119
+ list.pop_at (2 ); // 29 22 57
120
+ assert (list.empty () == false );
121
+ assert (list.size () == 3 );
122
+ assert (list.get_at (1 ) == 22 );
123
+ assert (list.front () == 29 );
124
+ assert (list.back () == 57 );
125
+ list.pop_front ();
126
+ list.pop_back ();
127
+ list.pop_at (0 ); // null
128
+ try {
129
+ list.push_at (1 , 99 );
130
+ std::cerr << " push_at() throw an exception FAILED!" << std::endl;
131
+ } catch (const std::out_of_range &e){
132
+ std::cout << " push_at() throw an exception PASSED: )" << e.what () << std::endl;
133
+ }
134
+ try {
135
+ list.pop_at (1 );
136
+ std::cerr << " pop_at() throw an exception FAILED!" << std::endl;
137
+ } catch (const std::out_of_range &e){
138
+ std::cout << " pop_at() throw an exception PASSED: )" << e.what () << std::endl;
139
+ }
140
+ try {
141
+ list.change_at (1 , 99 );
142
+ std::cerr << " change_at() throw an exception FAILED!" << std::endl;
143
+ } catch (const std::out_of_range &e){
144
+ std::cout << " change_at() throw an exception PASSED: )" << e.what () << std::endl;
145
+ }
146
+ try {
147
+ list.pop_front ();
148
+ std::cerr << " pop_front() throw an exception FAILED!" << std::endl;
149
+ } catch (const std::underflow_error &e){
150
+ std::cout << " pop_front() throw an exception PASSED: )" << e.what () << std::endl;
151
+ }
152
+ try {
153
+ list.pop_back ();
154
+ std::cerr << " pop_back() throw an exception FAILED!" << std::endl;
155
+ } catch (const std::underflow_error &e){
156
+ std::cout << " pop_back() throw an exception PASSED: )" << e.what () << std::endl;
157
+ }
158
+ try {
159
+ list.change_eq_first (1 , 99 );
160
+ std::cerr << " change_eq_first() throw an exception FAILED!" << std::endl;
161
+ } catch (const std::underflow_error &e){
162
+ std::cout << " change_eq_first() throw an exception PASSED: )" << e.what () << std::endl;
163
+ }
164
+ try {
165
+ list.change_eq_all (1 , 99 );
166
+ std::cerr << " change_eq_all() throw an exception FAILED!" << std::endl;
167
+ } catch (const std::underflow_error &e){
168
+ std::cout << " change_eq_all() throw an exception PASSED: )" << e.what () << std::endl;
169
+ }
170
+ try {
171
+ list.front ();
172
+ std::cerr << " front() throw an exception FAILED!" << std::endl;
173
+ } catch (const std::underflow_error &e){
174
+ std::cout << " front() throw an exception PASSED: )" << e.what () << std::endl;
175
+ }
176
+ try {
177
+ list.back ();
178
+ std::cerr << " back() throw an exception FAILED!" << std::endl;
179
+ } catch (const std::underflow_error &e){
180
+ std::cout << " back() throw an exception PASSED: )" << e.what () << std::endl;
181
+ }
182
+ try {
183
+ list.contains (99 );
184
+ std::cerr << " contains() throw an exception FAILED!" << std::endl;
185
+ } catch (const std::underflow_error &e){
186
+ std::cout << " contains() throw an exception PASSED: )" << e.what () << std::endl;
187
+ }
188
+ list.print ();
189
+ std::cout << " ALL TEST PASSED!" << std::endl;
93
190
return 0 ;
94
191
}
0 commit comments