Ejemplo n.º 1
0
 public int Empty(Reservoire into)
 {
     int amount = this.CurrentFill;
     into.PutWater(amount);
     this.CurrentFill = 0;
     return amount;
 }
Ejemplo n.º 2
0
 public object Clone()
 {
     var clone = new Reservoire();
     clone.VolumeUsed = this.VolumeUsed;
     clone.VolumeThrownOut = this.VolumeThrownOut;
     return clone;
 }
Ejemplo n.º 3
0
 public BaseSolutionStrategy(string name, int waterGoal, Reservoire waterSource)
 {
     if (waterSource == null)
     {
         throw new ArgumentNullException("waterSource");
     }
     StrategyName = name;
     WaterGoal = waterGoal;
     WaterSource = waterSource.Clone() as Reservoire;
 }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
 public Problem(Bucket first, Bucket second, int goalWaterVolume)
 {
     if (goalWaterVolume < 0)
     {
         throw new ArgumentOutOfRangeException("Cannot solve for a problem with a negative goal");
     }
     FirstBucket = first;
     SecondBucket = second;
     WaterSource = new Reservoire();
     GoalWaterVolume = goalWaterVolume;
 }
 public SingleBucketSolutionStrategy(string name, Bucket from, Bucket to, int waterGoal, Reservoire waterSource)
     : base(name, waterGoal, waterSource)
 {
     if (from == null)
     {
         throw new ArgumentNullException("from");
     }
     if (to == null)
     {
         throw new ArgumentNullException("to");
     }
     From = from.Clone() as Bucket;
     To = to.Clone() as Bucket;
 }
Ejemplo n.º 7
0
        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 + "]");
            }
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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");
        }
Ejemplo n.º 10
0
        public void TestBucketToFromBytes()
        {
            // Arrange
            Bucket sourceBucket = new Bucket(54, "SourceBucket");
            Bucket otherBucket = new Bucket(4, "OtherBucket");
            Encoding textEncoding = Encoding.UTF8;
            Bucket utNewBucket = new Bucket();
            Bucket utFullBucket = new Bucket();
            Bucket utTransBucket = new Bucket();
            Bucket utEmptiedBucket = new Bucket();
            Bucket utNewBucketFromBuffer = new Bucket();
            Bucket utFullBucketFromBuffer = new Bucket();
            Bucket utTransBucketFromBuffer = new Bucket();
            Bucket utEmptiedBucketFromBuffer = new Bucket();
            Reservoire waterSrc = new Reservoire();

            // Act
            byte[] utNewBytes = sourceBucket.GetBytes(textEncoding);
            sourceBucket.Fill(waterSrc);
            byte[] utFullBytes = sourceBucket.GetBytes(textEncoding);
            sourceBucket.TransferTo(otherBucket);
            byte[] utTransBytes = sourceBucket.GetBytes(textEncoding);
            sourceBucket.Empty(waterSrc);
            byte[] utEmptiedBytes = sourceBucket.GetBytes(textEncoding);

            utNewBucket.FromBytes(utNewBytes, textEncoding: textEncoding);
            utFullBucket.FromBytes(utFullBytes, textEncoding: textEncoding);
            utTransBucket.FromBytes(utTransBytes, textEncoding: textEncoding);
            utEmptiedBucket.FromBytes(utEmptiedBytes, textEncoding: textEncoding);

            byte[] utBuffer = new byte[utNewBytes.Length + utFullBytes.Length + utTransBytes.Length + utEmptiedBytes.Length];
            int toBufferOffset = 0;
            toBufferOffset = utNewBucket.OntoBuffer(utBuffer, toBufferOffset, textEncoding);
            toBufferOffset = utFullBucket.OntoBuffer(utBuffer, toBufferOffset, textEncoding);
            toBufferOffset = utTransBucket.OntoBuffer(utBuffer, toBufferOffset, textEncoding);
            toBufferOffset = utEmptiedBucket.OntoBuffer(utBuffer, toBufferOffset, textEncoding);
            int fromBufferOffset = 0;
            fromBufferOffset = utNewBucketFromBuffer.FromBytes(utBuffer, fromBufferOffset, textEncoding);
            fromBufferOffset = utFullBucketFromBuffer.FromBytes(utBuffer, fromBufferOffset, textEncoding);
            fromBufferOffset = utTransBucketFromBuffer.FromBytes(utBuffer, fromBufferOffset, textEncoding);
            fromBufferOffset = utEmptiedBucketFromBuffer.FromBytes(utBuffer, fromBufferOffset, textEncoding);

            // Assert
            Assert.AreEqual(sourceBucket.Capacity, utNewBucket.Capacity, "Retrieved new bucket has different capacity");
            Assert.AreEqual(0, utNewBucket.CurrentFill, "Retrieved new bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utNewBucket.Name, "Retrieved new bucket has wrong name");
            Assert.IsTrue(utNewBucket.IsEmpty, "Retrieved new bucket is not empty");
            Assert.IsFalse(utNewBucket.IsFull, "Retrieved new bucket is full");

            Assert.AreEqual(sourceBucket.Capacity, utFullBucket.Capacity, "Retrieved full bucket has different capacity");
            Assert.AreEqual(sourceBucket.Capacity, utFullBucket.CurrentFill, "Retrieved full bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utFullBucket.Name, "Retrieved full bucket has wrong name");
            Assert.IsFalse(utFullBucket.IsEmpty, "Retrieved full bucket is empty");
            Assert.IsTrue(utFullBucket.IsFull, "Retrieved full bucket is not full");

            Assert.AreEqual(sourceBucket.Capacity, utTransBucket.Capacity, "Retrieved transferred bucket has different capacity");
            Assert.AreEqual(sourceBucket.Capacity - otherBucket.Capacity, utTransBucket.CurrentFill, "Retrieved transferred bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utTransBucket.Name, "Retrieved transferred bucket has wrong name");
            Assert.IsFalse(utTransBucket.IsEmpty, "Retrieved transferred bucket is empty");
            Assert.IsFalse(utTransBucket.IsFull, "Retrieved transferred bucket is full");

            Assert.AreEqual(sourceBucket.Capacity, utEmptiedBucket.Capacity, "Retrieved emptied bucket has different capacity");
            Assert.AreEqual(0, utEmptiedBucket.CurrentFill, "Retrieved emptied bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utEmptiedBucket.Name, "Retrieved emptied bucket has wrong name");
            Assert.IsTrue(utEmptiedBucket.IsEmpty, "Retrieved emptied bucket is not empty");
            Assert.IsFalse(utEmptiedBucket.IsFull, "Retrieved emptied bucket is full");

            Assert.AreEqual(utNewBucket.Capacity, utNewBucketFromBuffer.Capacity, "Retrieved new bucket from buffer has different capacity");
            Assert.AreEqual(utNewBucket.CurrentFill, utNewBucketFromBuffer.CurrentFill, "Retrieved new bucket from buffer has wrong current fill");
            Assert.AreEqual(utNewBucket.Name, utNewBucketFromBuffer.Name, "Retrieved new bucket from buffer has wrong name");
            Assert.IsTrue(utNewBucketFromBuffer.IsEmpty, "Retrieved new bucket from buffer is not empty");
            Assert.IsFalse(utNewBucketFromBuffer.IsFull, "Retrieved new bucket from buffer is full");

            Assert.AreEqual(utFullBucket.Capacity, utFullBucketFromBuffer.Capacity, "Retrieved Full bucket from buffer has different capacity");
            Assert.AreEqual(utFullBucket.CurrentFill, utFullBucketFromBuffer.CurrentFill, "Retrieved Full bucket from buffer has wrong current fill");
            Assert.AreEqual(utFullBucket.Name, utFullBucketFromBuffer.Name, "Retrieved Full bucket from buffer has wrong name");
            Assert.IsFalse(utFullBucketFromBuffer.IsEmpty, "Retrieved Full bucket from buffer is empty");
            Assert.IsTrue(utFullBucketFromBuffer.IsFull, "Retrieved Full bucket from buffer is not full");

            Assert.AreEqual(utTransBucket.Capacity, utTransBucketFromBuffer.Capacity, "Retrieved Transferred bucket from buffer has different capacity");
            Assert.AreEqual(utTransBucket.CurrentFill, utTransBucketFromBuffer.CurrentFill, "Retrieved Transferred bucket from buffer has wrong current fill");
            Assert.AreEqual(utTransBucket.Name, utTransBucketFromBuffer.Name, "Retrieved Transferred bucket from buffer has wrong name");
            Assert.IsFalse(utTransBucketFromBuffer.IsEmpty, "Retrieved Transferred bucket from buffer is empty");
            Assert.IsFalse(utTransBucketFromBuffer.IsFull, "Retrieved Transferred bucket from buffer is full");

            Assert.AreEqual(utEmptiedBucket.Capacity, utEmptiedBucketFromBuffer.Capacity, "Retrieved Emptied bucket from buffer has different capacity");
            Assert.AreEqual(utEmptiedBucket.CurrentFill, utEmptiedBucketFromBuffer.CurrentFill, "Retrieved Emptied bucket from buffer has wrong current fill");
            Assert.AreEqual(utEmptiedBucket.Name, utEmptiedBucketFromBuffer.Name, "Retrieved Emptied bucket from buffer has wrong name");
            Assert.IsTrue(utEmptiedBucketFromBuffer.IsEmpty, "Retrieved Emptied bucket from buffer is not empty");
            Assert.IsFalse(utEmptiedBucketFromBuffer.IsFull, "Retrieved Emptied bucket from buffer is full");

            Assert.AreEqual(toBufferOffset, fromBufferOffset, "Buffer offsets differ");
        }
Ejemplo n.º 11
0
        public void TestBucketEmpty()
        {
            // Arrange
            int bucketCapacity = 5;
            Bucket utBucket = new Bucket(bucketCapacity);
            Reservoire waterSource = new Reservoire();

            // Act
            utBucket.Fill(waterSource);
            utBucket.Empty(waterSource);

            //Assert
            Assert.AreEqual(0, utBucket.CurrentFill, "Emptied the Bucket but CurrentFill = " + utBucket.CurrentFill);
            Assert.AreNotEqual(bucketCapacity, utBucket.CurrentFill, "Emptied the bucket but it still has CurrentFill as test capacity");
            Assert.AreNotEqual(utBucket.CurrentFill, utBucket.Capacity, "Emptied the bucket but it is reporting CurrentFill == Capacity");
            Assert.IsTrue(utBucket.IsEmpty, "Emptied the bucket but reporting it is not empty");
            Assert.IsFalse(utBucket.IsFull, "Emptied the bucket but it is reporting it is full");
        }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 13
0
        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 + "]");
            }
        }
Ejemplo n.º 14
0
 public static Reservoire GetFromBytes(byte[] data, int offset = 0)
 {
     Reservoire r = new Reservoire();
     r.FromBytes(data, offset);
     return r;
 }
Ejemplo n.º 15
0
        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");
        }
Ejemplo n.º 16
0
 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;
 }
Ejemplo n.º 17
0
        public void TestBucketFraming()
        {
            // Arrange
            Bucket sourceBucket = new Bucket(54, "SourceBucket");
            Bucket otherBucket = new Bucket(4, "OtherBucket");
            Encoding textEncoding = Encoding.UTF8;
            Bucket utNewBucket = new Bucket();
            Bucket utFullBucket = new Bucket();
            Bucket utTransBucket = new Bucket();
            Bucket utEmptiedBucket = new Bucket();
            Bucket utNewBucketFromMessage = new Bucket();
            Bucket utFullBucketFromMessage = new Bucket();
            Bucket utTransBucketFromMessage = new Bucket();
            Bucket utEmptiedBucketFromMessage = new Bucket();
            Reservoire waterSrc = new Reservoire();

            // Act
            // Use ToList since IEnumerable/yield methods are lazy evaluated
            IEnumerable<Frame> utNewFrames = sourceBucket.GetFrames(textEncoding).ToList();
            sourceBucket.Fill(waterSrc);
            IEnumerable<Frame> utFullFrames = sourceBucket.GetFrames(textEncoding).ToList();
            sourceBucket.TransferTo(otherBucket);
            IEnumerable<Frame> utTransFrames = sourceBucket.GetFrames(textEncoding).ToList();
            sourceBucket.Empty(waterSrc);
            IEnumerable<Frame> utEmptiedFrames = sourceBucket.GetFrames(textEncoding).ToList();
            utNewBucket.FromFrames(utNewFrames, textEncoding: textEncoding);
            utFullBucket.FromFrames(utFullFrames, textEncoding: textEncoding);
            utTransBucket.FromFrames(utTransFrames, textEncoding: textEncoding);
            utEmptiedBucket.FromFrames(utEmptiedFrames, textEncoding: textEncoding);

            int numFrames = utNewFrames.Count() + utFullFrames.Count() + utTransFrames.Count() + utEmptiedFrames.Count();
            List<Frame> msgFrames = new List<Frame>(numFrames);
            msgFrames.AddRange(utNewBucket.GetFrames(textEncoding));
            msgFrames.AddRange(utFullBucket.GetFrames(textEncoding));
            msgFrames.AddRange(utTransBucket.GetFrames(textEncoding));
            msgFrames.AddRange(utEmptiedBucket.GetFrames(textEncoding));
            int offset = 0;
            offset = utNewBucketFromMessage.FromFrames(msgFrames, offset, textEncoding);
            offset = utFullBucketFromMessage.FromFrames(msgFrames, offset, textEncoding);
            offset = utTransBucketFromMessage.FromFrames(msgFrames, offset, textEncoding);
            offset = utEmptiedBucketFromMessage.FromFrames(msgFrames, offset, textEncoding);

            // Assert
            Assert.AreEqual(sourceBucket.Capacity, utNewBucket.Capacity, "Retrieved new bucket has different capacity");
            Assert.AreEqual(0, utNewBucket.CurrentFill, "Retrieved new bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utNewBucket.Name, "Retrieved new bucket has wrong name");
            Assert.IsTrue(utNewBucket.IsEmpty, "Retrieved new bucket is not empty");
            Assert.IsFalse(utNewBucket.IsFull, "Retrieved new bucket is full");

            Assert.AreEqual(sourceBucket.Capacity, utFullBucket.Capacity, "Retrieved full bucket has different capacity");
            Assert.AreEqual(sourceBucket.Capacity, utFullBucket.CurrentFill, "Retrieved full bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utFullBucket.Name, "Retrieved full bucket has wrong name");
            Assert.IsFalse(utFullBucket.IsEmpty, "Retrieved full bucket is empty");
            Assert.IsTrue(utFullBucket.IsFull, "Retrieved full bucket is not full");

            Assert.AreEqual(sourceBucket.Capacity, utTransBucket.Capacity, "Retrieved transferred bucket has different capacity");
            Assert.AreEqual(sourceBucket.Capacity - otherBucket.Capacity, utTransBucket.CurrentFill, "Retrieved transferred bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utTransBucket.Name, "Retrieved transferred bucket has wrong name");
            Assert.IsFalse(utTransBucket.IsEmpty, "Retrieved transferred bucket is empty");
            Assert.IsFalse(utTransBucket.IsFull, "Retrieved transferred bucket is full");

            Assert.AreEqual(sourceBucket.Capacity, utEmptiedBucket.Capacity, "Retrieved emptied bucket has different capacity");
            Assert.AreEqual(0, utEmptiedBucket.CurrentFill, "Retrieved emptied bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utEmptiedBucket.Name, "Retrieved emptied bucket has wrong name");
            Assert.IsTrue(utEmptiedBucket.IsEmpty, "Retrieved emptied bucket is not empty");
            Assert.IsFalse(utEmptiedBucket.IsFull, "Retrieved emptied bucket is full");

            Assert.AreEqual(utNewBucket.Capacity, utNewBucketFromMessage.Capacity, "Retrieved new bucket from message has different capacity");
            Assert.AreEqual(utNewBucket.CurrentFill, utNewBucketFromMessage.CurrentFill, "Retrieved new bucket from message has wrong current fill");
            Assert.AreEqual(utNewBucket.Name, utNewBucketFromMessage.Name, "Retrieved new bucket from message has wrong name");
            Assert.IsTrue(utNewBucketFromMessage.IsEmpty, "Retrieved new bucket from message is not empty");
            Assert.IsFalse(utNewBucketFromMessage.IsFull, "Retrieved new bucket from message is full");

            Assert.AreEqual(utFullBucket.Capacity, utFullBucketFromMessage.Capacity, "Retrieved Full bucket from message has different capacity");
            Assert.AreEqual(utFullBucket.CurrentFill, utFullBucketFromMessage.CurrentFill, "Retrieved Full bucket from message has wrong current fill");
            Assert.AreEqual(utFullBucket.Name, utFullBucketFromMessage.Name, "Retrieved Full bucket from message has wrong name");
            Assert.IsFalse(utFullBucketFromMessage.IsEmpty, "Retrieved Full bucket from message is empty");
            Assert.IsTrue(utFullBucketFromMessage.IsFull, "Retrieved Full bucket from message is not full");

            Assert.AreEqual(utTransBucket.Capacity, utTransBucketFromMessage.Capacity, "Retrieved Transferred bucket from message has different capacity");
            Assert.AreEqual(utTransBucket.CurrentFill, utTransBucketFromMessage.CurrentFill, "Retrieved Transferred bucket from message has wrong current fill");
            Assert.AreEqual(utTransBucket.Name, utTransBucketFromMessage.Name, "Retrieved Transferred bucket from message has wrong name");
            Assert.IsFalse(utTransBucketFromMessage.IsEmpty, "Retrieved Transferred bucket from message is empty");
            Assert.IsFalse(utTransBucketFromMessage.IsFull, "Retrieved Transferred bucket from message is full");

            Assert.AreEqual(utEmptiedBucket.Capacity, utEmptiedBucketFromMessage.Capacity, "Retrieved Emptied bucket from message has different capacity");
            Assert.AreEqual(utEmptiedBucket.CurrentFill, utEmptiedBucketFromMessage.CurrentFill, "Retrieved Emptied bucket from message has wrong current fill");
            Assert.AreEqual(utEmptiedBucket.Name, utEmptiedBucketFromMessage.Name, "Retrieved Emptied bucket from message has wrong name");
            Assert.IsTrue(utEmptiedBucketFromMessage.IsEmpty, "Retrieved Emptied bucket from message is not empty");
            Assert.IsFalse(utEmptiedBucketFromMessage.IsFull, "Retrieved Emptied bucket from message is full");

            Assert.AreEqual(numFrames, offset, "Wrong number of frames after retrieving data");
        }
Ejemplo n.º 18
0
        public void TestBucketClone()
        {
            // Arrange
            Bucket sourceBucket = new Bucket(20, "SourceBucket");
            Bucket otherBucket = new Bucket(8, "OtherBucket");
            Reservoire waterSrc = new Reservoire();
            Bucket utNewBucket, utFullBucket, utTransBucket, utEmptiedBucket;
            Bucket utNewBucketFromClone, utFullBucketFromClone, utTransBucketFromClone, utEmptiedBucketFromClone;

            // Act
            utNewBucket = sourceBucket.Clone() as Bucket;
            sourceBucket.Fill(waterSrc);
            utFullBucket = sourceBucket.Clone() as Bucket;
            sourceBucket.TransferTo(otherBucket);
            utTransBucket = sourceBucket.Clone() as Bucket;
            sourceBucket.Empty(waterSrc);
            utEmptiedBucket = sourceBucket.Clone() as Bucket;
            utNewBucketFromClone = utNewBucket.Clone() as Bucket;
            utFullBucketFromClone = utFullBucket.Clone() as Bucket;
            utTransBucketFromClone = utTransBucket.Clone() as Bucket;
            utEmptiedBucketFromClone = utEmptiedBucket.Clone() as Bucket;

            // Assert
            Assert.AreEqual(sourceBucket.Capacity, utNewBucket.Capacity, "Cloned new bucket has different capacity");
            Assert.AreEqual(0, utNewBucket.CurrentFill, "Cloned new bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utNewBucket.Name, "Cloned new bucket has wrong name");
            Assert.IsTrue(utNewBucket.IsEmpty, "Cloned new bucket is not empty");
            Assert.IsFalse(utNewBucket.IsFull, "Cloned new bucket is full");

            Assert.AreEqual(sourceBucket.Capacity, utFullBucket.Capacity, "Cloned full bucket has different capacity");
            Assert.AreEqual(sourceBucket.Capacity, utFullBucket.CurrentFill, "Cloned full bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utFullBucket.Name, "Cloned full bucket has wrong name");
            Assert.IsFalse(utFullBucket.IsEmpty, "Cloned full bucket is empty");
            Assert.IsTrue(utFullBucket.IsFull, "Cloned full bucket is not full");

            Assert.AreEqual(sourceBucket.Capacity, utTransBucket.Capacity, "Cloned transferred bucket has different capacity");
            Assert.AreEqual(sourceBucket.Capacity - otherBucket.Capacity, utTransBucket.CurrentFill, "Cloned transferred bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utTransBucket.Name, "Cloned transferred bucket has wrong name");
            Assert.IsFalse(utTransBucket.IsEmpty, "Cloned transferred bucket is empty");
            Assert.IsFalse(utTransBucket.IsFull, "Cloned transferred bucket is full");

            Assert.AreEqual(sourceBucket.Capacity, utEmptiedBucket.Capacity, "Cloned emptied bucket has different capacity");
            Assert.AreEqual(0, utEmptiedBucket.CurrentFill, "Cloned emptied bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utEmptiedBucket.Name, "Cloned emptied bucket has wrong name");
            Assert.IsTrue(utEmptiedBucket.IsEmpty, "Cloned emptied bucket is not empty");
            Assert.IsFalse(utEmptiedBucket.IsFull, "Cloned emptied bucket is full");

            Assert.AreEqual(utNewBucket.Capacity, utNewBucketFromClone.Capacity, "Cloned new bucket from Clone has different capacity");
            Assert.AreEqual(utNewBucket.CurrentFill, utNewBucketFromClone.CurrentFill, "Cloned new bucket from Clone has wrong current fill");
            Assert.AreEqual(utNewBucket.Name, utNewBucketFromClone.Name, "Cloned new bucket from Clone has wrong name");
            Assert.IsTrue(utNewBucketFromClone.IsEmpty, "Cloned new bucket from Clone is not empty");
            Assert.IsFalse(utNewBucketFromClone.IsFull, "Cloned new bucket from Clone is full");

            Assert.AreEqual(utFullBucket.Capacity, utFullBucketFromClone.Capacity, "Cloned Full bucket from Clone has different capacity");
            Assert.AreEqual(utFullBucket.CurrentFill, utFullBucketFromClone.CurrentFill, "Cloned Full bucket from Clone has wrong current fill");
            Assert.AreEqual(utFullBucket.Name, utFullBucketFromClone.Name, "Cloned Full bucket from Clone has wrong name");
            Assert.IsFalse(utFullBucketFromClone.IsEmpty, "Cloned Full bucket from Clone is empty");
            Assert.IsTrue(utFullBucketFromClone.IsFull, "Cloned Full bucket from Clone is not full");

            Assert.AreEqual(utTransBucket.Capacity, utTransBucketFromClone.Capacity, "Cloned Transferred bucket from Clone has different capacity");
            Assert.AreEqual(utTransBucket.CurrentFill, utTransBucketFromClone.CurrentFill, "Cloned Transferred bucket from Clone has wrong current fill");
            Assert.AreEqual(utTransBucket.Name, utTransBucketFromClone.Name, "Cloned Transferred bucket from Clone has wrong name");
            Assert.IsFalse(utTransBucketFromClone.IsEmpty, "Cloned Transferred bucket from Clone is empty");
            Assert.IsFalse(utTransBucketFromClone.IsFull, "Cloned Transferred bucket from Clone is full");

            Assert.AreEqual(utEmptiedBucket.Capacity, utEmptiedBucketFromClone.Capacity, "Cloned Emptied bucket from Clone has different capacity");
            Assert.AreEqual(utEmptiedBucket.CurrentFill, utEmptiedBucketFromClone.CurrentFill, "Cloned Emptied bucket from Clone has wrong current fill");
            Assert.AreEqual(utEmptiedBucket.Name, utEmptiedBucketFromClone.Name, "Cloned Emptied bucket from Clone has wrong name");
            Assert.IsTrue(utEmptiedBucketFromClone.IsEmpty, "Cloned Emptied bucket from Clone is not empty");
            Assert.IsFalse(utEmptiedBucketFromClone.IsFull, "Cloned Emptied bucket from Clone is full");

            // Act 2

            // Assert 2

            // Act 3

            // Assert 3
        }
Ejemplo n.º 19
0
 public ProblemState(Reservoire source, params Bucket[] buckets)
 {
     BucketState = buckets.Select(b => b.Clone() as Bucket).ToList();
     SourceState = source.CurrentState;
 }
Ejemplo n.º 20
0
        public void TestBucketTransferBigToSmall()
        {
            // Arrange
            int bucketCapacityBig = 5, bucketCapacitySmall = 3;
            int bucketDifference = Math.Abs(bucketCapacityBig - bucketCapacitySmall);
            Bucket utBucketBig = new Bucket(bucketCapacityBig), utBucketSmall = new Bucket(bucketCapacitySmall);
            Reservoire waterSource = new Reservoire();

            // Act
            utBucketBig.Fill(waterSource);
            Assert.IsTrue(utBucketSmall.IsEmpty, "Something went wrong is Arrange as small bucket is not empty");
            utBucketBig.TransferTo(utBucketSmall);

            //Assert
            Assert.AreEqual(bucketCapacitySmall, utBucketSmall.CurrentFill, "Transferred from big to small but small bucket not at test capacity");
            Assert.AreEqual(utBucketSmall.Capacity, utBucketSmall.CurrentFill, "Transferred from big to small but small bucket not at reported capacity");
            Assert.AreNotEqual(0, utBucketSmall.CurrentFill, "Transferred big to small but small reporting CurrentFill == 0");
            Assert.IsTrue(utBucketSmall.IsFull, "Transferred big to small but small reporting is not full");
            Assert.IsFalse(utBucketSmall.IsEmpty, "Transferred big to small but small reporting it is empty");
            Assert.AreNotEqual(0, utBucketBig.CurrentFill, "Transferred from big to small but big reporting CurrentFill == 0");
            Assert.AreEqual(bucketDifference, utBucketBig.CurrentFill, "Transferred from big to small but big CurrentFill != bucketDifference(" + bucketDifference + ")");
            Assert.AreNotEqual(bucketCapacityBig, utBucketBig.CurrentFill, "Transferred from big to small but big has CurrentFill == test capacity big");
            Assert.AreNotEqual(utBucketBig.CurrentFill, utBucketBig.Capacity, "Transferred from big to small but big has CurrentFill == Capacity");
            Assert.IsFalse(utBucketBig.IsFull, "Transferred big to small but big reporting full");
            Assert.IsFalse(utBucketBig.IsEmpty, "Transferred from big to small but big reporting is empty");
        }
Ejemplo n.º 21
0
        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 + "]");
            }
        }
Ejemplo n.º 22
0
        public void TestBucketFill()
        {
            // Arrange
            int bucketCapacity = 5;
            Bucket utBucket = new Bucket(bucketCapacity);
            Reservoire waterSource = new Reservoire();

            // Act
            utBucket.Fill(waterSource);

            //Assert
            Assert.AreEqual(bucketCapacity, utBucket.CurrentFill, "Filled the Bucket but it is not at Capacity");
            Assert.AreNotEqual(0, utBucket.CurrentFill, "Filled the bucket but it is reporting 0 CurrentFill");
            Assert.AreEqual(utBucket.CurrentFill, utBucket.Capacity, "Filled bucket but CurrentFill not equal to Capacity");
            Assert.IsTrue(utBucket.IsFull, "Filled bucket to capacity but it is reporting not full");
            Assert.IsFalse(utBucket.IsEmpty, "Filled bucket to capacity but it is reporting empty");
        }
Ejemplo n.º 23
0
        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");
        }
Ejemplo n.º 24
0
        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 + "]");
                }
            }
        }
Ejemplo n.º 25
0
        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");
        }
Ejemplo n.º 26
0
        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);
        }
Ejemplo n.º 27
0
        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 + "]");
            }
        }
Ejemplo n.º 28
0
        public void TestBucketSerialization()
        {
            // Arrange
            Bucket utBucket = new Bucket(88, "utBucket");
            Bucket otherBucket = new Bucket(32, "other Bucket");
            Reservoire waterSource = new Reservoire();
            utBucket.Fill(waterSource);
            utBucket.TransferTo(otherBucket);

            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                try
                {
                    // Act - Serialize
                    formatter.Serialize(ms, utBucket);
                    // Assert - Serialize
                }
                catch (Exception ex)
                {
                    Assert.Fail("Serialization failed for Bucket with message: " + ex.Message);
                }

                ms.Position = 0;

                try
                {
                    // Act - Deserialize
                    Bucket deserializedBucket = (Bucket)formatter.Deserialize(ms);

                    // Assert - Deserialize
                    Assert.IsTrue(utBucket.Equals(deserializedBucket), "Deserialized Bucket not equal to utBucket that was Serialized");
                }
                catch (Exception ex)
                {
                    Assert.Fail("Deserialization failed for Bucket with message: " + ex.Message);
                }
            }
        }
Ejemplo n.º 29
0
        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);
        }
Ejemplo n.º 30
0
        public void TestReservoireSuccessfulConstruction()
        {
            // Arrange
            Reservoire utReservoire = null;
            ReservoireState newState = new ReservoireState(0, 0), testState = null;

            try
            {
                // Act
                utReservoire = new Reservoire();
                testState = utReservoire.CurrentState;
            }
            catch (Exception ex)
            {
                Assert.Fail("Reservoire constructor threw an Exception - " + ex.GetType().Name + ": " + ex.Message);
            }

            // Assert
            Assert.IsNotNull(utReservoire, "Constructor for Reservoire returned null");
            Assert.AreEqual(0, utReservoire.VolumeUsed, "New Reservoire has VolumeUsed != 0");
            Assert.AreEqual(0, utReservoire.VolumeThrownOut, "New Reservoire has VolumeThrownOut != 0");
            Assert.AreEqual(newState.VolumeUsed, testState.VolumeUsed, "Test state VolumeUsed != New state VolumeUsed");
            Assert.AreEqual(newState.VolumeThrownOut, testState.VolumeThrownOut, "Test state VolumeThrownOut != New state VolumeThrownOut");
        }