-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
230 lines (198 loc) · 10.8 KB
/
Copy pathtests.cpp
File metadata and controls
230 lines (198 loc) · 10.8 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
// Dawid Bartosiak
#define CATCH_CONFIG_MAIN
#include <iostream>
#include "BankIO.h"
#include "catch.hpp"
#include "Bank.h"
#include "Loans.h"
#include "Person.h"
std::string fixed = "fixed";
std::string descending = "descending";
TEST_CASE("Testing constructors correct.") {
std::shared_ptr<Bank> bank = std::make_shared<Bank>(0.21);
std::shared_ptr<BankBranchFixed>bankFixed = std::make_shared<BankBranchFixed>(0.21);
std::shared_ptr<BankBranchDescending>bankDescending = std::make_shared<BankBranchDescending>(0.21);
std::shared_ptr<Person> Dawid = std::make_shared<Person>(bank, 25000, 2000);
Dawid->get_loan(fixed, 10000, 12, 12);
SECTION("Check if Bank created.") {
REQUIRE(bank->get_interest_rate() == 0.21);
REQUIRE(bankFixed->get_interest_rate() == 0.21);
REQUIRE(bankDescending->get_interest_rate() == 0.21);
}
SECTION("Check if Person created.") {
REQUIRE(Dawid->get_living_cost() == 2000);
}
SECTION("Check if Loan created.") {
REQUIRE(Dawid->get_amount_single_left(fixed, 0) == Approx(11173.65));
}
}
TEST_CASE("Testing constructors incorrect.") {
SECTION("Bank wrong constructor.") {
CHECK_THROWS_AS(Bank(-0.21), std::out_of_range);
CHECK_THROWS_AS(Bank(0), std::out_of_range);
CHECK_THROWS_AS(BankBranchFixed(0), std::out_of_range);
CHECK_THROWS_AS(BankBranchFixed(-0.21), std::out_of_range);
CHECK_THROWS_AS(BankBranchDescending(0), std::out_of_range);
CHECK_THROWS_AS(BankBranchDescending(-0.21), std::out_of_range);
}
SECTION("Person wrong constructor.") {
std::shared_ptr<Bank> bank = std::make_shared<Bank>(0.21);
CHECK_THROWS_AS(Person(bank, 10000, 0), std::out_of_range);
}
SECTION("Loan wrong constructors.") {
std::shared_ptr<Bank> bank = std::make_shared<Bank>(0.21);
std::shared_ptr<Person> Dawid = std::make_shared<Person>(bank, 25000, 2000);
CHECK_THROWS_AS(Dawid->get_loan(fixed, -10000, 12, 12), std::out_of_range);
CHECK_THROWS_AS(Dawid->get_loan(fixed, 10000, 0, 12), std::out_of_range);
CHECK_THROWS_AS(Dawid->get_loan(fixed, 10000, 12, 0), std::out_of_range);
CHECK_THROWS_AS(Dawid->get_loan(descending, -10000, 12, 12), std::out_of_range);
CHECK_THROWS_AS(Dawid->get_loan(descending, 10000, 0, 12), std::out_of_range);
CHECK_THROWS_AS(Dawid->get_loan(descending, 10000, 12, 0), std::out_of_range);
CHECK_THROWS_AS(Dawid->get_loan("Name", 10000, 12, 12), std::out_of_range);
CHECK_THROWS_AS(Dawid->get_loan(fixed, 1000000, 12, 12), std::out_of_range);
CHECK_THROWS_AS(Dawid->get_loan(descending, 1000000, 12, 12), std::out_of_range);
}
}
TEST_CASE("Testing methods incorrect.") {
std::shared_ptr<Bank> bank = std::make_shared<Bank>(0.21);
std::shared_ptr<Person> Dawid = std::make_shared<Person>(bank, 25000, 2000);
std::shared_ptr<BankBranchFixed>bankFixed = std::make_shared<BankBranchFixed>(0.21);
std::shared_ptr<BankBranchDescending>bankDescending = std::make_shared<BankBranchDescending>(0.21);
Dawid->get_loan(fixed, 10000, 12, 12);
Dawid->get_loan(descending, 10000, 12, 12);
FixedInstallmentLoan loan1 = Dawid->get_fixed().at(0);
DescendingInstallmentLoan loan2 = Dawid->get_descending().at(0);
SECTION("Bank wrong methods") {
REQUIRE_THROWS_WITH(bank->set_interest_rate(-1), "Interest rate cannot be negative.");
REQUIRE_THROWS_WITH(bank->overpay_descending(loan2, 0.0), "You can't overpay negative amount of money.");
REQUIRE_THROWS_WITH(bank->overpay_fixed(loan1, 0), "You can't overpay negative amount of money.");
REQUIRE_THROWS_WITH(bankDescending->overpay_descending(loan2, 0.0), "You can't overpay negative amount of money.");
REQUIRE_THROWS_WITH(bankFixed->overpay_fixed(loan1, 0), "You can't overpay negative amount of money.");
REQUIRE_THROWS_WITH(bankDescending->overpay_descending(loan2, 0.0), "You can't overpay negative amount of money.");
REQUIRE_THROWS_WITH(bankFixed->overpay_fixed(loan1, 0), "You can't overpay negative amount of money.");
REQUIRE_THROWS_WITH(bank->give_loan_fixed(Dawid, -1000, 12, 12, Dawid->get_creditworthiness()), "Any of loan parameter cannot be negative.");
REQUIRE_THROWS_WITH(bank->give_loan_descending(Dawid, -1000, 12, 12, Dawid->get_creditworthiness()), "Any of loan parameter cannot be negative.");
REQUIRE_THROWS_WITH(bankFixed->give_loan(bank, Dawid, -1000, 12, 12, Dawid->get_creditworthiness()), "Any of loan parameter cannot be negative.");
REQUIRE_THROWS_WITH(bankDescending->give_loan(bank, Dawid, -1000, 12, 12, Dawid->get_creditworthiness()), "Any of loan parameter cannot be negative.");
}
SECTION("Person wrong methods.") {
REQUIRE_THROWS_WITH(Dawid->set_living_cost(-0), "Living cost can't be negative or equal 0.");
REQUIRE_THROWS_WITH(Dawid->overpay(fixed, 0, -1), "You can't overpay negative amount of money.");
REQUIRE_THROWS_WITH(Dawid->overpay(fixed, 1, 1), "Invalid vector subscript.");
REQUIRE_THROWS_WITH(Dawid->overpay("Name", 0, 1), "Incorrect loan type.");
}
SECTION("Loan wrong methods.") {
REQUIRE_THROWS_WITH(bank->give_loan_fixed(Dawid, 10000, 1, 1, 12.0), "Your creditworthiness is too low. Try more rates or lower loan.");
REQUIRE_THROWS_WITH(bank->give_loan_descending(Dawid, 10000, 1, 1, 12.0), "Your creditworthiness is too low. Try more rates or lower loan.");
}
}
TEST_CASE("Testing methods correct.") {
std::shared_ptr<Bank> bank = std::make_shared<Bank>(0.21);
std::shared_ptr<Person> Dawid = std::make_shared<Person>(bank, 25000, 2000);
Dawid->get_loan(fixed, 10000, 12, 12);
Dawid->get_loan(descending, 10000, 12, 12);
FixedInstallmentLoan loan1 = Dawid->get_fixed().at(0);
DescendingInstallmentLoan loan2 = Dawid->get_descending().at(0);
SECTION("Bank correct methods") {
bank->set_interest_rate(1);
REQUIRE(bank->get_interest_rate() == 1);
bank->set_interest_rate(0.21);
double diff1 = loan1.get_amount_left() - loan1.get_interest();
double diff2 = loan2.get_amount_left() - loan2.get_interest();
bank->overpay_fixed(loan1, loan1.get_interest());
bank->overpay_descending(loan2, loan2.get_interest());
REQUIRE(loan1.get_amount_left() == Approx(diff1));
REQUIRE(loan2.get_amount_left() == Approx(diff2));
FixedInstallmentLoan loan3 = bank->give_loan_fixed(Dawid, 10, 1, 1, Dawid->get_creditworthiness());
DescendingInstallmentLoan loan4 =bank->give_loan_descending(Dawid, 10, 1, 1, Dawid->get_creditworthiness());
REQUIRE(loan3.get_rates_amount() == 1);
REQUIRE(loan4.get_rates_amount() == 1);
}
SECTION("Person correct methods.") {
REQUIRE(Dawid->get_fixed().size() == 1);
REQUIRE(Dawid->get_descending().size() == 1);
Dawid->get_loan(fixed, 10, 1, 1);
Dawid->get_loan(descending, 10, 1, 1);
REQUIRE(Dawid->get_fixed().size() == 2);
REQUIRE(Dawid->get_descending().size() == 2);
Dawid->set_income(100000);
Dawid->set_living_cost(1000);
REQUIRE(Dawid->get_income() == 100000);
REQUIRE(Dawid->get_living_cost() == 1000);
Dawid->overpay(fixed, 1, 1000000);
REQUIRE(Dawid->get_fixed().size() == 1);
Dawid->overpay(fixed, 0, Dawid->get_interest_single(fixed, 0));
REQUIRE(Dawid->get_rates_amount(fixed, 0) == 11);
Dawid->overpay(descending, 1, 1000000);
REQUIRE(Dawid->get_descending().size() == 1);
Dawid->overpay(descending, 0, Dawid->get_interest_single(descending, 0));
REQUIRE(Dawid->get_rates_amount(descending, 0) == 11);
}
SECTION("Loan correct methods.") {
REQUIRE(Dawid->get_amount_single_left(fixed, 0) == Approx(11173.65));
REQUIRE(Dawid->get_amount_single_left(descending, 0) == Approx(11137.5));
bank->set_interest_rate(0.28);
REQUIRE(Dawid->get_amount_single_left(fixed, 0) == Approx(11580.71));
REQUIRE(Dawid->get_amount_single_left(descending, 0) == Approx(11516.66));
}
}
TEST_CASE("Operators overloads") {
std::shared_ptr<Bank> bank = std::make_shared<Bank>(0.21);
std::shared_ptr<Person> Dawid = std::make_shared<Person>(bank, 25000, 2000);
FixedInstallmentLoan loan1 = bank->create_loan_fixed(1000, 12, 12);
DescendingInstallmentLoan loan2 = bank->create_loan_descending(1000, 12, 12);
FixedInstallmentLoan loan3 = bank->create_loan_fixed(1600, 12, 12);
DescendingInstallmentLoan loan4 = bank->create_loan_descending(1500, 12, 12);
SECTION("Person operator") {
*Dawid + loan1;
REQUIRE(Dawid->get_creditworthiness() < 11500);
*Dawid + loan2;
REQUIRE(Dawid->get_creditworthiness() < 11400);
}
SECTION("Loans operators") {
REQUIRE((loan1 < loan2) == false);
REQUIRE((loan1 > loan2) == true);
REQUIRE((loan1 < loan3) == true);
REQUIRE((loan2 < loan4) == true);
}
}
TEST_CASE("Input/output with file") {
SECTION("Shared ptr") {
std::shared_ptr<Bank> bank = std::make_shared<Bank>(0.21);
std::shared_ptr<Person> Dawid = std::make_shared<Person>(bank, 25000, 2000);
Dawid->get_loan(fixed, 10000, 12, 12);
Dawid->get_loan(descending, 10000, 12, 12);
BankIO::save_file_one_person(bank, Dawid);
bank->set_interest_rate(0.001);
Dawid->overpay(fixed, 0, 100000);
Dawid->overpay(descending, 0, 100000);
Dawid->set_income(0);
Dawid->set_living_cost(1);
BankIO::load_file_single_person(bank, Dawid);
REQUIRE(bank->get_interest_rate() == 0.21);
REQUIRE(Dawid->get_living_cost() == 2000);
REQUIRE(Dawid->get_income() == 25000);
REQUIRE(Dawid->get_loan_costs_total() > 20000);
REQUIRE(Dawid->get_creditworthiness() < 11500);
REQUIRE(Dawid->get_loan_costs_single(fixed, 0) == Dawid->get_loan_costs_total());
}
SECTION("Classic referece - ptr dereferance") {
std::shared_ptr<Bank> bank = std::make_shared<Bank>(0.21);
std::shared_ptr<Person> Dawid = std::make_shared<Person>(bank, 25000, 2000);
Dawid->get_loan(fixed, 10000, 12, 12);
Dawid->get_loan(descending, 10000, 12, 12);
BankIO::save_file_one_person(*bank, *Dawid);
bank->set_interest_rate(0.001);
Dawid->overpay(fixed, 0, 100000);
Dawid->overpay(descending, 0, 100000);
Dawid->set_income(0);
Dawid->set_living_cost(1);
BankIO::load_file_single_person(*bank, *Dawid);
REQUIRE(bank->get_interest_rate() == 0.21);
REQUIRE(Dawid->get_living_cost() == 2000);
REQUIRE(Dawid->get_income() == 25000);
REQUIRE(Dawid->get_loan_costs_total() > 20000);
REQUIRE(Dawid->get_creditworthiness() < 11500);
REQUIRE(Dawid->get_loan_costs_single(fixed, 0) == Dawid->get_loan_costs_total());
}
}