Ejemplo n.º 1
0
        protected void DoTest <T>(IEmittingCodec codec, T value, Action <T, T> assert)
        {
            Assert.That(codec.Type, Is.EqualTo(typeof(T)));

            var manualCodec = new ManualCodec <T>(CodecContainer, codec);

            if (codec.FixedSize.HasValue)
            {
                Assert.That(manualCodec.CalculateSize(value), Is.EqualTo(codec.FixedSize));
            }
            int size = manualCodec.CalculateSize(value);

            var data = new byte[size];

            fixed(byte *pData = data)
            {
                var p = pData;

                manualCodec.Encode(ref p, value);
                Assert.That(p - pData, Is.EqualTo(size));

                p = pData;
                int remainingBytes = size;
                var decodedValue   = manualCodec.Decode(ref p, ref remainingBytes, false);

                assert(decodedValue, value);
                Assert.That(p - pData, Is.EqualTo(size));
                Assert.That(remainingBytes, Is.EqualTo(0));

                p = pData;
                remainingBytes = size;
                decodedValue   = manualCodec.Decode(ref p, ref remainingBytes, true);
                assert(decodedValue, value);
                Assert.That(p - pData, Is.EqualTo(size));
                Assert.That(remainingBytes, Is.EqualTo(0));

                if (size > 0)
                {
                    p = pData;
                    remainingBytes = size - 1;
                    Assert.Throws <InvalidDataException>(() => manualCodec.Decode(ref p, ref remainingBytes, false));
                }
            }
        }
Ejemplo n.º 2
0
        public void CalculateSize()
        {
            var item = new MyClass(222);
            const int size = 123123;

            var emittingCodec = Substitute.For<IEmittingCodec>();
            emittingCodec.WhenForAnyArgs(x => x.EmitCalculateSize(null, null)).Do(x =>
                {
                    var context = x.Arg<IEmittingContext>();
                    context.IL.Ldc_I4(size);
                });
            var manualCodec = new ManualCodec<MyClass>(null, emittingCodec);

            Assert.That(manualCodec.CalculateSize(item), Is.EqualTo(size));
        }
Ejemplo n.º 3
0
        public void CalculateSize()
        {
            var       item = new MyClass(222);
            const int size = 123123;

            var emittingCodec = Substitute.For <IEmittingCodec>();

            emittingCodec.WhenForAnyArgs(x => x.EmitCalculateSize(null, null)).Do(x =>
            {
                var context = x.Arg <IEmittingContext>();
                context.IL.Ldc_I4(size);
            });
            var manualCodec = new ManualCodec <MyClass>(null, emittingCodec);

            Assert.That(manualCodec.CalculateSize(item), Is.EqualTo(size));
        }
Ejemplo n.º 4
0
        public void UseOwnCodecContainer()
        {
            var innerCodec = Substitute.For <IManualCodec <int> >();

            var codecContainer = Substitute.For <ICodecContainer>();

            codecContainer.GetManualCodecFor <int>().Returns(innerCodec);

            var emittingCodec = Substitute.For <IEmittingCodec>();

            emittingCodec.WhenForAnyArgs(x => x.EmitCalculateSize(null, null)).Do(x =>
            {
                var context = x.Arg <IEmittingContext>();
                context.EmitLoadManualCodecFor(typeof(int));
                context.IL.Callvirt(typeof(object).GetMethod("GetHashCode"));
            });

            var manualCodec = new ManualCodec <object>(codecContainer, emittingCodec);
            var result      = manualCodec.CalculateSize(null);

            Assert.That(result, Is.EqualTo(innerCodec.GetHashCode()));
        }
Ejemplo n.º 5
0
        public void UseOwnCodecContainer()
        {
            var innerCodec = Substitute.For<IManualCodec<int>>();

            var codecContainer = Substitute.For<ICodecContainer>();
            codecContainer.GetManualCodecFor<int>().Returns(innerCodec);

            var emittingCodec = Substitute.For<IEmittingCodec>();
            emittingCodec.WhenForAnyArgs(x => x.EmitCalculateSize(null, null)).Do(x =>
            {
                var context = x.Arg<IEmittingContext>();
                context.EmitLoadManualCodecFor(typeof(int));
                context.IL.Callvirt(typeof(object).GetMethod("GetHashCode"));
            });

            var manualCodec = new ManualCodec<object>(codecContainer, emittingCodec);
            var result = manualCodec.CalculateSize(null);

            Assert.That(result, Is.EqualTo(innerCodec.GetHashCode()));
        }