Beispiel #1
0
        public virtual void TestReadShortEndianness()
        {
            var read = _input.ReadShort(Endianness.LittleEndian);
            var val  = BytesExtensions.ReadShort(InitData, 0, Endianness.LittleEndian);

            Assert.AreEqual(val, read);
        }
        public void ReadIntSequence()
        {
            var bytes    = new byte[] { 0 };
            var sequence = new ReadOnlySequence <byte>(bytes);

            Assert.Throws <ArgumentException>(() => _ = BytesExtensions.ReadInt(ref sequence));

            bytes    = new byte[] { 7, 91, 205, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            sequence = new ReadOnlySequence <byte>(bytes);
            Assert.That(BytesExtensions.ReadInt(ref sequence), Is.EqualTo(123456789));
            Assert.That(sequence.Length, Is.EqualTo(12));

            bytes    = new byte[] { 21, 205, 91, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            sequence = new ReadOnlySequence <byte>(bytes);
            Assert.That(BytesExtensions.ReadInt(ref sequence, Endianness.LittleEndian), Is.EqualTo(123456789));
            Assert.That(sequence.Length, Is.EqualTo(12));

            var bytes1       = new byte[] { 7, 91 };
            var bytes2       = new byte[] { 205, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            var firstSegment = new MemorySegment <byte>(bytes1);
            var lastSegment  = firstSegment.Append(bytes2);

            sequence = new ReadOnlySequence <byte>(firstSegment, 0, lastSegment, lastSegment.Memory.Length);
            Assert.That(BytesExtensions.ReadInt(ref sequence), Is.EqualTo(123456789));
            Assert.That(sequence.Length, Is.EqualTo(12));
        }
Beispiel #3
0
        public virtual void TestReadLongForPositionEndianness()
        {
            var readLong = _input.ReadLong(2, Endianness.LittleEndian);
            var longB    = BytesExtensions.ReadLongL(InitData, 2);

            Assert.AreEqual(longB, readLong);
        }
Beispiel #4
0
        public virtual void TestReadIntPosition()
        {
            var readInt = _input.ReadInt(2);
            var theInt  = BytesExtensions.ReadInt(InitData, 2, Endianness.BigEndian);

            Assert.AreEqual(theInt, readInt);
        }
Beispiel #5
0
        public virtual void TestReadLongPosition()
        {
            var readLong = _input.ReadLong(2);
            var longB    = BytesExtensions.ReadLong(InitData, 2, Endianness.BigEndian);

            Assert.AreEqual(longB, readLong);
        }
        public void ReadUShortSequence()
        {
            var bytes    = new byte[] { 0 };
            var sequence = new ReadOnlySequence <byte>(bytes);

            Assert.Throws <ArgumentException>(() => _ = BytesExtensions.ReadUShort(ref sequence, Endianness.BigEndian));

            bytes    = new byte[] { 48, 57, 0, 0, 0 };
            sequence = new ReadOnlySequence <byte>(bytes);
            Assert.That(BytesExtensions.ReadUShort(ref sequence, Endianness.BigEndian), Is.EqualTo(12345));
            Assert.That(sequence.Length, Is.EqualTo(3));

            bytes    = new byte[] { 57, 48, 0, 0, 0 };
            sequence = new ReadOnlySequence <byte>(bytes);
            Assert.That(BytesExtensions.ReadUShort(ref sequence, Endianness.LittleEndian), Is.EqualTo(12345));
            Assert.That(sequence.Length, Is.EqualTo(3));

            var bytes1       = new byte[] { 48 };
            var bytes2       = new byte[] { 57, 0, 0, 0 };
            var firstSegment = new MemorySegment <byte>(bytes1);
            var lastSegment  = firstSegment.Append(bytes2);

            sequence = new ReadOnlySequence <byte>(firstSegment, 0, lastSegment, lastSegment.Memory.Length);
            Assert.That(BytesExtensions.ReadUShort(ref sequence, Endianness.BigEndian), Is.EqualTo(12345));
            Assert.That(sequence.Length, Is.EqualTo(3));
        }
Beispiel #7
0
        public virtual void TestReadIntForPositionEndianness()
        {
            var readInt = _input.ReadInt(3, Endianness.LittleEndian);
            var theInt  = BytesExtensions.ReadIntL(InitData, 3);

            Assert.AreEqual(theInt, readInt);
        }
Beispiel #8
0
        public virtual void TestReadShortPosition()
        {
            var read = _input.ReadShort(1);
            var val  = BytesExtensions.ReadShort(InitData, 1, Endianness.BigEndian);

            Assert.AreEqual(val, read);
        }
Beispiel #9
0
        public virtual void TestReadFloatPosition()
        {
            double readFloat = _input.ReadFloat(2);
            var    intB      = BytesExtensions.ReadInt(InitData, 2, Endianness.BigEndian);
            double aFloat    = BitConverter.ToSingle(BitConverter.GetBytes(intB), 0);

            Assert.AreEqual(aFloat, readFloat, 0);
        }
Beispiel #10
0
        public virtual void TestReadFloatEndianness()
        {
            double readFloat = _input.ReadFloat(Endianness.LittleEndian);
            var    intB      = BytesExtensions.ReadIntL(InitData, 0);
            double aFloat    = BitConverter.ToSingle(BitConverter.GetBytes(intB), 0);

            Assert.AreEqual(aFloat, readFloat, 0);
        }
Beispiel #11
0
        public virtual void TestReadDoublePosition()
        {
            var readDouble = _input.ReadDouble(2);
            var longB      = BytesExtensions.ReadLong(InitData, 2, Endianness.BigEndian);
            var aDouble    = BitConverter.Int64BitsToDouble(longB);

            Assert.AreEqual(aDouble, readDouble, 0);
        }
        public virtual void TestWriteIntBigEndian()
        {
            var expected = 100;

            _output.WriteIntBigEndian(expected);
            var actual = BytesExtensions.ReadInt(_output.Buffer, 0);

            Assert.AreEqual(actual, expected);
        }
        public virtual void TestWriteIntForVEndianness()
        {
            var expected = 100;

            _output.Write(expected, Endianness.LittleEndian);
            var actual = BytesExtensions.ReadIntL(_output.Buffer, 0);

            Assert.AreEqual(actual, expected);
        }
        public virtual void TestWriteLongForVEndianness()
        {
            long expected = 100;

            _output.Write(2, expected, Endianness.LittleEndian);
            var actual = BytesExtensions.ReadLongL(_output.Buffer, 2);

            Assert.AreEqual(actual, expected);
        }
        public virtual void TestWriteIntForPositionV()
        {
            var expected = 100;

            _output.Write(1, expected);
            var actual = BytesExtensions.ReadInt(_output.Buffer, 1, Endianness.BigEndian);

            Assert.AreEqual(actual, expected);
        }
        public virtual void TestWriteShortForVEndianness()
        {
            short expected = 100;

            _output.Write(2, expected, Endianness.LittleEndian);
            var actual = BytesExtensions.ReadShort(_output.Buffer, 2, Endianness.LittleEndian);

            Assert.AreEqual(actual, expected);
        }
        public virtual void TestWriteShortV()
        {
            short expected = 100;

            _output.Write(expected);
            var actual = BytesExtensions.ReadShort(_output.Buffer, 0, Endianness.BigEndian);

            Assert.AreEqual(actual, expected);
        }
        public virtual void TestWriteLongForPositionV()
        {
            long expected = 100;

            _output.Write(2, expected);
            var actual = BytesExtensions.ReadLong(_output.Buffer, 2, Endianness.BigEndian);

            Assert.AreEqual(actual, expected);
        }
        public virtual void TestWriteFloatForPositionVEndianness()
        {
            var v = 1.1f;

            _output.Write(1, v, Endianness.LittleEndian);
            var expected = BitConverter.ToInt32(BitConverter.GetBytes(v), 0);
            var actual   = BytesExtensions.ReadIntL(_output.Buffer, 1);

            Assert.AreEqual(actual, expected);
        }
        public virtual void TestWriteDoubleForVEndianness()
        {
            var v = 1.1d;

            _output.Write(v, Endianness.LittleEndian);
            var theLong   = BitConverter.DoubleToInt64Bits(v);
            var readLongB = BytesExtensions.ReadLongL(_output.Buffer, 0);

            Assert.AreEqual(theLong, readLongB);
        }
        public virtual void TestWriteDoubleForPositionV()
        {
            var v = 1.1d;

            _output.Write(1, v);
            var theLong   = BitConverter.DoubleToInt64Bits(v);
            var readLongB = BytesExtensions.ReadLong(_output.Buffer, 1, Endianness.BigEndian);

            Assert.AreEqual(theLong, readLongB);
        }
        public virtual void TestWriteFloatV()
        {
            var v = 1.1f;

            _output.Write(v);
            var expected = BitConverter.ToInt32(BitConverter.GetBytes(v), 0);
            var actual   = BytesExtensions.ReadInt(_output.Buffer, 0, Endianness.BigEndian);

            Assert.AreEqual(actual, expected);
        }
Beispiel #23
0
        public void Sequences1()
        {
            const int origin = 1234;
            var       bytes  = new byte[4];

            bytes.WriteInt(0, origin, Endianness.BigEndian);
            var buffer = new ReadOnlySequence <byte>(bytes);
            var value  = BytesExtensions.ReadInt(ref buffer, Endianness.BigEndian);

            NUnit.Framework.Assert.AreEqual(origin, value);
        }
Beispiel #24
0
        private void ParseBody(byte[] data)
        {
            this.UnprocessedData = new byte[] { };
            if (StartBoundary == "--") //isEmpty = > singlepart
            {
                this._bodyBytes = data;
            }
            else //multipart
            {
                var    crlf  = DefaultEncoding.GetBytes(CRLF);
                int    index = 0;
                byte[] stringBytes;
                //чтение собственного текста
                {
                    int nextIndex = index;
                    while (readLine(data, ref nextIndex, out stringBytes))
                    {
                        if (stringBytes.ValueEquals(this.StartBoundaryBytes1) || stringBytes.ValueEquals(this.EndBoundaryBytes))
                        {
                            this._bodyBytes = data.GetSubArray(0, index);
                            break;
                        }

                        index = nextIndex;
                    }
                    index = nextIndex;
                }

                //достигнут конец данных
                if (stringBytes == null || stringBytes.ValueEquals(this.EndBoundaryBytes))
                {
                    return;
                }

                int endIndex = BytesExtensions.IndexOf(data, this.EndBoundaryBytes, index);
                if (endIndex == -1)
                {
                    endIndex = data.Length;
                }

                while (index < endIndex)
                {
                    int nextIndex = BytesExtensions.IndexOf(data, this.StartBoundaryBytes1, index);
                    if (nextIndex == -1)
                    {
                        nextIndex = data.Length + crlf.Length;
                    }
                    var childData = data.GetSubArray(index, nextIndex - index - crlf.Length);
                    var child     = createMimeReader(this, childData);
                    this.Children.Add(child);
                    index = nextIndex + this.StartBoundaryBytes1.Length + crlf.Length;
                }
            }
        }
Beispiel #25
0
        /// <summary>
        /// Get a linenumber
        /// </summary>
        /// <param name="PCode">Source bytes</param>
        /// <param name="offset">position</param>
        /// <returns></returns>
        private static string GetLineNumber(byte[] PCode, ref int offset)
        {
            string result = "";

            offset++; //1 byte = TOKEN {1}
            short LineNumber = BytesExtensions.ToInt16(PCode, ref offset);

            result += LineNumber.ToString(); //LINE NUMBER, 2 Bytes
            //Populate a list of offsets of every linenumbers
            JumpLines.Add(new EditorJumpInfo(JumpType.GOTO, (int)LineNumber, offset - 3));
            return(result);
        }
        /// <summary>
        /// Decode a ProgramCode Into Plain Text
        /// </summary>
        /// <param name="PCode">Byte array (encoded program)</param>
        static public string DecodeBytes(byte[] PCode)
        {
            byte[] prgsize = new byte[2];
            string result  = "";

            Array.Copy(PCode, 0, prgsize, 0, 2);
            int ProgLenght = BytesExtensions.ToInt16(prgsize);

            int  offset; //offset after count of total encoded bytes
            bool isFirstToken = true;

            offset = 2;

            while (offset <= ProgLenght)
            {
                var tokenvalue = (byte)PCode[offset];
                switch (tokenvalue)
                {
                //linenumbers
                case (byte)TYPE_TOKEN.NUMBER:
                    if (isFirstToken)
                    {
                        offset++;
                        short LineNumber = BytesExtensions.ToInt16(PCode, ref offset);
                        result += LineNumber.ToString();     //LINE NUMBER, 2 Bytes
                    }

                    isFirstToken = false;
                    break;

                //comments
                case (byte)LINE_TOKEN.REM:

                    result      += " " + GetComment(PCode, ref offset) + System.Environment.NewLine;
                    isFirstToken = true;
                    break;

                //assigments
                case (byte)LINE_TOKEN.ASSIGN:
                    result      += " " + GetAssigment(PCode, ref offset) + System.Environment.NewLine;
                    isFirstToken = true;
                    break;

                default:
                    offset++;     //TODO: This line only for debugging purposes
                    break;
                }
            }

            return(result);
        }
Beispiel #27
0
        public virtual void TestReadFloat()
        {
            // expected 9.2557164867118492E-41 which is what the original code reads
            // now we read 66051.0d? -- but ReadInt *does* return 66051 on original code, how can that become? meh?

            // so our reading of the int is ok, our conversion to aFloat is ok
            // but our reading (and writing?) of a float is not?

            double readFloat = _input.ReadFloat();
            var    intB      = BytesExtensions.ReadInt(InitData, 0, Endianness.BigEndian);
            double aFloat    = BitConverter.ToSingle(BitConverter.GetBytes(intB), 0);

            Assert.AreEqual(aFloat, readFloat, 0);
        }
Beispiel #28
0
        public void CouldSerializeManySortedMaps()
        {
            var sw = new Stopwatch();

            sw.Start();

            SortedMap <DateTime, double> sortedMap = new SortedMap <DateTime, double>();

            byte[] bytes = null;
            var    rng   = new System.Random();

            for (int i = 0; i < 1000; i++)
            {
                sortedMap.Add(DateTime.UtcNow.Date.AddSeconds(i), Math.Round(i + rng.NextDouble(), 2));
                bytes = Serializer.Serialize(sortedMap);
                var sortedMap2 = Serializer.Deserialize <SortedMap <DateTime, double> >(bytes);
                Assert.AreEqual(sortedMap.Count, sortedMap2.Count);
                unsafe
                {
                    fixed(DateTime *ptr1 = &sortedMap.keys[0])
                    fixed(DateTime * ptr2 = &sortedMap2.keys[0])
                    {
                        Assert.IsTrue(BytesExtensions.UnsafeCompare((IntPtr)ptr1, (IntPtr)ptr2, sortedMap.keys.Length * 8));
                    }

                    fixed(double *ptr1 = &sortedMap.values[0])
                    fixed(double *ptr2 = &sortedMap2.values[0])
                    {
                        Assert.IsTrue(BytesExtensions.UnsafeCompare((IntPtr)ptr1, (IntPtr)ptr2, sortedMap.size * 8));
                    }
                }

                //Assert.IsTrue(sortedMap.Keys.SequenceEqual(sortedMap2.Keys));
                //Assert.IsTrue(sortedMap.Values.SequenceEqual(sortedMap2.Values));
            }
            sw.Stop();
            Console.WriteLine("Elapsed msecs: " + sw.ElapsedMilliseconds);
            Console.WriteLine("Uncompressed size: 16000");
            Console.WriteLine("Compressed size: " + bytes.Length);
        }
Beispiel #29
0
        private byte[] ParseHeaders(byte[] data)
        {
            var crlf = DefaultEncoding.GetBytes(CRLF);

            var endOfHeader   = false;
            var currentHeader = "";
            int index         = 0;

            while (index < data.Length && !endOfHeader)
            {
                int lineBreakIndex = BytesExtensions.IndexOf(data, crlf, index);
                var breakFound     = (lineBreakIndex != -1);
                if (!breakFound)
                {
                    lineBreakIndex = data.Length;
                }

                int subStringLength = lineBreakIndex - index;

                var headerLineBytes = new byte[subStringLength];
                Array.Copy(data, index, headerLineBytes, 0, subStringLength);

                var line = DefaultEncoding.GetString(headerLineBytes);
                processHeaderLine(ref currentHeader, line, ref endOfHeader);
                index = lineBreakIndex;
                if (breakFound)
                {
                    index += crlf.Length;
                }
            }

            this._headerBytes = new byte[index];
            Array.Copy(data, this._headerBytes, index);
            processCommonHeaderAttribs();
            return(getUnprocessedData(data, index));
        }
Beispiel #30
0
 private bool readLine(byte[] data, ref int index, out byte[] stringBytes)
 {
     if (index >= data.Length)
     {
         stringBytes = null;
         return(false);
     }
     else
     {
         var crlf      = DefaultEncoding.GetBytes(CRLF);
         var crlfIndex = BytesExtensions.IndexOf(data, crlf, index);
         if (crlfIndex == -1)
         {
             stringBytes = data.GetSubArrayStartingAt(index);
             index       = data.Length;
         }
         else
         {
             stringBytes = data.GetSubArray(index, crlfIndex - index);
             index       = crlfIndex + crlf.Length;
         }
         return(true);
     }
 }