public override bool Contains(bool item)
 {
     lock (_syncRoot)
         return(_list.Contains(item));
 }
        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));
        }
Example #3
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));
        }