Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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());
            }
        }
Ejemplo n.º 8
0
        public void Basic_1_AddGet_AlternatingSequence_InOut()
        {
            bool    value    = false;
            int     bitCount = 1_000;
            BVector d        = new BVector();

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

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

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


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

            for (int i = 0; i < bitCount; i++)
            {
                Assert.AreEqual(!value, d2.Get1());
            }
        }
Ejemplo n.º 9
0
        public void Basic_32_AddGet_Safe_Random_InOut()
        {
            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     valueCount = 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 < valueCount; i++)
                {
                    value = (uint)rnd.NextLong(maxValue);
                    d.AddU32(value, bits);
                    values.Add(value);
                    // add 1 bit to split bytes
                    d.Add1(false);
                }
            }

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

            for (int j = 2; j < maxBits; j++)
            {
                bits = (byte)j;
                for (int i = 0; i < valueCount; i++)
                {
                    value    = d2.GetU32(bits);
                    expected = values[index];
                    Assert.AreEqual(expected, value);
                    Assert.AreEqual(false, d2.Get1());
                    index++;
                }
            }
        }
Ejemplo n.º 10
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.º 11
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.º 12
0
        public void Ext_Short_AddGet_BitsMinMax_InOut()
        {
            int     maxBits = 16;
            BVector d       = new BVector();
            short   value;
            short   val;
            short   expected;
            short   maxVal = short.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 = (short)(maxVal >> i);
                // add max
                d.Add(value, bits);

                // add min
                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 = (short)(maxVal >> i);
                // add max
                d.Add(value, bits);
                d.Add1(false);

                // add min
                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 = (short)(maxVal >> i);
                val      = d2.GetShort(bits);
                Assert.AreEqual(expected, val);

                // get min
                expected = (short)-expected;
                val      = d2.GetShort(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 = (short)(maxVal >> i);
                val      = d2.GetShort(bits);
                Assert.AreEqual(expected, val);
                Assert.AreEqual(false, d2.Get1());

                // get min
                expected = (short)-expected;
                val      = d2.GetShort(bits);
                Assert.AreEqual(expected, val);
                Assert.AreEqual(false, d2.Get1());
            }
        }
Ejemplo n.º 13
0
        public void Ext_Short_AddGet_Rnd_InOut()
        {
            BVector      d           = new BVector();
            var          rnd         = new CryptoRandom();
            List <short> values      = new List <short>();
            int          maxBits     = 16;
            int          index       = 0;
            int          itemsPerBit = 100;
            short        value;
            short        val;
            short        expected;
            short        maxVal = short.MaxValue;
            short        tmp;
            byte         bits;


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

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

            // add random values for 2-32 bits seperated by 1 bit
            for (int j = 0; j < itemsPerBit; j++)
            {
                d.Add1(false);
                for (int i = 0; i < (maxBits - 2); i++)
                {
                    bits = (byte)(maxBits - i);
                    tmp  = (short)(maxVal >> (i + 1));

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

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

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

                    // get max
                    expected = values[index];
                    val      = d2.GetShort(bits);
                    Assert.AreEqual(expected, val);
                    index++;
                }
            }

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

                    // get max
                    expected = values[index];
                    val      = d2.GetShort(bits);
                    Assert.AreEqual(expected, val);
                    Assert.AreEqual(false, d2.Get1());
                    index++;
                }
            }
        }
Ejemplo n.º 14
0
        public void Ext_ULong_AddGet_Rnd_InOut()
        {
            BVector      d           = new BVector();
            var          rnd         = new CryptoRandom();
            List <ulong> values      = new List <ulong>();
            int          maxBits     = 64;
            int          index       = 0;
            int          itemsPerBit = 100;
            ulong        value;
            ulong        val;
            ulong        expected;
            long         maxVal = long.MaxValue;
            long         tmp;
            byte         bits;


            // add random values for 2-32 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 = (ulong)rnd.NextLong(0, tmp) + (ulong)rnd.NextLong(0, tmp);
                    values.Add(value);
                    d.Add(value, bits);
                }
            }

            // add random values for 2-32 bits seperated by 1 bit
            for (int j = 0; j < itemsPerBit; j++)
            {
                d.Add1(false);
                for (int i = 0; i < (maxBits - 2); i++)
                {
                    bits = (byte)(maxBits - i);
                    tmp  = maxVal >> (i + 1);

                    // add random value
                    value = (ulong)rnd.NextLong(0, tmp) + (ulong)rnd.NextLong(0, tmp);
                    values.Add(value);
                    d.Add(value, bits);
                    d.Add1(false);
                }
            }

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

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

                    // get max
                    expected = values[index];
                    val      = d2.GetULong(bits);
                    Assert.AreEqual(expected, val);
                    index++;
                }
            }

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

                    // get max
                    expected = values[index];
                    val      = d2.GetULong(bits);
                    Assert.AreEqual(expected, val);
                    Assert.AreEqual(false, d2.Get1());
                    index++;
                }
            }
        }