static void TestComposeStateSync(Protocol proto, String fileName, Func <mtest.publishable_dictionary> getState) { mtest.publishable_dictionary_publisher publ = new mtest.publishable_dictionary_publisher(); mtest.publishable_dictionary data = getState(); publ.debugOnlySetData(data); SimpleBuffer buffer = new SimpleBuffer(); IPublishableComposer composer = makePublishableComposer(proto, buffer); publ.generateStateSyncMessage(composer); // uncomment to update file if (WriteFiles) { buffer.writeToFile(fileName); } SimpleBuffer expected = SimpleBuffer.readFromFile(fileName); if (proto == Protocol.Json) { Assert.True(SimpleBuffer.AreEqualIgnoreEol(expected, buffer)); } else if (proto == Protocol.Gmq) { Assert.AreEqual(expected, buffer); } }
static void deliverAllMessages(MetaPool pool, MwsrThreadQueue msgQueue, String filePrefix, ref int msgCnt) { ThreadQueueItem[] messages = new ThreadQueueItem[16]; while (true) { int popped = msgQueue.pop_front(messages); if (popped == 0) { break; } for (int i = 0; i < popped; ++i) { pool.onMessage(messages[i].msg); ++msgCnt; if (filePrefix != null) { SimpleBuffer buffer = (SimpleBuffer)messages[i].msg; String fileName = TestCommon.DataPathPrefix + filePrefix + msgCnt + ".json"; // uncomment to update file //buffer.writeToFile(fileName); SimpleBuffer expected = SimpleBuffer.readFromFile(fileName); Assert.True(SimpleBuffer.AreEqualIgnoreEol(expected, buffer)); } } } }
public void AppendTest() { string correct = "0000: __| __|0x02|0x03 - \n" + "0004: 0x04|0x05|0x06|0x07 - 04*05*\n" + "0008: 0x08|0x09|0x0A|0x0B - 08*09*\n" + "000C: 0x0C|0x0D|0x0E|0x10 - 0C*0D*\n" + "0010: 0x10|0x11|0x12|0x13 - 10*11*\n" + "0014: 0x14| __| __| __ - 14* \n"; MemoryStream ms = new MemoryStream(); TextExportBuilder teb = new TextExportBuilder(ms); InterpretedPatternExporter ipe = new InterpretedPatternExporter(teb); IBuffer buffer = new SimpleBuffer(); buffer.Append(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 16, 17, 18, 19, 20 }, 0, 21); ipe.Pattern = "%A\"4\"%[%O\"4\"%: %E\"4\"e\"_\"p\"0x\"x\"|\"%] - %E\"2\"s\"*\"%%I\"2\"%\n"; bool cancelled = false; ipe.Export(buffer, 2, buffer.Size - 1, ref cancelled); Assert.AreEqual(correct, ASCIIEncoding.ASCII.GetString(ms.ToArray()), "#1"); }
/// <summary> /// <para>从指定的<paramref name="buffer"/>反序列化给定类型的值</para> /// <para>Deserializes the value of the given type from the specified <paramref name="buffer"/></para> /// </summary> /// <param name="buffer">反序列化所需要的的二进制数组. The binary array to deserialize from</param> /// <param name="bufOffset">数组的起始位置. array start</param> /// <param name="readSize">读取的字节数. read buffer size</param> /// <param name="option">使用的配置,如果为<c>null</c>,则使用默认配置. The options. Use <c>null</c> to use default options</param> /// <param name="cancellationToken">取消该操作的令牌. The token to cancel the operation</param> /// <returns>反序列化的值. The deserialized value</returns> public static T Deserialize <T>(byte[] buffer, int bufOffset, out int readSize, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { if (buffer == null) { throw new ArgumentException(nameof(buffer)); } if (bufOffset > buffer.Length - 1) { throw new ArgumentException(nameof(bufOffset)); } if (option == null) { option = BssomSerializerOptions.Default; } BssomDeserializeContext context = new BssomDeserializeContext(option, cancellationToken); SimpleBuffer buf = new SimpleBuffer(buffer, bufOffset); BssomReader reader = new BssomReader(buf); T value = context.Option.FormatterResolver.GetFormatterWithVerify <T>().Deserialize(ref reader, ref context); readSize = (int)buf.Position; return(value); }
static void TestParseUpdate(TestCommon.Protocol proto, String fileNameInit, String fileName, Func <mtest.publishable_dictionary> getState, Action <mtest.Ipublishable_dictionary> updateDelegate) { mtest.publishable_dictionary_subscriber subs = new mtest.publishable_dictionary_subscriber(); //mtest.publishable_dictionary data = getState(); //subs.debugOnlySetData(data); SimpleBuffer bufferInit = SimpleBuffer.readFromFile(fileNameInit); IPublishableParser parserInit = TestCommon.makePublishableParser(proto, bufferInit.getReadIterator()); subs.applyStateSyncMessage(parserInit); mtest.publishable_dictionary expected = getState(); Assert.True(expected.isEquivalent(subs)); SimpleBuffer buffer = SimpleBuffer.readFromFile(fileName); IPublishableParser parser = TestCommon.makePublishableParser(proto, buffer.getReadIterator()); subs.applyMessageWithUpdates(parser); Assert.False(expected.isEquivalent(subs)); updateDelegate(expected); Assert.True(expected.isEquivalent(subs)); }
public globalmq.marshalling.SimpleBuffer tryGetNextMessage() { int size = 0; ErrorCodeT retCode = getNextMessageSize(handle, out size); checkError(retCode); // throws in case of retCode != 0 if (size == 0) { return(null); } byte[] arr = new byte[size]; int bytesCopied = 0; retCode = getNextMessage(handle, arr, size, out bytesCopied); checkError(retCode); // throws in case of retCode != 0 Debug.Assert(bytesCopied == size); globalmq.marshalling.SimpleBuffer sb = new SimpleBuffer(); sb.setInternalBuffer(arr, size); return(sb); }
static void TestComposeUpdate(TestCommon.Protocol proto, String fileName, Func <mtest.publishable_dictionary> getState, Action <mtest.Ipublishable_dictionary> updateDelegate) { mtest.publishable_dictionary_publisher publ = new mtest.publishable_dictionary_publisher(); mtest.publishable_dictionary data = getState(); publ.debugOnlySetData(data); SimpleBuffer buffer = new SimpleBuffer(); IPublishableComposer composer = TestCommon.makePublishableComposer(proto, buffer); publ.startTick(composer); updateDelegate(publ); publ.endTick(); if (WriteFiles) { buffer.writeToFile(fileName); } SimpleBuffer expected = SimpleBuffer.readFromFile(fileName); if (proto == TestCommon.Protocol.Json) { Assert.True(SimpleBuffer.AreEqualIgnoreEol(expected, buffer)); } else if (proto == TestCommon.Protocol.Gmq) { Assert.AreEqual(expected, buffer); } }
static void gmq2json(string input, string output) { SimpleBuffer buffer = SimpleBuffer.readFromFile(input); GmqParser parser = new GmqParser(buffer.getReadIterator()); mtest.message_one msg = new mtest.message_one(); mtest.message_one_message.parse(parser, msg); SimpleBuffer buffer2 = new SimpleBuffer(); JsonComposer composer = new JsonComposer(buffer2); mtest.message_one_message.compose(composer, firstParam: msg.firstParam, secondParam: msg.secondParam, thirdParam: msg.thirdParam, forthParam: msg.forthParam, fifthParam: msg.fifthParam, sixthParam: msg.sixthParam, seventhParam: msg.seventhParam, eighthParam: msg.eighthParam, ninethParam: msg.ninethParam, tenthParam: msg.tenthParam ); buffer2.writeToFile(output); }
internal static void TestParseUpdate(ITestPlatformSupport platform, String fileNameInit, String fileName, Action <mtest.IStructSix> updateDelegate) { mtest.StructSix_subscriber subs = new mtest.StructSix_subscriber(); //subs.debugOnlySetData(GetPublishableSix()); SimpleBuffer bufferInit = SimpleBuffer.readFromFile(fileNameInit); IPublishableParser parserInit = platform.makePublishableParser(bufferInit.getReadIterator()); subs.applyStateSyncMessage(parserInit); SimpleBuffer buffer = SimpleBuffer.readFromFile(fileName); IPublishableParser parser = platform.makePublishableParser(buffer.getReadIterator()); subs.applyMessageWithUpdates(parser); //mtest.StructSix actual = ( mtest.StructSix)subs.debugOnlyGetData(); mtest.StructSix expected = GetPublishableSix(); if (updateDelegate != null) { // not equal yet Assert.False(subs.isEquivalent(expected)); updateDelegate(expected); } Assert.True(subs.isEquivalent(expected)); }
public void TryReadFixedRefIsCorrectly() { var buf = new SimpleBuffer(new byte[5], 2); buf.TryReadFixedRef(3, out bool b1); VerifyHelper.Throws <BssomSerializationOperationException>(() => buf.TryReadFixedRef(4, out bool b2), e => e.ErrorCode == BssomSerializationOperationException.SerializationErrorCode.ReachedEndOfBuffer); }
private void CreateBaseObject(Vector3 pos, string name) { IBufferable bufferable = SimpleBuffer.GetObject(name); Transform trm = bufferable.GetTransform(); trm.SetParent(this.GetTransform()); trm.localPosition = pos; }
public static SimpleBuffer Initialize() { if (m_Instance == null) { m_Instance = new SimpleBuffer(); } return(m_Instance); }
public void DeserializeTestWithT_IBssomBuffer_Type() { int val = int.MaxValue; var buf = BssomSerializer.Serialize(val); var sbuf = new SimpleBuffer(buf); ((int)BssomSerializer.Deserialize(sbuf, typeof(int))).Is(int.MaxValue); }
public void DeserializeTestWithT_IBssomBuffer() { int val = int.MaxValue; var buf = BssomSerializer.Serialize(val); var sbuf = new SimpleBuffer(buf); BssomSerializer.Deserialize <int>(sbuf).Is(int.MaxValue); }
public void DeserializeTestWithT_IBssomBuffer_Type_Context() { int val = int.MaxValue; var buf = BssomSerializer.Serialize(val); var sbuf = new SimpleBuffer(buf); var context = new BssomDeserializeContext(); ((int)BssomSerializer.Deserialize(ref context, sbuf, typeof(int))).Is(int.MaxValue); }
static void TestParseComposeUpdate(TestCommon.Protocol proto, String fileNameInit, String fileName, Action <mtest.Ipublishable_html_tag> updateDelegate) { mtest.publishable_html_tag_publisher publ = new mtest.publishable_html_tag_publisher(); SimpleBuffer bufferInit = SimpleBuffer.readFromFile(fileNameInit); IPublishableParser parserInit = TestCommon.makePublishableParser(proto, bufferInit.getReadIterator()); publ.applyStateSyncMessage(parserInit); mtest.publishable_html_tag_subscriber subs = new mtest.publishable_html_tag_subscriber(); SimpleBuffer bufferInit2 = SimpleBuffer.readFromFile(fileNameInit); IPublishableParser parserInit2 = TestCommon.makePublishableParser(proto, bufferInit2.getReadIterator()); subs.applyStateSyncMessage(parserInit2); // init state at publ and subs are equal Assert.True(publ.isEquivalent(subs)); SimpleBuffer buffer = new SimpleBuffer(); IPublishableComposer composer = TestCommon.makePublishableComposer(proto, buffer); publ.startTick(composer); updateDelegate(publ); publ.endTick(); if (WriteFiles) { buffer.writeToFile(fileName); } SimpleBuffer expected = SimpleBuffer.readFromFile(fileName); if (proto == TestCommon.Protocol.Json) { Assert.True(SimpleBuffer.AreEqualIgnoreEol(expected, buffer)); } else if (proto == TestCommon.Protocol.Gmq) { Assert.AreEqual(expected, buffer); } // now state is different Assert.False(publ.isEquivalent(subs)); IPublishableParser parser = TestCommon.makePublishableParser(proto, buffer.getReadIterator()); subs.applyMessageWithUpdates(parser); // now state is equal Assert.True(publ.isEquivalent(subs)); }
public void Read(ref Dictionary <string, object> value) { int count; Stream.ReadInt32(out count); if (value == null) { value = new Dictionary <string, object>(); } for (var i = 0; i < count; i++) { string key; Stream.ReadString(out key); byte type; Stream.ReadByte(out type); switch (type) { case 1: string strVal; Stream.ReadString(out strVal); value[key] = strVal; break; case 2: int intVal; Stream.ReadInt32(out intVal); value[key] = intVal; break; case 3: float fVal; Stream.ReadSingle(out fVal); value[key] = fVal; break; case 4: SimpleBuffer sb = new SimpleBuffer(); Read(ref sb); value[key] = sb; break; case 5: bool bVal; Stream.ReadBoolean(out bVal); value[key] = bVal; break; default: throw new Exception("Encountered (read) unknown dictionary entry type " + type); } } }
public static void TestGmqParse() { SimpleBuffer buffer = SimpleBuffer.readFromFile(PathGmq); GmqParser parser = new GmqParser(buffer.getReadIterator()); mtest.SimpleStruct msg = new mtest.SimpleStruct(); mtest.SimpleStruct_message.parse(parser, msg); Assert.True(GetSample1().isEquivalent(msg)); }
public static void TestJsonParse() { SimpleBuffer buffer = SimpleBuffer.readFromFile(PathJson); JsonParser parser = new JsonParser(buffer.getReadIterator()); mtest.struct_one msg = new mtest.struct_one(); mtest.struct_one_message.parse(parser, msg); Assert.AreEqual(msg, GetSampleData()); }
static void TestJsonParse(Func <mtest.struct_dictionary> sampleData, string fileName) { SimpleBuffer buffer = SimpleBuffer.readFromFile(fileName); JsonParser parser = new JsonParser(buffer.getReadIterator()); mtest.struct_dictionary msg = new mtest.struct_dictionary(); mtest.struct_dictionary_message.parse(parser, msg); Assert.AreEqual(msg, sampleData()); }
public static void TestVlqEncoding(UInt64 val) { SimpleBuffer buffer = new SimpleBuffer(); IntegralVlq.writeVlqIntegral(buffer, val); UInt64 result = IntegralVlq.readVlqIntegral(buffer.getReadIterator()); Assert.AreEqual(val, result); }
public void MakePrivateCopyOfData() { SimpleBuffer sb = new SimpleBuffer(); sb.AppendBuffer(buffer, start, Size); buffer = sb; start = 0; end = sb.Size - 1; }
static void TestGmqParse(Func <mtest.struct_du> sampleData, string fileName) { SimpleBuffer buffer = SimpleBuffer.readFromFile(fileName); GmqParser parser = new GmqParser(buffer.getReadIterator()); mtest.struct_du msg = new mtest.struct_du(); mtest.struct_du_message.parse(parser, msg); Assert.AreEqual(sampleData(), msg); }
public static void TestEqualsIgnoreEol(String l, String r) { SimpleBuffer left = new SimpleBuffer(); SimpleBuffer right = new SimpleBuffer(); left.appendAscii(l); right.appendAscii(r); Assert.True(SimpleBuffer.AreEqualIgnoreEol(left, right)); }
internal static void TestParseStateSync(ITestPlatformSupport platform, String fileName) { mtest.StructSix_subscriber subs = new mtest.StructSix_subscriber(); SimpleBuffer buffer = SimpleBuffer.readFromFile(fileName); IPublishableParser parser = platform.makePublishableParser(buffer.getReadIterator()); subs.applyStateSyncMessage(parser); Assert.True(subs.isEquivalent(GetPublishableSix())); }
public void AppendTest() { SimpleBuffer sb = new SimpleBuffer(); byte[] ba = { 1, 2, 3 }; sb.Append(ba, 0, ba.Length); Assert.AreEqual(1, sb[0]); Assert.AreEqual(2, sb[1]); Assert.AreEqual(3, sb[2]); Assert.AreEqual(0, sb[3]); }
public void CanRead_ECSBuffer_InsideFromDFG( [Values] NodeSet.RenderExecutionModel model, [Values(1, 3, 13, 50)] int bufferSize) { const int k_Loops = 10; using (var f = new Fixture <UpdateSystem>()) { f.Set.RendererModel = model; var entity = f.EM.CreateEntity(typeof(SimpleBuffer)); var entityNode = f.Set.CreateComponentNode(entity); var dfgNode = f.Set.Create <BufferNode>(); var gv = f.Set.CreateGraphValue(dfgNode, BufferNode.KernelPorts.Output); f.Set.Connect(entityNode, ComponentNode.Output <SimpleBuffer>(), dfgNode, BufferNode.KernelPorts.Input); var rng = new Mathematics.Random(0x7f); f.Set.SetBufferSize(dfgNode, BufferNode.KernelPorts.Output, Buffer <SimpleBuffer> .SizeRequest(bufferSize)); for (int i = 0; i < k_Loops; ++i) { var ecsBuffer = f.EM.GetBuffer <SimpleBuffer>(entity); ecsBuffer.ResizeUninitialized(bufferSize); for (int n = 0; n < bufferSize; ++n) { ecsBuffer[n] = new SimpleBuffer { Values = rng.NextFloat3() }; } f.System.Update(); var resolver = f.Set.GetGraphValueResolver(out var dependency); dependency.Complete(); ecsBuffer = f.EM.GetBuffer <SimpleBuffer>(entity); var dfgBuffer = resolver.Resolve(gv); Assert.AreEqual(ecsBuffer.Length, dfgBuffer.Length); // TODO: can compare alias here for (int n = 0; n < bufferSize; ++n) { Assert.AreEqual(ecsBuffer[n], dfgBuffer[n]); } } f.Set.ReleaseGraphValue(gv); f.Set.Destroy(entityNode, dfgNode); } }
public static void TestParseFail(String asText) { SimpleBuffer buffer = new SimpleBuffer(); buffer.appendAscii(asText); JsonPublishableParser parser = new JsonPublishableParser(buffer.getReadIterator()); UInt64[] addr = null; Assert.Throws <Exception>(() => { parser.parseAddress(ref addr); }); }
public static void TestParseEmptyAddress() { SimpleBuffer buffer = new SimpleBuffer(); buffer.appendAscii(" { }"); JsonPublishableParser parser = new JsonPublishableParser(buffer.getReadIterator()); UInt64[] addr = null; Assert.False(parser.parseAddress(ref addr)); }
public static void TestParseAddress(String asText, UInt64[] parsed) { SimpleBuffer buffer = new SimpleBuffer(); buffer.appendAscii(asText); JsonPublishableParser parser = new JsonPublishableParser(buffer.getReadIterator()); UInt64[] addr = null; Assert.True(parser.parseAddress(ref addr)); Assert.AreEqual(parsed, addr); }
public void Multiple_IStateRecorders_Are_Written_In_Chronological_Order_Into_A_Shared_Stream() { var guid = Guid.NewGuid(); var stream = new ConcurrentStream(new MemoryStream()); var stateController = new SimpleStateController { Guid = guid }; var stateResolver = new StateResolver(); stateResolver.Add(stateController); var captureService = new CaptureService { Stream = stream, StateResolver = stateResolver }; var recorder1 = new Mock<IStateRecorder>(); var recorder2 = new Mock<IStateRecorder>(); var recorderBuffer1 = new SimpleBuffer<ICaptureState>(); var recorderBuffer2 = new SimpleBuffer<ICaptureState>(); recorder1.Setup(b => b.Buffer).Returns(recorderBuffer1); recorder2.Setup(b => b.Buffer).Returns(recorderBuffer2); //push some dummy states into the buffers var state1 = new Mock<ICaptureState>(); var firstTimestamp = DateTime.Now.AddDays(1); state1.Setup(s => s.Timestamp).Returns(firstTimestamp); state1.Setup(s => s.Guid).Returns(guid); var state2 = new Mock<ICaptureState>(); var secondTimestamp = DateTime.Now.AddDays(2); state2.Setup(s => s.Timestamp).Returns(secondTimestamp); state2.Setup(s => s.Guid).Returns(guid); recorderBuffer1.Enqueue(state1.Object); recorderBuffer2.Enqueue(state2.Object); //add the 2nd recorder in first as it's timestamp is at a time in the future beyond the first recorder's state captureService.Add(recorder2.Object); captureService.Add(recorder1.Object); captureService.Start(); captureService.Flush(); Assert.Equal(2, captureService.CaptureStream.Count); //open the stream for reading now stream.Position = 0; var captureStream = new CaptureStream(stream, FileAccess.Read, stateResolver); //first value read back should be from state1 Assert.Equal(firstTimestamp, captureStream.Read().Timestamp); Assert.Equal(secondTimestamp, captureStream.Read().Timestamp); }
public void Multiple_IStateRecorders_Can_Be_Written_Into_The_Same_IStream() { var guid = Guid.NewGuid(); var stateController = new Mock<IStateController>(); stateController.Setup(s => s.Guid).Returns(guid); //stateController.Setup(s => s.Create(It.IsAny<byte[]>(), It.IsAny<DateTime>())).Returns(new Mock<ICaptureState>().Object); var stateResolver = new StateResolver(); stateResolver.Add(stateController.Object); var captureService = new CaptureService { Stream = new ConcurrentStream(new MemoryStream()), StateResolver = stateResolver }; var recorder1 = new Mock<IStateRecorder>(); var recorder2 = new Mock<IStateRecorder>(); var recorderBuffer1 = new SimpleBuffer<ICaptureState>(); var recorderBuffer2 = new SimpleBuffer<ICaptureState>(); recorder1.Setup(b => b.Buffer).Returns(recorderBuffer1); recorder2.Setup(b => b.Buffer).Returns(recorderBuffer2); //push some dummy states into the buffers var state1 = new Mock<ICaptureState>(); state1.Setup(s=>s.Timestamp).Returns(new DateTime(2011, 2, 3)); state1.Setup(s => s.Guid).Returns(guid); var state2 = new Mock<ICaptureState>(); state2.Setup(s => s.Timestamp).Returns(new DateTime(2011, 2, 4)); state2.Setup(s => s.Guid).Returns(guid); recorderBuffer1.Enqueue(state1.Object); recorderBuffer2.Enqueue(state2.Object); captureService.Add(recorder1.Object); captureService.Add(recorder2.Object); captureService.Start(); captureService.Flush(); Assert.Equal(2, captureService.CaptureStream.Count); }