Add IsEmpty and Length properties to System.BinaryData (#86721)
authorCédric Luthi <cedric.luthi@gmail.com>
Thu, 25 May 2023 09:40:39 +0000 (11:40 +0200)
committerGitHub <noreply@github.com>
Thu, 25 May 2023 09:40:39 +0000 (11:40 +0200)
Fixes #77970

src/libraries/System.Memory.Data/ref/System.Memory.Data.cs
src/libraries/System.Memory.Data/src/System/BinaryData.cs
src/libraries/System.Memory.Data/tests/BinaryDataTests.cs

index bbf5b00..5dfa892 100644 (file)
@@ -30,6 +30,8 @@ namespace System
         public static System.BinaryData FromString(string data) { throw null; }
         [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
         public override int GetHashCode() { throw null; }
+        public bool IsEmpty { get { throw null; } }
+        public int Length { get { throw null; } }
         public static implicit operator System.ReadOnlyMemory<byte> (System.BinaryData? data) { throw null; }
         public static implicit operator System.ReadOnlySpan<byte> (System.BinaryData? data) { throw null; }
         public byte[] ToArray() { throw null; }
index 82615b3..0832a5b 100644 (file)
@@ -33,6 +33,18 @@ namespace System
         public static BinaryData Empty { get; } = new BinaryData(ReadOnlyMemory<byte>.Empty);
 
         /// <summary>
+        /// Gets the number of bytes of this data.
+        /// </summary>
+        /// <returns>The number of bytes of this data.</returns>
+        public int Length => _bytes.Length;
+
+        /// <summary>
+        /// Gets a value that indicates whether this data is empty.
+        /// </summary>
+        /// <returns><see langword="true" /> if the data is empty (that is, its <see cref="Length" /> is 0); otherwise, <see langword="false" />.</returns>
+        public bool IsEmpty => _bytes.IsEmpty;
+
+        /// <summary>
         /// Creates a <see cref="BinaryData"/> instance by wrapping the
         /// provided byte array.
         /// </summary>
index aa2d18c..d3f1fe7 100644 (file)
@@ -1,4 +1,4 @@
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System;
@@ -614,6 +614,93 @@ namespace System.Tests
             Assert.Equal(string.Empty, BinaryData.Empty.ToString());
         }
 
+        [Theory]
+        [InlineData(0)]
+        [InlineData(4)]
+        [InlineData(7)]
+        public void LengthReturnsNumberOfBytesForBinaryDataFromReadOnlyMemory(int count)
+        {
+            var data = BinaryData.FromBytes(new ReadOnlyMemory<byte>(new byte[count]));
+            Assert.Equal(count, data.Length);
+        }
+
+        [Theory]
+        [InlineData(0)]
+        [InlineData(4)]
+        [InlineData(7)]
+        public void LengthReturnsNumberOfBytesForBinaryDataFromArray(int count)
+        {
+            var data = BinaryData.FromBytes(new byte[count]);
+            Assert.Equal(count, data.Length);
+        }
+
+        [Theory]
+        [InlineData(0)]
+        [InlineData(4)]
+        [InlineData(7)]
+        public void LengthReturnsNumberOfBytesForBinaryDataFromString(int count)
+        {
+            var data = BinaryData.FromString(new string('*', count));
+            Assert.Equal(count, data.Length);
+        }
+
+        [Fact]
+        public void BinaryDataEmptyIsEmpty()
+        {
+            Assert.True(BinaryData.Empty.IsEmpty);
+        }
+
+        [Fact]
+        public void BinaryDataFromEmptyReadOnlyMemoryIsEmpty()
+        {
+            var data = BinaryData.FromBytes(ReadOnlyMemory<byte>.Empty);
+            Assert.True(data.IsEmpty);
+        }
+
+        [Fact]
+        public void BinaryDataFromEmptyArrayIsEmpty()
+        {
+            var data = BinaryData.FromBytes(Array.Empty<byte>());
+            Assert.True(data.IsEmpty);
+        }
+
+        [Fact]
+        public void BinaryDataFromEmptyStringIsEmpty()
+        {
+            var data = BinaryData.FromString("");
+            Assert.True(data.IsEmpty);
+        }
+
+        [Theory]
+        [InlineData(1)]
+        [InlineData(4)]
+        [InlineData(7)]
+        public void NonEmptyBinaryDataFromReadOnlyMemoryIsNotEmpty(int count)
+        {
+            var data = BinaryData.FromBytes(new ReadOnlyMemory<byte>(new byte[count]));
+            Assert.False(data.IsEmpty);
+        }
+
+        [Theory]
+        [InlineData(1)]
+        [InlineData(4)]
+        [InlineData(7)]
+        public void NonEmptyBinaryDataFromArrayIsNotEmpty(int count)
+        {
+            var data = BinaryData.FromBytes(new byte[count]);
+            Assert.False(data.IsEmpty);
+        }
+
+        [Theory]
+        [InlineData(1)]
+        [InlineData(4)]
+        [InlineData(7)]
+        public void NonEmptyBinaryDataFromStringIsNotEmpty(int count)
+        {
+            var data = BinaryData.FromString(new string('*', count));
+            Assert.False(data.IsEmpty);
+        }
+
         [Fact]
         public void IsBinaryDataMemberPropertySerialized()
         {