Exemplo n.º 1
0
        public void GivenCapacityWhenUpdateThenQueueReflectChanges()
        {
            var tree = new SqrtTree <int>(10, Sum);

            tree[1] = 10;
            tree[5] = 20;
            Assert.Equal(30, tree.Query(0, 9));
            Assert.Equal(10, tree.Query(1, 1));
            Assert.Equal(20, tree.Query(5, 5));
        }
Exemplo n.º 2
0
        public void GivenArrayAndConstructTreeWhenChangeArrayThenResultsInTreeAreNotChanged()
        {
            var data = new int[4] {
                1, 2, 3, 4
            };
            var tree = new SqrtTree <int>(data, Sum);
            var sum  = tree.Query(0, 3);

            data[1] = 10;
            tree[2] = 10;
            tree[2] = 3;
            Assert.Equal(sum, tree.Query(0, 3));
        }
Exemplo n.º 3
0
        public void GivenTreeWhenQueryIntervalThenReturnResultOfOperationOnRange(int size)
        {
            var data = Enumerable.Range(1, size).ToArray();
            var tree = new SqrtTree <int>(data, Sum);

            for (int i = 0; i < size; i++)
            {
                var res = 0;
                for (int j = i; j < size; j++)
                {
                    res = Sum(res, data[j]);
                    Assert.Equal(res, tree.Query(i, j));
                    Assert.Equal(res, tree.Query(j, i));
                }
            }
        }
Exemplo n.º 4
0
        public void GivenTreeWhenQueueOutsideOfRangeThenThrowException(int left, int right)
        {
            var tree = new SqrtTree <int>(100, Sum);

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                tree.Query(left, right);
            });
        }
Exemplo n.º 5
0
        public void GivenTreeWhenUpdateElementThenQueuedReflectsChanges()
        {
            int size = 100;
            Func <int, int, int> add = (a, b) => a + b;
            var updatedTree          = new SqrtTree <int>(Enumerable.Range(1, size), add);
            var canonicalTree        = new SqrtTree <int>(Enumerable.Range(1, size), add);

            updatedTree[size / 2] = updatedTree[size / 2] + 100;
            for (int i = 0; i < size; i++)
            {
                for (int j = i; j < size; j++)
                {
                    Assert.Equal(canonicalTree.Query(i, j), updatedTree.Query(i, j)
                                 - ((i <= size / 2 && j >= size / 2) ? 100 : 0));
                }
            }
        }