public void BlobPropertyMsg_Serialize() { BlobPropertyMsg msgOut, msgIn; EnhancedMemoryStream ms = new EnhancedMemoryStream(); Msg.ClearTypes(); Msg.LoadTypes(Assembly.GetExecutingAssembly()); msgOut = new BlobPropertyMsg(); msgOut._Set("string", "hello"); msgOut._Set("bool", true); msgOut._Set("int", 10); msgOut._Set("timespan", new TimeSpan(0, 0, 0, 0, 55)); msgOut._Set("endpoint", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 56)); msgOut._Set("bytes", new byte[] { 5, 6, 7, 8 }); msgOut._Data = null; Msg.Save(ms, msgOut); ms.Seek(0, SeekOrigin.Begin); msgIn = (BlobPropertyMsg)Msg.Load(ms); Assert.AreEqual(msgOut["string"], msgIn["string"]); Assert.AreEqual(msgOut["bool"], msgIn["bool"]); Assert.AreEqual(msgOut["int"], msgIn["int"]); Assert.AreEqual(msgOut["timespan"], msgIn["timespan"]); Assert.AreEqual(msgOut["endpoint"], msgIn["endpoint"]); Assert.AreEqual(msgOut["bytes"], msgIn["bytes"]); Assert.IsNull(msgIn._Data); ms.SetLength(0); msgOut._Data = new byte[] { 0, 1, 2 }; Msg.Save(ms, msgOut); ms.Seek(0, SeekOrigin.Begin); msgIn = (BlobPropertyMsg)Msg.Load(ms); Assert.AreEqual(msgOut["string"], msgIn["string"]); Assert.AreEqual(msgOut["bool"], msgIn["bool"]); Assert.AreEqual(msgOut["int"], msgIn["int"]); Assert.AreEqual(msgOut["timespan"], msgIn["timespan"]); Assert.AreEqual(msgOut["endpoint"], msgIn["endpoint"]); Assert.AreEqual(msgOut["bytes"], msgIn["bytes"]); CollectionAssert.AreEqual(msgOut._Data, msgIn._Data); }
public void BlobPropertyMsg_Access() { BlobPropertyMsg msg = new BlobPropertyMsg(); msg._Set("string", "hello"); msg._Set("bool", true); msg._Set("int", 10); msg._Set("timespan", new TimeSpan(0, 0, 0, 0, 55)); msg._Set("endpoint", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 56)); msg._Set("bytes", new byte[] { 5, 6, 7, 0x1F }); msg._Data = new byte[] { 0, 1, 2 }; Assert.AreEqual("hello", msg._Get("string")); Assert.AreEqual("hello", msg["string"]); Assert.AreEqual("hello", msg["STRING"]); Assert.IsNull(msg["foobar"]); Assert.AreEqual("foobar", msg._Get("foobar", "foobar")); CollectionAssert.AreEqual(new byte[] { 0, 1, 2 }, msg._Data); msg["test"] = "test"; Assert.AreEqual("test", msg["test"]); msg["test"] = "test #2"; Assert.AreEqual("test #2", msg["test"]); Assert.IsTrue(msg._Get("bool", false)); Assert.IsFalse(msg._Get("foobar", false)); Assert.AreEqual("1", msg["bool"]); msg._Set("bool", false); Assert.IsFalse(msg._Get("bool", true)); Assert.AreEqual("0", msg["bool"]); Assert.IsTrue(msg._Get("test", true)); Assert.AreEqual(10, msg._Get("int", 0)); Assert.AreEqual(20, msg._Get("foobar", 20)); msg._Set("int", -566); Assert.AreEqual(-566, msg._Get("int", 0)); Assert.AreEqual("-566", msg["int"]); Assert.AreEqual(666, msg._Get("test", 666)); Assert.AreEqual(new TimeSpan(0, 0, 0, 0, 55), msg._Get("timespan", TimeSpan.FromSeconds(60))); Assert.AreEqual(new TimeSpan(0, 0, 0, 60), msg._Get("foobar", TimeSpan.FromSeconds(60))); msg._Set("timespan", TimeSpan.FromSeconds(77)); Assert.AreEqual(TimeSpan.FromSeconds(77), LillTek.Common.Serialize.Parse(msg["timespan"], TimeSpan.Zero)); Assert.AreEqual(TimeSpan.FromSeconds(1), msg._Get("test", TimeSpan.FromSeconds(1))); Assert.AreEqual(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 56), msg._Get("endpoint", (IPEndPoint)null)); Assert.AreEqual("127.0.0.1:56", msg["endpoint"]); Assert.AreEqual(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 57), msg._Get("foobar", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 57))); msg._Set("endpoint", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 58)); Assert.AreEqual(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 58), msg._Get("endpoint", (IPEndPoint)null)); Assert.AreEqual(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 58), msg._Get("test", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 58))); CollectionAssert.AreEqual(new byte[] { 5, 6, 7, 0x1F }, msg._Get("bytes", (byte[])null)); CollectionAssert.AreEqual(new byte[] { 10, 11 }, msg._Get("foobar", new byte[] { 10, 11 })); Assert.AreEqual("0506071f", msg["bytes"]); msg._Set("bytes", new byte[] { 0x0a, 0x1b, 0x2c, 0x3d, 0x4e, 0x5f }); CollectionAssert.AreEqual(new byte[] { 0x0a, 0x1b, 0x2c, 0x3d, 0x4e, 0x5f }, msg._Get("bytes", (byte[])null)); CollectionAssert.AreEqual(new byte[] { 10 }, msg._Get("test", new byte[] { 10 })); var id = Helper.NewGuid(); msg._Set("id", id); Assert.AreEqual(id, msg._Get("id", Guid.Empty)); Assert.AreEqual(Guid.Empty, msg._Get("_id", Guid.Empty)); msg._Set("_id", "sss s s s"); Assert.AreEqual(Guid.Empty, msg._Get("_id", Guid.Empty)); var now = Helper.UtcNowRounded; msg._Set("date", now); Assert.AreEqual(now, msg._Get("date", DateTime.MaxValue)); }