Zobrazujú sa príspevky s označením MSTest. Zobraziť všetky príspevky
Zobrazujú sa príspevky s označením MSTest. Zobraziť všetky príspevky

štvrtok 12. januára 2012

Example of compressing and decompressing byte array in C# using Gzip algorithm

public static class Zip
    {
        public static byte[] Decompress(byte[] zippedData)
        {
            byte[] decompressedData = null;
            using (MemoryStream outputStream = new MemoryStream())
            {
                using (MemoryStream inputStream = new MemoryStream(zippedData))
                {
                    using (GZipStream zip = new GZipStream(inputStream, CompressionMode.Decompress))
                    {
                        zip.CopyTo(outputStream);
                    }
                }
                decompressedData = outputStream.ToArray();
            }

            return decompressedData;
        }

        public static byte[] Compress(byte[] plainData)
        {
            byte[] compressesData = null;
            using (MemoryStream outputStream = new MemoryStream())
            {
                using (GZipStream zip = new GZipStream(outputStream, CompressionMode.Compress))
                {
                    zip.Write(plainData, 0, plainData.Length);                   
                }
                //Dont get the MemoryStream data before the GZipStream is closed
                //since it doesn’t yet contain complete compressed data.
                //GZipStream writes additional data including footer information when its been disposed
                compressesData = outputStream.ToArray();
            }

            return compressesData;
        }

    }


And MS Test


 [TestClass]
    public class ZipTEST
    {
        [TestMethod]
        public void TestMethod1()
        {
            string text = "Hello World";
   
            byte [] data = System.Text.Encoding.UTF8.GetBytes(text);

             byte [] compressedData = Zip.Compress(data);
           
            byte [] decompressedData = Zip.Decompress(compressedData);



            string textAfterComDecompress = System.Text.Encoding.UTF8.GetString(decompressedData);

            Assert.AreEqual<string>(text, textAfterComDecompress);               
        }
    }







pondelok 9. januára 2012

Unit Test Atributes in commom testing frameworks

I'm rewriting some my old test classes, writed in NUnit to the "new" MS Test.
I was looking for the alternative of TestFixtureTearDown

and Whoa la, I found this very usefull table:


NUnit 2.x MbUnit 2.4 MSTest xUnit.net
[Test] [Test] [TestMethod] [Fact]
[TestFixture] [TestFixture] [TestClass] n/a
[ExpectedException] [ExpectedException] [ExpectedException] Assert.Throws or Record.Exception
[SetUp] [SetUp] [TestInitialize] Constructor
[TearDown] [TearDown] [TestCleanup] IDisposable.Dispose
[TestFixtureSetUp] [TestFixtureSetUp] [ClassInitialize] IUseFixture<T>
[TestFixtureTearDown] [TestFixtureTearDown] [ClassCleanup] IUseFixture<T>
[Ignore] [Ignore] [Ignore] [Fact(Skip="reason")]
n/a [Timeout] [Timeout] [Fact(Timeout=n)]
[Property] n/a [TestProperty] [Trait]
n/a [Row], [RowTest] [DataSource] [Theory], [XxxData]