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(); } }
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); }
public void Can_add_variable_to_outer_scope_after_defining_in_inner() { // TODO: This behavior could be changed; for now it is simpler var map = new ScopedVariableMap(); map.PushScope(); map.PushScope(); Assert.That(map.TryAddVariable("a", 1), Is.True); map.PopScope(); Assert.That(map.TryAddVariable("a", 2), Is.True); }
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); }
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); }
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); }