public void StringIntDictSingleValue() { var dict = new Dictionary <string, int>() { { "a", 12 } }; Check( new byte[] { 97, 0, 1, 3, 1, 1, 1, 12, 4, 2, 36, 1 }, FlexBuffer.From(dict)); }
public void IntArray() { var value = new[] { 1, 2, 3, -1 }; var bytes = FlexBuffer.From(value); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(value.Length, flx.AsVector.Length); for (var i = 0; i < value.Length; i++) { Assert.AreEqual(value[i], flx[i].AsLong, $"Not equal at index {i}"); } }
public void StringIntDict() { var dict = new Dictionary <string, int>() { { "", 45 }, { "a", 12 } }; Check( new byte[] { 0, 97, 0, 2, 4, 4, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1 }, FlexBuffer.From(dict)); }
public void StringArray() { var value = new[] { "Max", "Maxim", "Alex", "Hi 😂🤣😍🤪" }; var bytes = FlexBuffer.From(value); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(value.Length, flx.AsVector.Length); for (var i = 0; i < value.Length; i++) { var v = flx[i].AsString; Assert.AreEqual(value[i], v, $"Not equal at index {i}"); } }
public void MixedVectorWithVectorAndInt() { var value = new List <object>() { new [] { 61 }, 64 }; // Swift has {1, 61, 4, 2, 3, 64, 40, 4, 4, 40, 1} but it is also untyped Check( new byte[] { 1, 61, 2, 2, 64, 44, 4, 4, 40, 1 }, FlexBuffer.From(value)); }
public void BoolArray() { var value = new[] { true, false, true, true }; var bytes = FlexBuffer.From(value); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(value.Length, flx.AsVector.Length); for (var i = 0; i < value.Length; i++) { var v = flx[i].AsBool; Assert.AreEqual(value[i], v, $"Not equal at index {i}"); } }
////////////////////////////// // TaggedDataOutput methods // ////////////////////////////// #region TaggedDataOutput methods public void WriteMessage(Message msg, FlexBuffer buf) { try { this.buf = buf; StartMessage(msg); WriteKeysAndValues(msg); EndMessage(msg); } finally { this.buf = null; } }
public void SimpleMap() { var value = new Dictionary <string, int>() { { "a", 12 }, { "b", 45 } }; var bytes = FlexBuffer.From(value); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(value.Count, flx.AsMap.Length); Assert.AreEqual(value["a"], flx["a"].AsLong); Assert.AreEqual(value["b"], flx["b"].AsLong); }
public void IterateOverVector() { var ints = new[] { 1, 2, 3 }; var bytes = FlexBuffer.From(ints); var flx = FlxValue.FromBytes(bytes); var i = 0; foreach (var value in flx.AsVector) { Assert.AreEqual(ints[i], value.AsLong); i++; } Assert.AreEqual(3, i); }
public void SendPacket() { SetUp(); FlexBuffer buf = new FlexBuffer(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }); byte[][] result = (FlexBuffer.littleEndian)? new byte[][] { new byte[] { 239, 190, 173, 222, 0, 0, 0, 0 }, } : new byte[][] { new byte[] { 222, 173, 190, 239, 0, 0, 0, 0 }, }; // p.Packet( whoDummy, buf ); p.TransportPacket(whoDummy, buf); Assert.AreEqual(dh.what, What.DATA); Assert.IsTrue(dh.Check(result)); Assert.IsNull(dh.xsender); Assert.IsNull(dh.xbuf); }
public void Long2() { Check( new byte[] { 1, 2, 2, 64, 1 }, FlexBuffer.SingleValue(1, 2)); Check( new byte[] { 255, 255, 0, 1, 4, 65, 1 }, FlexBuffer.SingleValue(-1, 256)); Check( new byte[] { 211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1 }, FlexBuffer.SingleValue(-45, 256000)); Check( new byte[] { 211, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127, 16, 67, 1 }, FlexBuffer.SingleValue(-45, long.MaxValue)); }
public void Double2() { var bytes = FlexBuffer.SingleValue(1.1f, 2); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(2, flx.AsVector.Length); Assert.AreEqual(flx[0].AsDouble, 1.1f); Assert.AreEqual(flx[1].AsDouble, 2); bytes = FlexBuffer.SingleValue(1.1, 256); flx = FlxValue.FromBytes(bytes); Assert.AreEqual(2, flx.AsVector.Length); Assert.AreEqual(flx[0].AsDouble, 1.1); Assert.AreEqual(flx[1].AsDouble, 256); }
public void ULong2() { Check( new byte[] { 1, 2, 2, 68, 1 }, FlexBuffer.SingleValue(1UL, 2)); Check( new byte[] { 1, 0, 0, 1, 4, 69, 1 }, FlexBuffer.SingleValue(1UL, 256)); Check( new byte[] { 45, 0, 0, 0, 0, 232, 3, 0, 8, 70, 1 }, FlexBuffer.SingleValue(45UL, 256000)); Check( new byte[] { 45, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 16, 71, 1 }, FlexBuffer.SingleValue(45, ulong.MaxValue)); }
public void SendPacket2() { SetUp(); // Create Packet to send FlexBuffer buf = new FlexBuffer(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 2, 3 }); byte[][] result = (FlexBuffer.littleEndian) ? new byte[][] { new byte[] { 239, 190, 173, 222, 2, 0, 0, 0, 2, 3 }, } : new byte[][] { new byte[] { 222, 173, 190, 239, 0, 0, 0, 2, 2, 3 }, }; p.TransportPacket(null, buf); Assert.AreEqual(dh.what, What.DATA); Assert.IsTrue(dh.Check(result)); Assert.IsNull(dh.xsender); Assert.IsNull(dh.xbuf); }
public void SendDoubleSingleData0() { SetUp(); // length = 0 FlexBuffer buf = (FlexBuffer.littleEndian) ? new FlexBuffer(new byte[] { 239, 190, 173, 222, 0, 0, 0, 0, 239, 190, 173, 222, 0, 0, 0, 0 }) : new FlexBuffer(new byte[] { 222, 173, 190, 239, 0, 0, 0, 0, 222, 173, 190, 239, 0, 0, 0, 0 }); byte[][] result = new byte[][] {}; p.SessionData(null, buf); Assert.IsTrue(mph.Check(result)); Assert.IsNull(mph.what); Assert.IsNull(mph.xbuf); Assert.IsNull(mph.xsender); }
public void MixedVector() { Check(new byte[] { 3, 102, 111, 111, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 251, 255, 255, 255, 255, 255, 255, 255, 205, 204, 204, 204, 204, 204, 244, 63, 1, 0, 0, 0, 0, 0, 0, 0, 20, 4, 4, 15, 104, 45, 43, 1 }, FlexBuffer.From(new List <object> { "foo", 1, -5, 1.3, true })); }
public void SendDoubleSingleData2() { SetUp(); // length = 2 FlexBuffer buf = (FlexBuffer.littleEndian) ? new FlexBuffer(new byte[] { 239, 190, 173, 222, 2, 0, 0, 0, 3, 4, 239, 190, 173, 222, 2, 0, 0, 0, 5, 6 }) : new FlexBuffer(new byte[] { 222, 173, 190, 239, 0, 0, 0, 2, 3, 4, 222, 173, 190, 239, 0, 0, 0, 2, 5, 6 }); byte[][] result = new byte[][] { new byte[] { 3, 4 }, new byte[] { 5, 6 } }; p.SessionData(null, buf); Assert.IsTrue(mph.Check(result)); Assert.AreEqual(mph.what, What.PACKET); Assert.IsNull(mph.xbuf); Assert.IsNull(mph.xsender); }
public void DictionaryWithCapitalAndNonCapitalKeyValues() { Dictionary <string, object> dict = new Dictionary <string, object>(); dict.Add("aieorjhioa", "aerhj"); dict.Add("e98rh8d9i3qaktgr", "eaihjor"); dict.Add("T", "toeks"); byte[] testBytes = FlexBuffer.From(dict); FlxMap map = FlxValue.FromBytes(testBytes).AsMap; Assert.AreEqual("aerhj", map["aieorjhioa"].AsString); Assert.AreEqual("eaihjor", map["e98rh8d9i3qaktgr"].AsString); Assert.AreEqual("toeks", map["T"].AsString); }
private byte[] Msg2bytes(Message msg, bool?stringTypeAndField) { FlexBuffer buf = new FlexBuffer(); URL u = new URL("none:"); if (stringTypeAndField != null) { u.AddTerm(BinaryTaggedDataOutput.STRING_TYPE_AND_FIELD, stringTypeAndField.ToString()); } BinaryTaggedDataOutput btdo = new BinaryTaggedDataOutput(vf, u.ToString()); btdo.WriteMessage(msg, buf); buf.SetIndex(0); return(buf.GetAvailBytes()); }
public void MixedArray() { var value = new object[] { "Max", 1, 0.1, 0.5, true, int.MinValue, ulong.MaxValue, "Hi 😂🤣😍🤪", null }; var bytes = FlexBuffer.From(value); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(value.Length, flx.AsVector.Length); Assert.AreEqual(value[0], flx[0].AsString); Assert.AreEqual(value[1], flx[1].AsLong); Assert.AreEqual(value[2], flx[2].AsDouble); Assert.AreEqual(value[3], flx[3].AsDouble); Assert.AreEqual(value[4], flx[4].AsBool); Assert.AreEqual(value[5], flx[5].AsLong); Assert.AreEqual(value[6], flx[6].AsULong); Assert.AreEqual(value[7], flx[7].AsString); Assert.AreEqual(true, flx[8].IsNull); }
public void Packet4() { SessionPacket ph = m; FlexBuffer buf = new FlexBuffer(new byte[] { (byte)VERSION, unchecked ((byte)BYTE), 2, 0, unchecked ((byte)NONE) }); mh.handled = false; ph.SessionPacket(who, buf); Assert.AreEqual(What.OOB_NOTIFY_HANDLER, mh.what); Assert.AreEqual(ps, m.GetTransport()); Assert.AreEqual(who, mh.xsender); Assert.IsNotNull(mh.xmsg); Assert.AreEqual(MyValueFactory.mt_add_result, mh.xmsg.GetXType); Assert.AreEqual(0, mh.xmsg.Count); }
public void GetValueIndex() { var dict = new Dictionary <string, int>() { { "a", 1 }, { "b", 2 }, { "c", 3 }, }; var bytes = FlexBuffer.From(dict); var flx = FlxValue.FromBytes(bytes); var map = flx.AsMap; Assert.AreEqual(3, map.Length); Assert.AreEqual(0, map.KeyIndex("a")); Assert.AreEqual(1, map.KeyIndex("b")); Assert.AreEqual(2, map.KeyIndex("c")); Assert.AreEqual(-1, map.KeyIndex("d")); Assert.AreEqual(1, map.ValueByIndex(0).AsLong); }
public void BiggerDictionary() { var dict = new Dictionary <string, int>(); for (var i = 0; i < 2600; i++) { dict[i.ToString()] = i; } var bytes = FlexBuffer.From(dict); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(2600, flx.AsMap.Length); for (var i = 0; i < 2600; i++) { Assert.AreEqual(i, flx[i.ToString()].AsLong); } }
public void BiggerBoolArray() { var list = new List <bool>(); for (var i = 0; i < 2600; i++) { list.Add(i % 3 == 0); } var bytes = FlexBuffer.From(list); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(2600, flx.AsVector.Length); for (var i = 0; i < 2600; i++) { Assert.AreEqual(list[i], flx[i].AsBool); } }
public void BiggerStringArray() { var list = new List <string>(); for (var i = 0; i < 2600; i++) { list.Add("abc"); } var bytes = FlexBuffer.From(list); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(2600, flx.AsVector.Length); for (var i = 0; i < 2600; i++) { Assert.AreEqual("abc", flx[i].AsString); } }
public void Double4() { var bytes = FlexBuffer.SingleValue(1.1f, 2, 3.3f, 0.5); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(4, flx.AsVector.Length); Assert.AreEqual(flx[0].AsDouble, 1.1f); Assert.AreEqual(flx[1].AsDouble, 2); Assert.AreEqual(flx[2].AsDouble, 3.3f); Assert.AreEqual(flx[3].AsDouble, 0.5); bytes = FlexBuffer.SingleValue(1.1, 256, 3.3, 0.5); flx = FlxValue.FromBytes(bytes); Assert.AreEqual(4, flx.AsVector.Length); Assert.AreEqual(flx[0].AsDouble, 1.1); Assert.AreEqual(flx[1].AsDouble, 256); Assert.AreEqual(flx[2].AsDouble, 3.3); Assert.AreEqual(flx[3].AsDouble, 0.5); }
public void IterateOverMap() { var dict = new Dictionary <string, int>() { { "a", 1 }, { "b", 2 }, { "c", 3 }, }; var bytes = FlexBuffer.From(dict); var flx = FlxValue.FromBytes(bytes); var i = 0; foreach (var value in flx.AsMap) { Assert.AreEqual(dict[value.Key], value.Value.AsLong); i++; } Assert.AreEqual(3, i); }
public void Packet1() { // PacketHandler ph = m; SessionPacket ph = m; FlexBuffer buf = new FlexBuffer(new byte[] { (byte)VERSION, unchecked ((byte)BYTE), 1, 0, unchecked ((byte)NONE) }); mh.handled = true; // ph.Packet( who, buf ); ph.SessionPacket(who, buf); Assert.AreEqual(What.TESTMESSAGEHANDLERMESSAGE, mh.what); Assert.AreEqual(ps, m.GetTransport()); Assert.AreEqual(who, mh.xsender); Assert.IsNotNull(mh.xmsg); Assert.AreEqual(MyValueFactory.mt_add, mh.xmsg.GetXType); Assert.AreEqual(0, mh.xmsg.Count); }
public void Double3() { Check( new byte[] { 205, 204, 140, 63, 0, 0, 0, 64, 0, 0, 128, 64, 12, 86, 1 }, FlexBuffer.SingleValue(1.1f, 2, 4)); Check( new byte[] { 154, 153, 153, 153, 153, 153, 241, 63, 0, 0, 0, 0, 0, 0, 112, 64, 0, 0, 0, 0, 0, 0, 16, 64, 24, 87, 1 }, FlexBuffer.SingleValue(1.1, 256, 4)); }
public void ULong2() { var bytes = FlexBuffer.SingleValue(1UL, 2); var flx = FlxValue.FromBytes(bytes); Assert.AreEqual(2, flx.AsVector.Length); Assert.AreEqual(flx[0].AsULong, 1); Assert.AreEqual(flx[1].AsULong, 2); bytes = FlexBuffer.SingleValue(1, 256); flx = FlxValue.FromBytes(bytes); Assert.AreEqual(2, flx.AsVector.Length); Assert.AreEqual(flx[0].AsULong, 1); Assert.AreEqual(flx[1].AsULong, 256); bytes = FlexBuffer.SingleValue(1, ulong.MaxValue); flx = FlxValue.FromBytes(bytes); Assert.AreEqual(2, flx.AsVector.Length); Assert.AreEqual(flx[0].AsULong, 1); Assert.AreEqual(flx[1].AsULong, ulong.MaxValue); }