Created
December 3, 2014 13:06
-
-
Save MikeMKH/8e5899a2c82d912871fb to your computer and use it in GitHub Desktop.
Coin Changer kata with Map in C# using MS Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace CoinChanger | |
{ | |
[TestClass] | |
public class CoinChangerTests | |
{ | |
[TestMethod] | |
[ExpectedException(typeof(ArgumentException))] | |
public void GivenNoCoins_WhenChangeForIsGiven0_ItMustThrowAnArgumentException() | |
{ | |
var target = new CoinChanger(); | |
target.ChangeFor(0); | |
} | |
[TestMethod] | |
[ExpectedException(typeof(ArgumentException))] | |
public void GivenNoCoins_WhenChangeForIsGiven1_ItMustThrowAnArgumentException() | |
{ | |
var target = new CoinChanger(); | |
target.ChangeFor(1); | |
} | |
[TestMethod] | |
public void GivenPennies_WhenChangeForIsGiven0_ItMustReturn0Pennies() | |
{ | |
var target = new CoinChanger{Coins = new []{1}}; | |
var actual = target.ChangeFor(0); | |
var expected = new[] { 0 }; | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenPennies_WhenChangeForIsGiven1_ItMustReturn1Penny() | |
{ | |
var target = new CoinChanger{Coins = new []{1}}; | |
var actual = target.ChangeFor(1); | |
var expected = new[] {1}; | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenPennies_WhenChangeForIsGiven4_ItMustReturn4Pennies() | |
{ | |
var target = new CoinChanger{Coins = new []{1}}; | |
var actual = target.ChangeFor(4); | |
var expected = new[] {4}; | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenNickels_WhenChangeForIsGiven5_ItMustReturn1Nickel() | |
{ | |
var target = new CoinChanger{Coins = new []{5}}; | |
var actual = target.ChangeFor(5); | |
var expected = new[] {1}; | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenNickelsAndPennies_WhenChangeForIsGiven5_ItMustReturn1NickelAnd0Pennies() | |
{ | |
var target = new CoinChanger{Coins = new []{5, 1}}; | |
var actual = target.ChangeFor(5); | |
var expected = new[] {1, 0}; | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenDimesNickelsAndPennies_WhenChangeForIsGiven17_ItMustReturn1Dime1NickelAnd2Pennies() | |
{ | |
var target = new CoinChanger{Coins = new []{10, 5, 1}}; | |
var actual = target.ChangeFor(17); | |
var expected = new[] {1, 1, 2}; | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenQuartersDimesNickelsAndPennies_WhenChangeForIsGiven99_ItMustReturn3Quarters2Dimes0NickelsAnd4Pennies() | |
{ | |
var target = new CoinChanger{Coins = new []{25, 10, 5, 1}}; | |
var actual = target.ChangeFor(99); | |
var expected = new[] {3, 2, 0, 4}; | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
} | |
public class CoinChanger | |
{ | |
public int[] Coins { get; set; } | |
public int[] ChangeFor(int amount) | |
{ | |
if (Coins == null) throw new ArgumentException("No coins"); | |
return Coins.Select(coin => | |
{ | |
var result = amount/coin; | |
amount %= coin; | |
return result; | |
}).ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment