public void ThrowingForBadArgs()
        {
            var bl = new BitList {
                true, true, false, false
            };
            IList <bool> blInterface = bl;

            Assert.Throws <ArgumentOutOfRangeException>("index", () => bl[-1]);
            Assert.Throws <ArgumentOutOfRangeException>("index", () => bl[4]);
            Assert.Throws <ArgumentOutOfRangeException>("index", () => bl[-1] = false);
            Assert.Throws <ArgumentOutOfRangeException>("index", () => bl[4]  = false);
            Assert.Throws <ArgumentOutOfRangeException>("index", () => bl.Insert(-1, false));
            Assert.Throws <ArgumentOutOfRangeException>("index", () => bl.Insert(5, true));
            Assert.Throws <ArgumentOutOfRangeException>("index", () => bl.RemoveAt(-1));
            Assert.Throws <ArgumentOutOfRangeException>("index", () => bl.RemoveAt(4));
            Assert.Throws <ArgumentNullException>("array", () => blInterface.CopyTo(null, 5));
            Assert.Throws <ArgumentNullException>("bitArray", () => new BitList(default(BitArray)));
            Assert.Throws <ArgumentNullException>("values", () => new BitList(default(IEnumerable <bool>)));

            bool[] things = new bool[5];
            Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => blInterface.CopyTo(things, -1));
            Assert.Throws <ArgumentException>("array", () => blInterface.CopyTo(things, 2));
            Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => blInterface.CopyTo(things, 5));

            things = new bool[3];
            Assert.Throws <ArgumentException>("array", () => blInterface.CopyTo(things, 0));
            Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => blInterface.CopyTo(things, 3));
        }
        public void Contains()
        {
            var bl = new BitList();

            Assert.False(bl.Contains(true));
            Assert.False(bl.Contains(false));

            bl.Add(true);

            Assert.Equal(1, bl.Count);
            Assert.True(bl.Contains(true));
            Assert.False(bl.Contains(false));

            bl.Clear();
            bl.TrimExcess();

            Assert.Equal(0, bl.Count);
            Assert.False(bl.Contains(true));
            Assert.False(bl.Contains(false));

            bl.Insert(0, false);
            Assert.Equal(1, bl.Count);
            Assert.False(bl.Contains(true));
            Assert.True(bl.Contains(false));

            bl[0] = true;
            Assert.Equal(1, bl.Count);
            Assert.True(bl.Contains(true));
            Assert.False(bl.Contains(false));

            bl[0] = false;
            Assert.Equal(1, bl.Count);
            Assert.False(bl.Contains(true));
            Assert.True(bl.Contains(false));
        }
        public void InsertAndRemoveAtShouldBeConsistent()
        {
            var bl = new BitList {
                true, false, true
            };

            bl.Insert(1, false);

            Assert.Equal(4, bl.Count);

            bool[] expected = { true, false, false, true };
            Assert.Equal(expected, bl);

            bl.RemoveAt(2);

            Assert.Equal(3, bl.Count);
            expected = new[] { true, false, true };
            Assert.Equal(expected, bl);
        }
 public override void Insert(int index, bool item)
 {
     lock (_syncRoot)
         _list.Insert(index, item);
 }
示例#5
0
        private void TestBitListWith(int size, int seed)
        {
            BitList     blist = new BitList();
            List <bool> list  = new List <bool>();
            Random      r     = new Random(seed);

            for (int i = 0; i < size; i++)
            {
                bool b = r.NextDouble() < 0.5;

                blist.Add(b);
                list.Add(b);

                AssertListEquals(list, blist);
            }

            for (int i = size - 1; i >= 0; i -= 3)
            {
                blist.RemoveAt(i);
                list.RemoveAt(i);

                AssertListEquals(list, blist);

                if (i % 4 == 0)
                {
                    bool b     = r.NextDouble() < 0.5;
                    int  index = r.Next(100);

                    blist.Insert(index, b);
                    list.Insert(index, b);

                    AssertListEquals(list, blist);
                }
            }

            for (int i = 0; i < blist.Count; i++)
            {
                bool b     = r.NextDouble() < 0.5;
                int  index = r.Next(blist.Count);

                blist[index] = b;
                list[index]  = b;

                AssertListEquals(list, blist);
            }

            for (int i = 0; i < size; i++)
            {
                bool b = r.NextDouble() < 0.5;

                blist.Add(b);
                list.Add(b);

                AssertListEquals(list, blist);
            }

            bool[] arrayA = new bool[list.Count + 2];
            bool[] arrayB = new bool[blist.Count + 2];

            Assert.AreEqual(arrayA.Length, arrayB.Length);

            list.CopyTo(arrayA, 2);
            blist.CopyTo(arrayB, 2);

            for (int i = 0; i < arrayA.Length; i++)
            {
                Assert.AreEqual(arrayA[i], arrayB[i]);
            }

            for (int i = list.Count - 1; i >= 0; i--)
            {
                blist.RemoveAt(i);
                list.RemoveAt(i);

                AssertListEquals(list, blist);
            }
        }
示例#6
0
        public void TestBitList()
        {
            BitList list = new BitList();

            for (int i = 0; i < 1025; i++)
            {
                list.Add(true);
            }

            Assert.AreEqual(0, list.IndexOf(true));
            Assert.AreEqual(-1, list.IndexOf(false));

            list.Add(false);
            Assert.AreEqual(0, list.IndexOf(true));
            Assert.AreEqual(1025, list.IndexOf(false));

            Assert.IsTrue(list.Remove(false));
            Assert.AreEqual(0, list.IndexOf(true));
            Assert.AreEqual(-1, list.IndexOf(false));

            try
            {
                var x = new bool[1024];
                list.CopyTo(x, 0);

                Assert.Fail();
            }
            catch (InvalidOperationException) { }

            try
            {
                var x = new bool[1026];
                list.CopyTo(x, 3);

                Assert.Fail();
            }
            catch (InvalidOperationException) { }

            var array = new bool[1025];

            list.CopyTo(array, 0);
            int index = 0;

            foreach (bool value in array)
            {
                Assert.AreEqual(list[index++], value);
            }

            Assert.AreEqual(1025, list.Count);

            for (int i = 0; i < 1025; i++)
            {
                list.RemoveAt(list.Count / 2);
            }

            Assert.AreEqual(0, list.Count);

            if (list.Remove(false))
            {
                Assert.Fail();
            }

            if (list.Remove(true))
            {
                Assert.Fail();
            }

            try
            {
                list.RemoveAt(0);
                Assert.Fail();
            }
            catch (IndexOutOfRangeException) { }

            try
            {
                list[0] = false;

                Assert.Fail();
            }
            catch (IndexOutOfRangeException) { }

            try
            {
                var a = list[0];

                Assert.Fail();
            }
            catch (IndexOutOfRangeException) { }

            list.Insert(0, true);

            if (!list.Remove(true))
            {
                Assert.Fail();
            }

            try
            {
                list.Insert(1, true);
                Assert.Fail();
            }
            catch (IndexOutOfRangeException) { }


            try
            {
                list.CopyTo(null, 0);
                Assert.Fail();
            }
            catch (NullReferenceException) { }

            array = new bool[0];
            list.CopyTo(array, 0);

            try
            {
                list.CopyTo(array, -1);
                Assert.Fail();
            }
            catch (IndexOutOfRangeException) { }

            TestBitListWith(128, 1234);
            TestBitListWith(1024, 54321);
            TestBitListWith(256, 11);
            TestBitListWith(2048, 242);
            TestBitListWith(1111, 42);

            BitList blist = new BitList();

            blist.Add(false);
            blist.Add(false);

            Assert.IsTrue(blist.Contains(false));
            Assert.IsFalse(blist.Contains(true));

            blist.Add(true);

            Assert.IsTrue(blist.Contains(false));
            Assert.IsTrue(blist.Contains(true));

            blist.Clear();
            Assert.AreEqual(0, blist.Count);

            blist.Add(true);
            blist.Add(true);

            Assert.IsFalse(blist.Contains(false));
            Assert.IsTrue(blist.Contains(true));

            blist.Add(false);

            Assert.IsTrue(blist.Contains(false));
            Assert.IsTrue(blist.Contains(true));

            blist.Clear();
            Assert.AreEqual(0, blist.Count);

            for (int i = 0; i < 128; i++)
            {
                blist.Add(false);
            }

            Assert.IsTrue(blist.Contains(false));
            Assert.IsFalse(blist.Contains(true));

            blist.Add(true);

            Assert.IsTrue(blist.Contains(false));
            Assert.IsTrue(blist.Contains(true));

            Assert.AreEqual(129, blist.Count);

            blist.Remove(true);
            Assert.AreEqual(128, blist.Count);

            Assert.IsTrue(blist.Contains(false));
            Assert.IsFalse(blist.Contains(true));

            blist.Clear();
            Assert.AreEqual(0, blist.Count);

            for (int i = 0; i < 128; i++)
            {
                blist.Add(true);
            }

            Assert.IsFalse(blist.Contains(false));
            Assert.IsTrue(blist.Contains(true));

            blist.Add(false);

            Assert.IsTrue(blist.Contains(false));
            Assert.IsTrue(blist.Contains(true));

            Assert.AreEqual(129, blist.Count);

            blist.Remove(false);
            Assert.AreEqual(128, blist.Count);

            Assert.IsFalse(blist.Contains(false));
            Assert.IsTrue(blist.Contains(true));
        }