Пример #1
0
        public void Variable_found_in_same_scope()
        {
            var map = new ScopedVariableMap();

            map.PushScope();
            Assert.That(map.TryAddVariable("a", 1), Is.True);
            Assert.That(map.TryAddVariable("b", 17), Is.True);
            Assert.That(map.TryGetVariable("a", out var a), Is.True);
            Assert.That(a, Is.EqualTo(1));
            Assert.That(map.TryGetVariable("b", out var b), Is.True);
            Assert.That(b, Is.EqualTo(17));
        }
Пример #2
0
        public void Variable_visible_in_inner_but_not_outer_scope()
        {
            var map = new ScopedVariableMap();

            map.PushScope();
            Assert.That(map.TryAddVariable("a", 1), Is.True);
            map.PushScope();
            Assert.That(map.TryAddVariable("b", 2), Is.True);

            Assert.That(map.TryGetVariable("b", out var index), Is.True);
            Assert.That(index, Is.EqualTo(2));

            map.PopScope();
            Assert.That(map.TryGetVariable("b", out _), Is.False);
        }
Пример #3
0
        public void Pushing_and_popping_very_many_scopes()
        {
            var       map        = new ScopedVariableMap();
            const int scopeCount = 100;

            for (var i = 0; i < scopeCount; i++)
            {
                map.PushScope();
                map.TryAddVariable(i.ToString(), i);
            }
            for (var i = 0; i < scopeCount; i++)
            {
                map.PopScope();
            }

            // Now some of the new scopes should come from the cache
            // Verify that the cached scopes are empty
            for (var i = 0; i < scopeCount; i++)
            {
                map.PushScope();
            }
            for (var i = 0; i < scopeCount; i++)
            {
                Assert.That(map.TryGetVariable(i.ToString(), out _), Is.False);
            }
            for (var i = 0; i < scopeCount; i++)
            {
                map.PopScope();
            }
        }
Пример #4
0
        public void Cannot_do_operations_if_there_is_no_scope()
        {
            var map = new ScopedVariableMap();

            Assert.That(() => map.TryAddVariable("a", 0), Throws.InvalidOperationException);
            Assert.That(() => map.TryGetVariable("a", out _), Throws.InvalidOperationException);
            Assert.That(() => map.PopScope(), Throws.InvalidOperationException);
        }
Пример #5
0
        public void Variable_not_found_in_any_scope()
        {
            var map = new ScopedVariableMap();

            map.PushScope();
            Assert.That(map.TryAddVariable("a", 1), Is.True);
            map.PushScope();
            Assert.That(map.TryAddVariable("b", 2), Is.True);
            Assert.That(map.TryGetVariable("c", out _), Is.False);
        }
Пример #6
0
        public void Variable_found_in_outer_scope()
        {
            var map = new ScopedVariableMap();

            map.PushScope();
            Assert.That(map.TryAddVariable("a", 1), Is.True);
            map.PushScope();
            Assert.That(map.TryGetVariable("a", out var index), Is.True);
            Assert.That(index, Is.EqualTo(1));
        }
Пример #7
0
        public int SmallMethod()
        {
            // This is based on NameParsing.IsDigit
            var map = new ScopedVariableMap();
            var sum = 0;

            // Execute the scenario a few times to simulate the map being reused
            for (var i = 0; i < 5; i++)
            {
                map.PushScope();
                map.TryAddVariable("ch", 1);
                map.TryGetVariable("ch", out var a);
                map.TryGetVariable("ch", out var b);
                map.PopScope();

                sum += a + b;
            }

            return(sum);
        }
Пример #8
0
        public void Reset_clears_stack()
        {
            var map = new ScopedVariableMap();

            map.PushScope();
            map.PushScope();
            map.TryAddVariable("a", 1);
            map.PushScope();

            map.Reset();

            Assert.That(() => map.TryGetVariable("a", out _), Throws.InvalidOperationException);
        }
Пример #9
0
        public int LargerMethod()
        {
            // This is a synthetic benchmark, aiming to still be somewhat realistic
            var map = new ScopedVariableMap();
            var sum = 0;

            // Execute the scenario a few times to simulate the map being reused
            for (var iteration = 0; iteration < 5; iteration++)
            {
                // Create a lot of nested scopes and add variables to them
                for (var i = 0; i < ScopeCount; i++)
                {
                    map.PushScope();
                    for (var j = 0; j < VariablesPerScope; j++)
                    {
                        var index = i * ScopeCount + j;
                        map.TryAddVariable(_variableNames[index], index);
                    }
                }

                // Go through all the variables
                for (var i = 0; i < ScopeCount * VariablesPerScope; i++)
                {
                    // Do not go through them in exact order
                    map.TryGetVariable(_variableNames[i * 3 % (ScopeCount * VariablesPerScope)], out var index);
                    sum += index;
                }

                // Remove the scopes
                for (var i = 0; i < ScopeCount; i++)
                {
                    map.PopScope();
                }
            }

            return(sum);
        }