Пример #1
0
        public static T DoRoundtripTest <T>(
            Func <Stream> getOriginalStream,
            Func <Stream, T> parseCallback,
            Action <T, Stream> serializeCallback,
            bool skipRoundtripEqualityTest = false)
        {
            byte[] originalUncompressedBytes;
            using (var originalUncompressedStream = new MemoryStream())
                using (var entryStream = getOriginalStream())
                {
                    entryStream.CopyTo(originalUncompressedStream);
                    originalUncompressedBytes = originalUncompressedStream.ToArray();
                }

            T parsedFile = default;

            try
            {
                using (var entryStream = new MemoryStream(originalUncompressedBytes, false))
                {
                    parsedFile = parseCallback(entryStream);
                }
            }
            catch
            {
                File.WriteAllBytes("original.bin", originalUncompressedBytes);
                throw;
            }

            byte[] serializedBytes;
            using (var serializedStream = new MemoryStream())
            {
                serializeCallback(parsedFile, serializedStream);
                serializedBytes = serializedStream.ToArray();
            }

            if (originalUncompressedBytes.Length != serializedBytes.Length)
            {
                File.WriteAllBytes("original.bin", originalUncompressedBytes);
                File.WriteAllBytes("serialized.bin", serializedBytes);
            }

            Assert.Equal(originalUncompressedBytes.Length, serializedBytes.Length);

            if (!skipRoundtripEqualityTest)
            {
                AssertUtility.Equal(originalUncompressedBytes, serializedBytes);
            }

            return(parsedFile);
        }
Пример #2
0
        public static void DoRoundtripTest <T>(
            Func <Stream> getOriginalStream,
            Func <Stream, T> parseCallback,
            Action <T, Stream> serializeCallback,
            bool skipRoundtripEqualityTest = false)
        {
            byte[] originalUncompressedBytes;
            using (var originalUncompressedStream = new MemoryStream())
                using (var entryStream = getOriginalStream())
                {
                    entryStream.CopyTo(originalUncompressedStream);
                    originalUncompressedBytes = originalUncompressedStream.ToArray();
                }

            T parsedFile;

            using (var entryStream = new MemoryStream(originalUncompressedBytes, false))
            {
                parsedFile = parseCallback(entryStream);
            }

            byte[] serializedBytes;
            using (var serializedStream = new MemoryStream())
            {
                serializeCallback(parsedFile, serializedStream);
                serializedBytes = serializedStream.ToArray();
            }

            if (!skipRoundtripEqualityTest)
            {
                //File.WriteAllBytes("original.bin", originalUncompressedBytes);
                //File.WriteAllBytes("serialized.bin", serializedBytes);

                AssertUtility.Equal(originalUncompressedBytes, serializedBytes);
            }
        }