-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
201 lines (172 loc) · 6.99 KB
/
test.cpp
File metadata and controls
201 lines (172 loc) · 6.99 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
//
// main.cpp
// ArrayInt
//
// Created by Jim Bailey on 4/1/18.
// Copyright © 2018 Jim Bailey. All rights reserved.
//
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include "ArrayInt.hpp"
//#define TEST_CONSTRUCTOR
//#define TEST_SET_GET
//#define TEST_APPEND
//#define TEST_MAKE_ROOM
//#define TEST_INSERT_REMOVE
int main(int argc, const char * argv[])
{
#ifdef TEST_CONSTRUCTOR
const int DEFAULT = 10;
const int OVERLOAD = 15;
std::cout << "Testing default and overloaded constructors." << std::endl;
ArrayInt defaultSize;
ArrayInt defineSize(OVERLOAD);
std::cout << "Default size should be " << DEFAULT << " and is " << defaultSize.getSize() << std::endl;
std::cout << "Overload size should be " << OVERLOAD << " and is " << defineSize.getSize() << std::endl;
std::cout << std::endl << std::endl;
#endif // TEST_CONSTRUCTOR
#ifdef TEST_SET_GET
std::cout << "Testing setting values and reading them back" << std::endl << std::endl;
ArrayInt setGet(7);
std::cout << "Testing invalid setAt index of -1" << std::endl;
try {
setGet.setAt(-1, 12);
std::cout << "Should have thrown an exception" << std::endl;
}
catch (std::out_of_range &ex) {
std::cout << "Caught out of range with message: " << ex.what() << std::endl;
}
catch ( ... ) {
std::cout << "Caught something weird " << std::endl;
}
std::cout << std::endl << "Now loading some values and displaying them" << std::endl;
setGet.setAt(0, 3);
setGet.setAt(2, 7);
setGet.setAt(1, 5);
setGet.setAt(4, 13);
setGet.setAt(3, 11);
setGet.setAt(5, 17);
std::cout << "The values should be: 3, 5, 7, 11, 13, 17, unknown value" << std::endl;
std::cout << "The values really are: ";
for(int i = 0; i < 6; i++)
std::cout << setGet.getAt(i) << ", ";
std::cout << setGet.getAt(6) << std::endl;
std::cout << std::endl << "Testing invalid getAt index that is larger than array size" << std::endl;
try {
setGet.getAt(7);
std::cout << "Should have thrown an exception" << std::endl;
}
catch (std::out_of_range &ex) {
std::cout << "Caught out of range with message: " << ex.what() << std::endl;
}
catch ( ... ) {
std::cout << "Caught something weird " << std::endl;
}
std::cout << std::endl << std::endl;
#endif // TEST_SET_GET
#ifdef TEST_APPEND
std::cout << "Testing append with mix of appends and sets" << std::endl << std::endl;
ArrayInt appends;
appends.append(2);
appends.append(4);
appends.setAt(3, 16);
appends.append(32);
std::cout << "The array should be: 2, 4, unknown, 16, 32" << std::endl;
std::cout << "The array really is: ";
for(int i = 0; i < 4; i++)
std::cout << appends.getAt(i) << ", ";
std::cout << appends.getAt(4) << std::endl;
std::cout << std::endl << std::endl;
#endif // TEST_APPEND
#ifdef TEST_MAKE_ROOM
const int START = 7;
const int UPDATE = 12;
std::cout << "Testing setSize and auto expansion on appends" << std::endl << std::endl;
ArrayInt room(START);
std::cout << "Starting size should be " << START << " and is " << room.getSize() << std::endl;
room.setSize(UPDATE);
std::cout << "After setSize, size should be " << UPDATE << " and is " << room.getSize() << std::endl;
std::cout << std::endl << "Get at 8 should succeed, get at 12 should fail " << std::endl;
try {
room.getAt(8);
std::cout << "Get at 8 succeeded" << std::endl;
}
catch ( ... ) {
std::cout << "Get at 8 failed" << std::endl;
}
try {
room.getAt(12);
std::cout << "Get at 12 succeeded" << std::endl;
}
catch ( ... ) {
std::cout << "Get at 12 failed" << std::endl;
}
std::cout << std::endl << "Now going to fill array and see if expands" << std::endl;
for(int i = 0; i < UPDATE; i++)
room.append(2 * i + 1);
std::cout << "Filled with 12 values, no problem" << std::endl;
std::cout << "Size should still be " << UPDATE << " and is " << room.getSize() << std::endl;
std::cout << std::endl << "Adding one more" << std::endl;
room.append(25);
std::cout << "Size should now be " << 2 * UPDATE << " and is " << room.getSize() << std::endl;
std::cout << std::endl << "Displaying values, should be odd numbers 1 - 25" << std::endl;
for(int i = 0; i <= UPDATE; i++)
std::cout << room.getAt(i) << " ";
std::cout << std::endl << std::endl;
#endif // TEST_MAKE_ROOM
#ifdef TEST_INSERT_REMOVE
const int BEGIN = 10;
std::cout << "Testing insert and remove " << std::endl << std::endl;
ArrayInt insertRemove;
for(int i = 0; i < BEGIN; i++)
insertRemove.append(2*i);
std::cout << "Array starting with: ";
for(int i = 0; i < BEGIN; i++)
std::cout << insertRemove.getAt(i) << " ";
std::cout << std::endl << "Size should be " << BEGIN << " and is " << insertRemove.getSize() << std::endl;
std::cout << std::endl << "Now inserting the numbers 5, 9, and 13" << std::endl;
insertRemove.insertAt(7, 13);
insertRemove.insertAt(5, 9);
insertRemove.insertAt(3, 5);
std::cout << "Size should be " << 2 * BEGIN << " and is " << insertRemove.getSize() << std::endl;
std::cout << "The values should be: 0 2 4 5 6 8 9 10 12 13 14 16 18" << std::endl;
std::cout << "The values really are: ";
for(int i = 0; i < BEGIN+3; i++)
std::cout << insertRemove.getAt(i) << " ";
std::cout << std::endl << std::endl << "Now removing the values: ";
std::cout << insertRemove.removeAt(8) << " "
<< insertRemove.removeAt(5) << " "
<< insertRemove.removeAt(0) << std::endl;
std::cout << "The array should be: 2 4 5 6 9 10 13 14 16 18 " << std::endl;
std::cout << "The array really is: ";
for(int i = 0; i < BEGIN; i++)
std::cout << insertRemove.getAt(i) << " ";
std::cout << std::endl << std::endl << "Now testing illegal inserts and removes " << std::endl;
std::cout << std::endl << "Testing invalid insertAt at index larger than array size" << std::endl;
try {
insertRemove.insertAt(BEGIN*3, -1);
std::cout << "Should have thrown an exception" << std::endl;
}
catch (std::out_of_range &ex) {
std::cout << "Caught out of range with message: " << ex.what() << std::endl;
}
catch ( ... ) {
std::cout << "Caught something weird " << std::endl;
}
std::cout << std::endl << "Testing removeAt on empty array" << std::endl;
try {
for(int i = 0; i < BEGIN*3; i++)
insertRemove.removeAt(0);
std::cout << "Should have thrown an exception" << std::endl;
}
catch (std::out_of_range &ex) {
std::cout << "Caught out of range with message: " << ex.what() << std::endl;
}
catch ( ... ) {
std::cout << "Caught something weird " << std::endl;
}
std::cout << std::endl << std::endl;
#endif // TEST_INSERT_REMOVE
return 0;
}