Пример #1
0
 public void DepositIntegrationTest()
 {
     ba = new BankAccount2(new ConsoleLog())
     {
         Balance = 100
     };
     ba.Deposit(100);
     Assert.That(ba.Balance, Is.EqualTo(200));
 }
Пример #2
0
        public void DepositUnitTestWithStub()
        {
            var log = new NullLogStube(true);

            ba = new BankAccount2(log)
            {
                Balance = 100
            };
            ba.Deposit(100);
            Assert.That(ba.Balance, Is.EqualTo(200));
        }
Пример #3
0
        public void DepositUnitTestWithFake()
        {
            var log = new NullLog();

            ba = new BankAccount2(new NullLog())
            {
                Balance = 100
            };
            ba.Deposit(100);
            Assert.That(ba.Balance, Is.EqualTo(200));
        }
Пример #4
0
 public bool TransferTo(double amount, BankAccount2 another)
 {
     if (Withdraw(amount))
     {
         another.Deposit(amount);
         return(true);
     }
     else
     {
         Console.Error.WriteLine("TransferTo for {0} is unsuccessful", AccountName);
         return(false);
     }
 }
Пример #5
0
        public void DepositTestWithMock()
        {
            var log = new LogMock(true);

            bawm = new BankAccount2(log)
            {
                Balance = 100
            };
            bawm.Deposit(100);

            Assert.Multiple(() =>
            {
                Assert.That(bawm.Balance, Is.EqualTo(200));
                Assert.That(log.MethodCallCount[nameof(LogMock.Write)], Is.EqualTo(1));
            });
        }
Пример #6
0
        static void Main(string[] args)
        {
            //sample 3
            var ba = new BankAccount2(100);
            var m1 = ba.Deposit(50); //150
            var m2 = ba.Deposit(25); //175

            Console.WriteLine(ba);

            ba.Undo(); //150
            ba.Undo(); //100
            Console.WriteLine(ba);

            ba.Redo(); //150
            Console.WriteLine(ba);

            //=====================================================================

            //sample 2
            //var ba = new BankAccount(100);
            //var m1 = ba.Deposit(50); //150
            //var m2 = ba.Deposit(25); //175
            //Console.WriteLine(ba);

            //ba.Restore(m1); //150
            //Console.WriteLine(ba);

            //ba.Restore(m2); //175
            //Console.WriteLine(ba);

            //=====================================================================

            //sample 1
            //var editor = new Editor();
            //editor.Type("this is 1");
            //editor.Type("this is 2");
            //Console.WriteLine(editor.Content);
            //editor.Save();

            //editor.Type("this is 3");
            //Console.WriteLine(editor.Content);

            //editor.Restore();
            //Console.WriteLine(editor.Content);

            Console.ReadKey();
        }
Пример #7
0
        static void Main(string[] args)
        {
            Customer     y = new Customer("Tan Ah Kow", "20, Seaside Road", "XXX20", new DateTime(1989, 10, 11));
            Customer     z = new Customer("Kim Lee Keng", "2, Rich View", "XXX9F", new DateTime(1993, 4, 25));
            BankAccount2 a = new BankAccount2("001-001-001", y, 2000);
            BankAccount2 b = new BankAccount2("001-001-002", z, 5000);

            Console.WriteLine(a.Show());
            Console.WriteLine(b.Show());
            a.Deposit(100);
            Console.WriteLine(a.Show());
            a.Withdraw(200);
            Console.WriteLine(a.Show());
            a.TransferTo(300, b);
            Console.WriteLine(a.Show());
            Console.WriteLine(b.Show());
        }
Пример #8
0
        static void Main(string[] args)
        {
            var tasks0 = new List <Task>();
            var ba0    = new BankAccount2();

            for (int i = 0; i < 10; ++i)
            {
                tasks0.Add(Task.Factory.StartNew(() =>
                {
                    for (int j = 0; j < 1000; ++j)
                    {
                        ba0.Deposit(100);
                    }
                }));
                tasks0.Add(Task.Factory.StartNew(() =>
                {
                    for (int j = 0; j < 1000; ++j)
                    {
                        ba0.Withdraw(100);
                    }
                }));
            }

            Task.WaitAll(tasks0.ToArray());

            Console.WriteLine($"Final balance is {ba0.Balance}.");

            // show interlocked methods here

            // Interlocked.MemoryBarrier is a wrapper for Thread.MemoryBarrier
            // only required on memory systems that have weak memory ordering (e.g., Itanium)
            // prevents the CPU from reordering the instructions such that those before the barrier
            // execute after those after

            Console.WriteLine("All done here.");

            Console.WriteLine();

            #region Mutex
            var tasks = new List <Task>();
            var ba    = new BankAccount3(0);
            var ba2   = new BankAccount3(0);

            // many synchro types deriving from WaitHandle
            // Mutex = mutual exclusion

            // two types of mutexes
            // this is a _local_ mutex
            Mutex mutex  = new Mutex();
            Mutex mutex2 = new Mutex();

            for (int i = 0; i < 10; ++i)
            {
                tasks.Add(Task.Factory.StartNew(() =>
                {
                    for (int j = 0; j < 1000; ++j)
                    {
                        bool haveLock = mutex.WaitOne();
                        try
                        {
                            ba.Deposit(1); // deposit 10000 overall
                        }
                        finally
                        {
                            if (haveLock)
                            {
                                mutex.ReleaseMutex();
                            }
                        }
                    }
                }));
                tasks.Add(Task.Factory.StartNew(() =>
                {
                    for (int j = 0; j < 1000; ++j)
                    {
                        bool haveLock = mutex2.WaitOne();
                        try
                        {
                            ba2.Deposit(1); // deposit 10000
                        }
                        finally
                        {
                            if (haveLock)
                            {
                                mutex2.ReleaseMutex();
                            }
                        }
                    }
                }));

                // transfer needs to lock both accounts
                tasks.Add(Task.Factory.StartNew(() =>
                {
                    for (int j = 0; j < 1000; j++)
                    {
                        bool haveLock = Mutex.WaitAll(new[] { mutex, mutex2 });
                        try
                        {
                            ba.Transfer(ba2, 1); // transfer 10k from ba to ba2
                        }
                        finally
                        {
                            if (haveLock)
                            {
                                mutex.ReleaseMutex();
                                mutex2.ReleaseMutex();
                            }
                        }
                    }
                }));
            }

            Task.WaitAll(tasks.ToArray());

            Console.WriteLine($"Final balance is: ba={ba.Balance}, ba2={ba2.Balance}.");
            #endregion
        }