Ejemplo n.º 1
0
        public unsafe void UnsignedValuesUnsafe()
        {
            using (var codec = new CodecWriter(BufferSize))
            {
                foreach (var valList in BatchGroup(TestValuesGenerator(), codec.BufferSize / 10))
                {
                    codec.Count = 0;
                    foreach (ulong val in valList)
                        codec.WriteUnsignedValue(val);

                    fixed(byte *pbuf = codec.Buffer)
                    {
                        int pos = 0;

                        foreach (ulong val in valList)
                        {
                            if (val != CodecReader.ReadUnsignedValueUnsafe(pbuf, ref pos))
                            {
                                Assert.Fail("Failed ulong {0:X}", val);
                            }
                        }
                        codec.Count = pos;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public unsafe void CompareLoopAndSequenceUnsigned()
        {
            const int bufSize  = BufferSize;
            const int valCount = bufSize / 10;
            const int runs     = 100;

            Stopwatch sw;

            using (var codec = new CodecWriter(bufSize))
            {
                ulong[] valList = TestValuesGenerator().Take(valCount).ToArray();
                foreach (ulong val in valList)
                    codec.WriteUnsignedValue(val);

                // precache
                fixed(byte *buff2 = codec.Buffer)
                {
                    int pos = 0;

                    CodecReader.ReadSignedValueUnsafe(buff2, ref pos);
                    CodecReader.ReadUnsignedValueUnsafe(buff2, ref pos);
                }

                sw = Stopwatch.StartNew();
                for (int i = 0; i < runs; i++)
                {
                    fixed(byte *pbuff = codec.Buffer)
                    {
                        int pos = 0;

                        for (int r = 0; r < valCount; r++)
                        {
                            CodecReader.ReadSignedValueUnsafe(pbuff, ref pos);
                        }
                    }
                }
                Console.WriteLine("{0} ReadSignedValueUnsafe() time", sw.Elapsed);

                sw.Restart();
                for (int i = 0; i < runs; i++)
                {
                    fixed(byte *pbuff = codec.Buffer)
                    {
                        int pos = 0;

                        for (int r = 0; r < valCount; r++)
                        {
                            CodecReader.ReadUnsignedValueUnsafe(pbuff, ref pos);
                        }
                    }
                }
            }
            Console.WriteLine("{0} ReadUnsignedValueUnsafe() time", sw.Elapsed);
        }
Ejemplo n.º 3
0
        public unsafe void ErrorValuesTests()
        {
            var buf = new byte[10];

            for (int i = 0; i < buf.Length; i++)
            {
                buf[i] = 0xFF;
            }

            int pos = 0;

            // ReSharper disable AccessToModifiedClosure
            Assert.Throws <SerializerException>(
                () =>
            {
                fixed(byte *pbuf = buf)
                CodecReader.ReadSignedValueUnsafe(pbuf, ref pos);
            });
            // ReSharper restore AccessToModifiedClosure

            Assert.AreEqual(0, pos, "ReadSignedValueUnsafe pos shifted");

            Assert.Throws <SerializerException>(
                () =>
            {
                fixed(byte *pbuf = buf)
                CodecReader.ReadUnsignedValueUnsafe(pbuf, ref pos);
            });

            Assert.AreEqual(0, pos, "ReadSignedValueUnsafe pos shifted");


            using (var codecRdr = new CodecReader(new ArraySegment <byte>(buf)))
            {
                // ReSharper disable AccessToDisposedClosure
                Assert.Throws <SerializerException>(() => codecRdr.ReadSignedValue());
                // ReSharper restore AccessToDisposedClosure
                Assert.AreEqual(0, codecRdr.BufferPos, "ReadSignedValue pos shifted");

                // ReSharper disable AccessToDisposedClosure
                Assert.Throws <SerializerException>(() => codecRdr.ReadUnsignedValue());
                // ReSharper restore AccessToDisposedClosure
                Assert.AreEqual(0, codecRdr.BufferPos, "ReadUnsignedValue pos shifted");
            }
        }