1
exercism fetch csharp bank-account

BankAccountTest.cs

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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

public class BankAccountTest
{
    [Fact]
    public void Returns_empty_balance_after_opening()
    {
        var account = new BankAccount();
        account.Open();

        Assert.Equal(0, account.Balance);
    }

    [Fact(Skip = "Remove to run test")]
    public void Check_basic_balance()
    {
        var account = new BankAccount();
        account.Open();

        var openingBalance = account.Balance;

        account.UpdateBalance(10);
        var updatedBalance = account.Balance;

        Assert.Equal(0, openingBalance);
        Assert.Equal(10, updatedBalance);
    }

    [Fact(Skip = "Remove to run test")]
    public void Balance_can_increment_and_decrement()
    {
        var account = new BankAccount();
        account.Open();
        var openingBalance = account.Balance;

        account.UpdateBalance(10);
        var addedBalance = account.Balance;

        account.UpdateBalance(-15);
        var subtractedBalance = account.Balance;

        Assert.Equal(0, openingBalance);
        Assert.Equal(10, addedBalance);
        Assert.Equal(-5, subtractedBalance);
    }

    [Fact(Skip = "Remove to run test")]
    public void Closed_account_throws_exception_when_checking_balance()
    {
        var account = new BankAccount();
        account.Open();
        account.Close();

        Assert.Throws<InvalidOperationException>(() => account.Balance);
    }

    [Fact(Skip = "Remove to run test")]
    public void Change_account_balance_from_multiple_threads()
    {
        var account = new BankAccount();
        var tasks = new List<Task>();

        var threads = 500;
        var iterations = 100;

        account.Open();
        for (int i = 0; i < threads; i++)
        {
            tasks.Add(Task.Factory.StartNew(() =>
            {
                for (int j = 0; j < iterations; j++)
                {
                    account.UpdateBalance(1);
                    account.UpdateBalance(-1);
                }
            }));
        }
        Task.WaitAll(tasks.ToArray());

        Assert.Equal(0, account.Balance);
    }
}