public void DoubleN_MinMax()
        {
            BVector d     = new BVector();
            double? max   = double.MaxValue;
            double? min   = double.MinValue;
            double? zero  = (double)0;
            double? @null = null;


            d.Add(max);
            d.Add1(false);
            d.Add(@null);
            d.Add1(true);
            d.Add(zero);
            d.Add1(false);
            d.Add(min);

            Assert.AreEqual(max, d.GetDoubleN());
            Assert.AreEqual(false, d.Get1());
            Assert.AreEqual(@null, d.GetDoubleN());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual(zero, d.GetDoubleN());
            Assert.AreEqual(false, d.Get1());
            Assert.AreEqual(min, d.GetDoubleN());
        }
Ejemplo n.º 2
0
        public void DynamicInt()
        {
            var rnd       = new CryptoRandom();
            int itemCount = 1_000_000;
            int value     = 0;

            int[]   values = new int[itemCount];
            BVector d      = new BVector();

            for (int i = 0; i < itemCount; i++)
            {
                value = (int)rnd.NextLong(int.MinValue, int.MaxValue);
                d.DynamicAdd(value);
                d.Add1();
                values[i] = value;
            }
            d.DynamicAdd(int.MinValue);
            d.Add1();
            d.DynamicAdd(int.MaxValue);
            d.Add1();
            d.DynamicAdd((int)0);


            for (uint i = 0; i < itemCount; i++)
            {
                Assert.AreEqual(values[i], d.DynamicGetInt());
                Assert.AreEqual(true, d.Get1());
            }
            Assert.AreEqual(int.MinValue, d.DynamicGetInt());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual(int.MaxValue, d.DynamicGetInt());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual((int)0, d.DynamicGetInt());
        }
Ejemplo n.º 3
0
        public void DynamicShort()
        {
            var   rnd   = new CryptoRandom();
            short value = 0;

            short[] values = new short[ushort.MaxValue];
            int     index  = 0;
            BVector d      = new BVector();

            for (int i = short.MinValue; i < short.MaxValue; i++)
            {
                value = (short)rnd.NextLong(short.MinValue, short.MaxValue);
                d.DynamicAdd(value);
                d.Add1();
                values[index++] = (short)value;
            }
            d.DynamicAdd(short.MinValue);
            d.Add1();
            d.DynamicAdd(short.MaxValue);
            d.Add1();
            d.DynamicAdd((short)0);

            index = 0;
            for (int i = 0; i < values.Length; i++)
            {
                Assert.AreEqual(values[i], d.DynamicGetShort());
                Assert.AreEqual(true, d.Get1());
            }
            Assert.AreEqual(short.MinValue, d.DynamicGetShort());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual(short.MaxValue, d.DynamicGetShort());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual((short)0, d.DynamicGetShort());
        }
Ejemplo n.º 4
0
        public void DecimalN_MinMax()
        {
            BVector  d     = new BVector();
            decimal? max   = decimal.MaxValue;
            decimal? min   = decimal.MinValue;
            decimal? zero  = (decimal)0;
            decimal? @null = null;


            d.Add(max);
            d.Add1(false);
            d.Add(@null);
            d.Add1(true);
            d.Add(zero);
            d.Add1(false);
            d.Add(min);

            Assert.AreEqual(max, d.GetDecimalN());
            Assert.AreEqual(false, d.Get1());
            Assert.AreEqual(@null, d.GetDecimalN());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual(zero, d.GetDecimalN());
            Assert.AreEqual(false, d.Get1());
            Assert.AreEqual(min, d.GetDecimalN());
        }
Ejemplo n.º 5
0
        public void Int_AddGet_BitsMinMax_Separated_Loopback()
        {
            int maxBits = 32;
            List<int> values = new List<int>();
            BVector d = new BVector();
            int positiveValue;
            int negativeValue;
            int actualValue;
            int index;
            int expected;
            int maxVal = int.MaxValue;
            byte bits;


            // add min and max values for 2-32 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-32 bits
            index = 0;
            for (int i = 0; i < (maxBits - 1); i++)
            {
                bits = (byte)(maxBits - i);

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

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

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

                // false separator
                Assert.AreEqual(false, d.Get1());
            }
        }
Ejemplo n.º 6
0
        public void Decimal_Rnd()
        {
            BVector        d = new BVector();
            decimal        value;
            const decimal  maxValue = decimal.MaxValue;
            const decimal  minValue = decimal.MinValue;
            var            rnd      = new CryptoRandom();
            List <decimal> values   = new List <decimal>();

            // number of items to add per bit
            int itemsCount = 1_000;

            for (int i = 0; i < itemsCount; i++)
            {
                // add positive
                value = (decimal)rnd.NextDouble() * maxValue;
                d.Add(value);
                values.Add(value);
                d.Add1(false);

                // add negative
                value = (decimal)rnd.NextDouble() * minValue;
                d.Add(value);
                values.Add(value);
                d.Add1(true);
            }

            for (int i = 0; i < itemsCount; i++)
            {
                value = d.GetDecimal();
                Assert.AreEqual(values[i], value);
                Assert.AreEqual(i % 2 != 0, d.Get1());
            }
        }
Ejemplo n.º 7
0
        public void DynamicUShort()
        {
            var    rnd       = new CryptoRandom();
            int    itemCount = ushort.MaxValue;
            ushort value     = 0;

            ushort[] values = new ushort[itemCount];
            BVector  d      = new BVector();

            for (uint i = 0; i < itemCount; i++)
            {
                value = (ushort)rnd.NextLong(0, ushort.MaxValue);
                d.DynamicAdd(value);
                d.Add1();
                values[i] = value;
            }
            d.DynamicAdd(ushort.MinValue);
            d.Add1();
            d.DynamicAdd(ushort.MaxValue);

            for (uint i = 0; i < itemCount; i++)
            {
                Assert.AreEqual(values[i], d.DynamicGetUShort());
                Assert.AreEqual(true, d.Get1());
            }
            Assert.AreEqual(ushort.MinValue, d.DynamicGetUShort());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual(ushort.MaxValue, d.DynamicGetUShort());
        }
Ejemplo n.º 8
0
        public void DynamicSByte()
        {
            var   rnd       = new CryptoRandom();
            int   itemCount = byte.MaxValue;
            sbyte value     = 0;

            sbyte[] values = new sbyte[itemCount];
            BVector d      = new BVector();

            for (uint i = 0; i < itemCount; i++)
            {
                value = (sbyte)rnd.NextLong(sbyte.MinValue, sbyte.MaxValue);
                d.DynamicAdd(value);
                d.Add1();
                values[i] = value;
            }
            d.DynamicAdd(sbyte.MinValue);
            d.Add1();
            d.DynamicAdd(sbyte.MaxValue);
            d.Add1();
            d.DynamicAdd((sbyte)0);

            for (uint i = 0; i < itemCount; i++)
            {
                Assert.AreEqual(values[i], d.DynamicGetSByte());
                Assert.AreEqual(true, d.Get1());
            }
            Assert.AreEqual(sbyte.MinValue, d.DynamicGetSByte());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual(sbyte.MaxValue, d.DynamicGetSByte());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual((sbyte)0, d.DynamicGetSByte());
        }
Ejemplo n.º 9
0
        public void DynamicLong()
        {
            var  rnd       = new CryptoRandom();
            long itemCount = 1_000_000;
            long value     = 0;

            long[]  values = new long[itemCount];
            BVector d      = new BVector();

            for (long i = 0; i < itemCount; i++)
            {
                value = (long)rnd.NextLong(long.MinValue, long.MaxValue);
                d.DynamicAdd(value);
                d.Add1();
                values[i] = value;
            }
            d.DynamicAdd(long.MinValue);
            d.Add1();
            d.DynamicAdd(long.MaxValue);
            d.Add1();
            d.DynamicAdd((long)0);


            for (uint i = 0; i < itemCount; i++)
            {
                Assert.AreEqual(values[i], d.DynamicGetLong());
                Assert.AreEqual(true, d.Get1());
            }
            Assert.AreEqual(long.MinValue, d.DynamicGetLong());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual(long.MaxValue, d.DynamicGetLong());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual((long)0, d.DynamicGetLong());
        }
Ejemplo n.º 10
0
        public void Ext_ULong_AddGet_BitsMinMax_InOut()
        {
            int     maxBits = 64;
            BVector d       = new BVector();
            ulong   value;
            ulong   val;
            ulong   expected;
            ulong   maxVal = ulong.MaxValue;
            byte    bits;


            // add min and max values for 2-32 bits
            for (int i = 0; i < (maxBits - 1); i++)
            {
                bits  = (byte)(maxBits - i);
                value = maxVal >> i;
                // add max
                d.Add(value, bits);
            }

            // add min and max values for 2-32 bits seperated by 1 bit
            d.Add1(false);
            for (int i = 0; i < (maxBits - 1); i++)
            {
                bits  = (byte)(maxBits - i);
                value = maxVal >> i;
                // add max
                d.Add(value, bits);
                d.Add1(false);
            }

            BVector d2 = new BVector(d.ToBytes());

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

                // get max
                expected = maxVal >> i;
                val      = d2.GetULong(bits);
                Assert.AreEqual(expected, val);
            }

            // get min and max values for 2-32 bits seperated by 1 bit
            Assert.AreEqual(false, d2.Get1());
            for (int i = 0; i < (maxBits - 1); i++)
            {
                bits = (byte)(maxBits - i);

                // get max
                expected = maxVal >> i;
                val      = d2.GetULong(bits);
                Assert.AreEqual(expected, val);
                Assert.AreEqual(false, d2.Get1());
            }
        }
Ejemplo n.º 11
0
        public void Ext_UInt_AddGet_BitsMax_InOut()
        {
            BVector d = new BVector();
            uint    value;
            uint    val;
            uint    expected;
            uint    maxVal = uint.MaxValue;
            byte    bits;


            // add max values for 2-32 bits
            for (int i = 0; i < 31; i++)
            {
                bits  = (byte)(32 - i);
                value = maxVal >> i;
                // add max
                d.Add(value, bits);
            }

            // add max values for 2-32 bits seperated by 1 bit
            d.Add1(false);
            for (int i = 0; i < 31; i++)
            {
                bits  = (byte)(32 - i);
                value = maxVal >> i;
                // add max
                d.Add(value, bits);
                d.Add1(false);
            }

            BVector d2 = new BVector(d.ToBytes());

            // get max values for 2-32 bits
            for (int i = 0; i < 31; i++)
            {
                bits = (byte)(32 - i);

                // get max
                expected = maxVal >> i;
                val      = d2.GetUInt(bits);
                Assert.AreEqual(expected, val);
            }

            // get max values for 2-32 bits seperated by 1 bit
            Assert.AreEqual(false, d2.Get1());
            for (int i = 0; i < 31; i++)
            {
                bits = (byte)(32 - i);

                // get max
                expected = maxVal >> i;
                val      = d2.GetUInt(bits);
                Assert.AreEqual(expected, val);
                Assert.AreEqual(false, d2.Get1());
            }
        }
Ejemplo n.º 12
0
        public void Basic_1_AddGet_RandomSequence_InOut()
        {
            var         rnd = new CryptoRandom();
            bool        value;
            List <bool> values   = new List <bool>();
            int         bitCount = 1_000;

            BVector d = new BVector();

            // create sequenece and add
            for (int i = 0; i < bitCount; i++)
            {
                value = rnd.Next(100) > 50;
                values.Add(value);
                d.Add1(value);
            }

            BVector d2 = new BVector(d.ToBytes());

            Assert.AreEqual(d.ToBytes().SequenceEqual(d2.ToBytes()), true);


            // verify
            for (int i = 0; i < bitCount; i++)
            {
                Assert.AreEqual(values[i], d2.Get1());
            }
        }
Ejemplo n.º 13
0
        public void Basic_8_AddGet_Safe_Random_InOut()
        {
            int         index    = 0;
            byte        maxValue = byte.MaxValue;
            byte        value;
            byte        expected;
            var         rnd    = new CryptoRandom();
            List <byte> values = new List <byte>();

            // number of items to add per bit
            int     itemsCount = 1_000;
            BVector d          = new BVector();

            for (int i = 0; i < itemsCount; i++)
            {
                value = (byte)rnd.NextLong(maxValue);
                d.Add8(value);
                values.Add(value);
                // add 1 bit to split bytes
                d.Add1(false);
            }

            BVector d2 = new BVector(d.ToBytes());

            for (int i = 0; i < itemsCount; i++)
            {
                value    = d2.Get8();
                expected = values[index];
                Assert.AreEqual(expected, value);
                Assert.AreEqual(false, d2.Get1());
                index++;
            }
        }
Ejemplo n.º 14
0
        public void Basic_64_AddGet_Safe_Random_InOut()
        {
            var          rnd    = new CryptoRandom();
            List <ulong> values = new List <ulong>();
            ulong        value;
            int          valueCount = 1_000;
            BVector      d          = new BVector();

            for (int i = 0; i < valueCount; i++)
            {
                value = (ulong)rnd.NextLong(int.MaxValue, long.MaxValue);
                d.AddU64(value);
                values.Add(value);
                // add 1 bit to split bytes
                d.Add1(false);
            }

            BVector d2 = new BVector(d.ToBytes());

            for (int i = 0; i < valueCount; i++)
            {
                Assert.AreEqual(values[i], d2.GetU64());
                Assert.AreEqual(false, d2.Get1());
            }
        }
Ejemplo n.º 15
0
        public void SerialAddGetAll()
        {
            var rnd = new CryptoRandom();
            // using 2 BVectors cause of the extensive length required
            BVector d  = new BVector();
            BVector d2 = new BVector();

            for (ushort i = 0; i < ushort.MaxValue; i++)
            {
                if (i % 2 == 0)
                {
                    d.SerialAdd(i);
                    d.Add1(rnd.Next(100) < 50);
                }
                else
                {
                    d2.SerialAdd(i);
                    d2.Add1(rnd.Next(100) < 50);
                }
            }

            for (ushort i = 0; i < ushort.MaxValue; i++)
            {
                if (i % 2 == 0)
                {
                    Assert.AreEqual(d.SerialGet(), i);
                    d.Get1();
                }
                else
                {
                    Assert.AreEqual(d2.SerialGet(), i);
                    d2.Get1();
                }
            }
        }
Ejemplo n.º 16
0
        public void Basic_32_AddGet_UintMinMaxZero_InOut()
        {
            List <uint> values = new List <uint>();
            BVector     d      = new BVector();

            d.AddU32(uint.MinValue);
            d.AddU32(uint.MaxValue);
            d.AddU32(0);

            // add 1 bit to test byte split
            d.Add1(false);
            d.AddU32(uint.MinValue);
            d.AddU32(uint.MaxValue);
            d.AddU32(0);

            BVector d2 = new BVector(d.ToBytes());

            Assert.AreEqual(uint.MinValue, d2.GetU32());
            Assert.AreEqual(uint.MaxValue, d2.GetU32());
            Assert.AreEqual((uint)0, d2.GetU32());
            Assert.AreEqual(false, d2.Get1());
            Assert.AreEqual(uint.MinValue, d2.GetU32());
            Assert.AreEqual(uint.MaxValue, d2.GetU32());
            Assert.AreEqual((uint)0, d2.GetU32());
        }
Ejemplo n.º 17
0
        public void DateTimeN_MinMax()
        {
            DateTime? max   = DateTime.MaxValue;
            DateTime? min   = DateTime.MinValue;
            DateTime? @null = null;
            BVector   d     = new BVector();

            d.Add(max);
            d.Add1(false);
            d.Add(@null);
            d.Add1(false);
            d.Add(min);

            Assert.AreEqual(max, d.GetDateTimeN());
            Assert.AreEqual(false, d.Get1());
            Assert.AreEqual(@null, d.GetDateTimeN());
            Assert.AreEqual(false, d.Get1());
            Assert.AreEqual(min, d.GetDateTimeN());
        }
Ejemplo n.º 18
0
        public void Double_MinMax()
        {
            BVector d    = new BVector();
            double  max  = double.MaxValue;
            double  min  = double.MinValue;
            double  zero = (double)0;

            d.Add(max);
            d.Add1(false);
            d.Add(zero);
            d.Add1(true);
            d.Add(min);

            Assert.AreEqual(max, d.GetDouble());
            Assert.AreEqual(false, d.Get1());
            Assert.AreEqual(zero, d.GetDouble());
            Assert.AreEqual(true, d.Get1());
            Assert.AreEqual(min, d.GetDouble());
        }
Ejemplo n.º 19
0
        public void Basic_64_AddGet_Safe_BitMaxValues_InOut()
        {
            List <uint> values = new List <uint>();
            BVector     d      = new BVector();

            for (int i = 0; i < 32; i++)
            {
                byte bits = (byte)(64 - i);
                d.AddU64(ulong.MaxValue >> i, bits);
            }

            // add 1 bit to test byte split
            d.Add1(false);
            for (int i = 0; i < 32; i++)
            {
                byte bits = (byte)(64 - i);
                d.AddU64(ulong.MaxValue >> i, bits);
                d.Add1(false);
            }

            BVector d2 = new BVector(d.ToBytes());

            for (int i = 0; i < 32; i++)
            {
                byte  bits     = (byte)(64 - i);
                ulong expected = ulong.MaxValue >> i;
                ulong val      = d2.GetU64(bits);
                Assert.AreEqual(expected, val);
            }

            Assert.AreEqual(false, d2.Get1());
            for (int i = 0; i < 32; i++)
            {
                byte  bits     = (byte)(64 - i);
                ulong expected = ulong.MaxValue >> i;
                ulong val      = d2.GetU64(bits);
                Assert.AreEqual(expected, val);
                Assert.AreEqual(false, d2.Get1());
            }
        }
Ejemplo n.º 20
0
        public void Basic_32_AddGet_Safe_BitMaxValues_InOut()
        {
            List <uint> values = new List <uint>();
            BVector     d      = new BVector();

            for (int i = 0; i < 32; i++)
            {
                byte bits = (byte)(32 - i);
                d.AddU32(uint.MaxValue >> i, bits);
            }

            // add 1 bit to test byte split - false to ensure there are 0 bits
            d.Add1(false);
            for (int i = 0; i < 32; i++)
            {
                byte bits = (byte)(32 - i);
                d.AddU32(uint.MaxValue >> i, bits);
                d.Add1(false);
            }

            BVector d2 = new BVector(d.ToBytes());

            for (int i = 0; i < 32; i++)
            {
                byte bits     = (byte)(32 - i);
                uint expected = uint.MaxValue >> i;
                uint val      = d2.GetU32(bits);
                Assert.AreEqual(expected, val);
            }
            Assert.AreEqual(false, d2.Get1());
            for (int i = 0; i < 32; i++)
            {
                byte bits     = (byte)(32 - i);
                uint expected = uint.MaxValue >> i;
                uint val      = d2.GetU32(bits);
                Assert.AreEqual(expected, val);
                Assert.AreEqual(false, d2.Get1());
            }
        }
Ejemplo n.º 21
0
        public void Basic_32_AddGet_Safe_BitMaxValues_Loopback()
        {
            List <uint> values = new List <uint>();
            BVector     d      = new BVector();

            for (int i = 0; i < 32; i++)
            {
                byte bits = (byte)(32 - i);
                d.AddU32(uint.MaxValue >> i, bits);
            }

            // add 1 bit to test byte split
            d.Add1(false);
            for (int i = 0; i < 32; i++)
            {
                byte bits = (byte)(32 - i);
                d.AddU32(uint.MaxValue >> i, bits);
                d.Add1(false);
            }

            for (int i = 0; i < 32; i++)
            {
                byte bits     = (byte)(32 - i);
                uint expected = uint.MaxValue >> i;
                uint val      = d.GetU32(bits);
                Assert.AreEqual(expected, val);
            }

            Assert.AreEqual(false, d.Get1());
            for (int i = 0; i < 32; i++)
            {
                byte bits     = (byte)(32 - i);
                uint expected = uint.MaxValue >> i;
                uint val      = d.GetU32(bits);
                Assert.AreEqual(expected, val);
                Assert.AreEqual(false, d.Get1());
            }
        }
Ejemplo n.º 22
0
        public void DateTime_MinMax()
        {
            DateTime max = DateTime.MaxValue;
            DateTime min = DateTime.MinValue;
            BVector  d   = new BVector();

            d.Add(max);
            d.Add1(false);
            d.Add(min);

            Assert.AreEqual(max, d.GetDateTime());
            Assert.AreEqual(false, d.Get1());
            Assert.AreEqual(min, d.GetDateTime());
        }
Ejemplo n.º 23
0
        public void TimeSpan_MinMax()
        {
            TimeSpan max = TimeSpan.MaxValue;
            TimeSpan min = TimeSpan.MinValue;
            BVector  d   = new BVector();

            d.Add(max);
            d.Add1(false);
            d.Add(min);

            Assert.AreEqual(max, d.GetTimeSpan());
            Assert.AreEqual(false, d.Get1());
            Assert.AreEqual(min, d.GetTimeSpan());
        }
Ejemplo n.º 24
0
        public void DateTimeN_Current()
        {
            DateTime? dt    = DateTime.Now;
            DateTime? @null = null;
            BVector   d     = new BVector();

            d.Add(@null);
            d.Add1(false);
            d.Add(dt);


            Assert.AreEqual(@null, d.GetDateTimeN());
            Assert.AreEqual(false, d.Get1());
            Assert.AreEqual(dt, d.GetDateTimeN());
        }
Ejemplo n.º 25
0
        public void Ext_UInt_AddGet_Rnd_InOut()
        {
            int         maxBits = 32;
            int         index   = 0;
            byte        bits;
            uint        value;
            uint        expected;
            const uint  maxValue = uint.MaxValue;
            uint        rngMax;
            var         rnd    = new CryptoRandom();
            List <uint> values = new List <uint>();

            // number of items to add per bit
            int     itemsCount = 1_000;
            BVector d          = new BVector();

            for (int j = 2; j <= maxBits; j++)
            {
                rngMax = maxValue >> (maxBits - j);
                bits   = (byte)j;
                for (int i = 0; i < itemsCount; i++)
                {
                    // add positive
                    value = (uint)rnd.NextLong(rngMax);
                    d.Add(value, bits);
                    values.Add(value);
                    d.Add1(false);
                }
            }

            BVector d2 = d;

            for (int j = 2; j < maxBits; j++)
            {
                bits = (byte)j;
                for (int i = 0; i < itemsCount; i++)
                {
                    // read positive
                    value    = d2.GetUInt(bits);
                    expected = values[index];
                    Assert.AreEqual(expected, value);
                    Assert.AreEqual(false, d2.Get1());
                    index++;
                }
            }
        }
Ejemplo n.º 26
0
        public void Basic_1_AddGet_FalseSequence_Loopback()
        {
            int     bitCount = 1_000;
            BVector d        = new BVector();

            for (int i = 0; i < bitCount; i++)
            {
                d.Add1(false);
            }

            List <bool> result = new List <bool>(bitCount);

            for (int i = 0; i < bitCount; i++)
            {
                Assert.AreEqual(false, d.Get1());
            }
        }
Ejemplo n.º 27
0
        public void Basic_64_AddGet_Safe_Random_Loopback()
        {
            int          maxBits = 64;
            int          index   = 0;
            byte         bits;
            const long   maxValue = long.MaxValue;
            long         rngMax;
            ulong        value;
            ulong        expected;
            var          rnd    = new CryptoRandom();
            List <ulong> values = new List <ulong>();

            // number of items to add per bit
            int     itemsCount = 1_000;
            BVector d          = new BVector();

            for (int j = 2; j <= maxBits; j++)
            {
                rngMax = (maxValue >> (maxBits - j));
                bits   = (byte)j;
                for (int i = 0; i < itemsCount; i++)
                {
                    value = (ulong)rnd.NextLong(rngMax) + (ulong)rnd.NextLong(rngMax) + (ulong)rnd.Next(2);
                    d.AddU64(value, bits);
                    values.Add(value);
                    // add 1 bit to split bytes
                    d.Add1(false);
                }
            }

            BVector d2 = d;

            for (int j = 2; j <= maxBits; j++)
            {
                bits = (byte)j;
                for (int i = 0; i < itemsCount; i++)
                {
                    value    = d2.GetU64(bits);
                    expected = values[index];
                    Assert.AreEqual(expected, value);
                    Assert.AreEqual(false, d2.Get1());
                    index++;
                }
            }
        }
Ejemplo n.º 28
0
        public void Basic_32_AddGet_Safe_Random_Loopback()
        {
            int         maxBits = 32;
            int         index   = 0;
            byte        bits;
            uint        maxValue = 0;
            uint        value;
            uint        expected;
            var         rnd    = new CryptoRandom();
            List <uint> values = new List <uint>();

            // number of items to add per bit
            int     itemsCount = 1_000;
            BVector d          = new BVector();

            for (int j = 2; j <= maxBits; j++)
            {
                maxValue = (uint)Math.Pow(2, j);
                bits     = (byte)j;
                for (int i = 0; i < itemsCount; i++)
                {
                    value = (uint)rnd.NextLong(maxValue);
                    d.AddU32(value, bits);
                    values.Add(value);
                    // add 1 bit to split bytes
                    d.Add1(false);
                }
            }

            BVector d2 = d;

            for (int j = 2; j <= maxBits; j++)
            {
                bits = (byte)j;
                for (int i = 0; i < itemsCount; i++)
                {
                    value    = d2.GetU32(bits);
                    expected = values[index];
                    Assert.AreEqual(expected, value);
                    Assert.AreEqual(false, d2.Get1());
                    index++;
                }
            }
        }
Ejemplo n.º 29
0
        public void Basic_1_AddGet_AlternatingSequence_Loopback()
        {
            bool    value    = false;
            int     bitCount = 1_000;
            BVector d        = new BVector();

            for (int i = 0; i < bitCount; i++)
            {
                d.Add1(!value);
            }


            value = false;
            List <bool> result = new List <bool>(bitCount);

            for (int i = 0; i < bitCount; i++)
            {
                Assert.AreEqual(!value, d.Get1());
            }
        }
Ejemplo n.º 30
0
        public void Basic_1_AddGet_Safe_TrueSequence_InOut()
        {
            int     bitCount = 1_000;
            BVector d        = new BVector();

            for (int i = 0; i < bitCount; i++)
            {
                d.Add1(true);
            }

            BVector d2 = new BVector(d.ToBytes());

            Assert.AreEqual(d.ToBytes().SequenceEqual(d2.ToBytes()), true);

            List <bool> result = new List <bool>(bitCount);

            for (int i = 0; i < bitCount; i++)
            {
                Assert.AreEqual(true, d2.Get1());
            }
        }