public void Long_AddGet_BitsMinMax_Separated_Loopback()
        {
            int         maxBits = 64;
            List <long> values  = new List <long>();
            BVector     d       = new BVector();
            long        positiveValue;
            long        negativeValue;
            long        actualValue;
            int         index;
            long        expected;
            long        maxVal = long.MaxValue;
            byte        bits;


            // add min and max values for 2-64 bits
            for (int i = 0; i < (maxBits - 1); i++)
            {
                bits          = (byte)(maxBits - i);
                positiveValue = maxVal >> i;
                negativeValue = -positiveValue - 1;

                // add positive
                d.Add(positiveValue, bits);
                values.Add(positiveValue);

                // true separator
                d.Add1();

                // add negative
                d.Add(negativeValue, bits);
                values.Add(negativeValue);

                // false separator
                d.Add1(false);
            }


            // get min and max values for 2-64 bits
            index = 0;
            for (int i = 0; i < (maxBits - 1); i++)
            {
                bits = (byte)(maxBits - i);

                // get positive
                expected    = values[index++];
                actualValue = d.GetLong(bits);
                Assert.AreEqual(expected, actualValue);

                // true separator
                Assert.AreEqual(true, d.Get1());

                // get min
                expected    = values[index++];
                actualValue = d.GetLong(bits);
                Assert.AreEqual(expected, actualValue);

                // false separator
                Assert.AreEqual(false, d.Get1());
            }
        }
        public void Long_Get_Exc_LessThan2Bits_Loopback()
        {
            BVector d = new BVector();

            d.Add(0, 32);
            d.GetLong(1);
        }
        public void Long_Get_Exc_MoreThan64Bits_Loopback()
        {
            BVector d = new BVector();

            d.Add(0, 32);
            d.GetLong(65);
        }
        public void Long_AddGet_Rnd_Sequence_Loopback()
        {
            int         maxBits = 64;
            List <long> values  = new List <long>();
            BVector     d       = new BVector();
            long        actualValue;
            int         index;
            long        expected;
            long        maxVal = long.MaxValue;
            byte        bits;
            var         rnd         = new CryptoRandom();
            int         itemsPerBit = 10000;
            long        value;
            long        tmp;


            // add random values for 2-64 bits
            for (int j = 0; j < itemsPerBit; j++)
            {
                for (int i = 0; i < (maxBits - 2); i++)
                {
                    bits = (byte)(maxBits - i);
                    tmp  = maxVal >> (i + 1);

                    // add random value
                    value = rnd.NextLong(-tmp - 1, tmp);
                    values.Add(value);
                    d.Add(value, bits);
                }
            }

            // get values for 2-64 bits
            index = 0;
            for (int j = 0; j < itemsPerBit; j++)
            {
                for (int i = 0; i < (maxBits - 2); i++)
                {
                    bits = (byte)(maxBits - i);

                    // get value
                    expected    = values[index];
                    actualValue = d.GetLong(bits);
                    Assert.AreEqual(expected, actualValue);
                    index++;
                }
            }
        }
Example #5
0
        /// <summary>
        /// Reads the next random value added by <see cref="WriteRandomValue(BVector)"/>
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static object ReadNextRandomValue(BVector d)
        {
            bool nullable  = d.Get1();
            byte valueType = d.Get8();

            switch (valueType)
            {
            case 0:
                if (nullable)
                {
                    return(d.GetIntN());
                }
                return(d.GetInt());

            case 1:
                if (nullable)
                {
                    return(d.GetLongN());
                }
                return(d.GetLong());

            case 2:
                if (nullable)
                {
                    return(d.GetShortN());
                }
                return(d.GetShort());

            case 3:
                if (nullable)
                {
                    return(d.GetByteN());
                }
                return(d.GetByte());

            case 4:
                return(d.GetString());

            case 5:
                return(d.GetAscii());

            case 6:
                if (nullable)
                {
                    return(d.GetDateTimeN());
                }
                return(d.GetDateTime());

            case 7:
                if (nullable)
                {
                    return(d.GetDecimalN());
                }
                return(d.GetDecimal());

            case 8:
                if (nullable)
                {
                    return(d.GetDoubleN());
                }
                return(d.GetDouble());

            case 9:
                return(d.Get1());

            case 10:
                return(d.GetTimeSpan());

            case 11:
                return(d.GetByteArray());
            }
            return(null);
        }