Example #1
0
 public int FromBytes(byte[] data, int offset = 0, Encoding textEncoding = null)
 {
     ReservoireState rs = new ReservoireState();
     int updatedOffset = rs.FromBytes(data, offset, textEncoding);
     CurrentState = rs;
     return updatedOffset;
 }
Example #2
0
        public void TestReservoireStateConstructorZeros()
        {
            // Arrange
            ReservoireState utStateEmpty = null, utStateArgs = null;

            // Act
            utStateEmpty = new ReservoireState();
            utStateArgs = new ReservoireState(0, 0);

            // Assert
            Assert.IsNotNull(utStateEmpty, "Somehow new constructor for ReservoireState without arguments returned null");
            Assert.AreEqual(0, utStateEmpty.VolumeUsed, "VolumeUsed not equal to 0 passed to constructor without arguments");
            Assert.AreEqual(0, utStateEmpty.VolumeThrownOut, "VolumeThrownOut not equal to 0 passed to constructor without arguments");
            Assert.IsNotNull(utStateArgs, "Somehow new constructor for ReservoireState with arguments returned null");
            Assert.AreEqual(0, utStateArgs.VolumeUsed, "VolumeUsed not equal to 0 passed to constructor with arguments");
            Assert.AreEqual(0, utStateArgs.VolumeThrownOut, "VolumeThrownOut not equal to 0 passed to constructor with arguments");
        }
Example #3
0
        public void TestReservoireStateConstructorThrowsArgumentOutOfRangeExceptionForMinusOne()
        {
            // Arrange
            ReservoireState utStateMinusZero = null, utStateZeroMinus = null, utStateMinusMinus = null;

            // Act
            try
            {
                utStateMinusZero = new ReservoireState(-1, 0);
            }
            // Assert
            catch (ArgumentOutOfRangeException ex)
            {
                CaughtEx = true;
                Assert.AreEqual("volumeUsed", ex.ParamName, "ReservoireState constructor(-1, 0) did not identify volumeUsed param as out of range");
            }
            catch (Exception ex)
            {
                Assert.Fail("ReservoireState constructor(-1, 0) threw different Exception - " + ex.GetType().Name + ": " + ex.Message + " instead of ArgumentOutOfRangeException");
            }
            Assert.IsTrue(CaughtEx, "ReservoireState constructor(-1, 0) did not throw ArgumentOutOfRangeException");
            Assert.IsNull(utStateMinusZero, "ReservoireState constructor(-1, 0) did return an object");

            CaughtEx = false;

            // Act
            try
            {
                utStateZeroMinus = new ReservoireState(0, -1);
            }
            // Assert
            catch (ArgumentOutOfRangeException ex)
            {
                CaughtEx = true;
                Assert.AreEqual("volumeThrownOut", ex.ParamName, "ReservoireState constructor(0, -1) did not identify volumeThrownOut param as out of range");
            }
            catch (Exception ex)
            {
                Assert.Fail("ReservoireState constructor(0, -1) threw different Exception - " + ex.GetType().Name + ": " + ex.Message + " instead of ArgumentOutOfRangeException");
            }
            Assert.IsTrue(CaughtEx, "ReservoireState constructor(0, -1) did not throw ArgumentOutOfRangeException");
            Assert.IsNull(utStateZeroMinus, "ReservoireState constructor(0, -1) did return an object");

            CaughtEx = false;

            // Act
            try
            {
                utStateMinusMinus = new ReservoireState(-1, -1);
            }
            // Assert
            catch (ArgumentOutOfRangeException ex)
            {
                CaughtEx = true;
                Assert.AreEqual("volumeUsed", ex.ParamName, "ReservoireState constructor(-1, -1) did not identify volumeUsed param as out of range");
            }
            catch (Exception ex)
            {
                Assert.Fail("ReservoireState constructor(-1, -1) threw different Exception - " + ex.GetType().Name + ": " + ex.Message + " instead of ArgumentOutOfRangeException");
            }
            Assert.IsTrue(CaughtEx, "ReservoireState constructor(-1, -1) did not throw ArgumentOutOfRangeException");
            Assert.IsNull(utStateMinusMinus, "ReservoireState constructor(-1, -1) did return an object");
        }
Example #4
0
        public void TestReservoireStateConstructorThrowsArgumentOutOfRangeException()
        {
            // Arrange
            int numIterations = Rand.Next(5000, 10000);
            ReservoireState utStateMinusOk = null, utStateOkMinus = null, utStateMinusMinus = null;

            for (int i = 0; i < numIterations; i++)
            {
                // Arrange
                int vUsedMinus = Rand.Next(int.MinValue + 1, -2), vUsedOk = Rand.Next(),
                    vThrownOutMinus = Rand.Next(int.MinValue + 1, -2), vThrownOutOk = Rand.Next();

                // Act
                try
                {
                    utStateMinusOk = new ReservoireState(vUsedMinus, vThrownOutOk);
                }
                // Assert
                catch (ArgumentOutOfRangeException ex)
                {
                    CaughtEx = true;
                    Assert.AreEqual("volumeUsed", ex.ParamName, "ReservoireState constructor(" + vUsedMinus + ", " + vThrownOutOk + ") did not identify volumeUsed param as out of range [i=" + i + "/" + numIterations + "]");
                }
                catch (Exception ex)
                {
                    Assert.Fail("ReservoireState constructor(" + vUsedMinus + ", " + vThrownOutOk + ") threw different Exception - " + ex.GetType().Name + ": " + ex.Message + " instead of ArgumentOutOfRangeException [i=" + i + "/" + numIterations + "]");
                }
                Assert.IsTrue(CaughtEx, "ReservoireState constructor(" + vUsedMinus + ", " + vThrownOutOk + ") did not throw ArgumentOutOfRangeException [i=" + i + "/" + numIterations + "]");
                Assert.IsNull(utStateMinusOk, "ReservoireState constructor(" + vUsedMinus + ", " + vThrownOutOk + ") did return an object [i=" + i + "/" + numIterations + "]");

                CaughtEx = false;

                // Act
                try
                {
                    utStateOkMinus = new ReservoireState(vUsedOk, vThrownOutMinus);
                }
                // Assert
                catch (ArgumentOutOfRangeException ex)
                {
                    CaughtEx = true;
                    Assert.AreEqual("volumeThrownOut", ex.ParamName, "ReservoireState constructor(" + vUsedOk + ", " + vThrownOutMinus + ") did not identify volumeThrownOut param as out of range [i=" + i + "/" + numIterations + "]");
                }
                catch (Exception ex)
                {
                    Assert.Fail("ReservoireState constructor(" + vUsedOk + ", " + vThrownOutMinus + ") threw different Exception - " + ex.GetType().Name + ": " + ex.Message + " instead of ArgumentOutOfRangeException [i=" + i + "/" + numIterations + "]");
                }
                Assert.IsTrue(CaughtEx, "ReservoireState constructor(" + vUsedOk + ", " + vThrownOutMinus + ") did not throw ArgumentOutOfRangeException [i=" + i + "/" + numIterations + "]");
                Assert.IsNull(utStateOkMinus, "ReservoireState constructor(" + vUsedOk + ", " + vThrownOutMinus + ") did return an object [i=" + i + "/" + numIterations + "]");

                CaughtEx = false;

                // Act
                try
                {
                    utStateMinusMinus = new ReservoireState(vUsedMinus, vThrownOutMinus);
                }
                // Assert
                catch (ArgumentOutOfRangeException ex)
                {
                    CaughtEx = true;
                    Assert.AreEqual("volumeUsed", ex.ParamName, "ReservoireState constructor(" + vUsedMinus + ", " + vThrownOutMinus + ") did not identify volumeUsed param as out of range [i=" + i + "/" + numIterations + "]");
                }
                catch (Exception ex)
                {
                    Assert.Fail("ReservoireState constructor(" + vUsedMinus + ", " + vThrownOutMinus + ") threw different Exception - " + ex.GetType().Name + ": " + ex.Message + " instead of ArgumentOutOfRangeException [i=" + i + "/" + numIterations + "]");
                }
                Assert.IsTrue(CaughtEx, "ReservoireState constructor(" + vUsedMinus + ", " + vThrownOutMinus + ") did not throw ArgumentOutOfRangeException [i=" + i + "/" + numIterations + "]");
                Assert.IsNull(utStateMinusMinus, "ReservoireState constructor(" + vUsedMinus + ", " + vThrownOutMinus + ") did return an object [i=" + i + "/" + numIterations + "]");
            }
        }
Example #5
0
        public void TestReservoireStateConstructorSuccessful()
        {
            // Arrange
            ReservoireState utState = null;
            int numIterations = Rand.Next(5000, 10000);

            for (int i = 0; i < numIterations; i++)
            {
                // Act
                int vUsed = Rand.Next(), vThrownOut = Rand.Next();
                utState = new ReservoireState(vUsed, vThrownOut);

                // Assert
                Assert.IsNotNull(utState, "Somehow new constructor for ReservoireState returned null [" + i + "/" + numIterations + "]");
                Assert.AreEqual(vUsed, utState.VolumeUsed, "VolumeUsed not equal to value passed to constructor [" + i + "/" + numIterations + "]");
                Assert.AreEqual(vThrownOut, utState.VolumeThrownOut, "VolumeThrownOut not equal to value passed to constructor [" + i + "/" + numIterations + "]");
            }
        }
Example #6
0
        public void TestReservoireStateConstructorIntMax()
        {
            // Arrange
            ReservoireState utStateMaxZero = null, utStateZeroMax = null, utStateMaxMax = null;

            // Act
            utStateMaxZero = new ReservoireState(int.MaxValue, 0);
            utStateZeroMax = new ReservoireState(0, int.MaxValue);
            utStateMaxMax = new ReservoireState(int.MaxValue, int.MaxValue);

            // Assert
            Assert.IsNotNull(utStateMaxZero, "Somehow new constructor for ReservoireState without arguments returned null");
            Assert.AreEqual(int.MaxValue, utStateMaxZero.VolumeUsed, "VolumeUsed not equal to int.MaxValue passed to constructor without arguments");
            Assert.AreEqual(0, utStateMaxZero.VolumeThrownOut, "VolumeThrownOut not equal to 0 passed to constructor without arguments");
            Assert.IsNotNull(utStateZeroMax, "Somehow new constructor for ReservoireState with arguments returned null");
            Assert.AreEqual(0, utStateZeroMax.VolumeUsed, "VolumeUsed not equal to 0 passed to constructor with arguments");
            Assert.AreEqual(int.MaxValue, utStateZeroMax.VolumeThrownOut, "VolumeThrownOut not equal to int.MaxValue passed to constructor with arguments");
            Assert.IsNotNull(utStateMaxMax, "Somehow new constructor for ReservoireState without arguments returned null");
            Assert.AreEqual(int.MaxValue, utStateMaxMax.VolumeUsed, "VolumeUsed not equal to int.MaxValue passed to constructor without arguments");
            Assert.AreEqual(int.MaxValue, utStateMaxMax.VolumeThrownOut, "VolumeThrownOut not equal to int.MaxValue passed to constructor without arguments");
        }
Example #7
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");
        }
Example #8
0
 public int FromFrames(IEnumerable<Frame> frames, int offsetFrame = 0, Encoding textEncoding = null)
 {
     ReservoireState rs = new ReservoireState();
     int updatedOffset = rs.FromFrames(frames, offsetFrame, textEncoding);
     CurrentState = rs;
     return updatedOffset;
 }
Example #9
0
 public Reservoire(ReservoireState state)
 {
     VolumeUsed = state.VolumeUsed;
     VolumeThrownOut = state.VolumeThrownOut;
 }
Example #10
0
 public static ReservoireState GetFromBytes(byte[] data, ref int offset)
 {
     ReservoireState rs = new ReservoireState();
     offset = rs.FromBytes(data, offset);
     return rs;
 }
Example #11
0
        public int FromBytes(byte[] data, int offset = 0, Encoding textEncoding = null)
        {
            int updatedOffset = offset;
            textEncoding = textEncoding ?? Encoding.UTF8;
            int numBuckets = BitConverter.ToInt32(data, updatedOffset);
            updatedOffset += 4;

            var buckets = new List<Bucket>(numBuckets);
            for (int b = 0; b < numBuckets; b++)
            {
                buckets.Add(Bucket.GetFromBytes(data, ref updatedOffset, textEncoding));
            }
            BucketState = buckets;
            SourceState = ReservoireState.GetFromBytes(data, ref updatedOffset);
            return updatedOffset;
        }
Example #12
0
 public SolutionResult(int numberOfActions, ReservoireState endingReservoireState)
 {
     NumberOfActions = numberOfActions;
     EndingReservoireState = endingReservoireState;
 }