Skip to content

Commit d48c46c

Browse files
authored
Fixed bank-account tests (#826)
1 parent 90706bb commit d48c46c

File tree

2 files changed

+105
-58
lines changed

2 files changed

+105
-58
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[983a1528-4ceb-45e5-8257-8ce01aceb5ed]
13+
description = "Newly opened account has zero balance"
14+
15+
[e88d4ec3-c6bf-4752-8e59-5046c44e3ba7]
16+
description = "Single deposit"
17+
18+
[3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d]
19+
description = "Multiple deposits"
20+
21+
[08f1af07-27ae-4b38-aa19-770bde558064]
22+
description = "Withdraw once"
23+
24+
[6f6d242f-8c31-4ac6-8995-a90d42cad59f]
25+
description = "Withdraw twice"
26+
27+
[45161c94-a094-4c77-9cec-998b70429bda]
28+
description = "Can do multiple operations sequentially"
29+
30+
[f9facfaa-d824-486e-8381-48832c4bbffd]
31+
description = "Cannot check balance of closed account"
32+
33+
[7a65ba52-e35c-4fd2-8159-bda2bde6e59c]
34+
description = "Cannot deposit into closed account"
35+
36+
[a0a1835d-faae-4ad4-a6f3-1fcc2121380b]
37+
description = "Cannot deposit into unopened account"
38+
39+
[570dfaa5-0532-4c1f-a7d3-0f65c3265608]
40+
description = "Cannot withdraw from closed account"
41+
42+
[c396d233-1c49-4272-98dc-7f502dbb9470]
43+
description = "Cannot close an account that was not opened"
44+
45+
[c06f534f-bdc2-4a02-a388-1063400684de]
46+
description = "Cannot open an already opened account"
47+
48+
[0722d404-6116-4f92-ba3b-da7f88f1669c]
49+
description = "Reopened account does not retain balance"
50+
51+
[ec42245f-9361-4341-8231-a22e8d19c52f]
52+
description = "Cannot withdraw more than deposited"
53+
54+
[4f381ef8-10ef-4507-8e1d-0631ecc8ee72]
55+
description = "Cannot withdraw negative"
56+
57+
[d45df9ea-1db0-47f3-b18c-d365db49d938]
58+
description = "Cannot deposit negative"
59+
60+
[ba0c1e0b-0f00-416f-8097-a7dfc97871ff]
61+
description = "Can handle concurrent transactions"

exercises/practice/bank-account/bank_account_test.cpp

+44-58
Original file line numberDiff line numberDiff line change
@@ -8,164 +8,150 @@
88
#include <string>
99
#include <thread>
1010

11-
TEST_CASE("newly_opened_account_has_zero_balance")
12-
{
11+
TEST_CASE("Newly opened account has zero balance", "[983a1528-4ceb-45e5-8257-8ce01aceb5ed]") {
1312
Bankaccount::Bankaccount account{};
1413
account.open();
1514
REQUIRE(account.balance() == 0);
1615
}
1716

1817
#if defined(EXERCISM_RUN_ALL_TESTS)
19-
TEST_CASE("deposit_money_increases_balance")
20-
{
18+
TEST_CASE("Single deposit", "[e88d4ec3-c6bf-4752-8e59-5046c44e3ba7]") {
2119
Bankaccount::Bankaccount account{};
2220
account.open();
2321
account.deposit(100);
2422
REQUIRE(account.balance() == 100);
2523
}
2624

27-
TEST_CASE("deposit_money_sequentially_increases_balance")
28-
{
25+
TEST_CASE("Multiple deposits", "[3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d]") {
2926
Bankaccount::Bankaccount account{};
3027
account.open();
3128
account.deposit(100);
3229
account.deposit(50);
3330
REQUIRE(account.balance() == 150);
3431
}
3532

36-
TEST_CASE("withdraw_money_decreases_balance")
37-
{
33+
TEST_CASE("Withdraw once", "[08f1af07-27ae-4b38-aa19-770bde558064]") {
3834
Bankaccount::Bankaccount account{};
3935
account.open();
4036
account.deposit(100);
41-
account.withdraw(50);
42-
REQUIRE(account.balance() == 50);
37+
account.withdraw(75);
38+
REQUIRE(account.balance() == 25);
4339
}
4440

45-
TEST_CASE("withdraw_money_sequentially_decreases_balance")
46-
{
41+
TEST_CASE("Withdraw twice", "[6f6d242f-8c31-4ac6-8995-a90d42cad59f]") {
4742
Bankaccount::Bankaccount account{};
4843
account.open();
4944
account.deposit(100);
50-
account.withdraw(50);
51-
account.withdraw(30);
52-
REQUIRE(account.balance() == 20);
45+
account.withdraw(80);
46+
account.withdraw(20);
47+
REQUIRE(account.balance() == 0);
5348
}
5449

55-
TEST_CASE("checking_balance_of_not_opened_account_throws_error")
56-
{
50+
TEST_CASE("Can do multiple operations sequentially", "[45161c94-a094-4c77-9cec-998b70429bda]") {
5751
Bankaccount::Bankaccount account{};
58-
REQUIRE_THROWS_AS(account.balance(), std::runtime_error);
52+
account.open();
53+
account.deposit(100);
54+
account.deposit(110);
55+
account.withdraw(200);
56+
account.deposit(60);
57+
account.withdraw(50);
58+
REQUIRE(account.balance() == 20);
5959
}
6060

61-
TEST_CASE("checking_balance_of_a_closed_account_throws_error")
62-
{
61+
TEST_CASE("annot check balance of closed account", "[f9facfaa-d824-486e-8381-48832c4bbffd]") {
6362
Bankaccount::Bankaccount account{};
6463
account.open();
6564
account.close();
6665

6766
REQUIRE_THROWS_AS(account.balance(), std::runtime_error);
6867
}
6968

70-
TEST_CASE("deposit_into_closed_account_throws_error")
71-
{
69+
TEST_CASE("Cannot deposit into closed account", "[7a65ba52-e35c-4fd2-8159-bda2bde6e59c]") {
7270
Bankaccount::Bankaccount account{};
7371
account.open();
7472
account.close();
7573

7674
REQUIRE_THROWS_AS(account.deposit(50), std::runtime_error);
7775
}
7876

79-
TEST_CASE("withdraw_from_closed_account_throws_error")
80-
{
77+
TEST_CASE("Cannot deposit into unopened account", "[a0a1835d-faae-4ad4-a6f3-1fcc2121380b]") {
78+
Bankaccount::Bankaccount account{};
79+
REQUIRE_THROWS_AS(account.deposit(50), std::runtime_error);
80+
}
81+
82+
TEST_CASE("Cannot withdraw from closed account", "[570dfaa5-0532-4c1f-a7d3-0f65c3265608]") {
8183
Bankaccount::Bankaccount account{};
8284
account.open();
8385
account.close();
8486

8587
REQUIRE_THROWS_AS(account.withdraw(50), std::runtime_error);
8688
}
8789

88-
TEST_CASE("close_an_unopened_account_throws_error")
89-
{
90+
TEST_CASE("Cannot close an account that was not opened", "[c396d233-1c49-4272-98dc-7f502dbb9470]") {
9091
Bankaccount::Bankaccount account;
9192

9293
REQUIRE_THROWS_AS(account.close(), std::runtime_error);
9394
}
9495

95-
TEST_CASE("close_an_already_closed_account_throws_error")
96-
{
97-
Bankaccount::Bankaccount account;
98-
account.open();
99-
account.close();
100-
101-
REQUIRE_THROWS_AS(account.close(), std::runtime_error);
102-
}
103-
104-
TEST_CASE("open_an_already_opened_account_throws_error")
105-
{
96+
TEST_CASE("Cannot open an already opened account", "[c06f534f-bdc2-4a02-a388-1063400684de]") {
10697
Bankaccount::Bankaccount account;
10798
account.open();
10899

109100
REQUIRE_THROWS_AS(account.open(), std::runtime_error);
110101
}
111102

112-
TEST_CASE("reopened_account_does_not_retain_balance")
113-
{
103+
104+
TEST_CASE("Reopened account does not retain balance", "[0722d404-6116-4f92-ba3b-da7f88f1669c]") {
114105
Bankaccount::Bankaccount account;
115106
account.open();
116-
account.deposit(100);
107+
account.deposit(50);
117108
account.close();
118109
account.open();
119110

120111
REQUIRE(account.balance() == 0);
121112
}
122113

123-
TEST_CASE("cannot_withdraw_more_than_deposited")
124-
{
114+
TEST_CASE("Cannot withdraw more than deposited", "[ec42245f-9361-4341-8231-a22e8d19c52f]") {
125115
Bankaccount::Bankaccount account;
126116
account.open();
127-
account.deposit(100);
117+
account.deposit(25);
128118

129-
REQUIRE_THROWS_AS(account.withdraw(150), std::runtime_error);
119+
REQUIRE_THROWS_AS(account.withdraw(50), std::runtime_error);
130120
}
131121

132-
TEST_CASE("deposit_negativ_amount_throws_error")
133-
{
122+
TEST_CASE("Cannot withdraw negative", "[4f381ef8-10ef-4507-8e1d-0631ecc8ee72]") {
134123
Bankaccount::Bankaccount account;
135124
account.open();
136-
137-
REQUIRE_THROWS_AS(account.deposit(-100), std::runtime_error);
125+
account.deposit(100);
126+
REQUIRE_THROWS_AS(account.withdraw(-50), std::runtime_error);
138127
}
139128

140-
TEST_CASE("withdraw_negativ_amount_throws_error")
141-
{
129+
TEST_CASE("Cannot deposit negative", "[d45df9ea-1db0-47f3-b18c-d365db49d938]") {
142130
Bankaccount::Bankaccount account;
143131
account.open();
144132

145-
REQUIRE_THROWS_AS(account.withdraw(-100), std::runtime_error);
133+
REQUIRE_THROWS_AS(account.deposit(-50), std::runtime_error);
146134
}
147135

148-
TEST_CASE("can_handle_concurrent_transactions")
149-
{
136+
TEST_CASE("Can handle concurrent transactions", "[ba0c1e0b-0f00-416f-8097-a7dfc97871ff]") {
150137
Bankaccount::Bankaccount account;
151138
account.open();
152-
account.deposit(1000);
153139

154140
std::vector<std::thread> vec_of_threads;
155141

156142
for (int i = 0; i < 1000; ++i) {
157143
vec_of_threads.push_back(std::thread([&]() {
158144
using namespace std::chrono_literals;
159-
account.deposit(5);
145+
account.deposit(1);
160146
std::this_thread::sleep_for(5ms);
161-
account.withdraw(5);
147+
account.withdraw(1);
162148
}));
163149
}
164150

165151
for (auto& th : vec_of_threads) {
166152
th.join();
167153
}
168154

169-
REQUIRE(account.balance() == 1000);
155+
REQUIRE(account.balance() == 0);
170156
}
171-
#endif
157+
#endif

0 commit comments

Comments
 (0)