public void TestJsonToBinarySimple() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var js = new JsonSerializer(); js.JsonOptions.FieldSeparator = ""; js.JsonOptions.Indent = ""; js.JsonOptions.SaveRootClass = true; var jd = new JsonDeserializer(); var data1 = "{\"class\":\"YuzuTest.Sample1, YuzuTest\",\"X\":98,\"Y\":\"\"}"; var result1 = js.ToString(bd.FromBytes(bs.ToBytes(jd.FromString(data1)))); Assert.AreEqual(data1, result1); }
public void TestAllowReadingFromAncestor() { var bs = new BinarySerializer(); var v1 = new Sample2 { X = 83, Y = "83" }; var result1 = bs.ToBytes(v1); Assert.AreEqual( "20 01 00 " + XS(typeof(Sample2)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 53 00 00 00 00 00", XS(result1)); var w1 = new Sample2Allow(); var bd = new BinaryDeserializer(); bd.FromBytes(w1, result1); Assert.AreEqual(v1.X, w1.X); }
public void TestAbstract() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); SampleAbstract v1 = new SampleConcrete { XX = 81 }; var result1 = bs.ToBytes(v1); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleConcrete)) + " 01 00 " + XS("XX", RoughType.Int) + " 01 00 51 00 00 00 00 00", XS(result1)); var w1 = bd.FromBytes<SampleAbstract>(result1); Assert.AreEqual((v1 as SampleConcrete).XX, (w1 as SampleConcrete).XX); var w1g = (SampleConcrete)(new BinaryDeserializerGen().FromBytes(result1)); Assert.AreEqual((v1 as SampleConcrete).XX, w1g.XX); var v2 = new List<SampleAbstract>(); v2.Add(new SampleConcrete { XX = 51 }); var w2 = bd.FromBytes<List<SampleAbstract>>(bs.ToBytes(v2)); Assert.AreEqual(v2.Count, w2.Count); Assert.AreEqual((v2[0] as SampleConcrete).XX, (w2[0] as SampleConcrete).XX); }
public void TestJsonToBinaryUnknown() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var js = new JsonSerializer(); js.JsonOptions.FieldSeparator = ""; js.JsonOptions.Indent = ""; js.JsonOptions.SaveClass = JsonSaveClass.UnknownOrRoot; var jd = new JsonDeserializer(); var data1 = "{\"class\":\"NewType\",\"X\":\"abc\"}"; var result1 = js.ToString(bd.FromBytes(bs.ToBytes(jd.FromString(data1)))); Assert.AreEqual(data1, result1); }
public void TestBinaryToJsonUnknown() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var js = new JsonSerializer(); js.JsonOptions.SaveRootClass = true; var jd = new JsonDeserializer(); var n = "NewType".ToCharArray().Select(ch => (byte)ch).ToList(); var data1 = new byte[] { 0x20, 0x01, 0x00, (byte)n.Count }.Concat(n). Concat(new byte[] { 0x01, 0x00, 0x01, (byte)'X', (byte)RoughType.String, 0x01, 0x00, 0x03, (byte)'a', (byte)'b', (byte)'c', 0, 0 }).ToArray(); var result1 = bs.ToBytes(jd.FromString(js.ToString(bd.FromBytes(data1)))); CollectionAssert.AreEqual(data1, result1); }
public void TestBinaryToJsonUnknown() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var js = new JsonSerializer(); js.JsonOptions.SaveClass = JsonSaveClass.UnknownOrRoot; var jd = new JsonDeserializer(); var n = "NewType".ToCharArray().Select(ch => (byte)ch).ToList(); var data1 = new byte[] { 0x20, 0x01, 0x00, (byte)n.Count }.Concat(n). Concat(new byte[] { 0x01, 0x00, 0x01, (byte)'X', (byte)RoughType.String, 0x01, 0x00, 0x03, (byte)'a', (byte)'b', (byte)'c', 0, 0 }).ToArray(); var result1 = bs.ToBytes(jd.FromString(js.ToString(bd.FromBytes(data1)))); CollectionAssert.AreEqual(data1, result1); }
public void TestBinaryToJsonSimple() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var js = new JsonSerializer(); js.JsonOptions.SaveRootClass = true; var jd = new JsonDeserializer(); var n = "YuzuTest.Sample1, YuzuTest".ToCharArray().Select(ch => (byte)ch).ToList(); var data1 = new byte[] { 0x20, 0x01, 0x00, (byte)n.Count }.Concat(n). Concat(new byte[] { 0x02, 0x00, 0x01, (byte)'X', (byte)RoughType.Int, 0x01, (byte)'Y', (byte)RoughType.String, 0x01, 0x00, 98, 0, 0, 0, 0x02, 0x00, 0, 0, 0, 0 }).ToArray(); var result1 = bs.ToBytes(jd.FromString(js.ToString(bd.FromBytes(data1)))); CollectionAssert.AreEqual(data1, result1); }
public void TestEscape() { var bs = new BinarySerializer(); var s = "\"/{\u0001}\n\t\"\""; var v = new Sample1 { Y = s }; var result = bs.ToBytes(v); Assert.AreEqual( "20 01 00 " + XS(typeof(Sample1)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 00 00 00 00 02 00 " + XS(s) + " 00 00", XS(result)); var w = new Sample1(); var bd = new BinaryDeserializer(); bd.FromBytes(w, result); Assert.AreEqual(s, w.Y); v.Y = "привет"; var result2 = bs.ToBytes(v); Assert.AreEqual( "20 01 00 01 00 00 00 00 00 02 00 0C " + XS(Encoding.UTF8.GetBytes("привет")) + " 00 00", XS(result2)); bd.FromBytes(w, result2); Assert.AreEqual(v.Y, w.Y); var ms = new MemoryStream(result2.Length); ms.Write(result2, 0, result2.Length); ms.Position = 0; bd.FromReader(w, new UnsafeBinaryReader(ms)); Assert.AreEqual(v.Y, w.Y); }
public void TestTopLevelDict() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var v0 = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } }; var result0 = bs.ToBytes(v0); Assert.AreEqual( "22 10 05 02 00 00 00 " + XS("a") + " 01 00 00 00 " + XS("b") + " 02 00 00 00", XS(result0)); var w0 = new Dictionary<string, int>(); bd.FromBytes(w0, result0); CollectionAssert.AreEqual(v0, w0); bd.FromBytes(w0, new byte[] { 0x22, 0x10, 05, 0, 0, 0, 0 }); CollectionAssert.AreEqual(v0, w0); bd.FromBytes(w0, SX("22 10 05 01 00 00 00 " + XS("c") + " 03 00 00 00")); CollectionAssert.AreEqual( new Dictionary<string, int> { { "a", 1 }, { "b", 2 }, { "c", 3 } }, w0); }
public void TestArray() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var bdg = new BinaryDeserializerGen(); var v0 = new SampleArray { A = new string[] { "a", "b", "c" } }; var result0 = bs.ToBytes(v0); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleArray)) + " 01 00 " + XS("A", RoughType.Sequence) + " 10 01 00 03 00 00 00 " + XS("a", "b", "c") + " 00 00", XS(result0)); var w0 = new SampleArray(); bd.FromBytes(w0, result0); CollectionAssert.AreEqual(v0.A, w0.A); var w0g = (SampleArray)bdg.FromBytes(result0); CollectionAssert.AreEqual(v0.A, w0g.A); var v2 = new SampleArray(); var result2 = bs.ToBytes(v2); Assert.AreEqual("20 01 00 01 00 FF FF FF FF 00 00", XS(result2)); var w2 = new SampleArray(); bd.FromBytes(w2, result2); CollectionAssert.AreEqual(v2.A, w2.A); var w2g = (SampleArray)bdg.FromBytes(result2); CollectionAssert.AreEqual(v2.A, w2g.A); }
public void TestNewFields() { var bd = new BinaryDeserializer(); bd.Options.TagMode = TagMode.Aliases; bd.Options.AllowUnknownFields = true; var w = new SampleTree(); bd.FromBytes(w, SX( "20 01 00 " + XS(typeof(SampleTree)) + " 03 00 " + XS("a", RoughType.Int, "a1", RoughType.Sequence) + " 10 " + XS("b", RoughType.Sequence) + " 20 01 00 09 00 00 00 02 00 00 00 00 00 03 00 FF FF FF FF 00 00")); Assert.AreEqual(9, w.Value); Assert.AreEqual(null, w.Children); bd.ClearClassIds(); bd.FromBytes(w, SX( "20 01 00 " + XS(typeof(SampleTree)) + " 04 00 " + XS("a", RoughType.Int, "a1", RoughType.Byte) + " " + XS("b", RoughType.Sequence) + " 20 " + XS("x", RoughType.Record) + " 01 00 0A 00 00 00 02 00 00 04 00 00 00 00 00")); Assert.AreEqual(10, w.Value); bd.FromBytes(w, SX( "20 01 00 01 00 0B 00 00 00 00 00")); Assert.AreEqual(11, w.Value); }
public void TestSignature() { var bs = new BinarySerializer(); bs.BinaryOptions.AutoSignature = true; var result1 = bs.ToBytes(17); Assert.AreEqual("59 42 30 31 05 11 00 00 00", XS(result1)); var bd = new BinaryDeserializer(); bd.BinaryOptions.AutoSignature = true; Assert.AreEqual(17, bd.FromBytes<int>(result1)); XAssert.Throws<YuzuException>(() => bd.FromBytes(new byte [] { 0x05, 0x11, 0, 0, 0 }), "ignature"); var ms = new MemoryStream(new byte[] { 0x05, 0x12, 0, 0, 0 }); bd.Reader = new BinaryReader(ms); Assert.IsFalse(bd.IsValidSignature()); bd.BinaryOptions.AutoSignature = false; Assert.AreEqual(18, bd.FromReader<int>(bd.Reader)); }
public void TestLong() { var bs = new BinarySerializer(); var v1 = new SampleLong { S = -1L << 33, U = 1UL << 33 }; var result = bs.ToBytes(v1); Assert.AreEqual( XS(RoughType.Record) + " 01 00 " + XS(typeof(SampleLong)) + " 02 00 " + XS("S", RoughType.Long, "U", RoughType.ULong) + " 01 00 00 00 00 00 FE FF FF FF 02 00 00 00 00 00 02 00 00 00 00 00", XS(result)); var v2 = new SampleLong(); var bd = new BinaryDeserializer(); bd.FromBytes(v2, result); Assert.AreEqual(v1.S, v2.S); Assert.AreEqual(v1.U, v2.U); v1.S = long.MinValue; v1.U = ulong.MaxValue; bd.FromBytes(v2, bs.ToBytes(v1)); Assert.AreEqual(v1.S, v2.S); Assert.AreEqual(v1.U, v2.U); }
public void TestClassNames() { var bs = new BinarySerializer(); bs.Options.TagMode = TagMode.Names; Assert.AreEqual( "20 01 00 " + XS(typeof(SampleBase)) + " 01 00 " + XS("FBase", RoughType.Int) + " 01 00 00 00 00 00 00 00", XS(bs.ToBytes(new SampleBase()))); Assert.AreEqual( "20 02 00 " + XS(typeof(SampleDerivedA)) + " 02 00 " + XS("FBase", RoughType.Int, "FA", RoughType.Int) + " 01 00 00 00 00 00 02 00 00 00 00 00 00 00", XS(bs.ToBytes(new SampleDerivedA()))); var bd = new BinaryDeserializer(); bd.Options.TagMode = TagMode.Names; var v = bd.FromBytes(SX( "20 01 00 " + XS(typeof(SampleDerivedB)) + " 02 00 " + XS("FBase", RoughType.Int, "FB", RoughType.Int) + " 01 00 03 00 00 00 02 00 07 00 00 00 00 00")); Assert.IsInstanceOfType(v, typeof(SampleDerivedB)); var b = (SampleDerivedB)v; Assert.AreEqual(3, b.FBase); Assert.AreEqual(7, b.FB); }
public void TestCollection() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var v0 = new SampleWithCollection(); v0.A.Add(new SampleInterfaced { X = 9 }); v0.B.Add(7); v0.B.Add(6); var result0 = bs.ToBytes(v0); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleWithCollection)) + " 02 00 " + XS("A", RoughType.Sequence) + " " + XS(RoughType.Record) + " " + XS("B", RoughType.Sequence) + " " + XS(RoughType.Int) + " 01 00 01 00 00 00 02 00 " + XS(typeof(SampleInterfaced)) + " 01 00 " + XS("X", RoughType.Int) + " 01 00 09 00 00 00 00 00" + " 02 00 02 00 00 00 07 00 00 00 06 00 00 00 00 00", XS(result0)); var w0 = new SampleWithCollection(); bd.FromBytes(w0, result0); Assert.AreEqual(1, w0.A.Count); Assert.IsInstanceOfType(w0.A.First(), typeof(SampleInterfaced)); Assert.AreEqual(9, w0.A.First().X); CollectionAssert.AreEqual(new int[] { 7, 6 }, w0.B.ToList()); var w1 = (SampleWithCollection)((new BinaryDeserializerGen()).FromBytes(result0)); Assert.AreEqual(1, w1.A.Count); Assert.IsInstanceOfType(w1.A.First(), typeof(SampleInterfaced)); Assert.AreEqual(9, w1.A.First().X); CollectionAssert.AreEqual(new int[] { 7, 6 }, w1.B.ToList()); var v2 = new SampleConcreteCollection { 2, 5, 4 }; var result1 = bs.ToBytes(v2); Assert.AreEqual("21 05 03 00 00 00 02 00 00 00 05 00 00 00 04 00 00 00", XS(result1)); SampleConcreteCollection w2 = new SampleConcreteCollection(); bd.FromBytes(w2, result1); CollectionAssert.AreEqual(v2.ToList(), w2.ToList()); }
public void TestUnknownStorage() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); bd.Options.AllowUnknownFields = true; var data = "20 01 00 " + XS(typeof(SampleUnknown)) + " 03 00 " + XS("A", RoughType.String, "X", RoughType.Int, "Z", RoughType.Bool) + " 01 00 " + XS("qq") + " 02 00 02 01 00 00 03 00 01 00 00"; var w = new SampleUnknown(); bd.FromBytes(w, SX(data)); Assert.AreEqual(258, w.X); Assert.AreEqual(2, w.Storage.Fields.Count); Assert.AreEqual("A", w.Storage.Fields[0].Name); Assert.AreEqual("qq", w.Storage.Fields[0].Value); Assert.AreEqual("Z", w.Storage.Fields[1].Name); Assert.AreEqual(true, w.Storage.Fields[1].Value); Assert.AreEqual(data, XS(bs.ToBytes(w))); bd.FromBytes(w, SX("20 01 00 00 00")); Assert.AreEqual(0, w.Storage.Fields.Count); Assert.AreEqual("20 01 00 00 00", XS(bs.ToBytes(new SampleUnknown()))); bd.ClearClassIds(); bs.ClearClassIds(); bd.FromBytes(w, SX( "20 01 00 " + XS(typeof(SampleUnknown)) + " 02 00 " + XS("A", RoughType.String, "Z", RoughType.Bool) + " 01 00 " + XS("tt") + " 02 00 01 00 00")); Assert.AreEqual(2, w.Storage.Fields.Count); Assert.AreEqual("A", w.Storage.Fields[0].Name); Assert.AreEqual("tt", w.Storage.Fields[0].Value); Assert.AreEqual("Z", w.Storage.Fields[1].Name); Assert.AreEqual(true, w.Storage.Fields[1].Value); Assert.AreEqual(258, w.X); w.X = 0; Assert.AreEqual( "20 01 00 " + XS(typeof(SampleUnknown)) + " 03 00 " + XS("A", RoughType.String, "X", RoughType.Int, "Z", RoughType.Bool) + " 01 00 " + XS("tt") + " 03 00 01 00 00", XS(bs.ToBytes(w))); bs.ClearClassIds(); bs.ToBytes(new SampleUnknown()); XAssert.Throws<YuzuException>(() => bs.ToBytes(w), "SampleUnknown"); bs.ClearClassIds(); bd.ClearClassIds(); var data2 = "20 01 00 " + XS(typeof(SampleUnknown)) + " 02 00 " + XS("A", RoughType.Record, "X", RoughType.Int) + " 01 00 02 00 " + XS("NewType") + " 01 00 " + XS("Fld", RoughType.SByte) + " 01 00 FE 00 00 02 00 14 00 00 00 00 00"; var w2 = bd.FromBytes<SampleUnknown>(SX(data2)); Assert.AreEqual(1, w2.Storage.Fields.Count); Assert.AreEqual("A", w2.Storage.Fields[0].Name); var u2 = (YuzuUnknown)w2.Storage.Fields[0].Value; Assert.AreEqual("NewType", u2.ClassTag); Assert.AreEqual(1, u2.Fields.Count); Assert.AreEqual((sbyte)-2, u2.Fields["Fld"]); Assert.AreEqual(20, w2.X); Assert.AreEqual("\n" + data2, "\n" + XS(bs.ToBytes(w2))); }
public void TestBool() { var bs = new BinarySerializer(); var v = new SampleBool { B = true }; var result1 = bs.ToBytes(v); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleBool)) + " 01 00 " + XS("B", RoughType.Bool) + " 01 00 01 00 00", XS(result1)); var bd = new BinaryDeserializer(); var w = new SampleBool(); bd.FromBytes(w, result1); Assert.AreEqual(true, w.B); }
public void TestUnknown() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var data1 = SX( "20 01 00 " + XS("NewType1") + " 02 00 " + XS("a", RoughType.Int, "b", RoughType.String) + " 01 00 07 07 00 00 00 00"); var w1 = (YuzuUnknown)bd.FromBytes<object>(data1); Assert.AreEqual("NewType1", w1.ClassTag); Assert.AreEqual(1, w1.Fields.Count); Assert.AreEqual(7*256 + 7, w1.Fields["a"]); CollectionAssert.AreEqual(data1, bs.ToBytes(w1)); var data2 = SX("20 01 00 02 00 " + XS("qwe") + " 00 00"); var w2 = (YuzuUnknown)bd.FromBytes(data2); Assert.AreEqual("NewType1", w2.ClassTag); Assert.AreEqual(1, w2.Fields.Count); Assert.AreEqual("qwe", w2.Fields["b"]); CollectionAssert.AreEqual(data2, bs.ToBytes(w2)); var data3 = SX( "20 02 00 " + XS(typeof(SampleBool)) + " 02 00 " + XS("B", RoughType.Bool, "a", RoughType.Record) + " 01 00 01 02 00 03 00 " + XS("NewType2") + " 00 00 00 00 00 00"); bd.Options.AllowUnknownFields = true; var w3 = bd.FromBytes<SampleBool>(data3); Assert.AreEqual(true, w3.B); }
public void TestTopLevelListOfNonPrimitiveTypes() { var bs = new BinarySerializer(); bs.Options.TagMode = TagMode.Names; var bd = new BinaryDeserializer(); bd.Options.TagMode = TagMode.Names; var v1 = new List<SampleDerivedB> { new SampleDerivedB { FB = 10 }, new SampleDerivedB { FB = 20 } }; var result1 = bs.ToBytes(v1); Assert.AreEqual( "21 20 02 00 00 00 01 00 " + XS(typeof(SampleDerivedB)) + " 02 00 " + XS("FBase", RoughType.Int, "FB", RoughType.Int) + " 01 00 00 00 00 00 02 00 0A 00 00 00 00 00 " + "01 00" + " 01 00 00 00 00 00 02 00 14 00 00 00 00 00", XS(result1)); var w1 = (List<object>)bd.FromBytes(result1); for (int i = 0; i < v1.Count; i++) { Assert.AreEqual(v1[i].FB, (w1[i] as SampleDerivedB).FB); } }
public void TestTopLevelList() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var v0 = new List<string> { "a", "b", "c" }; var result0 = bs.ToBytes(v0); Assert.AreEqual("21 10 03 00 00 00 " + XS("a", "b", "c"), XS(result0)); var w0 = new List<string>(); bd.FromBytes(w0, result0); CollectionAssert.AreEqual(v0, w0); bd.FromBytes(w0, new byte[] { 0x21, 0x10, 0, 0, 0, 0 }); CollectionAssert.AreEqual(v0, w0); bd.FromBytes(w0, result0); CollectionAssert.AreEqual(new List<string> { "a", "b", "c", "a", "b", "c" }, w0); }
public void TestFloat() { var bs = new BinarySerializer(); bs.Options.TagMode = TagMode.Names; var v = new SampleFloat { F = 1e-20f, D = -3.1415e100d }; var result1 = bs.ToBytes(v); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleFloat)) + " 02 00 " + XS("F", RoughType.Float, "D", RoughType.Double) + " 01 00 08 E5 3C 1E 02 00 CA DC 09 3E BE B9 CC D4 00 00", XS(result1)); var w = new SampleFloat(); var bd = new BinaryDeserializer(); bd.Options.TagMode = TagMode.Names; bd.FromBytes(w, result1); Assert.AreEqual(v.F, w.F); Assert.AreEqual(v.D, w.D); }
public void TestSmallTypes() { var bs = new BinarySerializer(); var v1 = new SampleSmallTypes { Ch = 'A', Sh = -2000, USh = 2001, B = 198, Sb = -109 }; var result = bs.ToBytes(v1); Assert.AreEqual( XS(RoughType.Record) + " 01 00 " + XS(typeof(SampleSmallTypes)) + " 05 00 " + XS("B", RoughType.Byte, "Ch", RoughType.Char) + " " + XS("Sb", RoughType.SByte, "Sh", RoughType.Short, "USh", RoughType.UShort) + " 01 00 C6 02 00 41 03 00 93 04 00 30 F8 05 00 D1 07 00 00", XS(result)); var v2 = new SampleSmallTypes(); var bd = new BinaryDeserializer(); bd.FromBytes(v2, result); Assert.AreEqual(v1.Ch, v2.Ch); Assert.AreEqual(v1.USh, v2.USh); Assert.AreEqual(v1.Sh, v2.Sh); Assert.AreEqual(v1.B, v2.B); Assert.AreEqual(v1.Sb, v2.Sb); v2 = (SampleSmallTypes)((new BinaryDeserializerGen()).FromBytes(result)); Assert.AreEqual(v1.Ch, v2.Ch); Assert.AreEqual(v1.USh, v2.USh); Assert.AreEqual(v1.Sh, v2.Sh); Assert.AreEqual(v1.B, v2.B); Assert.AreEqual(v1.Sb, v2.Sb); bd.FromBytes(v2, new byte[] { 0x20, 01, 00, 01, 00, 255, 02, 00, 65 + 25, 03, 00, 256 - 128, 04, 00, 00, 128, 05, 00, 255, 127, 00, 00 }); Assert.AreEqual('Z', v2.Ch); Assert.AreEqual(32767, v2.USh); Assert.AreEqual(-32768, v2.Sh); Assert.AreEqual(255, v2.B); Assert.AreEqual(-128, v2.Sb); }
public void TestInterface() { var bs = new BinarySerializer(); var v1 = new SampleInterfaceField { I = new SampleInterfaced { X = 34 } }; var result1 = bs.ToBytes(v1); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleInterfaceField)) + " 01 00 " + XS("I", RoughType.Record) + " 01 00 02 00 " + XS(typeof(SampleInterfaced)) + " 01 00 " + XS("X", RoughType.Int) + " 01 00 22 00 00 00 00 00 00 00", XS(result1)); var w1 = new SampleInterfaceField(); var bd = new BinaryDeserializer(); bd.FromBytes(w1, result1); Assert.IsInstanceOfType(w1.I, typeof(SampleInterfaced)); Assert.AreEqual(34, w1.I.X); var w1g = new SampleInterfaceField(); var bdg = new BinaryDeserializerGen(); bdg.FromBytes(w1g, result1); Assert.IsInstanceOfType(w1g.I, typeof(SampleInterfaced)); Assert.AreEqual(34, w1g.I.X); var w1n = (SampleInterfaceField)bd.FromBytes(new byte[] { 0x20, 01, 00, 01, 00, 00, 00, 00, 00 }); Assert.AreEqual(null, w1n.I); var w1ng = (SampleInterfaceField)bdg.FromBytes(new byte[] { 0x20, 01, 00, 01, 00, 00, 00, 00, 00 }); Assert.AreEqual(null, w1ng.I); var v2 = new List<ISample> { null, new SampleInterfaced { X = 37 } }; var result2 = bs.ToBytes(v2); Assert.AreEqual("21 20 02 00 00 00 00 00 02 00 01 00 25 00 00 00 00 00", XS(result2)); var w2 = new List<ISample>(); bd.FromBytes(w2, result2); Assert.AreEqual(2, w2.Count); Assert.IsNull(w2[0]); Assert.AreEqual(37, w2[1].X); ISampleField v3 = new SampleInterfacedField { X = 41 }; var result3 = bs.ToBytes(v3); Assert.AreEqual( "20 03 00 " + XS(typeof(SampleInterfacedField)) + " 01 00 " + XS("X", RoughType.Int) + " 01 00 29 00 00 00 00 00", XS(result3)); var w3 = (ISampleField)bd.FromBytes(result3); Assert.AreEqual(41, w3.X); }
public void TestSimple() { var bs = new BinarySerializer(); bs.Options.AllowEmptyTypes = true; Assert.AreEqual( XS(RoughType.Record) + " 01 00 " + XS(typeof(Empty)) + " 00 00 00 00", XS(bs.ToBytes(new Empty()))); var v1 = new Sample1 { X = 345, Y = "test" }; bs.ClearClassIds(); var result = bs.ToBytes(v1); Assert.AreEqual( XS(RoughType.Record) + " 01 00 " + XS(typeof(Sample1)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 59 01 00 00 02 00 " + XS("test") + " 00 00", XS(result)); Sample1 v2 = new Sample1(); var bd = new BinaryDeserializer(); bd.FromBytes(v2, result); Assert.AreEqual(v1.X, v2.X); Assert.AreEqual(v1.Y, v2.Y); bd.FromBytes(v2, new byte[] { 0x20, 01, 00, 01, 00, 0xE7, 03, 00, 00, 00, 00 }); Assert.AreEqual(999, v2.X); Assert.AreEqual(v1.Y, v2.Y); v1.X = int.MaxValue; bd.FromBytes(v2, bs.ToBytes(v1)); Assert.AreEqual(v1.X, v2.X); v1.X = int.MaxValue; bd.FromBytes(v2, bs.ToBytes(v1)); Assert.AreEqual(v1.X, v2.X); }
public void TestList() { var bs = new BinarySerializer(); bs.Options.TagMode = TagMode.Names; var bd = new BinaryDeserializer(); bd.Options.TagMode = TagMode.Names; var v0 = new SampleList { E = new List<string> { "a", "b", "c" } }; var result0 = bs.ToBytes(v0); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleList)) + " 01 00 " + XS("E", RoughType.Sequence) + " " + XS(RoughType.String) + " 01 00 03 00 00 00 " + XS("a", "b", "c") + " 00 00", XS(result0)); var w0 = new SampleList(); bd.FromBytes(w0, result0); CollectionAssert.AreEqual(v0.E, w0.E); var v1 = new SampleTree { Value = 11, Children = new List<SampleTree>() }; var result1 = bs.ToBytes(v1); Assert.AreEqual( "20 02 00 " + XS(typeof(SampleTree)) + " 02 00 " + XS("Value", RoughType.Int, "Children", RoughType.Sequence) + " " + XS(RoughType.Record) + " 01 00 0B 00 00 00 02 00 00 00 00 00 00 00", XS(result1)); Assert.AreEqual("20 02 00 01 00 0B 00 00 00 02 00 00 00 00 00 00 00", XS(bs.ToBytes(v1))); var w1 = new SampleTree(); bd.FromBytes(w1, result1); Assert.AreEqual(0, w1.Children.Count); var v2 = new SampleTree { Value = 11, Children = new List<SampleTree> { new SampleTree { Value = 12, Children = new List<SampleTree>(), }, new SampleTree { Value = 13, } } }; var result2 = bs.ToBytes(v2); Assert.AreEqual( "20 02 00 01 00 0B 00 00 00 02 00 02 00 00 00 " + "02 00 01 00 0C 00 00 00 02 00 00 00 00 00 00 00 " + "02 00 01 00 0D 00 00 00 02 00 FF FF FF FF 00 00 00 00", XS(result2)); SampleTree w2 = new SampleTree(); bd.FromBytes(w2, result2); Assert.AreEqual(v2.Value, w2.Value); Assert.AreEqual(v2.Children.Count, w2.Children.Count); Assert.AreEqual(v2.Children[0].Value, w2.Children[0].Value); Assert.AreEqual(v2.Children[1].Children, w2.Children[1].Children); Assert.AreEqual( "20 03 00 " + XS(typeof(SampleEmptyList)) + " 01 00 " + XS("E", RoughType.Sequence) + " " + XS(RoughType.String) + " 00 00", XS(bs.ToBytes(new SampleEmptyList()))); Assert.AreEqual( "20 03 00 01 00 FF FF FF FF 00 00", XS(bs.ToBytes(new SampleEmptyList { E = null }))); }
public void TestDefault() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var bdg = new BinaryDeserializerGen(); var v1 = new Sample1 { X = 6, Y = "ttt" }; var result1 = bs.ToBytes(v1); Assert.AreEqual( "20 01 00 " + XS(typeof(Sample1)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 06 00 00 00 00 00", XS(result1)); var w1 = (Sample1)bd.FromBytes(result1); Assert.AreEqual(6, w1.X); Assert.AreEqual("zzz", w1.Y); var w1g = (Sample1)bdg.FromBytes(result1); Assert.AreEqual(6, w1g.X); Assert.AreEqual("zzz", w1g.Y); var v2 = new Sample2 { X = 5, Y = "5" }; var result2 = bs.ToBytes(v2); Assert.AreEqual( "20 02 00 " + XS(typeof(Sample2)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 05 00 00 00 00 00", XS(result2)); Assert.IsInstanceOfType(bd.FromBytes(result2), typeof(Sample2)); var v3 = new SampleDefault(); var result3 = bs.ToBytes(new SampleDefault()); Assert.AreEqual( "20 03 00 " + XS(typeof(SampleDefault)) + " 03 00 " + XS("A", RoughType.Int, "B", RoughType.String, "P", RoughType.Record) + " 00 00", XS(result3)); Assert.IsInstanceOfType(bd.FromBytes(result3), typeof(SampleDefault)); v3.B = "z"; var result3m = bs.ToBytes(v3); Assert.AreEqual("20 03 00 02 00 " + XS("z") + " 00 00", XS(result3m)); var w3 = new SampleDefault(); bd.FromBytes(w3, result3m); Assert.AreEqual(3, w3.A); Assert.AreEqual("z", w3.B); Assert.AreEqual(new SamplePoint { X = 7, Y = 2 }, w3.P); var result4 = SX( "20 01 00 " + XS(typeof(SampleDefault)) + " 02 00 " + XS("A", RoughType.Int, "P", RoughType.Record) + " 01 00 05 00 00 00 " + "02 00 02 00 " + XS(typeof(SamplePoint)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.Int) + " 04 00 00 00 06 00 00 00 " + "00 00" ); bdg.ClearClassIds(); var w4 = bdg.FromBytes<SampleDefault>(result4); Assert.AreEqual(5, w4.A); Assert.AreEqual("default", w4.B); Assert.AreEqual(new SamplePoint { X = 4, Y = 6 }, w4.P); }
public void TestMemberOfInterface() { var bs = new BinarySerializer(); var v1 = new List<ISampleMember>(); var result1 = bs.ToBytes(v1); Assert.AreEqual("21 20 00 00 00 00", XS(result1)); var bd = new BinaryDeserializer(); var w1 = new List<ISampleMember>(); bd.FromBytes(w1, result1); Assert.AreEqual(0, w1.Count); v1.Add(new SampleMemberI()); var result2 = bs.ToBytes(v1); Assert.AreEqual( "21 20 01 00 00 00 01 00 " + XS(typeof(SampleMemberI)) + " 01 00 " + XS("X", RoughType.Int) + " 00 00", XS(result2)); bd.FromBytes(w1, result2); Assert.AreEqual(71, w1[0].X); Assert.AreEqual("21 20 00 00 00 00", XS(bs.ToBytes(new List<SampleMemberAbstract>()))); var v3 = new List<SampleMemberAbstract> { new SampleMemberConcrete() }; var result3 = bs.ToBytes(v3); Assert.AreEqual( "21 20 01 00 00 00 02 00 " + XS(typeof(SampleMemberConcrete)) + " 01 00 " + XS("X", RoughType.Int) + " 00 00", XS(result3)); var w3 = new List<SampleMemberAbstract>(); bd.FromBytes(w3, result3); Assert.AreEqual(72, w3[0].X); }
public void TestDelegate() { var bs = new BinarySerializer(); var v1 = new SampleSelfDelegate { x = 77 }; v1.OnSomething = v1.Handler1; var result = bs.ToBytes(v1); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleSelfDelegate)) + " 02 00 " + XS("OnSomething", RoughType.Record, "x", RoughType.Int) + " 01 00 " + XS("Handler1") + " 02 00 4D 00 00 00 00 00", XS(result)); var w1 = new SampleSelfDelegate(); var bd = new BinaryDeserializer(); bd.FromBytes(w1, result); Assert.AreEqual(v1.x, w1.x); w1.OnSomething(10); Assert.AreEqual(87, w1.x); result[69]++; // Replace("Handler1", "Handler2") (new BinaryDeserializer()).FromBytes(w1, result); w1.OnSomething(10); Assert.AreEqual(770, w1.x); }
public void TestNested() { var bs = new BinarySerializer(); bs.Options.TagMode = TagMode.Names; var v = new Sample3 { S1 = new Sample1 { X = 345, Y = "test" }, F = 222, S2 = new Sample2 { X = -346, Y = "test1" }, }; var result = bs.ToBytes(v); Assert.AreEqual( "20 01 00 " + XS(typeof(Sample3)) + " 03 00 " + XS("S1", RoughType.Record, "F", RoughType.Int, "S2", RoughType.Record) + " 01 00 02 00 " + XS(typeof(Sample1)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 59 01 00 00 02 00 " + XS("test") + " 00 00 " + "02 00 DE 00 00 00 " + "03 00 03 00 " + XS(typeof(Sample2)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 A6 FE FF FF 02 00 " + XS("test1") + " 00 00 00 00", XS(result)); var bd = new BinaryDeserializer(); bd.Options.TagMode = TagMode.Names; var w = new Sample3(); bd.FromBytes(w, result); Assert.AreEqual(v.S1.X, w.S1.X); Assert.AreEqual(v.S1.Y, w.S1.Y); Assert.AreEqual(v.F, w.F); Assert.AreEqual(v.S2.X, w.S2.X); Assert.AreEqual(v.S2.Y, w.S2.Y); }
public void TestDictionary() { var bs = new BinarySerializer(); bs.Options.TagMode = TagMode.Names; var bd = new BinaryDeserializer(); bd.Options.TagMode = TagMode.Names; var bdg = new BinaryDeserializer(); bdg.Options.TagMode = TagMode.Names; var v0 = new SampleDict { Value = 3, Children = new Dictionary<string, SampleDict> { { "a", new SampleDict { Value = 5, Children = new Dictionary<string, SampleDict>() } }, { "b", new SampleDict { Value = 7 } }, } }; var result0 = bs.ToBytes(v0); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleDict)) + " 02 00 " + XS("Value", RoughType.Int, "Children", RoughType.Mapping) + " 10 20" + " 01 00 03 00 00 00 02 00 02 00 00 00 " + XS("a") + " 01 00 01 00 05 00 00 00 02 00 00 00 00 00 00 00 " + XS("b") + " 01 00 01 00 07 00 00 00 02 00 FF FF FF FF 00 00 00 00", XS(result0)); var w0 = new SampleDict(); bd.FromBytes(w0, result0); Assert.AreEqual(v0.Value, w0.Value); Assert.AreEqual(v0.Children.Count, w0.Children.Count); Assert.AreEqual(v0.Children["a"].Value, w0.Children["a"].Value); var w1 = (SampleDict)bdg.FromBytes(result0); Assert.AreEqual(v0.Value, w1.Value); Assert.AreEqual(v0.Children.Count, w1.Children.Count); Assert.AreEqual(v0.Children["a"].Value, w1.Children["a"].Value); }
public override object DeepObject(object src) { bd.Options = Options; return(bd.FromBytes(bs.ToBytes(src))); }
public void TestErrors() { var bd = new BinaryDeserializer(); bd.Options.AllowEmptyTypes = true; var bdg = new BinaryDeserializerGenerator(); XAssert.Throws<YuzuException>(() => bdg.Generate<ISample>(), "ISample"); XAssert.Throws<YuzuException>(() => bdg.Generate<SampleAbstract>(), "SampleAbstract"); XAssert.Throws<YuzuException>(() => bd.FromBytes<int>(new byte[] { 0xFF }), "255"); XAssert.Throws<YuzuException>(() => bd.FromBytes(new byte[] { 0xFF }), "255"); XAssert.Throws<YuzuException>(() => bd.FromBytes<int>(new byte[] { 0xFF }), "255"); XAssert.Throws<YuzuException>(() => bd.FromBytes<int>(new byte[] { 07 }), "Int32"); XAssert.Throws<YuzuException>(() => bd.FromBytes(new byte[] { (byte)RoughType.Any }), "pure"); XAssert.Throws<YuzuException>(() => bd.FromBytes<Sample1>(SX( "20 01 00 " + XS("notype") + " 00 00 00 00" )), "YuzuUnknown"); var w = new Sample1(); XAssert.Throws<YuzuException>(() => bd.FromBytes(w, SX( "20 02 00 " + XS(typeof(Empty)) + " 00 00 00 00" )), "Sample1"); var w2 = new Sample2Allow(); XAssert.Throws<YuzuException>(() => bd.FromBytes(w2, SX( "20 02 00 00 00" )), "Sample2Allow"); XAssert.Throws<YuzuException>(() => bd.FromBytes(w, SX("20 05 00")), "5"); XAssert.Throws<YuzuException>(() => bd.FromBytes(w, SX( "20 03 00 " + XS(typeof(Sample1)) + " 00 00 00 00" )), " X "); XAssert.Throws<YuzuException>(() => bd.FromBytes(w, SX( "20 03 00 " + XS(typeof(Empty)) + " 00 01 " + XS("New", RoughType.Int) + " 00 00" )), "New"); XAssert.Throws<YuzuException>(() => bd.FromBytes(w, SX( "20 03 00 " + XS(typeof(Sample1)) + " 00 01 " + XS("X", RoughType.String) + " 00 00" )), "Int32"); }
public void TestDate() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var v1 = new SampleDate { D = new DateTime(2011, 3, 25), T = TimeSpan.FromMinutes(5) }; var result1 = bs.ToBytes(v1); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleDate)) + " 02 00 " + XS("D", RoughType.DateTime, "T", RoughType.TimeSpan) + " 01 00 00 00 F5 B7 96 B8 CD 08 02 00 00 5E D0 B2 00 00 00 00 00 00", XS(result1)); var w1 = new SampleDate(); bd.FromBytes(w1, result1); Assert.AreEqual(v1.D, w1.D); Assert.AreEqual(v1.T, w1.T); var v2 = new DateTime(2011, 3, 25, 1, 2, 3, DateTimeKind.Utc); var result2 = bs.ToBytes(v2); var w2 = bd.FromBytes<DateTime>(result2); Assert.AreEqual(v2, w2); Assert.AreEqual(v2.Kind, w2.Kind); var v3 = new DateTime(2011, 3, 25, 1, 2, 3, DateTimeKind.Local); var result3 = bs.ToBytes(v3); var w3 = bd.FromBytes<DateTime>(result3); Assert.AreEqual(v3, w3); Assert.AreEqual(v3.Kind, w3.Kind); }
public void TestStruct() { var v = new SampleRect { A = new SamplePoint { X = 33, Y = 44 }, B = new SamplePoint { X = 55, Y = 66 }, }; var bs = new BinarySerializer(); var result = bs.ToBytes(v); Assert.AreEqual( "20 01 00 " + XS(typeof(SampleRect)) + " 02 00 " + XS("A", RoughType.Record, "B", RoughType.Record) + " 01 00 02 00 " + XS(typeof(SamplePoint)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.Int) + " 21 00 00 00 2C 00 00 00 " + "02 00 02 00 37 00 00 00 42 00 00 00 00 00", XS(result)); var bd = new BinaryDeserializer(); var w = new SampleRect(); bd.FromBytes(w, result); CheckSampleRect(v, w); var bdg = new BinaryDeserializerGen(); w = (SampleRect)bdg.FromBytes(result); CheckSampleRect(v, w); var p = (SamplePoint)bdg.FromBytes(new SamplePoint(), SX("20 02 00 22 00 00 00 2D 00 00 00")); Assert.AreEqual(34, p.X); Assert.AreEqual(45, p.Y); }