Exemple #1
0
        public void ThreeChars()
        {
            var          sut   = new LzwCompression();
            const string input = "tes";

            var compressed = sut.Compress(input);

            Assert.Equal(input, sut.Decompress(compressed));
        }
Exemple #2
0
        public void LongString()
        {
            var          sut   = new LzwCompression();
            const string input = "klYyn23RSBfVn8bqbQa2TRpTWjAkiHM5rL2YTDCl6DOQLdT2nj3kBb8GKLxdyj5Hx56SURDBGPFFEOKdwWA14YIJTwtHV7yqYqSbYOvYkbKKCXP3jsCMGH4xNyeGqQBoIKrR3hLh9YIqVLDp1Nq942xj10Qe8Nk67oCfMWYciqLnS6bwIiaRg5XIImegDHbqq6Iod79bZEAB7ziY2hoimMNBDDsEvgfP4e1V2kRewDSQgGWK09ErRB1YWNISW24jxCfIxjpeH1psyVYfGzhzn5bvaW9vkP9SKOqWcR9CLkFJpB6lmSl8phytGpFNw9AtZq8TqZ23pKm4n4jNI3po6Q0vziVnE8Bm5AqXcM9RXDzJHUIfWq93gH6Eo2U15SYZ6hT3NTZxYIE2qTWK5ImVBvxC0iYlpk9nVrpnNLzgGdflPkkeDzz0QpXqp55F4kM3pE1zLZS7LonXg0f9p5Nc4dhuqEVZSeC4zQCpVlRrLTp1gxGDYrUQiQS6dYlfSlRUmusu0ckzqzMFBmwpmD1jZoI4EBjytJ9SFpvCBmHnpiImOc6f4Rh9K35KxCG6ehzCEAdn407kcD1yXzsTmSCEk4Q8fdzUAKOET32v7etpPiStA6AVhSueVnVZqMEkVW3MMhPU0ROYQ9pya3byIqXOSZgMCFzNBkSuwk1id89tooEKzwrWfUmfRY1mgFgt6Pj5WSsN7GcmfbiojOCjDSigqNmPukTx9TH0nlSBswoAHcqCc3gmdlk2q8KGWuKfmqaJhYnOclFSrj8Kkk7ek6sbMTDZqw2jGkl5olSAG8tmGsYX10OivLX5ZU1kVVj2oSICk83gMhC3tDH9WOnAs6Zp6boQxgML61pqfzgXkDxkuX07jX3ilZ1XmrrGUcrDeMjq6Gk4QBYnDN7IPSLpfs45lPtPQZxo9Cuj3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            Assert.Equal(input, sut.Decompress(compressed));
        }
Exemple #3
0
        // Not the best test, cause in a random string there wouldn't be enough 1-2-3-grams to really show the results of the compression.
        // But it's ok to check some corner cases should they exist
        public void Fuzzy()
        {
            var        sut           = new LzwCompression();
            const int  inputLength   = 10000;
            var        stringBuilder = new StringBuilder(inputLength);
            var        random        = new Random();
            const byte minSymbol     = (byte)'0';
            const byte maxSymbol     = (byte)'~';

            for (var i = 0; i < inputLength; i++)
            {
                stringBuilder.Append((char)random.Next(minSymbol, maxSymbol));
            }

            var input      = stringBuilder.ToString();
            var compressed = sut.Compress(input);

            Assert.Equal(input, sut.Decompress(compressed));
        }
Exemple #4
0
 public void LzwCompression()
 {
     var _ = lzwCompression.Decompress(lzwCompression.Compress(Data));
 }
        public byte[] Serialize(object obj)
        {
            if (obj == null)
            {
                return new byte[] { (byte)ObjectTypes.Null }
            }
            ;

            PayloadWriter pw         = new PayloadWriter();
            Type          ObjectType = obj.GetType();

            if (ObjectType == typeof(byte))
            {
                pw.WriteByte((byte)ObjectTypes.Byte);

                pw.WriteByte((byte)obj);
            }
            else if (ObjectType == typeof(byte[]))
            {
                byte[] data = (byte[])obj;
                pw.WriteByte((byte)ObjectTypes.ByteArray);
                pw.WriteInteger(data.Length);
                pw.WriteBytes(data);
            }
            else if (ObjectType == typeof(short))
            {
                pw.WriteByte((byte)ObjectTypes.Short);
                pw.WriteShort((short)obj);
            }
            else if (ObjectType == typeof(ushort))
            {
                pw.WriteByte((byte)ObjectTypes.UShort);
                pw.WriteUShort((ushort)obj);
            }
            else if (ObjectType == typeof(int))
            {
                pw.WriteByte((byte)ObjectTypes.Int);
                pw.WriteInteger((int)obj);
            }
            else if (ObjectType == typeof(uint))
            {
                pw.WriteByte((byte)ObjectTypes.UINT);
                pw.WriteUInteger((uint)obj);
            }
            else if (ObjectType == typeof(long))
            {
                pw.WriteByte((byte)ObjectTypes.Long);
                pw.WriteLong((long)obj);
            }
            else if (ObjectType == typeof(bool))
            {
                pw.WriteByte((byte)ObjectTypes.Bool);
                pw.WriteByte((bool)obj ? (byte)1 : (byte)0);
            }
            else if (ObjectType == typeof(string))
            {
                pw.WriteByte((byte)ObjectTypes.String);
                pw.WriteString(obj.ToString());
            }
            else if (ObjectType == typeof(SolidBrush))
            {
                pw.WriteByte((byte)ObjectTypes.SolidBrush);
                pw.WriteByte(((SolidBrush)obj).Color.A);
                pw.WriteByte(((SolidBrush)obj).Color.R);
                pw.WriteByte(((SolidBrush)obj).Color.G);
                pw.WriteByte(((SolidBrush)obj).Color.B);
            }
            else if (ObjectType == typeof(Rectangle))
            {
                pw.WriteByte((byte)ObjectTypes.Rectangle);
                pw.WriteInteger(((Rectangle)obj).X);
                pw.WriteInteger(((Rectangle)obj).Y);
                pw.WriteInteger(((Rectangle)obj).Width);
                pw.WriteInteger(((Rectangle)obj).Height);
            }
            else if (ObjectType == typeof(Size))
            {
                pw.WriteByte((byte)ObjectTypes.Size);
                pw.WriteInteger(((Size)obj).Width);
                pw.WriteInteger(((Size)obj).Height);
            }
            else if (ObjectType == typeof(Bitmap) || ObjectType == typeof(Image))
            {
                pw.WriteByte((byte)ObjectTypes.Bitmap);
                lzwCompression.Compress((Bitmap)obj, pw.vStream);
            }
            else
            {
                MemoryStream    ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, obj);
                pw.WriteByte((byte)ObjectTypes.Other);
                pw.WriteBytes(ms.GetBuffer(), 0, (int)ms.Length);
                ms.Close();
            }
            return(pw.ToByteArray());
        }