Example #1
0
    public static void TestEquals(ArraySegment <int> segment1, object obj, bool expected)
    {
        if (obj is ArraySegment <int> )
        {
            ArraySegment <int> segment2 = (ArraySegment <int>)obj;
            Assert.Equal(expected, segment1.Equals(segment2));
            Assert.Equal(expected, segment1 == segment2);
            Assert.Equal(!expected, segment1 != segment2);

            Assert.Equal(expected, segment1.GetHashCode().Equals(segment2.GetHashCode()));
        }
        Assert.Equal(expected, segment1.Equals(obj));
    }
Example #2
0
    public static void TestBasics()
    {
        int[] ia             = { 7, 8, 9, 10, 11 };
        ArraySegment <int> a = new ArraySegment <int>(ia, 2, 3);
        bool b;
        int  c;

        int[] ia2 = a.Array;
        Assert.True(Object.ReferenceEquals(ia, ia2));

        c = a.Offset;
        Assert.Equal(c, 2);

        c = a.Count;
        Assert.Equal(c, 3);

        b = a.Equals(a);
        Assert.True(b);

        ArraySegment <int> a2 = new ArraySegment <int>(ia, 2, 3);

        b = a.Equals(a2);
        Assert.True(b);

        int[] ia3             = (int[])(ia.Clone());
        ArraySegment <int> a3 = new ArraySegment <int>(ia3, 2, 3);

        b = a.Equals(a3);
        Assert.False(b);

        Object o;

        o = null;
        b = a.Equals(null);
        Assert.False(b);

        o = a2;
        b = a.Equals(o);
        Assert.True(b);

        int h1 = a.GetHashCode();
        int h2 = a.GetHashCode();
        int h3 = a2.GetHashCode();

        Assert.Equal(h1, h2);
        Assert.Equal(h1, h3);
    }
Example #3
0
        public void TestArraySegmentEqual()
        {
            string[] myArr_1 = { "The", "good" };
            string[] myArr_2 = { "The", "good" };

            ArraySegment <string> myArrSeg_1 = new ArraySegment <string> (myArr_1);
            ArraySegment <string> myArrSeg_2 = new ArraySegment <string> (myArr_2);

            // Should return true.
            Assert.AreEqual(myArrSeg_1.Equals(myArrSeg_1), true);

            // Should return false. Allthough the strings are the same.
            Assert.AreEqual(myArrSeg_1.Equals(myArrSeg_2), false);
            Assert.AreEqual(myArrSeg_1 == myArrSeg_2, false);

            // Should return true.
            Assert.AreEqual(myArrSeg_1 != myArrSeg_2, true);
        }
Example #4
0
    public static void TestBasics()
    {
        int[] ia = { 7, 8, 9, 10, 11 };
        ArraySegment<int> a = new ArraySegment<int>(ia, 2, 3);
        bool b;
        int c;

        int[] ia2 = a.Array;
        Assert.True(Object.ReferenceEquals(ia, ia2));

        c = a.Offset;
        Assert.Equal(c, 2);

        c = a.Count;
        Assert.Equal(c, 3);

        b = a.Equals(a);
        Assert.True(b);

        ArraySegment<int> a2 = new ArraySegment<int>(ia, 2, 3);
        b = a.Equals(a2);
        Assert.True(b);

        int[] ia3 = (int[])(ia.Clone());
        ArraySegment<int> a3 = new ArraySegment<int>(ia3, 2, 3);
        b = a.Equals(a3);
        Assert.False(b);

        object o;
        o = null;
        b = a.Equals(null);
        Assert.False(b);

        o = a2;
        b = a.Equals(o);
        Assert.True(b);

        int h1 = a.GetHashCode();
        int h2 = a.GetHashCode();
        int h3 = a2.GetHashCode();
        Assert.Equal(h1, h2);
        Assert.Equal(h1, h3);
    }
Example #5
0
        public override Int64 Encrypt(ArraySegment <byte> src, ArraySegment <byte> dst, ref string out_header)
        {
            if (dst.Count != src.Count)
            {
                return(-1);
            }

            if (!src.Equals(dst))
            {
                dst = new ArraySegment <byte>(src.Array, 0, src.Count);
            }

            return(src.Count);
        }
Example #6
0
        public override Int64 Encrypt(ArraySegment <byte> src, ArraySegment <byte> dst, ref string out_header)
        {
            DebugUtils.Assert(state == State.kEstablished);

            if (dst.Count < src.Count)
            {
                return(-1);
            }

            if (!src.Equals(dst))
            {
                dst = new ArraySegment <byte>(src.Array, 0, src.Count);
            }

            return(src.Count);
        }
Example #7
0
        /// <summary>
        /// Copies an array segment of type T into this buffer, ending at the
        /// given offset into this buffer. The starting offset is calculated
        /// based on the count of the array segment and is the value returned.
        /// </summary>
        /// <typeparam name="T">The type of the input data (must be a struct)
        /// </typeparam>
        /// <param name="offset">The offset into this buffer where the copy
        /// will end</param>
        /// <param name="x">The array segment to copy data from</param>
        /// <returns>The 'start' location of this buffer now, after the copy
        /// completed</returns>
        public int Put <T>(int offset, ArraySegment <T> x)
            where T : struct
        {
            if (x.Equals(default(ArraySegment <T>)))
            {
                throw new ArgumentNullException("Cannot put a uninitialized array segment");
            }

            if (x.Count == 0)
            {
                throw new ArgumentException("Cannot put an empty array");
            }

            if (!IsSupportedType <T>())
            {
                throw new ArgumentException("Cannot put an array of type "
                                            + typeof(T) + " into this buffer");
            }

            if (BitConverter.IsLittleEndian)
            {
                int numBytes = ByteBuffer.ArraySize(x);
                offset -= numBytes;
                AssertOffsetAndLength(offset, numBytes);
                // if we are LE, just do a block copy
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
                MemoryMarshal.Cast <T, byte>(x).CopyTo(_buffer.Span.Slice(offset, numBytes));
#else
                var srcOffset = ByteBuffer.SizeOf <T>() * x.Offset;
                Buffer.BlockCopy(x.Array, srcOffset, _buffer.Buffer, offset, numBytes);
#endif
            }
            else
            {
                throw new NotImplementedException("Big Endian Support not implemented yet " +
                                                  "for putting typed arrays");
                // if we are BE, we have to swap each element by itself
                //for(int i = x.Length - 1; i >= 0; i--)
                //{
                //  todo: low priority, but need to genericize the Put<T>() functions
                //}
            }
            return(offset);
        }
Example #8
0
		public void TestArraySegmentEqual ()
		{
			string[] myArr_1 = { "The", "good" };
			string[] myArr_2 = { "The", "good" };

			ArraySegment<string> myArrSeg_1 = new ArraySegment<string> (myArr_1);
			ArraySegment<string> myArrSeg_2 = new ArraySegment<string> (myArr_2);

			// Should return true.
			Assert.AreEqual (myArrSeg_1.Equals (myArrSeg_1), true);

			// Should return false. Allthough the strings are the same.
			Assert.AreEqual (myArrSeg_1.Equals (myArrSeg_2), false);
			Assert.AreEqual (myArrSeg_1 == myArrSeg_2, false);

			// Should return true.
			Assert.AreEqual (myArrSeg_1 != myArrSeg_2, true);
		}
Example #9
0
    public static void TestEquals(ArraySegment<int> segment1, object obj, bool expected)
    {
        if (obj is ArraySegment<int>)
        {
            ArraySegment<int> segment2 = (ArraySegment<int>)obj;
            Assert.Equal(expected, segment1.Equals(segment2));
            Assert.Equal(expected, segment1 == segment2);
            Assert.Equal(!expected, segment1 != segment2);

            Assert.Equal(expected, segment1.GetHashCode().Equals(segment2.GetHashCode()));
        }
        Assert.Equal(expected, segment1.Equals(obj));
    }
        public override Int64 Encrypt(ArraySegment<byte> src, ArraySegment<byte> dst, ref string out_header)
        {
            DebugUtils.Assert(state == State.kEstablished);

            if (dst.Count < src.Count)
                return -1;

            if (!src.Equals(dst))
                dst = new ArraySegment<byte>(src.Array, 0, src.Count);

            return src.Count;
        }