8
8
#include < string>
9
9
#include < thread>
10
10
11
- TEST_CASE (" newly_opened_account_has_zero_balance" )
12
- {
11
+ TEST_CASE (" Newly opened account has zero balance" , " [983a1528-4ceb-45e5-8257-8ce01aceb5ed]" ) {
13
12
Bankaccount::Bankaccount account{};
14
13
account.open ();
15
14
REQUIRE (account.balance () == 0 );
16
15
}
17
16
18
17
#if defined(EXERCISM_RUN_ALL_TESTS)
19
- TEST_CASE (" deposit_money_increases_balance" )
20
- {
18
+ TEST_CASE (" Single deposit" , " [e88d4ec3-c6bf-4752-8e59-5046c44e3ba7]" ) {
21
19
Bankaccount::Bankaccount account{};
22
20
account.open ();
23
21
account.deposit (100 );
24
22
REQUIRE (account.balance () == 100 );
25
23
}
26
24
27
- TEST_CASE (" deposit_money_sequentially_increases_balance" )
28
- {
25
+ TEST_CASE (" Multiple deposits" , " [3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d]" ) {
29
26
Bankaccount::Bankaccount account{};
30
27
account.open ();
31
28
account.deposit (100 );
32
29
account.deposit (50 );
33
30
REQUIRE (account.balance () == 150 );
34
31
}
35
32
36
- TEST_CASE (" withdraw_money_decreases_balance" )
37
- {
33
+ TEST_CASE (" Withdraw once" , " [08f1af07-27ae-4b38-aa19-770bde558064]" ) {
38
34
Bankaccount::Bankaccount account{};
39
35
account.open ();
40
36
account.deposit (100 );
41
- account.withdraw (50 );
42
- REQUIRE (account.balance () == 50 );
37
+ account.withdraw (75 );
38
+ REQUIRE (account.balance () == 25 );
43
39
}
44
40
45
- TEST_CASE (" withdraw_money_sequentially_decreases_balance" )
46
- {
41
+ TEST_CASE (" Withdraw twice" , " [6f6d242f-8c31-4ac6-8995-a90d42cad59f]" ) {
47
42
Bankaccount::Bankaccount account{};
48
43
account.open ();
49
44
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 );
53
48
}
54
49
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]" ) {
57
51
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 );
59
59
}
60
60
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]" ) {
63
62
Bankaccount::Bankaccount account{};
64
63
account.open ();
65
64
account.close ();
66
65
67
66
REQUIRE_THROWS_AS (account.balance (), std::runtime_error);
68
67
}
69
68
70
- TEST_CASE (" deposit_into_closed_account_throws_error" )
71
- {
69
+ TEST_CASE (" Cannot deposit into closed account" , " [7a65ba52-e35c-4fd2-8159-bda2bde6e59c]" ) {
72
70
Bankaccount::Bankaccount account{};
73
71
account.open ();
74
72
account.close ();
75
73
76
74
REQUIRE_THROWS_AS (account.deposit (50 ), std::runtime_error);
77
75
}
78
76
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]" ) {
81
83
Bankaccount::Bankaccount account{};
82
84
account.open ();
83
85
account.close ();
84
86
85
87
REQUIRE_THROWS_AS (account.withdraw (50 ), std::runtime_error);
86
88
}
87
89
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]" ) {
90
91
Bankaccount::Bankaccount account;
91
92
92
93
REQUIRE_THROWS_AS (account.close (), std::runtime_error);
93
94
}
94
95
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]" ) {
106
97
Bankaccount::Bankaccount account;
107
98
account.open ();
108
99
109
100
REQUIRE_THROWS_AS (account.open (), std::runtime_error);
110
101
}
111
102
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] " ) {
114
105
Bankaccount::Bankaccount account;
115
106
account.open ();
116
- account.deposit (100 );
107
+ account.deposit (50 );
117
108
account.close ();
118
109
account.open ();
119
110
120
111
REQUIRE (account.balance () == 0 );
121
112
}
122
113
123
- TEST_CASE (" cannot_withdraw_more_than_deposited" )
124
- {
114
+ TEST_CASE (" Cannot withdraw more than deposited" , " [ec42245f-9361-4341-8231-a22e8d19c52f]" ) {
125
115
Bankaccount::Bankaccount account;
126
116
account.open ();
127
- account.deposit (100 );
117
+ account.deposit (25 );
128
118
129
- REQUIRE_THROWS_AS (account.withdraw (150 ), std::runtime_error);
119
+ REQUIRE_THROWS_AS (account.withdraw (50 ), std::runtime_error);
130
120
}
131
121
132
- TEST_CASE (" deposit_negativ_amount_throws_error" )
133
- {
122
+ TEST_CASE (" Cannot withdraw negative" , " [4f381ef8-10ef-4507-8e1d-0631ecc8ee72]" ) {
134
123
Bankaccount::Bankaccount account;
135
124
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);
138
127
}
139
128
140
- TEST_CASE (" withdraw_negativ_amount_throws_error" )
141
- {
129
+ TEST_CASE (" Cannot deposit negative" , " [d45df9ea-1db0-47f3-b18c-d365db49d938]" ) {
142
130
Bankaccount::Bankaccount account;
143
131
account.open ();
144
132
145
- REQUIRE_THROWS_AS (account.withdraw (- 100 ), std::runtime_error);
133
+ REQUIRE_THROWS_AS (account.deposit (- 50 ), std::runtime_error);
146
134
}
147
135
148
- TEST_CASE (" can_handle_concurrent_transactions" )
149
- {
136
+ TEST_CASE (" Can handle concurrent transactions" , " [ba0c1e0b-0f00-416f-8097-a7dfc97871ff]" ) {
150
137
Bankaccount::Bankaccount account;
151
138
account.open ();
152
- account.deposit (1000 );
153
139
154
140
std::vector<std::thread> vec_of_threads;
155
141
156
142
for (int i = 0 ; i < 1000 ; ++i) {
157
143
vec_of_threads.push_back (std::thread ([&]() {
158
144
using namespace std ::chrono_literals;
159
- account.deposit (5 );
145
+ account.deposit (1 );
160
146
std::this_thread::sleep_for (5ms);
161
- account.withdraw (5 );
147
+ account.withdraw (1 );
162
148
}));
163
149
}
164
150
165
151
for (auto & th : vec_of_threads) {
166
152
th.join ();
167
153
}
168
154
169
- REQUIRE (account.balance () == 1000 );
155
+ REQUIRE (account.balance () == 0 );
170
156
}
171
- #endif
157
+ #endif
0 commit comments