Beispiel #1
0
        public void Stress_Test()
        {
            const int iterations = 100;
            var       random     = new Random(0);
            var       selection  = new List <IndexRange>();
            var       expected   = new List <int>();

            IndexRange Generate()
            {
                var start = random.Next(100);

                return(new IndexRange(start, start + random.Next(20)));
            }

            for (var i = 0; i < iterations; ++i)
            {
                var toAdd = random.Next(5);

                for (var j = 0; j < toAdd; ++j)
                {
                    var range = Generate();
                    IndexRange.Add(selection, range);

                    for (var k = range.Begin; k <= range.End; ++k)
                    {
                        if (!expected.Contains(k))
                        {
                            expected.Add(k);
                        }
                    }

                    var actual = IndexRange.EnumerateIndices(selection).ToList();
                    expected.Sort();
                    Assert.Equal(expected, actual);
                }

                var toRemove = random.Next(5);

                for (var j = 0; j < toRemove; ++j)
                {
                    var range = Generate();
                    IndexRange.Remove(selection, range);

                    for (var k = range.Begin; k <= range.End; ++k)
                    {
                        expected.Remove(k);
                    }

                    var actual = IndexRange.EnumerateIndices(selection).ToList();
                    Assert.Equal(expected, actual);
                }

                selection.Clear();
                expected.Clear();
            }
        }
Beispiel #2
0
        public void Add_Should_Add_Range_To_Empty_List()
        {
            var ranges   = new List <IndexRange>();
            var selected = new List <IndexRange>();
            var result   = IndexRange.Add(ranges, new IndexRange(0, 4), selected);

            Assert.Equal(5, result);
            Assert.Equal(new[] { new IndexRange(0, 4) }, ranges);
            Assert.Equal(new[] { new IndexRange(0, 4) }, selected);
        }
Beispiel #3
0
        public void Add_Should_Add_Intersecting_Range_Both()
        {
            var ranges = new List <IndexRange> {
                new IndexRange(8, 10)
            };
            var selected = new List <IndexRange>();
            var result   = IndexRange.Add(ranges, new IndexRange(6, 12), selected);

            Assert.Equal(4, result);
            Assert.Equal(new[] { new IndexRange(6, 12) }, ranges);
            Assert.Equal(new[] { new IndexRange(6, 7), new IndexRange(11, 12) }, selected);
        }
Beispiel #4
0
        public void Add_Should_Add_Non_Intersecting_Range_In_Middle()
        {
            var ranges = new List <IndexRange> {
                new IndexRange(0, 4), new IndexRange(14, 16)
            };
            var selected = new List <IndexRange>();
            var result   = IndexRange.Add(ranges, new IndexRange(8, 10), selected);

            Assert.Equal(3, result);
            Assert.Equal(new[] { new IndexRange(0, 4), new IndexRange(8, 10), new IndexRange(14, 16) }, ranges);
            Assert.Equal(new[] { new IndexRange(8, 10) }, selected);
        }
Beispiel #5
0
        public void Add_Should_Not_Add_Already_Selected_Range()
        {
            var ranges = new List <IndexRange> {
                new IndexRange(8, 10)
            };
            var selected = new List <IndexRange>();
            var result   = IndexRange.Add(ranges, new IndexRange(9, 10), selected);

            Assert.Equal(0, result);
            Assert.Equal(new[] { new IndexRange(8, 10) }, ranges);
            Assert.Empty(selected);
        }
Beispiel #6
0
        public void Add_Should_Join_Two_Intersecting_Ranges_And_Add_Ranges()
        {
            var ranges = new List <IndexRange> {
                new IndexRange(8, 10), new IndexRange(12, 14)
            };
            var selected = new List <IndexRange>();
            var result   = IndexRange.Add(ranges, new IndexRange(6, 18), selected);

            Assert.Equal(7, result);
            Assert.Equal(new[] { new IndexRange(6, 18) }, ranges);
            Assert.Equal(new[] { new IndexRange(6, 7), new IndexRange(11, 11), new IndexRange(15, 18) }, selected);
        }