-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.cpp
229 lines (159 loc) · 6.01 KB
/
tests.cpp
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
// tests.cpp
// Tests for BSTree & Account classes
// Author: Juan Arias
#include <cassert>
#include <iostream>
#include "bstree.h"
// Test Deposit & RecordTransaction
void TestDeposit(Account* acctPtr) {
for (int amount(1000), fund(Account::MONEY_MARKET);
fund < Account::MAX_FUNDS; amount += 1000, ++fund) {
assert(acctPtr->Deposit(fund, amount));
std::string transaction("D " + std::to_string(acctPtr->GetID()) +
std::to_string(fund) + " " + std::to_string(amount));
// Test transaction recording
acctPtr->RecordTransaction(transaction, fund);
}
}
// Test Transfer
void TestTransfer(Account* acctPtr, int fund1, int fund2, int amount) {
std::string transaction("T " + std::to_string(acctPtr->GetID()) +
std::to_string(fund1) + " " + std::to_string(amount) + " " +
std::to_string(acctPtr->GetID()) + std::to_string(fund2));
if (acctPtr->Transfer(acctPtr, fund1, fund2, amount)) {
acctPtr->RecordTransaction(transaction, fund1);
acctPtr->RecordTransaction(transaction, fund2);
} else {
acctPtr->RecordFailedTransaction(transaction, fund1);
acctPtr->RecordFailedTransaction(transaction, fund2);
}
}
// Test Withdraw for valid & invalid amounts, covering between linked funds,
// RecordTransaction, RecordFailedTransaction
void TestWithdraw(Account* acctPtr) {
for (int amount(1000), fund(Account::MONEY_MARKET);
fund < Account::MAX_FUNDS; amount += 1000, ++fund) {
bool linkedFunds(fund == Account::PRIME_MONEY_MARKET ||
fund == Account::LONG_TERM_BOND);
int testAmount = (linkedFunds) ? amount + 100: amount;
std::string transaction("W " + std::to_string(acctPtr->GetID()) +
std::to_string(fund) + " " + std::to_string(testAmount));
// Test transaction recording
if (acctPtr->Withdraw(fund, testAmount)) {
acctPtr->RecordTransaction(transaction, fund);
} else {
acctPtr->RecordFailedTransaction(transaction, fund);
}
}
}
// Test Transfer between two different Accounts
void TestTransfer2Accounts(Account* acctPtr) {
Account otherAcct("Yusuf Pisan", 3420);
otherAcct.Deposit(Account::VALUE_FUND, 1000500);
otherAcct.RecordTransaction("D 34208 1000500", Account::VALUE_FUND);
assert(otherAcct.Transfer(acctPtr, Account::VALUE_FUND,
Account::SHORT_TERM_BOND, 450));
std::string transaction1("T 34208 450 " + std::to_string(acctPtr->GetID())
+ std::to_string(Account::SHORT_TERM_BOND));
otherAcct.RecordTransaction(transaction1, Account::VALUE_FUND);
acctPtr-> RecordTransaction(transaction1, Account::SHORT_TERM_BOND);
assert(!otherAcct.Transfer(acctPtr, Account::MONEY_MARKET,
Account::SHORT_TERM_BOND, 4000000));
std::string transaction2("T 34200 40000000 " +
std::to_string(acctPtr->GetID()) +
std::to_string(Account::SHORT_TERM_BOND));
otherAcct.RecordFailedTransaction(transaction2, Account::MONEY_MARKET);
acctPtr-> RecordFailedTransaction(transaction2, Account::SHORT_TERM_BOND);
otherAcct.DisplayHistory();
acctPtr->DisplayHistory();
// Test DisplayBalance
acctPtr->DisplayBalances();
otherAcct.DisplayBalances();
}
// Run Account Tests
void RunAccountTests() {
Account acct("Juan Arias", 2569);
// Test Deposit
std::cout << "-----Testing Deposit------" << std::endl;
TestDeposit(&acct);
// Test DisplayHistory
acct.DisplayHistory(Account::CAPITAL_VALUE_FUND);
acct.DisplayHistory();
std::cout << std::endl;
// Test Transfer
std::cout << "-----Testing Transfer------" << std::endl;
TestTransfer(&acct, Account::CAPITAL_VALUE_FUND, Account::INDEX_FUND_500,
50);
TestTransfer(&acct, Account::GROWTH_EQUITY_FUND,
Account::GROWTH_INDEX_FUND, 950500);
acct.DisplayHistory();
std::cout << std::endl;
// Test Withdraw
std::cout << "-----Testing Withdraw------" << std::endl;
TestWithdraw(&acct);
acct.DisplayHistory(Account::GROWTH_EQUITY_FUND);
acct.DisplayHistory();
std::cout << std::endl;
// Test Transfer with other Account
std::cout << "-----Testing Transfer with two Accounts------" << std::endl;
TestTransfer2Accounts(&acct);
}
// Test Insert, check for inserting duplicate Ids
void TestInsert(BSTree* treePtr) {
assert(treePtr->Insert(new Account("Kobe Bryant", 5824)));
assert(treePtr->Insert(new Account("LeBron James", 3600)));
assert(treePtr->Insert(new Account("Allen Iverson", 2901)));
assert(treePtr->Insert(new Account("Dwayne Wade", 7549)));
assert(treePtr->Insert(new Account("Tim Duncan", 6491)));
assert(treePtr->Insert(new Account("Michael Jordan", 2360)));
// Test Insert with same Id number, should be false and print error
assert(!treePtr->Insert(new Account("James Harden", 5824)));
assert(!treePtr->Insert(new Account("Chris Paul", 2360)));
}
// Test Retrieve, check for Accounts with ids not stored
void TestRetrieve(BSTree* treePtr) {
Account* kobePtr,
* lebronPtr,
* aiPtr,
* dWadePtr,
* duncanPtr,
* mjPtr;
assert(treePtr->Retrieve(5824, kobePtr) && kobePtr != nullptr);
assert(treePtr->Retrieve(3600, lebronPtr) && lebronPtr != nullptr);
assert(treePtr->Retrieve(2901, aiPtr) && aiPtr != nullptr);
assert(treePtr->Retrieve(7549, dWadePtr) && dWadePtr != nullptr);
assert(treePtr->Retrieve(6491, duncanPtr) && duncanPtr != nullptr);
assert(treePtr->Retrieve(2360, mjPtr) && mjPtr != nullptr);
// Test for ids not stored
Account* beardPtr,
* cp3Ptr;
assert(!treePtr->Retrieve(5825, beardPtr) && beardPtr == nullptr);
assert(!treePtr->Retrieve(2359, cp3Ptr) && cp3Ptr == nullptr);
}
// Run BSTree tests
void RunBSTreeTests() {
BSTree tree;
// Test Insert
TestInsert(&tree);
// Test Retrieve
TestRetrieve(&tree);
// Test Display, should visit Nodes in sorted order (inorder traversal)
tree.Display();
// Test isEmpty & Empty
assert(!tree.isEmpty());
tree.Empty();
assert(tree.isEmpty());
}
// Run all tests for each class
void RunAllTests() {
std::cout <<"-------------------Running Account Tests------------------\n";
RunAccountTests();
std::cout << std::endl << std::endl <<
"------------------Running BSTree Tests-------------------\n";
RunBSTreeTests();
}
// Tests classes
int main() {
RunAllTests();
return 0;
}