예제 #1
0
        public static byte[] Serialize <T>(this DsdlSerializer serializer, T data)
        {
            var buffer = new byte[serializer.GetMaxBufferLength <T>()];

            serializer.Serialize <T>(data, buffer);
            return(buffer);
        }
예제 #2
0
        public static void Test(object data, string expectedSerializedContent, bool doRoundtripTest)
        {
            var expectedBytes = Hex.Decode(expectedSerializedContent);

            var serializer = new DsdlSerializer(_uavcanTypeResolver);

            var bufferLength = serializer.GetMaxBufferLength(data.GetType());
            var buffer       = new byte[bufferLength];
            var size         = serializer.Serialize(data, buffer);

            buffer = buffer.Take(size).ToArray();

            Assert.True(expectedBytes.SequenceEqual(buffer), "Serialized payload mismatch.");

            var deserialized = serializer.Deserialize(data.GetType(), buffer);

            if (doRoundtripTest)
            {
                var comparisonResult = _compareLogic.Compare(data, deserialized);
                if (!comparisonResult.AreEqual)
                {
                    bool ignoreInequality = true;
                    foreach (var diff in comparisonResult.Differences)
                    {
                        if (!ignoreInequality)
                        {
                            break;
                        }

                        if (diff.Object1 is float f1 && diff.Object2 is float f2)
                        {
                            var fdiff = Math.Abs(f1 - f2);
                            if (fdiff > 0.0001)
                            {
                                ignoreInequality = false;
                            }
                        }
                        else
                        {
                            ignoreInequality = false;
                        }
                    }

                    Assert.True(ignoreInequality, comparisonResult.DifferencesString);
                }