public void TestReservoirePutWaterZero() { // Arrange int zeroPut = 0; Reservoire utReservoire = new Reservoire(); // Act try { utReservoire.PutWater(zeroPut); } // Assert catch (ArgumentOutOfRangeException ex) { CaughtEx = true; Assert.AreEqual("amount", ex.ParamName, "ArgumentOutOfRangeException didn't report 'amount' parameter"); } catch (Exception ex) { Assert.Fail("PutWater threw a different Exception (" + ex.GetType().Name + ") instead of ArgumentOutOfRangeException when putting amount == 0"); } Assert.IsTrue(CaughtEx, "PutWater did not throw ArgumentOutOfRangeException when putting 0 water"); }
public void TestReservoireBasicPut() { // Arrange int numAmounts = Rand.Next(5000, 10000), iterations = 0; int[] amounts = new int[numAmounts], totals = new int[numAmounts]; int amount = 0; long longTotal = 0; for (int a = 0; a < numAmounts; a++) { amount = Rand.Next(1, 30000); longTotal += amount; if ((longTotal >> 32) > 0) { iterations = a; break; } amounts[a] = amount; totals[a] = (int)longTotal; } if (iterations == 0) { iterations = numAmounts; amount = Rand.Next(int.MaxValue - totals[totals.Length - 1] + 2, int.MaxValue - 1); longTotal += amount; } Reservoire utReservoire = new Reservoire(); for (int i = 0; i < iterations; i++) { // Act utReservoire.PutWater(amounts[i]); // Assert Assert.AreEqual(totals[i], utReservoire.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + iterations + "]"); } try { // Act utReservoire.PutWater(amount); } // Assert catch (ArgumentOutOfRangeException ex) { CaughtEx = true; Assert.AreEqual("amount", ex.ParamName, "PutWater ArgumentOutOfRangeException didn't report 'amount' parameter"); } catch (Exception ex) { Assert.Fail("PutWater threw a different Exception (" + ex.GetType().Name + ") instead of ArgumentOutOfRangeException when VolumeThrownOut == " + totals[iterations - 1] + " && put amount == " + amount); } Assert.IsTrue(CaughtEx, "PutWater did not throw ArgumentOutOfRangeException when VolumeThrownOut == " + totals[iterations - 1] + " && put amount == " + amount); }
public void TestReservoirePutWaterRandomLargeInts() { // Arrange int numIterations = Rand.Next(5000, 10000); for (int i = 0; i < numIterations; i++) { Reservoire utReservoire = new Reservoire(); // Act int randWayBig = Rand.Next(1000000, int.MaxValue - 1); utReservoire.PutWater(randWayBig); // Assert Assert.AreEqual(randWayBig, utReservoire.VolumeThrownOut, "VolumeThrownOut isn't random way big (" + randWayBig + ") when putting random way big (" + randWayBig + ") water [i=" + i + "/" + numIterations + "]"); } }
public void TestReservoirePutWaterRandomNegatives() { // Arrange int numIterations = Rand.Next(5000, 10000), lowest = int.MinValue + 1; Reservoire utReservoire = new Reservoire(); for (int i = 0; i < numIterations; i++) { CaughtEx = false; int minusAmount = Rand.Next(lowest, -2); // Act try { utReservoire.PutWater(minusAmount); } // Assert catch (ArgumentOutOfRangeException ex) { CaughtEx = true; Assert.AreEqual("amount", ex.ParamName, "ArgumentOutOfRangeException didn't report 'amount' parameter [i=" + i + "/" + numIterations + "]"); } catch (Exception ex) { Assert.Fail("PutWater threw a different Exception (" + ex.GetType().Name + ") instead of ArgumentOutOfRangeException when putting amount == " + minusAmount + " [i=" + i + "/" + numIterations + "]"); } Assert.IsTrue(CaughtEx, "PutWater did not throw ArgumentOutOfRangeException when putting " + minusAmount + " water [i=" + i + "/" + numIterations + "]"); } }
public void TestReservoirePutWaterIntMax() { // Arrange int wayBig = int.MaxValue; Reservoire utReservoire = new Reservoire(); // Act utReservoire.PutWater(wayBig); // Assert Assert.AreEqual(wayBig, utReservoire.VolumeThrownOut, "VolumeThrownOut isn't int.MaxValue when putting int.MaxValue water"); }
public void TestReservoirePutWaterGreaterThanIntMax() { // Arrange int putAmount = 1, wayBig = int.MaxValue; Reservoire utReservoire = new Reservoire(); // Act utReservoire.PutWater(putAmount); int putVolume = utReservoire.VolumeThrownOut; try { utReservoire.PutWater(wayBig); } // Assert catch (ArgumentOutOfRangeException ex) { CaughtEx = true; Assert.AreEqual("amount", ex.ParamName, "ArgumentOutOfRangeException didn't report 'amount' parameter"); } catch (Exception ex) { Assert.Fail("PutWater threw a different Exception (" + ex.GetType().Name + ") instead of ArgumentOutOfRangeException when VolumeThrownOut == " + putVolume + " && put amount == " + wayBig); } Assert.IsTrue(CaughtEx, "PutWater did not throw ArgumentOutOfRangeException when VolumeThrownOut == " + putVolume + " && put amount == " + wayBig); }
public void TestReservoireGetThenPutWater() { // Arrange int numAmounts = Rand.Next(5000, 10000), getIterations = 0, putIterations = 0; int[] getAmounts = new int[numAmounts], gotTotals = new int[numAmounts], putAmounts = new int[numAmounts], putTotals = new int[numAmounts]; int getAmount = 0; long longGetTotal = 0; for (int a = 0; a < numAmounts; a++) { getAmount = Rand.Next(1, 30000); longGetTotal += getAmount; if ((longGetTotal >> 32) > 0) { getIterations = a; break; } getAmounts[a] = getAmount; gotTotals[a] = (int)longGetTotal; } if (getIterations == 0) { getIterations = numAmounts; getAmount = Rand.Next(int.MaxValue - gotTotals[gotTotals.Length - 1] + 2, int.MaxValue - 1); longGetTotal += getAmount; } int putAmount = 0; long longPutTotal = 0; for (int a = 0; a < numAmounts; a++) { putAmount = Rand.Next(1, 30000); longPutTotal += putAmount; if ((longPutTotal >> 32) > 0) { putIterations = a; break; } putAmounts[a] = putAmount; putTotals[a] = (int)longPutTotal; } if (putIterations == 0) { putIterations = numAmounts; putAmount = Rand.Next(int.MaxValue - putTotals[putTotals.Length - 1] + 2, int.MaxValue - 1); longPutTotal += putAmount; } Reservoire utReservoire = new Reservoire(); int amountGotten = 0; int initIterations = Math.Min(getIterations, putIterations); int finishIterations = Math.Max(getIterations, putIterations) - initIterations; for (int i = 0; i < initIterations; i++) { // Act amountGotten = utReservoire.GetWater(getAmounts[i]); utReservoire.PutWater(putAmounts[i]); // Assert Assert.AreEqual(getAmounts[i], amountGotten, "GetWater returned different amount that requested [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); Assert.AreEqual(gotTotals[i], utReservoire.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); Assert.AreEqual(putTotals[i], utReservoire.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); } if (initIterations < getIterations) { for (int i = initIterations; i < finishIterations; i++) { // Act amountGotten = utReservoire.GetWater(getAmounts[i]); // Assert Assert.AreEqual(getAmounts[i], amountGotten, "GetWater returned different amount that requested [i=" + i + "/" + finishIterations + "]"); Assert.AreEqual(gotTotals[i], utReservoire.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.AreEqual(putTotals[i], utReservoire.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + finishIterations + "]"); } } else if (initIterations < putIterations) { for (int i = initIterations; i < finishIterations; i++) { // Act utReservoire.PutWater(putAmounts[i]); // Assert Assert.AreEqual(gotTotals[i], utReservoire.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.AreEqual(putTotals[i], utReservoire.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + finishIterations + "]"); } } int noGet = -1; try { // Act noGet = utReservoire.GetWater(getAmount); } // Assert catch (ArgumentOutOfRangeException ex) { CaughtEx = true; Assert.AreEqual("amount", ex.ParamName, "GetWater ArgumentOutOfRangeException didn't report 'amount' parameter"); Assert.AreEqual(-1, noGet, "Reservoire GetWater returned value == " + noGet + " even though ArgumentOutOfRangeException was thrown for getting too much water"); } catch (Exception ex) { Assert.Fail("GetWater threw a different Exception (" + ex.GetType().Name + ") instead of ArgumentOutOfRangeException when VolumeUsed == " + gotTotals[getIterations - 1] + " && get amount == " + getAmount); } Assert.IsTrue(CaughtEx, "GetWater did not throw ArgumentOutOfRangeException when VolumeUsed == " + gotTotals[getIterations - 1] + " && get amount == " + getAmount); // Arrange CaughtEx = false; try { // Act utReservoire.PutWater(putAmount); } // Assert catch (ArgumentOutOfRangeException ex) { CaughtEx = true; Assert.AreEqual("amount", ex.ParamName, "PutWater ArgumentOutOfRangeException didn't report 'amount' parameter"); } catch (Exception ex) { Assert.Fail("PutWater threw a different Exception (" + ex.GetType().Name + ") instead of ArgumentOutOfRangeException when VolumeThrownOut == " + putTotals[putIterations - 1] + " && put amount == " + putAmount); } Assert.IsTrue(CaughtEx, "PutWater did not throw ArgumentOutOfRangeException when VolumeUsed == " + putTotals[putIterations - 1] + " && put amount == " + putAmount); }
public void TestReservoireCurrentState() { // Arrange int numAmounts = Rand.Next(5000, 10000), getIterations = 0, putIterations = 0; int[] getAmounts = new int[numAmounts], gotTotals = new int[numAmounts], putAmounts = new int[numAmounts], putTotals = new int[numAmounts]; int getAmount = 0; long longGetTotal = 0; for (int a = 0; a < numAmounts; a++) { getAmount = Rand.Next(1, 30000); longGetTotal += getAmount; if ((longGetTotal >> 32) > 0) { getIterations = a; break; } getAmounts[a] = getAmount; gotTotals[a] = (int)longGetTotal; } if (getIterations == 0) { getIterations = numAmounts; getAmount = Rand.Next(int.MaxValue - gotTotals[gotTotals.Length - 1] + 2, int.MaxValue - 1); longGetTotal += getAmount; } int putAmount = 0; long longPutTotal = 0; for (int a = 0; a < numAmounts; a++) { putAmount = Rand.Next(1, 30000); longPutTotal += putAmount; if ((longPutTotal >> 32) > 0) { putIterations = a; break; } putAmounts[a] = putAmount; putTotals[a] = (int)longPutTotal; } if (putIterations == 0) { putIterations = numAmounts; putAmount = Rand.Next(int.MaxValue - putTotals[putTotals.Length - 1] + 2, int.MaxValue - 1); longPutTotal += putAmount; } Reservoire utReservoire = new Reservoire(), utReservoireFromState = null; ReservoireState utStateInitial = null, utState = null; // Act utStateInitial = utReservoire.CurrentState; utReservoireFromState = new Reservoire(utStateInitial); // Assert Assert.AreEqual(0, utStateInitial.VolumeUsed, "CurrentState from empty constructed initial Reservoire VolumeUsed not equal to 0"); Assert.AreEqual(0, utStateInitial.VolumeThrownOut, "CurrentState from empty constructed initial Reservoire VolumeThrownOut not equal to 0"); Assert.AreEqual(0, utReservoireFromState.VolumeUsed, "ReservoireState constructor from empty constructed initial Reservoire VolumeUsed not equal to 0"); Assert.AreEqual(0, utReservoireFromState.VolumeThrownOut, "ReservoireState constructor from empty constructed initial Reservoire VolumeThrownOut not equal to 0"); // Arrange int initIterations = Math.Min(getIterations, putIterations); int finishIterations = Math.Max(getIterations, putIterations) - initIterations; for (int i = 0; i < initIterations; i++) { // Arrange utState = null; utReservoireFromState = null; // Act utReservoire.GetWater(getAmounts[i]); utReservoire.PutWater(putAmounts[i]); utState = utReservoire.CurrentState; utReservoireFromState = new Reservoire(utState); // Assert Assert.AreEqual(gotTotals[i], utReservoire.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); Assert.AreEqual(putTotals[i], utReservoire.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); Assert.IsNotNull(utState, "CurrentState returned null [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); Assert.AreEqual(utReservoire.VolumeUsed, utState.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); Assert.AreEqual(utReservoire.VolumeThrownOut, utState.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); Assert.IsNotNull(utReservoireFromState, "Constructing a new Reservoire from ReservoireState returned null (should be impossible with new call) [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); Assert.AreEqual(utReservoire.VolumeUsed, utReservoireFromState.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); Assert.AreEqual(utReservoire.VolumeThrownOut, utReservoireFromState.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + initIterations + ":" + finishIterations + "]"); } if (initIterations < getIterations) { for (int i = initIterations; i < finishIterations; i++) { // Arrange utState = null; utReservoireFromState = null; // Act utReservoire.GetWater(getAmounts[i]); utState = utReservoire.CurrentState; utReservoireFromState = new Reservoire(utState); // Assert Assert.AreEqual(gotTotals[i], utReservoire.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.AreEqual(putTotals[i], utReservoire.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.IsNotNull(utState, "CurrentState returned null"); Assert.AreEqual(utReservoire.VolumeUsed, utState.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.AreEqual(utReservoire.VolumeThrownOut, utState.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.IsNotNull(utReservoireFromState, "Constructing a new Reservoire from ReservoireState returned null (should be impossible with new call)"); Assert.AreEqual(utReservoire.VolumeUsed, utReservoireFromState.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.AreEqual(utReservoire.VolumeThrownOut, utReservoireFromState.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + finishIterations + "]"); } } else if (initIterations < putIterations) { for (int i = initIterations; i < finishIterations; i++) { // Arrange utState = null; utReservoireFromState = null; // Act utReservoire.PutWater(putAmounts[i]); utState = utReservoire.CurrentState; utReservoireFromState = new Reservoire(utState); // Assert Assert.AreEqual(gotTotals[i], utReservoire.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.AreEqual(putTotals[i], utReservoire.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.IsNotNull(utState, "CurrentState returned null"); Assert.AreEqual(utReservoire.VolumeUsed, utState.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.AreEqual(utReservoire.VolumeThrownOut, utState.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.IsNotNull(utReservoireFromState, "Constructing a new Reservoire from ReservoireState returned null (should be impossible with new call)"); Assert.AreEqual(utReservoire.VolumeUsed, utReservoireFromState.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + finishIterations + "]"); Assert.AreEqual(utReservoire.VolumeThrownOut, utReservoireFromState.VolumeThrownOut, "Current VolumeThrownOut not equal to expected total [i=" + i + "/" + finishIterations + "]"); } } }
public int Empty(Reservoire into) { int amount = this.CurrentFill; into.PutWater(amount); this.CurrentFill = 0; return amount; }