Esempio n. 1
0
        public void GetSizeTest()
        {
            var stream = new FastStream();
            int s1     = stream.GetSize();

            stream.Write((byte)1);
            int s2 = stream.GetSize();

            stream.Write(1);
            int s3 = stream.GetSize();

            Assert.Equal(0, s1);
            Assert.Equal(1, s2);
            Assert.Equal(5, s3);
        }
Esempio n. 2
0
        private uint[] Convert8To11(FastStream stream)
        {
            using Sha256 sha = new Sha256();
            byte[] hash = sha.ComputeHash(stream.ToByteArray());

            int bitSize = stream.GetSize() * 8;
            int cs      = 11 - (bitSize % 11);

            if (cs < 4)
            {
                cs += 11;
            }
            Debug.Assert(cs >= 4 && cs <= 14);
            stream.Write(hash, 0, cs > 8 ? 2 : 1);

            bitSize += cs;
            int wordCount = bitSize / 11;

            Debug.Assert(bitSize % 11 == 0);

            byte[] ba   = stream.ToByteArray();
            uint[] bits = new uint[(int)Math.Ceiling((double)bitSize / 32)];
            for (int i = 0, j = 0; j < bits.Length; i += 4, j++)
            {
                bits[j] = (uint)(ba[i + 3] | (ba[i + 2] << 8) | (ba[i + 1] << 16) | (ba[i] << 24));
            }

            int itemIndex = 0;
            int bitIndex  = 0;
            // Number of bits in a word
            int toTake = 11;
            // UInt32 is 32 bit!
            int maxBits = 32;

            uint[] wordIndexes = new uint[wordCount];
            for (int i = 0; i < wordIndexes.Length; i++)
            {
                if (bitIndex + toTake <= maxBits)
                {
                    wordIndexes[i] = (bits[itemIndex] << bitIndex) >> (maxBits - toTake);
                }
                else
                {
                    wordIndexes[i] = ((bits[itemIndex] << bitIndex) >> (maxBits - toTake)) |
                                     (bits[itemIndex + 1] >> (maxBits - toTake + maxBits - bitIndex));
                }

                bitIndex += toTake;
                if (bitIndex >= maxBits)
                {
                    bitIndex -= maxBits;
                    itemIndex++;
                }
            }

            return(wordIndexes);
        }
Esempio n. 3
0
        public void AddSerializedSizeTest(ulong val, int init, int expected)
        {
            var ci      = new CompactInt(val);
            var counter = new SizeCounter(init);

            ci.AddSerializedSize(counter);

            var stream = new FastStream(10);

            ci.WriteToStream(stream);
            Assert.Equal(expected, stream.GetSize() + init);

            Assert.Equal(expected, counter.Size);
        }
Esempio n. 4
0
        public void AddWithStackIntLengthTest(int init, int add)
        {
            var counter = new SizeCounter(init);

            counter.AddWithStackIntLength(add);

            var si     = new StackInt(add);
            var stream = new FastStream(5);

            si.WriteToStream(stream);
            int expected = stream.GetSize() + add + init;

            Assert.Equal(expected, counter.Size);
        }
Esempio n. 5
0
        public void AddCompactIntCountTest(int init, int add)
        {
            var counter = new SizeCounter(init);

            counter.AddCompactIntCount(add);

            var ci     = new CompactInt(add);
            var stream = new FastStream(9);

            ci.WriteToStream(stream);
            int expected = stream.GetSize() + init;

            Assert.Equal(expected, counter.Size);
        }
Esempio n. 6
0
        public void Resize_bigTest()
        {
            var stream  = new FastStream();
            int dataLen = FastStream.DefaultCapacity + (FastStream.DefaultCapacity + 5);

            byte[] data = Helper.GetBytes(dataLen);
            stream.Write(data);

            int actualSize   = stream.GetSize();
            int expectedSize = dataLen;

            byte[] expBuffer = new byte[dataLen];
            Buffer.BlockCopy(data, 0, expBuffer, 0, data.Length);

            Assert.Equal(expectedSize, actualSize);
            Helper.ComparePrivateField(stream, "buffer", expBuffer);
        }