public PortableObjectIT() { context = new PofContext(); context.RegisterPortableObjectType(0x11223344, typeof(PersonKey)); context.RegisterPortableObjectType(0x55667788, typeof(PersonEntry)); context.RegisterPortableObjectType(0x778899AA, typeof(RemovalByLevelThresholdProcessor)); context.RegisterPortableObjectType(0x30291dea, typeof(FriendClearingProcessor)); }
public void Run() { const string rootName = "root"; const string rootLeftName = "root_left"; const string rootRightName = "root_right"; const string rootRightRightName = "root_right_right"; var context = new PofContext(); context.RegisterPortableObjectType(0, typeof(Node)); var serializer = new PofSerializer(context); var rootRightRight = new Node(rootRightName, null, null); var rootRight = new Node(rootRightName, null, rootRightRight); var rootLeft = new Node(rootLeftName, null, null); var root = new Node("root", rootLeft, rootRight); using (var ms = new MemoryStream()) using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) { serializer.Serialize(writer, root); ms.Position = 0; var newRoot = serializer.Deserialize <Node>(reader); AssertEquals(root, newRoot); } }
public void PolymorphismTest() { var testObj1 = new TestClass(new object[] { 0xCDCDCDCD, "herp", new byte[] { 0xEE, 0xDD, 0xCC, 0xDD, 0xEE, 0xCC, 0xFF } }); var context = new PofContext(); context.RegisterPortableObjectType(0xFF, typeof(TestClass)); var serializer = new PofSerializer(context); using (var ms = new MemoryStream()) { using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) { serializer.Serialize(writer, testObj1); } Console.WriteLine(ms.ToArray().ToHex()); ms.Position = 0; using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) { var readObj1 = serializer.Deserialize <TestClass>(reader); AssertTrue(testObj1.Value.Take(2).SequenceEqual(readObj1.Value.Take(2))); AssertTrue(((IEnumerable <byte>)testObj1.Value[2]).SequenceEqual((IEnumerable <byte>)readObj1.Value[2])); } } }
public void Run() { var context = new PofContext(); context.RegisterPortableObjectType(1, typeof(TestClass)); var serializer = new PofSerializer(context); var testObj1 = new TestClass(EnumerateValues()); var testObj2 = new TestClass(new object[] { null, null }); var testObj3 = new TestClass(new List <object> { 2, "string", null }); using (var ms = new MemoryStream()) { using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) { serializer.Serialize(writer, testObj1); serializer.Serialize(writer, testObj2); serializer.Serialize(writer, testObj3); } ms.Position = 0; using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) { var readObj1 = serializer.Deserialize <TestClass>(reader); var a = ((IEnumerable <object>)testObj1.Value).ToArray(); var b = ((IEnumerable <object>)readObj1.Value).ToArray(); AssertEquals(5, a.Length); AssertEquals(5, b.Length); for (var i = 0; i < b.Length; i++) { var ai = a[i]; var bi = b[i]; if (ai is IEnumerable && !(ai is string)) { AssertTrue(((IEnumerable <object>)ai).SequenceEqual((IEnumerable <object>)bi)); } else { AssertEquals(ai, bi); } } var readObj2 = serializer.Deserialize <TestClass>(reader); AssertTrue(((IEnumerable <object>)testObj2.Value).SequenceEqual((IEnumerable <object>)readObj2.Value)); var readObj3 = serializer.Deserialize <TestClass>(reader); AssertTrue(((IEnumerable <object>)testObj3.Value).SequenceEqual((IEnumerable <object>)readObj3.Value)); } } }
public void ComplexTest() { var context = new PofContext(); context.RegisterPortableObjectType(0, typeof(DummyClass <>)); var serializer = new PofSerializer(context); using (var ms = new MemoryStream()) { var writtenTypes = new Type[] { typeof(int), typeof(DummyClass <>), typeof(DummyClass <int>) }; Debug.WriteLine("Written Types: " + writtenTypes.Join(", ")); serializer.Serialize(ms, writtenTypes); ms.Position = 0; var readTypes = serializer.Deserialize <Type[]>(ms); // AssertEquals(readType, writtenType); Debug.WriteLine(" Read Types: " + readTypes.Join(", ")); } }
public void SimpleTest() { var context = new PofContext(); context.RegisterPortableObjectType(0, typeof(DummyClass <>)); var serializer = new PofSerializer(context); using (var ms = new MemoryStream()) { var writtenType = typeof(DummyClass <int>); Debug.WriteLine("Written Type: " + writtenType); serializer.Serialize(ms, writtenType); ms.Position = 0; var readType = serializer.Deserialize <Type>(ms); AssertEquals(readType, writtenType); Debug.WriteLine(" Read Type: " + readType); } }
public void Run() { var context = new PofContext(); context.RegisterPortableObjectType(1, typeof(Box)); var serializer = new PofSerializer(context); var obj = new object[] { null, 1, 2, 3 }; var box = new Box(obj); using (var ms = new MemoryStream()) using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) { serializer.Serialize(writer, box); serializer.Serialize(writer, null); ms.Position = 0; var newBox = serializer.Deserialize <Box>(reader); var nullBox = serializer.Deserialize <Box>(reader); AssertEquals(box, newBox); AssertNull(nullBox); } }
public void NestedArraysTest() { var dict = new SCG.Dictionary <int, float[][]>(); var random = new Random(0); for (var i = 0; i < 10; i++) { dict.Add(i, Util.Generate(i, x => Util.Generate(x, y => (float)random.NextDouble()))); } var context = new PofContext(); context.RegisterPortableObjectType(1, typeof(Wrapper)); var serializer = new PofSerializer(context); using (var ms = new MemoryStream()) { using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) { serializer.Serialize(writer, new Wrapper(dict)); } ms.Position = 0; Console.WriteLine(ms.ToArray().ToHex()); using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) { var wrapperCopy = (Wrapper)serializer.Deserialize(reader); var dictClone = (SCG.Dictionary <int, float[][]>)wrapperCopy.Value; AssertTrue(new ICL.HashSet <int>(dict.Keys).SetEquals(dictClone.Keys)); random = new Random(); for (var i = 0; i < 10; i++) { var arr = dict[i]; for (var j = 0; j < arr.Length; j++) { AssertTrue(new ICL.HashSet <float>(arr[j]).SetEquals(dictClone[i][j])); } } } } }