static void TestWriteExt <T>() where T : IExtTest, IExtensible, new() { const float SOME_VALUE = 987.65F; T obj = new T(); var tm = TypeModel.Create(false, ProtoCompatibilitySettingsValue.FullCompatibility); Extensible.AppendValue <float>(tm, obj, 3, SOME_VALUE); byte[] raw = GetExtensionBytes(obj); Assert.AreEqual(5, raw.Length, "Extension Length"); Assert.AreEqual((3 << 3) | 5, raw[0], "Prefix (3 Fixed32)"); byte[] tmp = BitConverter.GetBytes(SOME_VALUE); if (!BitConverter.IsLittleEndian) { Array.Reverse(tmp); } Assert.AreEqual(tmp[0], raw[1], "Float32 Byte 0"); Assert.AreEqual(tmp[1], raw[2], "Float32 Byte 1"); Assert.AreEqual(tmp[2], raw[3], "Float32 Byte 2"); Assert.AreEqual(tmp[3], raw[4], "Float32 Byte 3"); float readBack = Extensible.GetValue <float>(obj, 3); Assert.AreEqual(SOME_VALUE, readBack, "read back"); BiggerObject big = tm.ChangeType <T, BiggerObject>(obj); Assert.AreEqual(SOME_VALUE, big.SomeFloat, "deserialize"); }
static void TestWriteExt <T>() where T : IExtTest, IExtensible, new() { const float SOME_VALUE = 987.65F; T obj = new T(); Extensible.AppendValue <float>(obj, 3, SOME_VALUE); byte[] raw = GetExtensionBytes(obj); Assert.Equal(5, raw.Length); //, "Extension Length"); Assert.Equal((3 << 3) | 5, raw[0]); //, "Prefix (3 Fixed32)"); byte[] tmp = BitConverter.GetBytes(SOME_VALUE); if (!BitConverter.IsLittleEndian) { Array.Reverse(tmp); } Assert.Equal(tmp[0], raw[1]); //, "Float32 Byte 0"); Assert.Equal(tmp[1], raw[2]); //, "Float32 Byte 1"); Assert.Equal(tmp[2], raw[3]); //, "Float32 Byte 2"); Assert.Equal(tmp[3], raw[4]); //, "Float32 Byte 3"); float readBack = Extensible.GetValue <float>(obj, 3); Assert.Equal(SOME_VALUE, readBack); //, "read back"); BiggerObject big = Serializer.ChangeType <T, BiggerObject>(obj); Assert.Equal(SOME_VALUE, big.SomeFloat); //, "deserialize"); }
static void TestReadExt <T>() where T : IExtTest, IExtensible, new() { BiggerObject obj = GetBigObject(); T small = Serializer.ChangeType <BiggerObject, T>(obj); _ = GetExtensionBytes(small); bool hasValue = Extensible.TryGetValue <float>(small, 3, out var val); Assert.True(hasValue); //, "has value"); Assert.Equal(obj.SomeFloat, val); //, "float value"); hasValue = Extensible.TryGetValue <float>(small, 1000, out val); Assert.False(hasValue); //, "no value"); Assert.Equal(default, val);
static void TestRoundTrip <T>() where T : IExtTest, IExtensible, new() { BiggerObject obj = GetBigObject(); T tmp = Serializer.ChangeType <BiggerObject, T>(obj); Assert.Equal(obj.Bof, tmp.Bof); //, "dehydrate"); Assert.Equal(obj.Eof, tmp.Eof); //, "dehydrate"); BiggerObject clone = Serializer.ChangeType <T, BiggerObject>(tmp); Assert.Equal(obj.Bof, clone.Bof); //, "rehydrate"); Assert.Equal(obj.Eof, clone.Eof); //, "rehydrate"); Assert.Equal(obj.SomeDouble, clone.SomeDouble); //, "rehydrate"); Assert.Equal(obj.SomeFloat, clone.SomeFloat); //, "rehydrate"); Assert.Equal(obj.SomeInt32, clone.SomeInt32); //, "rehydrate"); Assert.Equal(obj.SomeString, clone.SomeString); //, "rehydrate"); Assert.True(Program.ArraysEqual(obj.SomeBlob, clone.SomeBlob)); //, "rehydrate"); }
static void TestReadExt <T>() where T : IExtTest, IExtensible, new() { BiggerObject obj = GetBigObject(); T small = Serializer.ChangeType <BiggerObject, T>(obj); var tm = TypeModel.Create(false, ProtoCompatibilitySettingsValue.FullCompatibility); small = tm.ChangeType <BiggerObject, T>(obj); byte[] raw = GetExtensionBytes(small); float val; bool hasValue = Extensible.TryGetValue <float>(tm, small, 3, out val); Assert.IsTrue(hasValue, "has value"); Assert.AreEqual(obj.SomeFloat, val, "float value"); hasValue = Extensible.TryGetValue <float>(tm, small, 1000, out val); Assert.IsFalse(hasValue, "no value"); Assert.AreEqual(default(float), val); }