Пример #1
0
        public unsafe void CtorSpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks(byte[] arrayManaged)
        {
            fixed(byte *pinnedArray = arrayManaged)
            {
                byte *array = pinnedArray;

                // new byte[0] is internally null
                if (array == null && arrayManaged.Length == 0)
                {
                    array = (byte *)123;
                }
                ByteSpan span = new ByteSpan(array, arrayManaged.Length);

                Assert.Equal(arrayManaged.Length, span.Length);

                Assert.NotSame(arrayManaged, span.CreateArray());
                Assert.True(span.ReferenceEquals(span));
                Assert.True(span.Equals(span));
                Assert.True(span.Equals((object)span));
                Assert.Equal(span.GetHashCode(), span.GetHashCode());
                Assert.False(span.Equals(arrayManaged));

                ByteSpan.Enumerator it = span.GetEnumerator();
                for (int i = 0; i < span.Length; i++)
                {
                    Assert.True(it.MoveNext());
                    Assert.Equal(array[i], it.Current);
                    Assert.Equal(array[i], span.Slice(i)[0]);

                    array[i] = unchecked ((byte)(array[i] + 1));
                    Assert.Equal(array[i], it.Current);
                    Assert.Equal(array[i], span.Slice(i)[0]);

                    var slice = span.Slice(i);
                    slice[0] = (unchecked ((byte)(array[i] + 1)));
                    Assert.Equal(array[i], it.Current);
                    Assert.Equal(array[i], span.Slice(i)[0]);
                }
                Assert.False(it.MoveNext());

                it.Reset();
                for (int i = 0; i < span.Length; i++)
                {
                    Assert.True(it.MoveNext());
                    Assert.Equal(array[i], it.Current);
                }
                Assert.False(it.MoveNext());

                {
                    ByteSpan sameSpan = new ByteSpan(array, arrayManaged.Length);
                    Assert.Equal(span.GetHashCode(), sameSpan.GetHashCode());
                    Assert.True(span.ReferenceEquals(sameSpan));
                    Assert.True(span.Equals(sameSpan));
                    Assert.True(span.Equals((object)sameSpan));
                }

                {
                    ByteSpan structCopy = span;
                    Assert.Equal(span.GetHashCode(), structCopy.GetHashCode());
                    Assert.True(span.ReferenceEquals(structCopy));
                    Assert.True(span.Equals(structCopy));
                    Assert.True(span.Equals((object)structCopy));
                }

                {
                    byte[] differentArrayManaged = new byte[arrayManaged.Length * 2];
                    for (int i = 0; i < arrayManaged.Length; i++)
                    {
                        differentArrayManaged[i] = unchecked ((byte)(array[i] + 1));
                        differentArrayManaged[arrayManaged.Length + i] = array[i];
                    }

                    fixed(byte *differentArray = differentArrayManaged)
                    {
                        {
                            ByteSpan equivalentSpan = new ByteSpan(differentArray + arrayManaged.Length, arrayManaged.Length);
                            Assert.Equal(span.GetHashCode(), equivalentSpan.GetHashCode());
                            Assert.False(span.ReferenceEquals(equivalentSpan));
                            Assert.True(span.Equals(equivalentSpan));
                            Assert.True(span.Equals((object)equivalentSpan));

                            if (equivalentSpan.Length > 0)
                            {
                                ByteSpan similarSpan = equivalentSpan.Slice(0, equivalentSpan.Length - 1);
                                Assert.False(span.ReferenceEquals(similarSpan));
                                Assert.False(span.Equals(similarSpan));
                                Assert.False(span.Equals((object)similarSpan));
                            }
                        }

                        {
                            ByteSpan differentSpan = new ByteSpan(differentArray, arrayManaged.Length);
                            Assert.False(span.ReferenceEquals(differentSpan));
                            // This can be simplified although it is harder to understand after simplification
                            if (arrayManaged.Length == 0)
                            {
                                Assert.True(span.Equals(differentSpan));
                                Assert.True(span.Equals((object)differentSpan));
                            }
                            else
                            {
                                Assert.False(span.Equals(differentSpan));
                                Assert.False(span.Equals((object)differentSpan));
                            }
                        }
                    }
                }
            }
        }