forked from RezaSi/go-interview-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution-template_test.go
More file actions
383 lines (341 loc) · 9.26 KB
/
solution-template_test.go
File metadata and controls
383 lines (341 loc) · 9.26 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package challenge7
import (
"fmt"
"strings"
"sync"
"testing"
)
func TestNewBankAccount(t *testing.T) {
testCases := []struct {
name string
id string
owner string
initialBalance float64
minBalance float64
shouldError bool
errorType string
}{
{
name: "Valid account creation",
id: "ACC001",
owner: "Alice",
initialBalance: 1000.0,
minBalance: 100.0,
shouldError: false,
},
{
name: "Empty ID",
id: "",
owner: "Alice",
initialBalance: 1000.0,
minBalance: 100.0,
shouldError: true,
errorType: "AccountError",
},
{
name: "Empty owner",
id: "ACC001",
owner: "",
initialBalance: 1000.0,
minBalance: 100.0,
shouldError: true,
errorType: "AccountError",
},
{
name: "Negative initial balance",
id: "ACC001",
owner: "Alice",
initialBalance: -100.0,
minBalance: 100.0,
shouldError: true,
errorType: "NegativeAmountError",
},
{
name: "Negative min balance",
id: "ACC001",
owner: "Alice",
initialBalance: 1000.0,
minBalance: -100.0,
shouldError: true,
errorType: "NegativeAmountError",
},
{
name: "Initial balance less than min balance",
id: "ACC001",
owner: "Alice",
initialBalance: 50.0,
minBalance: 100.0,
shouldError: true,
errorType: "InsufficientFundsError",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
account, err := NewBankAccount(tc.id, tc.owner, tc.initialBalance, tc.minBalance)
if tc.shouldError {
if err == nil {
t.Errorf("Expected error but got none")
return
}
if !strings.Contains(fmt.Sprintf("%T", err), tc.errorType) {
t.Errorf("Expected error of type %s but got %T", tc.errorType, err)
}
} else {
if err != nil {
t.Errorf("Did not expect error but got: %v", err)
return
}
if account == nil {
t.Errorf("Expected account to be created but it was nil")
return
}
if account.ID != tc.id {
t.Errorf("Expected ID %s but got %s", tc.id, account.ID)
}
if account.Owner != tc.owner {
t.Errorf("Expected Owner %s but got %s", tc.owner, account.Owner)
}
if account.Balance != tc.initialBalance {
t.Errorf("Expected Balance %.2f but got %.2f", tc.initialBalance, account.Balance)
}
if account.MinBalance != tc.minBalance {
t.Errorf("Expected MinBalance %.2f but got %.2f", tc.minBalance, account.MinBalance)
}
}
})
}
}
func TestDeposit(t *testing.T) {
testCases := []struct {
name string
amount float64
shouldError bool
errorType string
}{
{
name: "Valid deposit",
amount: 500.0,
shouldError: false,
},
{
name: "Zero deposit",
amount: 0.0,
shouldError: false,
},
{
name: "Negative deposit",
amount: -100.0,
shouldError: true,
errorType: "NegativeAmountError",
},
{
name: "Exceeds limit deposit",
amount: MaxTransactionAmount + 1.0,
shouldError: true,
errorType: "ExceedsLimitError",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
account, _ := NewBankAccount("TEST", "TestUser", 1000.0, 100.0)
initialBalance := account.Balance
err := account.Deposit(tc.amount)
if tc.shouldError {
if err == nil {
t.Errorf("Expected error but got none")
return
}
if !strings.Contains(fmt.Sprintf("%T", err), tc.errorType) {
t.Errorf("Expected error of type %s but got %T", tc.errorType, err)
}
// Balance should not change on error
if account.Balance != initialBalance {
t.Errorf("Expected balance to remain %.2f but got %.2f", initialBalance, account.Balance)
}
} else {
if err != nil {
t.Errorf("Did not expect error but got: %v", err)
return
}
// Check if balance increased correctly
expectedBalance := initialBalance + tc.amount
if account.Balance != expectedBalance {
t.Errorf("Expected balance %.2f but got %.2f", expectedBalance, account.Balance)
}
}
})
}
}
func TestWithdraw(t *testing.T) {
testCases := []struct {
name string
amount float64
shouldError bool
errorType string
}{
{
name: "Valid withdrawal",
amount: 200.0,
shouldError: false,
},
{
name: "Zero withdrawal",
amount: 0.0,
shouldError: false,
},
{
name: "Negative withdrawal",
amount: -100.0,
shouldError: true,
errorType: "NegativeAmountError",
},
{
name: "Exceeds limit withdrawal",
amount: MaxTransactionAmount + 1.0,
shouldError: true,
errorType: "ExceedsLimitError",
},
{
name: "Insufficient funds withdrawal",
amount: 950.0, // Leaves 50, which is below min balance of 100
shouldError: true,
errorType: "InsufficientFundsError",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
account, _ := NewBankAccount("TEST", "TestUser", 1000.0, 100.0)
initialBalance := account.Balance
err := account.Withdraw(tc.amount)
if tc.shouldError {
if err == nil {
t.Errorf("Expected error but got none")
return
}
if !strings.Contains(fmt.Sprintf("%T", err), tc.errorType) {
t.Errorf("Expected error of type %s but got %T", tc.errorType, err)
}
// Balance should not change on error
if account.Balance != initialBalance {
t.Errorf("Expected balance to remain %.2f but got %.2f", initialBalance, account.Balance)
}
} else {
if err != nil {
t.Errorf("Did not expect error but got: %v", err)
return
}
// Check if balance decreased correctly
expectedBalance := initialBalance - tc.amount
if account.Balance != expectedBalance {
t.Errorf("Expected balance %.2f but got %.2f", expectedBalance, account.Balance)
}
}
})
}
}
func TestTransfer(t *testing.T) {
testCases := []struct {
name string
amount float64
shouldError bool
errorType string
}{
{
name: "Valid transfer",
amount: 300.0,
shouldError: false,
},
{
name: "Zero transfer",
amount: 0.0,
shouldError: false,
},
{
name: "Negative transfer",
amount: -100.0,
shouldError: true,
errorType: "NegativeAmountError",
},
{
name: "Exceeds limit transfer",
amount: MaxTransactionAmount + 1.0,
shouldError: true,
errorType: "ExceedsLimitError",
},
{
name: "Insufficient funds transfer",
amount: 950.0, // Leaves 50, which is below min balance of 100
shouldError: true,
errorType: "InsufficientFundsError",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
source, _ := NewBankAccount("SRC", "Source", 1000.0, 100.0)
target, _ := NewBankAccount("TGT", "Target", 500.0, 50.0)
sourceInitialBalance := source.Balance
targetInitialBalance := target.Balance
err := source.Transfer(tc.amount, target)
if tc.shouldError {
if err == nil {
t.Errorf("Expected error but got none")
return
}
if !strings.Contains(fmt.Sprintf("%T", err), tc.errorType) {
t.Errorf("Expected error of type %s but got %T", tc.errorType, err)
}
// Balances should not change on error
if source.Balance != sourceInitialBalance {
t.Errorf("Expected source balance to remain %.2f but got %.2f", sourceInitialBalance, source.Balance)
}
if target.Balance != targetInitialBalance {
t.Errorf("Expected target balance to remain %.2f but got %.2f", targetInitialBalance, target.Balance)
}
} else {
if err != nil {
t.Errorf("Did not expect error but got: %v", err)
return
}
// Check if balances changed correctly
expectedSourceBalance := sourceInitialBalance - tc.amount
expectedTargetBalance := targetInitialBalance + tc.amount
if source.Balance != expectedSourceBalance {
t.Errorf("Expected source balance %.2f but got %.2f", expectedSourceBalance, source.Balance)
}
if target.Balance != expectedTargetBalance {
t.Errorf("Expected target balance %.2f but got %.2f", expectedTargetBalance, target.Balance)
}
}
})
}
}
func TestConcurrency(t *testing.T) {
account, _ := NewBankAccount("CONC", "Concurrency Test", 1000.0, 100.0)
const numOperations = 100
var wg sync.WaitGroup
wg.Add(numOperations * 2) // Add and withdraw operations
// Perform concurrent deposits
for i := 0; i < numOperations; i++ {
go func() {
defer wg.Done()
_ = account.Deposit(10.0)
}()
}
// Perform concurrent withdrawals
for i := 0; i < numOperations; i++ {
go func() {
defer wg.Done()
_ = account.Withdraw(5.0)
}()
}
wg.Wait()
// After all operations, the final balance should be:
// Initial: 1000
// 100 deposits of 10: +1000
// 100 withdrawals of 5: -500
// Expected: 1500
expectedBalance := 1500.0
if account.Balance != expectedBalance {
t.Errorf("Expected balance after concurrent operations to be %.2f but got %.2f", expectedBalance, account.Balance)
}
}