Exemple #1
0
        public void DefaultConstructor()
        {
            var list = new LocalList2 <int>();

            Assert.AreEqual(0, list.Count);
            Assert.AreEqual(0, list.Capacity);
        }
Exemple #2
0
        public void CapacityConstructor()
        {
            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                var unused = new LocalList2 <int>(capacity: -1);
            });

            foreach (var forceArray in new[] { true, false })
            {
                for (var capacity = 0; capacity < 30; capacity++)
                {
                    var list = new LocalList2 <int>(capacity: capacity, forceArray);

                    Assert.AreEqual(0, list.Count);
                    Assert.AreEqual(capacity, list.Capacity);
                }
            }
        }
Exemple #3
0
        public void AddCountIndexer()
        {
            foreach (var count in CapacitiesToTest.Concat(new[] { 70000 }))
            {
                var list = new LocalList2 <byte>();
                Assert.AreEqual(0, list.Count);

                var bytes = NonZeroBytes(count).ToArray();

                foreach (var x in bytes)
                {
                    list.Add(x);
                }

                Assert.AreEqual(count, list.Count);
                Assert.IsTrue(list.AllFreeSlotsAreClear());

                for (var index = 0; index < list.Count; index++)
                {
                    Assert.AreEqual(bytes[index], list[index]);
                }

                Assert.Throws <ArgumentOutOfRangeException>(() => _                = list[-1]);
                Assert.Throws <ArgumentOutOfRangeException>(() => list[-1]         = 42);
                Assert.Throws <ArgumentOutOfRangeException>(() => _                = list[list.Count]);
                Assert.Throws <ArgumentOutOfRangeException>(() => list[list.Count] = 42);

                // store in reverse via indexer setter
                for (var index = 0; index < bytes.Length; index++)
                {
                    list[list.Count - index - 1] = bytes[index];
                }

                if (list.Count > 0)
                {
                    var enumerator = list.GetEnumerator();
                    list[0] = list[0];
                    Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
                }

                var resultingList = list.ResultingList();
                _ = list.Count; // do not throws

                Assert.AreEqual(list.Count, resultingList.Count);
                Assert.Throws <InvalidOperationException>(() => _ = list.ResultingList());

                // the same APIs via IList<T> interface
                for (var index = 0; index < list.Count; index++)
                {
                    Assert.AreEqual(bytes[bytes.Length - index - 1], resultingList[index]);
                }

                Assert.Throws <CollectionReadOnlyException>(() => resultingList[0] = 42);
                Assert.Throws <CollectionReadOnlyException>(() => resultingList.Add(42));

                // results obtained checks
                if (list.Count > 0)
                {
                    Assert.Throws <InvalidOperationException>(() => _       = list[0]);
                    Assert.Throws <InvalidOperationException>(() => list[0] = 42);
                }

                Assert.Throws <InvalidOperationException>(() => list.Add(42));
            }
 public void Setup()
 {
     myList       = new List <int>(capacity: Count);
     myLocalList  = new LocalList <int>(capacity: Count);
     myLocalList2 = new LocalList2 <int>(capacity: Count);
 }