Example #1
0
        public virtual void TestZeroCopy()
        {
            byte[]        bytes   = GetBytesForString("brock");
            BytesWritable zeroBuf = new BytesWritable(bytes, bytes.Length);
            // new
            BytesWritable copyBuf = new BytesWritable(bytes);

            // old
            // using zero copy constructor shouldn't result in a copy
            Assert.True(bytes == zeroBuf.Bytes, "copy took place, backing array != array passed to constructor");
            Assert.True(zeroBuf.Length == bytes.Length, "length of BW should backing byte array");
            Assert.Equal(zeroBuf, copyBuf, "objects with same backing array should be equal");
            Assert.Equal(zeroBuf.ToString(), copyBuf.ToString(),
                         "string repr of objects with same backing array should be equal");
            Assert.True(zeroBuf.CompareTo(copyBuf) == 0, "compare order objects with same backing array should be equal");
            Assert.True(zeroBuf.GetHashCode() == copyBuf.GetHashCode(),
                        "hash of objects with same backing array should be equal");

            // ensure expanding buffer is handled correctly
            // for buffers created with zero copy api
            byte[] buffer = new byte[bytes.Length * 5];
            zeroBuf.Set(buffer, 0, buffer.Length);
            // expand internal buffer
            zeroBuf.Set(bytes, 0, bytes.Length);
            // set back to normal contents
            Assert.Equal(zeroBuf, copyBuf, "buffer created with (array, len) has bad contents");
            Assert.True(zeroBuf.Length == copyBuf.Length, "buffer created with (array, len) has bad length");
        }