Expand Bit array tests (dotnet/corefx#9461)
authorStephen A. Imhoff <clockwork-muse@outlook.com>
Fri, 17 Jun 2016 10:53:49 +0000 (19:53 +0900)
committerStephen Toub <stoub@microsoft.com>
Fri, 17 Jun 2016 10:53:49 +0000 (06:53 -0400)
* Make test classes/methods static.
* Add missing data cases, make some cases explicit.
* Expand most data cases, extract bit numbers.
* Split copy type/size tests, add int as type.
* Make sure Get/Set operate over internal boundaries, fix some constant references.
* Extract magic numbers, tweak some test cases.
* Reference to shared definition.
* Extract test data for Not
* Conform test name
* Split operator tests (methods to test aren't data cases).
* Add stress case.
* Two more data cases.
* Correct dataset name, add empty array cases.

Commit migrated from https://github.com/dotnet/corefx/commit/fe2f2b30b6e21cf076c3bc2678cb55c82ad1da8a

src/libraries/System.Collections/tests/BitArray/BitArray_CtorTests.cs
src/libraries/System.Collections/tests/BitArray/BitArray_GetSetTests.cs
src/libraries/System.Collections/tests/BitArray/BitArray_OperatorsTests.cs

index 733d11a..698698c 100644 (file)
@@ -8,11 +8,18 @@ using Xunit;
 
 namespace System.Collections.Tests
 {
-    public class BitArray_CtorTests
+    public static class BitArray_CtorTests
     {
+        private const int BitsPerByte = 8;
+        private const int BitsPerInt32 = 32;
+
         [Theory]
         [InlineData(0)]
-        [InlineData(40)]
+        [InlineData(1)]
+        [InlineData(BitsPerByte)]
+        [InlineData(BitsPerByte * 2)]
+        [InlineData(BitsPerInt32)]
+        [InlineData(BitsPerInt32 * 2)]
         [InlineData(200)]
         [InlineData(65551)]
         public static void Ctor_Int(int length)
@@ -27,12 +34,25 @@ namespace System.Collections.Tests
             ICollection collection = bitArray;
             Assert.Equal(length, collection.Count);
             Assert.False(collection.IsSynchronized);
-
-            Ctor_BitArray(bitArray);
-            Ctor_Int_Bool(length, false);
-            Ctor_Int_Bool(length, true);
         }
 
+        [Theory]
+        [InlineData(0, true)]
+        [InlineData(0, false)]
+        [InlineData(1, true)]
+        [InlineData(1, false)]
+        [InlineData(BitsPerByte, true)]
+        [InlineData(BitsPerByte, false)]
+        [InlineData(BitsPerByte * 2, true)]
+        [InlineData(BitsPerByte * 2, false)]
+        [InlineData(BitsPerInt32, true)]
+        [InlineData(BitsPerInt32, false)]
+        [InlineData(BitsPerInt32 * 2, true)]
+        [InlineData(BitsPerInt32 * 2, false)]
+        [InlineData(200, true)]
+        [InlineData(200, false)]
+        [InlineData(65551, true)]
+        [InlineData(65551, false)]
         public static void Ctor_Int_Bool(int length, bool defaultValue)
         {
             BitArray bitArray = new BitArray(length, defaultValue);
@@ -45,8 +65,6 @@ namespace System.Collections.Tests
             ICollection collection = bitArray;
             Assert.Equal(length, collection.Count);
             Assert.False(collection.IsSynchronized);
-
-            Ctor_BitArray(bitArray);
         }
 
         [Fact]
@@ -56,9 +74,19 @@ namespace System.Collections.Tests
             Assert.Throws<ArgumentOutOfRangeException>("length", () => new BitArray(-1, false));
         }
 
+        public static IEnumerable<object[]> Ctor_BoolArray_TestData()
+        {
+            yield return new object[] { new bool[0] };
+            foreach (int size in new[] { 1, BitsPerByte, BitsPerByte * 2, BitsPerInt32, BitsPerInt32 * 2 })
+            {
+                yield return new object[] { Enumerable.Repeat(true, size).ToArray() };
+                yield return new object[] { Enumerable.Repeat(false, size).ToArray() };
+                yield return new object[] { Enumerable.Range(0, size).Select(x => x % 2 == 0).ToArray() };
+            }
+        }
+
         [Theory]
-        [InlineData(new bool[0])]
-        [InlineData(new bool[] { false, false, true, false, false, false, true, false, false, true })]
+        [MemberData(nameof(Ctor_BoolArray_TestData))]
         public static void Ctor_BoolArray(bool[] values)
         {
             BitArray bitArray = new BitArray(values);
@@ -71,11 +99,40 @@ namespace System.Collections.Tests
             ICollection collection = bitArray;
             Assert.Equal(values.Length, collection.Count);
             Assert.False(collection.IsSynchronized);
+        }
 
-            Ctor_BitArray(bitArray);
+        public static IEnumerable<object[]> Ctor_BitArray_TestData()
+        {
+            yield return new object[] { "bool[](empty)", new BitArray(new bool[0]) };
+            yield return new object[] { "byte[](empty)", new BitArray(new byte[0]) };
+            yield return new object[] { "int[](empty)", new BitArray(new int[0]) };
+
+            foreach (int size in new[] { 1, BitsPerByte, BitsPerByte * 2, BitsPerInt32, BitsPerInt32 * 2 })
+            {
+                yield return new object[] { "length", new BitArray(size) };
+                yield return new object[] { "length|default(true)", new BitArray(size, true) };
+                yield return new object[] { "length|default(false)", new BitArray(size, false) };
+                yield return new object[] { "bool[](all)", new BitArray(Enumerable.Repeat(true, size).ToArray()) };
+                yield return new object[] { "bool[](none)", new BitArray(Enumerable.Repeat(false, size).ToArray()) };
+                yield return new object[] { "bool[](alternating)", new BitArray(Enumerable.Range(0, size).Select(x => x % 2 == 0).ToArray()) };
+                if (size >= BitsPerByte)
+                {
+                    yield return new object[] { "byte[](all)", new BitArray(Enumerable.Repeat((byte)0xff, size / BitsPerByte).ToArray()) };
+                    yield return new object[] { "byte[](none)", new BitArray(Enumerable.Repeat((byte)0x00, size / BitsPerByte).ToArray()) };
+                    yield return new object[] { "byte[](alternating)", new BitArray(Enumerable.Repeat((byte)0xaa, size / BitsPerByte).ToArray()) };
+                }
+                if (size >= BitsPerInt32)
+                {
+                    yield return new object[] { "int[](all)", new BitArray(Enumerable.Repeat(unchecked((int)0xffffffff), size / BitsPerInt32).ToArray()) };
+                    yield return new object[] { "int[](none)", new BitArray(Enumerable.Repeat(0x00000000, size / BitsPerInt32).ToArray()) };
+                    yield return new object[] { "int[](alternating)", new BitArray(Enumerable.Repeat(unchecked((int)0xaaaaaaaa), size / BitsPerInt32).ToArray()) };
+                }
+            }
         }
 
-        public static void Ctor_BitArray(BitArray bits)
+        [Theory]
+        [MemberData(nameof(Ctor_BitArray_TestData))]
+        public static void Ctor_BitArray(string label, BitArray bits)
         {
             BitArray bitArray = new BitArray(bits);
             Assert.Equal(bits.Length, bitArray.Length);
@@ -91,14 +148,18 @@ namespace System.Collections.Tests
 
         public static IEnumerable<object[]> Ctor_IntArray_TestData()
         {
-            yield return new object[] { Enumerable.Repeat(unchecked((int)0xffffffff), 10).ToArray(), Enumerable.Repeat(true, 320).ToArray() };
-            yield return new object[] { Enumerable.Repeat(0, 10).ToArray(), Enumerable.Repeat(false, 320).ToArray() };
-            yield return new object[] { Enumerable.Repeat(unchecked((int)0xaaaaaaaa), 10).ToArray(), Enumerable.Range(0, 320).Select(i => i % 2 == 1).ToArray() };
+            yield return new object[] { new int[0], new bool[0] };
+            foreach (int size in new[] { 1, 10 })
+            {
+                yield return new object[] { Enumerable.Repeat(unchecked((int)0xffffffff), size).ToArray(), Enumerable.Repeat(true, size * BitsPerInt32).ToArray() };
+                yield return new object[] { Enumerable.Repeat(0x00000000, size).ToArray(), Enumerable.Repeat(false, size * BitsPerInt32).ToArray() };
+                yield return new object[] { Enumerable.Repeat(unchecked((int)0xaaaaaaaa), size).ToArray(), Enumerable.Range(0, size * BitsPerInt32).Select(i => i % 2 == 1).ToArray() };
+            }
         }
 
         [Theory]
         [MemberData(nameof(Ctor_IntArray_TestData))]
-        public void Ctor_IntArray(int[] array, bool[] expected)
+        public static void Ctor_IntArray(int[] array, bool[] expected)
         {
             BitArray bitArray = new BitArray(array);
             Assert.Equal(expected.Length, bitArray.Length);
@@ -117,7 +178,7 @@ namespace System.Collections.Tests
         {
             Assert.Throws<ArgumentNullException>("bits", () => new BitArray((BitArray)null));
         }
-        
+
         [Fact]
         public static void Ctor_BoolArray_Null_ThrowsArgumentNullException()
         {
@@ -132,8 +193,13 @@ namespace System.Collections.Tests
 
         public static IEnumerable<object[]> Ctor_ByteArray_TestData()
         {
-            yield return new object[] { new byte[] { 255, 255 }, Enumerable.Repeat(true, 16).ToArray() };
-            yield return new object[] { new byte[] { 0, 0 }, Enumerable.Repeat(false, 16).ToArray() };
+            yield return new object[] { new byte[0], new bool[0] };
+            foreach (int size in new[] { 1, 2, BitsPerInt32 / BitsPerByte, 2 * BitsPerInt32 / BitsPerByte })
+            {
+                yield return new object[] { Enumerable.Repeat((byte)0xff, size).ToArray(), Enumerable.Repeat(true, size * BitsPerByte).ToArray() };
+                yield return new object[] { Enumerable.Repeat((byte)0x00, size).ToArray(), Enumerable.Repeat(false, size * BitsPerByte).ToArray() };
+                yield return new object[] { Enumerable.Repeat((byte)0xaa, size).ToArray(), Enumerable.Range(0, size * BitsPerByte).Select(i => i % 2 == 1).ToArray() };
+            }
         }
 
         [Theory]
index 39576f6..9281aa3 100644 (file)
@@ -8,19 +8,29 @@ using Xunit;
 
 namespace System.Collections.Tests
 {
-    public class BitArray_GetSetTests
+    public static class BitArray_GetSetTests
     {
-        private static BitArray s_allTrue = new BitArray(320, true);
-        private static BitArray s_allFalse = new BitArray(320, false);
-        private static BitArray s_alternating = new BitArray(Enumerable.Repeat(unchecked((int)0xaaaaaaaa), 10).ToArray());
+        private const int BitsPerByte = 8;
+        private const int BitsPerInt32 = 32;
+
+        public static IEnumerable<object[]> Get_Set_Data()
+        {
+            foreach (int size in new[] { 0, 1, BitsPerByte, BitsPerByte * 2, BitsPerInt32, BitsPerInt32 * 2 })
+            {
+                foreach (bool def in new[] { true, false })
+                {
+                    yield return new object[] { def, Enumerable.Repeat(true, size).ToArray() };
+                    yield return new object[] { def, Enumerable.Repeat(false, size).ToArray() };
+                    yield return new object[] { def, Enumerable.Range(0, size).Select(i => i % 2 == 1).ToArray() };
+                }
+            }
+        }
 
         [Theory]
-        [InlineData(new bool[] { true })]
-        [InlineData(new bool[] { false })]
-        [InlineData(new bool[] { true, false, true, true, false, true })]
-        public static void Get_Set(bool[] newValues)
+        [MemberData(nameof(Get_Set_Data))]
+        public static void Get_Set(bool def, bool[] newValues)
         {
-            BitArray bitArray = new BitArray(newValues.Length, false);
+            BitArray bitArray = new BitArray(newValues.Length, def);
             for (int i = 0; i < newValues.Length; i++)
             {
                 bitArray.Set(i, newValues[i]);
@@ -52,12 +62,21 @@ namespace System.Collections.Tests
         }
 
         [Theory]
-        [InlineData(6, true)]
-        [InlineData(6, false)]
-        [InlineData(0x1000F, true)]
+        [InlineData(0, true)]
+        [InlineData(0, false)]
+        [InlineData(1, true)]
+        [InlineData(1, false)]
+        [InlineData(BitsPerByte, true)]
+        [InlineData(BitsPerByte, false)]
+        [InlineData(BitsPerByte + 1, true)]
+        [InlineData(BitsPerByte + 1, false)]
+        [InlineData(BitsPerInt32, true)]
+        [InlineData(BitsPerInt32, false)]
+        [InlineData(BitsPerInt32 + 1, true)]
+        [InlineData(BitsPerInt32 + 1, false)]
         public static void SetAll(int size, bool defaultValue)
         {
-            BitArray bitArray = new BitArray(6, defaultValue);
+            BitArray bitArray = new BitArray(size, defaultValue);
             bitArray.SetAll(!defaultValue);
             for (int i = 0; i < bitArray.Length; i++)
             {
@@ -72,10 +91,20 @@ namespace System.Collections.Tests
                 Assert.Equal(defaultValue, bitArray.Get(i));
             }
         }
-            
+
+        public static IEnumerable<object[]> GetEnumerator_Data()
+        {
+            foreach (int size in new[] { 0, 1, BitsPerByte, BitsPerByte + 1, BitsPerInt32, BitsPerInt32 + 1 })
+            {
+                foreach (bool lead in new[] { true, false })
+                {
+                    yield return new object[] { Enumerable.Range(0, size).Select(i => lead ^ (i % 2 == 0)).ToArray() };
+                }
+            }
+        }
+
         [Theory]
-        [InlineData(new bool[0])]
-        [InlineData(new bool[] { true, false, true, false, true, false, true, false, true, false })]
+        [MemberData(nameof(GetEnumerator_Data))]
         public static void GetEnumerator(bool[] values)
         {
             BitArray bitArray = new BitArray(values);
@@ -94,10 +123,16 @@ namespace System.Collections.Tests
             }
         }
 
-        [Fact]
-        public static void GetEnumerator_Invalid()
+        [Theory]
+        [InlineData(0)]
+        [InlineData(1)]
+        [InlineData(BitsPerByte)]
+        [InlineData(BitsPerByte + 1)]
+        [InlineData(BitsPerInt32)]
+        [InlineData(BitsPerInt32 + 1)]
+        public static void GetEnumerator_Invalid(int size)
         {
-            BitArray bitArray = new BitArray(10, true);
+            BitArray bitArray = new BitArray(size, true);
             IEnumerator enumerator = bitArray.GetEnumerator();
 
             // Has not started enumerating
@@ -112,18 +147,30 @@ namespace System.Collections.Tests
             Assert.Throws<InvalidOperationException>(() => enumerator.Current);
 
             // Has modified underlying collection
-            enumerator.MoveNext();
-            bitArray[0] = false;
-            Assert.True((bool)enumerator.Current);
-            Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
-            Assert.Throws<InvalidOperationException>(() => enumerator.Reset());
+            if (size > 0)
+            {
+                enumerator.MoveNext();
+                bitArray[0] = false;
+                Assert.True((bool)enumerator.Current);
+                Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
+                Assert.Throws<InvalidOperationException>(() => enumerator.Reset());
+            }
+        }
+
+        public static IEnumerable<object[]> Length_Set_Data()
+        {
+            int[] sizes = { 1, BitsPerByte, BitsPerByte + 1, BitsPerInt32, BitsPerInt32 + 1 };
+            foreach (int original in sizes.Concat(new[] { 16384 }))
+            {
+                foreach (int n in sizes)
+                {
+                    yield return new object[] { original, n };
+                }
+            }
         }
 
         [Theory]
-        [InlineData(16, 48)]
-        [InlineData(48, 24)]
-        [InlineData(16384, 256)]
-        [InlineData(48, 48)]
+        [MemberData(nameof(Length_Set_Data))]
         public static void Length_Set(int originalSize, int newSize)
         {
             BitArray bitArray = new BitArray(originalSize, true);
@@ -148,6 +195,7 @@ namespace System.Collections.Tests
 
             bitArray.Length = newSize;
             Assert.Equal(newSize, bitArray.Length);
+            Assert.False(bitArray.Get(0));
             Assert.False(bitArray.Get(newSize - 1));
         }
 
@@ -158,62 +206,106 @@ namespace System.Collections.Tests
             Assert.Throws<ArgumentOutOfRangeException>(() => bitArray.Length = -1);
         }
 
-        public static IEnumerable<object[]> CopyTo_IntArray_TestData()
+        public static IEnumerable<object[]> CopyTo_Array_TestData()
         {
-            yield return new object[] { s_allTrue, new int[10], 0, Enumerable.Repeat(unchecked((int)0xffffffff), 10).ToArray(), typeof(int) };
-            yield return new object[] { s_allFalse, new int[11], 1, Enumerable.Repeat(0, 10).ToArray(), typeof(int) };
-            yield return new object[] { s_alternating, new int[12], 1, Enumerable.Repeat(unchecked((int)0xaaaaaaaa), 10).ToArray(), typeof(int) };
+            yield return new object[] { new BitArray(0), 0, 0, new bool[0], default(bool) };
+            yield return new object[] { new BitArray(0), 0, 0, new byte[0], default(byte) };
+            yield return new object[] { new BitArray(0), 0, 0, new int[0], default(int) };
+
+            foreach (int bitArraySize in new[] { 0, 1, BitsPerByte, BitsPerByte * 2, BitsPerInt32, BitsPerInt32 * 2 })
+            {
+                BitArray allTrue = new BitArray(Enumerable.Repeat(true, bitArraySize).ToArray());
+                BitArray allFalse = new BitArray(Enumerable.Repeat(false, bitArraySize).ToArray());
+                BitArray alternating = new BitArray(Enumerable.Range(0, bitArraySize).Select(i => i % 2 == 1).ToArray());
+
+                foreach (var d in new[] { Tuple.Create(bitArraySize, 0),
+                    Tuple.Create(bitArraySize * 2 + 1, 0),
+                    Tuple.Create(bitArraySize * 2 + 1, bitArraySize + 1),
+                    Tuple.Create(bitArraySize * 2 + 1, bitArraySize / 2 + 1) })
+                {
+                    int arraySize = d.Item1;
+                    int index = d.Item2;
 
-            yield return new object[] { s_allTrue, new bool[320], 0, Enumerable.Repeat(true, 320).ToArray(), typeof(bool) };
-            yield return new object[] { s_allFalse, new bool[321], 1, Enumerable.Repeat(false, 320).ToArray(), typeof(bool) };
-            yield return new object[] { s_alternating, new bool[322], 1, Enumerable.Range(0, 320).Select(i => i % 2 == 1).ToArray(), typeof(bool) };
+                    yield return new object[] { allTrue, arraySize, index, Enumerable.Repeat(true, bitArraySize).ToArray(), default(bool) };
+                    yield return new object[] { allFalse, arraySize, index, Enumerable.Repeat(false, bitArraySize).ToArray(), default(bool) };
+                    yield return new object[] { alternating, arraySize, index, Enumerable.Range(0, bitArraySize).Select(i => i % 2 == 1).ToArray(), default(bool) };
 
-            yield return new object[] { s_allTrue, new byte[40], 0, Enumerable.Repeat((byte)255, 40).ToArray(), typeof(byte) };
-            yield return new object[] { s_allFalse, new byte[41], 1, Enumerable.Repeat((byte)0, 40).ToArray(), typeof(byte) };
-            yield return new object[] { s_alternating, new byte[42], 1, Enumerable.Repeat((byte)170, 40).ToArray(), typeof(byte) };
+                    if (bitArraySize >= BitsPerByte)
+                    {
+                        yield return new object[] { allTrue, arraySize / BitsPerByte, index / BitsPerByte, Enumerable.Repeat((byte)0xff, bitArraySize / BitsPerByte).ToArray(), default(byte) };
+                        yield return new object[] { allFalse, arraySize / BitsPerByte, index / BitsPerByte, Enumerable.Repeat((byte)0x00, bitArraySize / BitsPerByte).ToArray(), default(byte) };
+                        yield return new object[] { alternating, arraySize / BitsPerByte, index / BitsPerByte, Enumerable.Repeat((byte)0xaa, bitArraySize / BitsPerByte).ToArray(), default(byte) };
+                    }
+
+                    if (bitArraySize >= BitsPerInt32)
+                    {
+                        yield return new object[] { allTrue, arraySize / BitsPerInt32, index / BitsPerInt32, Enumerable.Repeat(unchecked((int)0xffffffff), bitArraySize / BitsPerInt32).ToArray(), default(int) };
+                        yield return new object[] { allFalse, arraySize / BitsPerInt32, index / BitsPerInt32, Enumerable.Repeat(0x00000000, bitArraySize / BitsPerInt32).ToArray(), default(int) };
+                        yield return new object[] { alternating, arraySize / BitsPerInt32, index / BitsPerInt32, Enumerable.Repeat(unchecked((int)0xaaaaaaaa), bitArraySize / BitsPerInt32).ToArray(), default(int) };
+                    }
+                }
+            }
         }
 
         [Theory]
-        [MemberData(nameof(CopyTo_IntArray_TestData))]
-        public void CopyTo(BitArray bitArray, Array array, int index, Array expected, Type arrayType)
+        [MemberData(nameof(CopyTo_Array_TestData))]
+        public static void CopyTo<T>(BitArray bitArray, int length, int index, T[] expected, T def)
         {
-            object defaultValue = Activator.CreateInstance(arrayType);
+            T[] array = (T[])Array.CreateInstance(typeof(T), length);
             ICollection collection = bitArray;
             collection.CopyTo(array, index);
             for (int i = 0; i < index; i++)
             {
-                Assert.Equal(defaultValue, array.GetValue(i));
+                Assert.Equal(def, array[i]);
             }
             for (int i = 0; i < expected.Length; i++)
             {
-                Assert.Equal(expected.GetValue(i), array.GetValue(i + index));
+                Assert.Equal(expected[i], array[i + index]);
             }
             for (int i = index + expected.Length; i < array.Length; i++)
             {
-                Assert.Equal(defaultValue, array.GetValue(i));
+                Assert.Equal(def, array[i]);
             }
         }
 
         [Fact]
-        public void CopyTo_Invalid()
+        public static void CopyTo_Type_Invalid()
         {
             ICollection bitArray = new BitArray(10);
-            // Invalid array
             Assert.Throws<ArgumentNullException>("array", () => bitArray.CopyTo(null, 0));
             Assert.Throws<ArgumentException>(null, () => bitArray.CopyTo(new long[10], 0));
             Assert.Throws<ArgumentException>(null, () => bitArray.CopyTo(new int[10, 10], 0));
+        }
 
-            // Invalid index
-            Assert.Throws<ArgumentOutOfRangeException>("index", () => bitArray.CopyTo(new byte[10], -1));
-            Assert.Throws<ArgumentException>(null, () => bitArray.CopyTo(new byte[1], 2));
-            Assert.Throws<ArgumentException>(null, () => bitArray.CopyTo(new bool[10], 2));
+        [Theory]
+        [InlineData(default(bool), 1, 0, 0)]
+        [InlineData(default(bool), 1, 1, 1)]
+        [InlineData(default(bool), BitsPerByte, BitsPerByte - 1, 0)]
+        [InlineData(default(bool), BitsPerByte, BitsPerByte, 1)]
+        [InlineData(default(bool), BitsPerInt32, BitsPerInt32 - 1, 0)]
+        [InlineData(default(bool), BitsPerInt32, BitsPerInt32, 1)]
+        [InlineData(default(byte), BitsPerByte, 0, 0)]
+        [InlineData(default(byte), BitsPerByte, 1, 1)]
+        [InlineData(default(byte), BitsPerByte * 4, 4 - 1, 0)]
+        [InlineData(default(byte), BitsPerByte * 4, 4, 1)]
+        [InlineData(default(int), BitsPerInt32, 0, 0)]
+        [InlineData(default(int), BitsPerInt32, 1, 1)]
+        [InlineData(default(int), BitsPerInt32 * 4, 4 - 1, 0)]
+        [InlineData(default(int), BitsPerInt32 * 4, 4, 1)]
+        public static void CopyTo_Size_Invalid<T>(T def, int bits, int arraySize, int index)
+        {
+            ICollection bitArray = new BitArray(bits);
+            T[] array = (T[])Array.CreateInstance(typeof(T), arraySize);
+            Assert.Throws<ArgumentOutOfRangeException>("index", () => bitArray.CopyTo(array, -1));
+            Assert.Throws<ArgumentException>(def is int ? string.Empty : null, () => bitArray.CopyTo(array, index));
         }
 
         [Fact]
-        public void SyncRoot()
+        public static void SyncRoot()
         {
             ICollection bitArray = new BitArray(10);
             Assert.Same(bitArray.SyncRoot, bitArray.SyncRoot);
+            Assert.NotSame(bitArray.SyncRoot, ((ICollection)new BitArray(10)).SyncRoot);
         }
     }
 }
index 520c5d7..d8e94de 100644 (file)
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+using System.Collections.Generic;
+using System.Linq;
 using Xunit;
 
 namespace System.Collections.Tests
 {
-    public enum Operator { Xor, Or, And };
-    
-    public class BitArray_OperatorsTests
+    public static class BitArray_OperatorsTests
     {
-        [Fact]
-        public static void And_EmptyArray()
+        private const int BitsPerByte = 8;
+        private const int BitsPerInt32 = 32;
+
+        public static IEnumerable<object[]> Not_Operator_Data()
         {
-            BitArray bitArray1 = new BitArray(0);
-            BitArray bitArray2 = new BitArray(0);
-            
-            Assert.Equal(0, bitArray1.And(bitArray2).Length);
+            foreach (int size in new[] { 0, 1, BitsPerByte, BitsPerByte * 2, BitsPerInt32, BitsPerInt32 * 2, short.MaxValue })
+            {
+                yield return new object[] { Enumerable.Repeat(true, size).ToArray() };
+                yield return new object[] { Enumerable.Repeat(false, size).ToArray() };
+                yield return new object[] { Enumerable.Range(0, size).Select(i => i % 2 == 1).ToArray() };
+            }
         }
-        
+
         [Theory]
-        [InlineData(0)]
-        [InlineData(6)]
-        [InlineData(0x1000F)]
-        public static void Not(int length)
+        [MemberData(nameof(Not_Operator_Data))]
+        public static void Not(bool[] data)
         {
-            BitArray bitArray = new BitArray(length, false);
-            if (length > 0)
-            {
-                bitArray[0] = true;
-                bitArray[1] = true;
-                bitArray[length - 2] = true;
-                bitArray[length - 1] = true;
-            }
+            BitArray bitArray = new BitArray(data);
 
             BitArray bitArrayNot = bitArray.Not();
             Assert.Equal(bitArray.Length, bitArrayNot.Length);
             Assert.Same(bitArray, bitArrayNot);
             for (int i = 0; i < bitArray.Length; i++)
             {
-                if (i <= 1 || i >= length - 2)
-                {
-                    Assert.False(bitArrayNot[i]);
-                }
-                else
-                {
-                    Assert.True(bitArrayNot[i]);
-                }
+                Assert.Equal(!data[i], bitArrayNot[i]);
+            }
+        }
+
+        public static IEnumerable<object[]> And_Operator_Data()
+        {
+            yield return new object[] { new bool[0], new bool[0], new bool[0] };
+            foreach (int size in new[] { 1, BitsPerByte, BitsPerByte * 2, BitsPerInt32, BitsPerInt32 * 2, short.MaxValue })
+            {
+                bool[] allTrue = Enumerable.Repeat(true, size).ToArray();
+                bool[] allFalse = Enumerable.Repeat(false, size).ToArray();
+                bool[] alternating = Enumerable.Range(0, size).Select(i => i % 2 == 1).ToArray();
+                yield return new object[] { allTrue, allTrue, allTrue };
+                yield return new object[] { allTrue, allFalse, allFalse };
+                yield return new object[] { allFalse, allTrue, allFalse };
+                yield return new object[] { allFalse, allFalse, allFalse };
+                yield return new object[] { allTrue, alternating, alternating };
+                yield return new object[] { alternating, allTrue, alternating };
+                yield return new object[] { allFalse, alternating, allFalse };
+                yield return new object[] { alternating, allFalse, allFalse };
             }
         }
-        
+
         [Theory]
-        [InlineData(Operator.And)]
-        [InlineData(Operator.Or)]
-        [InlineData(Operator.Xor)]
-        public static void OperatorTest(Operator op)
+        [MemberData(nameof(And_Operator_Data))]
+        public static void And_Operator(bool[] l, bool[] r, bool[] expected)
         {
-            BitArray bitArray1 = new BitArray(6, false);
-            BitArray bitArray2 = new BitArray(6, false);
-            BitArray result;
+            BitArray left = new BitArray(l);
+            BitArray right = new BitArray(r);
+
+            BitArray actual = left.And(right);
+            Assert.Same(left, actual);
+            Assert.Equal(actual.Length, expected.Length);
+
+            for (int i = 0; i < expected.Length; i++)
+            {
+                Assert.Equal(expected[i], actual[i]);
+            }
+        }
+
+        public static IEnumerable<object[]> Or_Operator_Data()
+        {
+            yield return new object[] { new bool[0], new bool[0], new bool[0] };
+            foreach (int size in new[] { 1, BitsPerByte, BitsPerByte * 2, BitsPerInt32, BitsPerInt32 * 2, short.MaxValue })
+            {
+                bool[] allTrue = Enumerable.Repeat(true, size).ToArray();
+                bool[] allFalse = Enumerable.Repeat(false, size).ToArray();
+                bool[] alternating = Enumerable.Range(0, size).Select(i => i % 2 == 1).ToArray();
+                yield return new object[] { allTrue, allTrue, allTrue };
+                yield return new object[] { allTrue, allFalse, allTrue };
+                yield return new object[] { allFalse, allTrue, allTrue };
+                yield return new object[] { allFalse, allFalse, allFalse };
+                yield return new object[] { allTrue, alternating, allTrue };
+                yield return new object[] { alternating, allTrue, allTrue };
+                yield return new object[] { allFalse, alternating, alternating };
+                yield return new object[] { alternating, allFalse, alternating };
+            }
+        }
+
+        [Theory]
+        [MemberData(nameof(Or_Operator_Data))]
+        public static void Or_Operator(bool[] l, bool[] r, bool[] expected)
+        {
+            BitArray left = new BitArray(l);
+            BitArray right = new BitArray(r);
 
-            bitArray1.Set(0, true);
-            bitArray1.Set(1, true);
+            BitArray actual = left.Or(right);
+            Assert.Same(left, actual);
+            Assert.Equal(actual.Length, expected.Length);
 
-            bitArray2.Set(1, true);
-            bitArray2.Set(2, true);
+            for (int i = 0; i < expected.Length; i++)
+            {
+                Assert.Equal(expected[i], actual[i]);
+            }
+        }
 
-            switch (op)
+        public static IEnumerable<object[]> Xor_Operator_Data()
+        {
+            yield return new object[] { new bool[0], new bool[0], new bool[0] };
+            foreach (int size in new[] { 1, BitsPerByte, BitsPerByte * 2, BitsPerInt32, BitsPerInt32 * 2, short.MaxValue })
             {
-                case Operator.Xor:
-                    result = bitArray1.Xor(bitArray2);
-                    Assert.Same(bitArray1, result);
-                    Assert.True(result.Get(0));
-                    Assert.False(result.Get(1));
-                    Assert.True(result.Get(2));
-                    Assert.False(result.Get(4));
-                    break;
-
-                case Operator.And:
-                    result = bitArray1.And(bitArray2);
-                    Assert.Same(bitArray1, result);
-                    Assert.False(result.Get(0));
-                    Assert.True(result.Get(1));
-                    Assert.False(result.Get(2));
-                    Assert.False(result.Get(4));
-                    break;
-
-                case Operator.Or:
-                    result = bitArray1.Or(bitArray2);
-                    Assert.Same(bitArray1, result);
-                    Assert.True(result.Get(0));
-                    Assert.True(result.Get(1));
-                    Assert.True(result.Get(2));
-                    Assert.False(result.Get(4));
-                    break;
+                bool[] allTrue = Enumerable.Repeat(true, size).ToArray();
+                bool[] allFalse = Enumerable.Repeat(false, size).ToArray();
+                bool[] alternating = Enumerable.Range(0, size).Select(i => i % 2 == 1).ToArray();
+                bool[] inverse = Enumerable.Range(0, size).Select(i => i % 2 == 0).ToArray();
+                yield return new object[] { allTrue, allTrue, allFalse };
+                yield return new object[] { allTrue, allFalse, allTrue };
+                yield return new object[] { allFalse, allTrue, allTrue };
+                yield return new object[] { allFalse, allFalse, allFalse };
+                yield return new object[] { allTrue, alternating, inverse };
+                yield return new object[] { alternating, allTrue, inverse };
+                yield return new object[] { allFalse, alternating, alternating };
+                yield return new object[] { alternating, allFalse, alternating };
+                yield return new object[] { alternating, inverse, allTrue };
+                yield return new object[] { inverse, alternating, allTrue };
             }
-            
-            // Size stress cases.
-            bitArray1 = new BitArray(0x1000F, false);
-            bitArray2 = new BitArray(0x1000F, false);
+        }
 
-            bitArray1.Set(0x10000, true); // The bit for 1 (2^0).
-            bitArray1.Set(0x10001, true); // The bit for 2 (2^1).
+        [Theory]
+        [MemberData(nameof(Xor_Operator_Data))]
+        public static void Xor_Operator(bool[] l, bool[] r, bool[] expected)
+        {
+            BitArray left = new BitArray(l);
+            BitArray right = new BitArray(r);
 
-            bitArray2.Set(0x10001, true); // The bit for 2 (2^1).
+            BitArray actual = left.Xor(right);
+            Assert.Same(left, actual);
+            Assert.Equal(actual.Length, expected.Length);
 
-            switch (op)
+            for (int i = 0; i < expected.Length; i++)
             {
-                case Operator.Xor:
-                    result = bitArray1.Xor(bitArray2);
-                    Assert.Same(bitArray1, result);
-                    Assert.True(result.Get(0x10000));
-                    Assert.False(result.Get(0x10001));
-                    Assert.False(result.Get(0x10002));
-                    Assert.False(result.Get(0x10004));
-                    break;
-
-                case Operator.And:
-                    result = bitArray1.And(bitArray2);
-                    Assert.Same(bitArray1, result);
-                    Assert.False(result.Get(0x10000));
-                    Assert.True(result.Get(0x10001));
-                    Assert.False(result.Get(0x10002));
-                    Assert.False(result.Get(0x10004));
-                    break;
-
-                case Operator.Or:
-                    result = bitArray1.Or(bitArray2);
-                    Assert.Same(bitArray1, result);
-                    Assert.True(result.Get(0x10000));
-                    Assert.True(result.Get(0x10001));
-                    Assert.False(result.Get(0x10002));
-                    Assert.False(result.Get(0x10004));
-                    break;
+                Assert.Equal(expected[i], actual[i]);
             }
         }
-        
+
         [Fact]
         public static void And_Invalid()
         {
@@ -148,7 +161,7 @@ namespace System.Collections.Tests
 
             Assert.Throws<ArgumentNullException>("value", () => bitArray1.And(null));
         }
-        
+
         [Fact]
         public static void Or_Invalid()
         {
@@ -163,7 +176,7 @@ namespace System.Collections.Tests
         }
 
         [Fact]
-        public static void BitArray_XorTest_Negative()
+        public static void Xor_Invalid()
         {
             BitArray bitArray1 = new BitArray(11, false);
             BitArray bitArray2 = new BitArray(6, false);