public void Simple() { var indirectCodec = new IndirectCodec(typeof(int)); var manualCodecOverIndirect = new ManualCodec <int>(codecContainer, indirectCodec); var data = manualCodecOverIndirect.EncodeSingle(1231231231); var result = manualCodecOverIndirect.DecodeSingle(data); Assert.That(result, Is.EqualTo(1231231231)); }
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)); }
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)); }
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)); } } }
private void DoTestDecode(bool fast) { const int expectedValue = 123123; const int expectedPointerDistance = 234; const int expetedRemainingBytesDistance = 345; var emittingCodec = Substitute.For <IEmittingCodec>(); emittingCodec.When(x => x.EmitDecode(Arg.Any <ILGenerator>(), Arg.Any <ILocalVariableCollection>(), fast)) .Do(x => { var il = x.Arg <ILGenerator>(); var locals = x.Arg <LocalVariableCollection>(); il.Emit_Ldc_I4(expectedValue); il.Emit(OpCodes.Newobj, MyClass.Constructor); il.Emit_IncreasePointer(locals.DataPointer, expectedPointerDistance); il.Emit_DecreaseInteger(locals.RemainingBytes, expetedRemainingBytesDistance); }); var manualCodec = new ManualCodec <MyClass>(emittingCodec); int value; int pointerDistance; int remainingBytesDistance; var data = new byte[4]; fixed(byte *pData = data) { var p = pData; int remainingBytes = 0; value = manualCodec.Decode(ref p, ref remainingBytes, fast).Value; pointerDistance = (int)(p - pData); remainingBytesDistance = -remainingBytes; } Assert.That(value, Is.EqualTo(expectedValue)); Assert.That(pointerDistance, Is.EqualTo(expectedPointerDistance)); Assert.That(remainingBytesDistance, Is.EqualTo(expetedRemainingBytesDistance)); }
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())); }
private void DoTestDecode(bool fast) { const int expectedValue = 123123; const int expectedPointerDistance = 234; const int expetedRemainingBytesDistance = 345; var emittingCodec = Substitute.For <IEmittingCodec>(); emittingCodec.When(x => x.EmitDecode(Arg.Any <IEmittingContext>(), fast)).Do(x => { var context = x.Arg <EmittingContextBase>(); var il = context.IL; il.Ldc_I4(expectedValue); il.Newobj(MyClass.Constructor); il.IncreasePointer(context.DataPointerVar, expectedPointerDistance); il.DecreaseInteger(context.RemainingBytesVar, expetedRemainingBytesDistance); }); var manualCodec = new ManualCodec <MyClass>(null, emittingCodec); int value; int pointerDistance; int remainingBytesDistance; var data = new byte[4]; fixed(byte *pData = data) { var p = pData; int remainingBytes = 0; value = manualCodec.Decode(ref p, ref remainingBytes, fast).Value; pointerDistance = (int)(p - pData); remainingBytesDistance = -remainingBytes; } Assert.That(value, Is.EqualTo(expectedValue)); Assert.That(pointerDistance, Is.EqualTo(expectedPointerDistance)); Assert.That(remainingBytesDistance, Is.EqualTo(expetedRemainingBytesDistance)); }
public void Encode() { const int expectedValue = 123123; const int expectedPointerDistance = 234; var emittingCodec = Substitute.For <IEmittingCodec>(); emittingCodec.WhenForAnyArgs(x => x.EmitEncode(null, 0)) .Do(x => { var context = x.Arg <IEmittingContext>(); var il = context.IL; il.Ldloc(context.DataPointerVar); il.Ldc_I4(expectedValue); il.Stind_I4(); il.IncreasePointer(context.DataPointerVar, expectedPointerDistance); }); var manualCodec = new ManualCodec <MyClass>(null, emittingCodec); int value; long pointerDistance; var data = new byte[4]; fixed(byte *pData = data) { var p = pData; manualCodec.Encode(ref p, new MyClass(222)); value = *(int *)pData; pointerDistance = p - pData; } Assert.That(value, Is.EqualTo(expectedValue)); Assert.That(pointerDistance, Is.EqualTo(expectedPointerDistance)); }
public void Encode() { const int expectedValue = 123123; const int expectedPointerDistance = 234; var emittingCodec = Substitute.For <IEmittingCodec>(); emittingCodec.WhenForAnyArgs(x => x.EmitEncode(null, null, 0)) .Do(x => { var il = x.Arg <ILGenerator>(); var locals = x.Arg <LocalVariableCollection>(); il.Emit(OpCodes.Ldloc, locals.DataPointer); il.Emit_Ldc_I4(expectedValue); il.Emit(OpCodes.Stind_I4); il.Emit_IncreasePointer(locals.DataPointer, expectedPointerDistance); }); var manualCodec = new ManualCodec <MyClass>(emittingCodec); int value; long pointerDistance; var data = new byte[4]; fixed(byte *pData = data) { var p = pData; manualCodec.Encode(ref p, new MyClass(222)); value = *(int *)pData; pointerDistance = p - pData; } Assert.That(value, Is.EqualTo(expectedValue)); Assert.That(pointerDistance, Is.EqualTo(expectedPointerDistance)); }
public void Encode() { const int expectedValue = 123123; const int expectedPointerDistance = 234; var emittingCodec = Substitute.For<IEmittingCodec>(); emittingCodec.WhenForAnyArgs(x => x.EmitEncode(null, 0)) .Do(x => { var context = x.Arg<IEmittingContext>(); var il = context.IL; il.Ldloc(context.DataPointerVar); il.Ldc_I4(expectedValue); il.Stind_I4(); il.IncreasePointer(context.DataPointerVar, expectedPointerDistance); }); var manualCodec = new ManualCodec<MyClass>(null, emittingCodec); int value; long pointerDistance; var data = new byte[4]; fixed (byte* pData = data) { var p = pData; manualCodec.Encode(ref p, new MyClass(222)); value = *(int*)pData; pointerDistance = p - pData; } Assert.That(value, Is.EqualTo(expectedValue)); Assert.That(pointerDistance, Is.EqualTo(expectedPointerDistance)); }
private void DoTestDecode(bool fast) { const int expectedValue = 123123; const int expectedPointerDistance = 234; const int expetedRemainingBytesDistance = 345; var emittingCodec = Substitute.For<IEmittingCodec>(); emittingCodec.When(x => x.EmitDecode(Arg.Any<IEmittingContext>(), fast)).Do(x => { var context = x.Arg<EmittingContextBase>(); var il = context.IL; il.Ldc_I4(expectedValue); il.Newobj(MyClass.Constructor); il.IncreasePointer(context.DataPointerVar, expectedPointerDistance); il.DecreaseInteger(context.RemainingBytesVar, expetedRemainingBytesDistance); }); var manualCodec = new ManualCodec<MyClass>(null, emittingCodec); int value; int pointerDistance; int remainingBytesDistance; var data = new byte[4]; fixed (byte* pData = data) { var p = pData; int remainingBytes = 0; value = manualCodec.Decode(ref p, ref remainingBytes, fast).Value; pointerDistance = (int)(p - pData); remainingBytesDistance = -remainingBytes; } Assert.That(value, Is.EqualTo(expectedValue)); Assert.That(pointerDistance, Is.EqualTo(expectedPointerDistance)); Assert.That(remainingBytesDistance, Is.EqualTo(expetedRemainingBytesDistance)); }
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())); }
public void Encode() { const int expectedValue = 123123; const int expectedPointerDistance = 234; var emittingCodec = Substitute.For<IEmittingCodec>(); emittingCodec.WhenForAnyArgs(x => x.EmitEncode(null, null, 0)) .Do(x => { var il = x.Arg<ILGenerator>(); var locals = x.Arg<LocalVariableCollection>(); il.Emit(OpCodes.Ldloc, locals.DataPointer); il.Emit_Ldc_I4(expectedValue); il.Emit(OpCodes.Stind_I4); il.Emit_IncreasePointer(locals.DataPointer, expectedPointerDistance); }); var manualCodec = new ManualCodec<MyClass>(emittingCodec); int value; long pointerDistance; var data = new byte[4]; fixed (byte* pData = data) { var p = pData; manualCodec.Encode(ref p, new MyClass(222)); value = *(int*)pData; pointerDistance = p - pData; } Assert.That(value, Is.EqualTo(expectedValue)); Assert.That(pointerDistance, Is.EqualTo(expectedPointerDistance)); }
private void DoTestDecode(bool fast) { const int expectedValue = 123123; const int expectedPointerDistance = 234; const int expetedRemainingBytesDistance = 345; var emittingCodec = Substitute.For<IEmittingCodec>(); emittingCodec.When(x => x.EmitDecode(Arg.Any<ILGenerator>(), Arg.Any<ILocalVariableCollection>(), fast)) .Do(x => { var il = x.Arg<ILGenerator>(); var locals = x.Arg<LocalVariableCollection>(); il.Emit_Ldc_I4(expectedValue); il.Emit(OpCodes.Newobj, MyClass.Constructor); il.Emit_IncreasePointer(locals.DataPointer, expectedPointerDistance); il.Emit_DecreaseInteger(locals.RemainingBytes, expetedRemainingBytesDistance); }); var manualCodec = new ManualCodec<MyClass>(emittingCodec); int value; int pointerDistance; int remainingBytesDistance; var data = new byte[4]; fixed (byte* pData = data) { var p = pData; int remainingBytes = 0; value = manualCodec.Decode(ref p, ref remainingBytes, fast).Value; pointerDistance = (int)(p - pData); remainingBytesDistance = -remainingBytes; } Assert.That(value, Is.EqualTo(expectedValue)); Assert.That(pointerDistance, Is.EqualTo(expectedPointerDistance)); Assert.That(remainingBytesDistance, Is.EqualTo(expetedRemainingBytesDistance)); }