public void TestReservoireBasicGet() { // 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(); int amountGotten = 0; for (int i = 0; i < iterations; i++) { // Act amountGotten = utReservoire.GetWater(amounts[i]); // Assert Assert.AreEqual(amounts[i], amountGotten, "GetWater returned different amount that requested [i=" + i + "/" + iterations + "]"); Assert.AreEqual(totals[i], utReservoire.VolumeUsed, "Current VolumeUsed not equal to expected total [i=" + i + "/" + iterations + "]"); } int noGet = -1; try { noGet = utReservoire.GetWater(amount); } 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 == " + totals[iterations - 1] + " && get amount == " + amount); } Assert.IsTrue(CaughtEx, "GetWater did not throw ArgumentOutOfRangeException when VolumeUsed == " + totals[iterations - 1] + " && get amount == " + amount); }
public void TestReservoireGetWaterZero() { // Arrange int zeroGet = 0; Reservoire utReservoire = new Reservoire(); // Act int noGet = -1; try { noGet = utReservoire.GetWater(zeroGet); } // Assert catch (ArgumentOutOfRangeException ex) { CaughtEx = true; Assert.AreEqual("amount", ex.ParamName, "ArgumentOutOfRangeException didn't report 'amount' parameter"); Assert.AreEqual(-1, noGet, "Reservoire GetWater returned value == " + noGet + " even though ArgumentOutOfRangeException was thrown for getting amount == 0"); } catch (Exception ex) { Assert.Fail("GetWater threw a different Exception (" + ex.GetType().Name + ") instead of ArgumentOutOfRangeException when getting amount == 0"); } Assert.IsTrue(CaughtEx, "GetWater did not throw ArgumentOutOfRangeException when getting 0 water"); }
public void TestReservoireGetWaterRandomNegatives() { // Arrange int numIterations = Rand.Next(5000, 10000); Reservoire utReservoire = new Reservoire(); for (int i = 0; i < numIterations; i++) { int noGet = 0, minusAmount = Rand.Next(int.MinValue + 1, -2); CaughtEx = false; // Act try { noGet = utReservoire.GetWater(minusAmount); } // Assert catch (ArgumentOutOfRangeException aore) { CaughtEx = true; Assert.AreEqual("amount", aore.ParamName, "ArgumentOutOfRangeException didn't report 'amount' parameter [i=" + i + "/" + numIterations + "]"); Assert.AreEqual(0, noGet, "Reservoire GetWater returned value == " + noGet + " even though ArgumentOutOfRangeException was thrown for getting amount == " + minusAmount + " [i=" + i + "/" + numIterations + "]"); } catch (Exception ex) { Assert.Fail("GetWater threw a different Exception (" + ex.GetType().Name + ") instead of ArgumentOutOfRangeException when getting amount == " + minusAmount + " [i=" + i + "/" + numIterations + "]"); } Assert.IsTrue(CaughtEx, "GetWater did not throw ArgumentOutOfRangeException when getting " + minusAmount + " water [i=" + i + "/" + numIterations + "]"); } }
public void TestReservoireGetWaterRandomLargeInts() { // 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); int randWayBigGot = utReservoire.GetWater(randWayBig); // Assert Assert.AreEqual(randWayBig, randWayBigGot, "Getting random way big (" + randWayBig + ") water returned different amount of water [i=" + i + "/" + numIterations + "]"); Assert.AreEqual(randWayBig, utReservoire.VolumeUsed, "VolumeUsed isn't random way big (" + randWayBig + ") when getting random way big (" + randWayBig + ") water [i=" + i + "/" + numIterations + "]"); } }
public void TestReservoireGetWaterIntMax() { // Arrange int wayBig = int.MaxValue; Reservoire utReservoire = new Reservoire(); // Act int gotWayBig = utReservoire.GetWater(wayBig); // Assert Assert.AreEqual(wayBig, gotWayBig, "Getting int.MaxValue water returned different amount of water"); Assert.AreEqual(wayBig, utReservoire.VolumeUsed, "VolumeUsed isn't int.MaxValue when getting int.MaxValue water"); }
public void TestReservoireGetWaterGreaterThanIntMax() { // Arrange int getAmount = 1, wayBig = int.MaxValue; Reservoire utReservoire = new Reservoire(); // Act int gotAmount = utReservoire.GetWater(getAmount); int gotVolume = utReservoire.VolumeUsed; int tooMuch = -1; try { tooMuch = utReservoire.GetWater(wayBig); } // Assert catch (ArgumentOutOfRangeException ex) { CaughtEx = true; Assert.AreEqual("amount", ex.ParamName, "ArgumentOutOfRangeException didn't report 'amount' parameter"); Assert.AreEqual(-1, tooMuch, "Reservoire GetWater returned value == " + tooMuch + " 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 == " + gotVolume + " && get amount == " + wayBig); } Assert.IsTrue(CaughtEx, "GetWater did not throw ArgumentOutOfRangeException when VolumeUsed == " + gotVolume + " && get 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 Fill(Reservoire source) { if (!this.IsFull) { int amount = this.Capacity - this.CurrentFill; this.CurrentFill += source.GetWater(this.Capacity - this.CurrentFill); return amount; } return 0; }