public void EqualityTest()
        {
            var seq1 = new BitSequence(16);
            var seq2 = new BitSequence(16);

            seq1.Push(true);
            seq1.Push(true);
            seq2.Push(seq1);
            Assert.Equal(seq1, seq2);
            Assert.Equal(seq1.GetHashCode(), seq2.GetHashCode());
        }
Beispiel #2
0
        public void KeysTest()
        {
            Dictionary <BitSequence, char> dict = new Dictionary <BitSequence, char>();
            var seq1 = new BitSequence(16);
            var seq2 = new BitSequence(16);

            seq1.Push(true);
            seq1.Push(true);
            seq2.Push(seq1);
            dict[seq1] = 'c';
            Assert.True(dict.ContainsKey(seq2));
        }
        public void PushSequenceTest()
        {
            var seq1 = new BitSequence(16);
            var seq2 = new BitSequence(8);

            seq1.Push();
            seq1.Push(true);
            seq2.Push();
            seq2.Push(true);
            seq2.Push(true);
            seq2.Push(true);
            seq1.Push(seq2);
            Assert.Equal("010111", seq1.ToString());
            Assert.Equal(6, seq1.Length);
        }
        public void DeepCopyTest()
        {
            var seq1 = new BitSequence(16);

            seq1.Push(true);
            seq1.Push(true);
            var seq2 = seq1.DeepCopy();

            Assert.True(seq2[0]);
            Assert.True(seq2[1]);
            Assert.True(seq1[0]);
            Assert.True(seq1[1]);
            Assert.Equal(seq1.rawData.Length, seq2.rawData.Length);
            seq2[0] = false;
            Assert.True(seq1[0]);
            Assert.False(seq2[0]);
        }
Beispiel #5
0
        private static (byte[] encoded, int padding) Encode(string text, Dictionary <char, BitSequence> dictionary)
        {
            BitSequence sequence = new BitSequence(8 * text.Length);

            foreach (char c in text)          // pętla przez każdy znak tekstu
            {
                sequence.Push(dictionary[c]); // dodaje do sekwencji bitów kod odpowiadajacy znakowi
            }
            return(sequence.GetBytes());
        }
        public void ReverseTest()
        {
            var seq = new BitSequence(16);

            seq.Push();
            seq.Push(true);
            seq.Push();
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push();
            seq.Push(true);
            Assert.Equal("010111101", seq.ToString());
            seq.Reverse();;
            Assert.Equal("101111010", seq.ToString());
        }
        public void ToBytesTest()
        {
            var seq = new BitSequence(16);

            seq.Push();
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            var(arr, pad) = seq.GetBytes();
            Assert.Equal(7, pad);
            Assert.Equal(2, arr.Length);
            Assert.Equal(new byte[] { 127, 128 }, arr);
        }
        public void BytesLengthTest()
        {
            var seq = new BitSequence(16);

            Assert.Equal(0, seq.BytesLength);
            seq.Push();
            Assert.Equal(1, seq.BytesLength);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            seq.Push(true);
            Assert.Equal(1, seq.BytesLength);
            seq.Push(true);
            Assert.Equal(2, seq.BytesLength);
        }
        public void ToStringTest()
        {
            var seq = new BitSequence(16);

            seq.Push();
            seq.Push(true);
            seq.Push(false);
            seq.Push(true);
            seq.Push(true);
            seq.Push(false);
            Assert.Equal("010110", seq.ToString());
        }
Beispiel #10
0
        private static string Decode(byte[] data, int padding, Dictionary <BitSequence, char> dictionary)
        {
            int           len     = data.Length * 8 - padding;
            StringBuilder sb      = new StringBuilder();
            BitSequence   encoded = new BitSequence(data, len);
            BitSequence   temp    = new BitSequence(8);

            for (int i = 0; i < len; i++)         // pętla przez każdy bit danych
            {
                temp.Push(encoded[i]);            // dodanie bitu do stosu
                if (dictionary.ContainsKey(temp)) // jezeli stos w obecnej postaci odpowiada jakiemuś kodowi
                {
                    // to dodaj odpowiedni znak do wyniku
                    sb.Append(dictionary[temp]);
                    temp = new BitSequence(16); // wyzeruj stos
                }
            }

            return(sb.ToString());
        }
        public void PushTest()
        {
            var seq = new BitSequence(16);

            seq.Push();
            seq.Push(true);
            seq.Push(false);
            seq.Push(true);
            seq.Push(true);
            seq.Push(false);
            Assert.False(seq[0]);
            Assert.True(seq[1]);
            Assert.False(seq[2]);
            Assert.True(seq[3]);
            Assert.True(seq[4]);
            Assert.False(seq[5]);
            Assert.Equal(6, seq.Length);
        }