Exemple #1
0
        public void TestIndexing_1()
        {
            var coll = new AscendingIntegerRangeCollection();

            coll.AddRangeByFirstAndLastInclusive(10, 19);
            Assert.AreEqual(10, coll[0]);
            Assert.AreEqual(19, coll[9]);

            coll.AddRangeByFirstAndLastInclusive(30, 39);
            Assert.AreEqual(10, coll[0]);
            Assert.AreEqual(19, coll[9]);
            Assert.AreEqual(30, coll[10]);
            Assert.AreEqual(39, coll[19]);

            coll.RemoveRangeByFirstAndLastInclusive(30, 35);
            Assert.AreEqual(10, coll[0]);
            Assert.AreEqual(19, coll[9]);
            Assert.AreEqual(36, coll[10]);
            Assert.AreEqual(39, coll[13]);

            coll.RemoveRangeByFirstAndLastInclusive(10, 14);
            Assert.AreEqual(15, coll[0]);
            Assert.AreEqual(19, coll[4]);
            Assert.AreEqual(36, coll[5]);
            Assert.AreEqual(39, coll[8]);
        }
Exemple #2
0
 public void TestRemoveCompleteRange_1()
 {
     for (int i = -2; i <= 2; ++i)
     {
         var coll = new AscendingIntegerRangeCollection();
         coll.AddRangeByFirstAndLastInclusive(10, 20);
         coll.RemoveRangeByFirstAndLastInclusive(i + 10 - 2, i + 20 + 2);
         Assert.AreEqual(0, coll.RangeCount, "i=" + i.ToString());
     }
 }
Exemple #3
0
        public void TestIndexing_3()
        {
            var rnd     = new System.Random();
            var coll    = new AscendingIntegerRangeCollection();
            var hashSet = new HashSet <int>();

            for (int i = 0; i < 3000; ++i)
            {
                var r = rnd.Next(10, 4000);
                coll.AddRangeByFirstAndLastInclusive(r, r);
                hashSet.Add(r);
            }

            for (int i = 0; i < 3000; ++i)
            {
                var r = rnd.Next(10, 4000);
                coll.RemoveRangeByFirstAndLastInclusive(r, r);
                hashSet.Remove(r);
            }

            var hashSorted = hashSet.OrderBy(x => x).ToArray();
            var collSorted = coll.ToArray();

            // Evaluation of the sorted arrays
            Assert.AreEqual(hashSorted.Length, collSorted.Length);
            for (int i = 0; i < hashSorted.Length; ++i)
            {
                Assert.AreEqual(hashSorted[i], collSorted[i], "i=" + i.ToString());
            }

            // Test of indexing
            Assert.AreEqual(hashSorted.Length, coll.Count);
            for (int i = 0; i < hashSorted.Length; ++i)
            {
                Assert.AreEqual(hashSorted[i], coll[i], "i=" + i.ToString());
            }

            // Test of Contains
            int minElement = hashSorted[0];
            int maxElement = hashSorted[hashSorted.Length - 1];

            for (int ele = minElement - 5; ele < maxElement + 5; ++ele)
            {
                Assert.AreEqual(hashSet.Contains(ele), coll.Contains(ele));
            }
        }
Exemple #4
0
        public void TestAddRemoveRandomly_1()
        {
            var    rnd     = new System.Random();
            var    coll    = new AscendingIntegerRangeCollection();
            var    hashSet = new HashSet <int>();
            var    addList = new List <int>();
            var    rmvList = new List <int>();
            string error   = null;

            int indexThatWentWrong = -1;

            for (int i = 0; i < 2000; ++i)
            {
                var r = rnd.Next(10, 4000);
                addList.Add(r);
            }

            for (int i = 0; i < 1000; ++i)
            {
                var r = rnd.Next(10, 4000);
                rmvList.Add(r);
            }

            for (int i = 0; i < addList.Count; ++i)
            {
                var r = addList[i];
                coll.AddRangeByFirstAndLastInclusive(r, r);
                hashSet.Add(r);
            }

            for (int i = 0; i < rmvList.Count; ++i)
            {
                var r = rmvList[i];
                hashSet.Remove(r);
                coll.RemoveRangeByFirstAndLastInclusive(r, r);

                error = AssertEqual(coll, hashSet);
                if (null != error)
                {
                    indexThatWentWrong = i;
                    break;
                }
            }

            // Replay the error

            coll    = new AscendingIntegerRangeCollection();
            hashSet = new HashSet <int>();

            for (int i = 0; i < addList.Count; ++i)
            {
                var r = addList[i];
                coll.AddRangeByFirstAndLastInclusive(r, r);
                hashSet.Add(r);
            }

            for (int i = 0; i < rmvList.Count; ++i)
            {
                var r = rmvList[i];
                hashSet.Remove(r);

                if (i == indexThatWentWrong)
                {
                    System.Diagnostics.Debugger.Break();
                }

                coll.RemoveRangeByFirstAndLastInclusive(r, r);

                error = AssertEqual(coll, hashSet);
                Assert.IsNull(error);
            }
        }
		public void TestAddRemoveRandomly_1()
		{
			var rnd = new System.Random();
			var coll = new AscendingIntegerRangeCollection();
			var hashSet = new HashSet<int>();
			List<int> addList = new List<int>();
			List<int> rmvList = new List<int>();
			string error = null;

			int indexThatWentWrong = -1;

			for (int i = 0; i < 2000; ++i)
			{
				var r = rnd.Next(10, 4000);
				addList.Add(r);
			}

			for (int i = 0; i < 1000; ++i)
			{
				var r = rnd.Next(10, 4000);
				rmvList.Add(r);
			}

			for (int i = 0; i < addList.Count; ++i)
			{
				var r = addList[i];
				coll.AddRangeByFirstAndLastInclusive(r, r);
				hashSet.Add(r);
			}

			for (int i = 0; i < rmvList.Count; ++i)
			{
				var r = rmvList[i];
				hashSet.Remove(r);
				coll.RemoveRangeByFirstAndLastInclusive(r, r);

				error = AssertEqual(coll, hashSet);
				if (null != error)
				{
					indexThatWentWrong = i;
					break;
				}
			}

			// Replay the error

			coll = new AscendingIntegerRangeCollection();
			hashSet = new HashSet<int>();

			for (int i = 0; i < addList.Count; ++i)
			{
				var r = addList[i];
				coll.AddRangeByFirstAndLastInclusive(r, r);
				hashSet.Add(r);
			}

			for (int i = 0; i < rmvList.Count; ++i)
			{
				var r = rmvList[i];
				hashSet.Remove(r);

				if (i == indexThatWentWrong)
				{
					System.Diagnostics.Debugger.Break();
				}

				coll.RemoveRangeByFirstAndLastInclusive(r, r);

				error = AssertEqual(coll, hashSet);
				Assert.IsNull(error);
			}
		}
		public void TestIndexing_3()
		{
			var rnd = new System.Random();
			var coll = new AscendingIntegerRangeCollection();
			var hashSet = new HashSet<int>();

			for (int i = 0; i < 3000; ++i)
			{
				var r = rnd.Next(10, 4000);
				coll.AddRangeByFirstAndLastInclusive(r, r);
				hashSet.Add(r);
			}

			for (int i = 0; i < 3000; ++i)
			{
				var r = rnd.Next(10, 4000);
				coll.RemoveRangeByFirstAndLastInclusive(r, r);
				hashSet.Remove(r);
			}

			var hashSorted = hashSet.OrderBy(x => x).ToArray();
			var collSorted = coll.ToArray();

			// Evaluation of the sorted arrays
			Assert.AreEqual(hashSorted.Length, collSorted.Length);
			for (int i = 0; i < hashSorted.Length; ++i)
			{
				Assert.AreEqual(hashSorted[i], collSorted[i], "i=" + i.ToString());
			}

			// Test of indexing
			Assert.AreEqual(hashSorted.Length, coll.Count);
			for (int i = 0; i < hashSorted.Length; ++i)
			{
				Assert.AreEqual(hashSorted[i], coll[i], "i=" + i.ToString());
			}

			// Test of Contains
			int minElement = hashSorted[0];
			int maxElement = hashSorted[hashSorted.Length - 1];

			for (int ele = minElement - 5; ele < maxElement + 5; ++ele)
			{
				Assert.AreEqual(hashSet.Contains(ele), coll.Contains(ele));
			}
		}
		public void TestIndexing_1()
		{
			var coll = new AscendingIntegerRangeCollection();
			coll.AddRangeByFirstAndLastInclusive(10, 19);
			Assert.AreEqual(10, coll[0]);
			Assert.AreEqual(19, coll[9]);

			coll.AddRangeByFirstAndLastInclusive(30, 39);
			Assert.AreEqual(10, coll[0]);
			Assert.AreEqual(19, coll[9]);
			Assert.AreEqual(30, coll[10]);
			Assert.AreEqual(39, coll[19]);

			coll.RemoveRangeByFirstAndLastInclusive(30, 35);
			Assert.AreEqual(10, coll[0]);
			Assert.AreEqual(19, coll[9]);
			Assert.AreEqual(36, coll[10]);
			Assert.AreEqual(39, coll[13]);

			coll.RemoveRangeByFirstAndLastInclusive(10, 14);
			Assert.AreEqual(15, coll[0]);
			Assert.AreEqual(19, coll[4]);
			Assert.AreEqual(36, coll[5]);
			Assert.AreEqual(39, coll[8]);
		}
		public void TestRemoveCompleteRange_1()
		{
			for (int i = -2; i <= 2; ++i)
			{
				var coll = new AscendingIntegerRangeCollection();
				coll.AddRangeByFirstAndLastInclusive(10, 20);
				coll.RemoveRangeByFirstAndLastInclusive(i + 10 - 2, i + 20 + 2);
				Assert.AreEqual(0, coll.RangeCount, "i=" + i.ToString());
			}
		}