public void TestStackLikeBehavior() { Random random = new Random(); TreeStack <int> stack = new TreeStack <int>(branchingFactor: 4); Stack <int> reference = new Stack <int>(); for (int i = 0; i < 2 * 4 * 4; i++) { int item = random.Next(); stack.Push(item); reference.Push(item); } while (stack.Count > 0) { var expected = reference.Peek(); Assert.Equal(expected, stack.Peek()); Assert.Equal(expected, reference.Pop()); Assert.Equal(expected, stack.Pop()); stack.Validate(ValidationRules.None); Assert.Equal(reference, stack); } Assert.Empty(stack); Assert.Empty(reference); }
/// <summary> /// 回傳當下的運算樹計算結果 /// </summary> /// <returns>運算樹的計算結果</returns> protected Result GetResult() { if (TreeStack.Count == 1) { //算完不能pop var answer = new Evaluator().EvaluateTree(TreeStack.Peek()); return(new Result(answer, 0)); } else if (TreeStack.Count > 1) { int extraRightBracket = 0; //這裡不用tmp嗎,再想想 while (TreeStack.Count > 1) { var subTree = TreeStack.Pop(); TreeStack.Peek().Add(subTree); extraRightBracket++; } //算完不能pop var ans = new Evaluator().EvaluateTree(TreeStack.Peek()); return(new Result(ans, extraRightBracket)); } else { throw new Exception("資料有誤"); } }
public void TestEnumerator() { var stack = new TreeStack <int>(); TreeStack <int> .Enumerator enumerator = stack.GetEnumerator(); Assert.Equal(0, enumerator.Current); Assert.False(enumerator.MoveNext()); Assert.Equal(0, enumerator.Current); Assert.False(enumerator.MoveNext()); Assert.Equal(0, enumerator.Current); // Adding an item to the stack invalidates it, but Current is still unchecked stack.Push(1); Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext()); Assert.Equal(0, enumerator.Current); enumerator = stack.GetEnumerator(); Assert.Equal(0, enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal(1, enumerator.Current); // Reset has no effect due to boxing the value type ((IEnumerator <int>)enumerator).Reset(); Assert.Equal(1, enumerator.Current); Assert.False(enumerator.MoveNext()); Assert.Equal(1, enumerator.Current); }
public void TestIEnumeratorT() { var stack = new TreeStack <int>(); IEnumerator <int> enumerator = stack.GetEnumerator(); Assert.Equal(0, enumerator.Current); Assert.False(enumerator.MoveNext()); Assert.Equal(0, enumerator.Current); // Adding an item to the stack invalidates it, but Current is still unchecked stack.Push(1); Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext()); Assert.Throws <InvalidOperationException>(() => enumerator.Reset()); Assert.Equal(0, enumerator.Current); enumerator = stack.GetEnumerator(); Assert.Equal(0, enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal(1, enumerator.Current); Assert.False(enumerator.MoveNext()); Assert.Equal(1, enumerator.Current); enumerator.Reset(); Assert.Equal(0, enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal(1, enumerator.Current); enumerator.Reset(); Assert.Equal(0, enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal(1, enumerator.Current); Assert.False(enumerator.MoveNext()); Assert.Equal(1, enumerator.Current); }
public void TestTreeStackBranchingFactorConstructor() { TreeStack <int> stack = new TreeStack <int>(8); Assert.Empty(stack); Assert.Throws <ArgumentOutOfRangeException>(() => new TreeStack <int>(-1)); Assert.Throws <ArgumentOutOfRangeException>(() => new TreeStack <int>(0)); Assert.Throws <ArgumentOutOfRangeException>(() => new TreeStack <int>(1)); }
private static TreeStack <T> CreateTreeStack <T>(IEnumerable <T> source) { var result = new TreeStack <T>(); foreach (T item in source) { result.Push(item); } return(result); }
public void TestEmptyStack() { var stack = new TreeStack <int>(); Assert.Throws <InvalidOperationException>(() => stack.Peek()); Assert.Throws <InvalidOperationException>(() => stack.Pop()); Assert.False(stack.TryPeek(out var result)); Assert.Equal(0, result); Assert.False(stack.TryPop(out result)); Assert.Equal(0, result); }
public void TestPush() { const int Value = 600; TreeStack <int> stack = new TreeStack <int>(); Assert.Empty(stack); stack.Push(Value); Assert.Single(stack); Assert.Equal(Value, stack.Peek()); int[] expected = { Value }; int[] actual = stack.ToArray(); Assert.Equal(expected, actual); }
public void TestClear() { var stack = new TreeStack <int>(branchingFactor: 3); stack.Clear(); Assert.Empty(stack); foreach (int item in Enumerable.Range(0, 10)) { stack.Push(item); } Assert.NotEmpty(stack); stack.Clear(); Assert.Empty(stack); }
/// <summary> /// 回傳當下最外層子樹的計算結果 /// </summary> /// <returns>最外層子樹的計算結果</returns> protected decimal GetTmpResult() { //暫時計算的結果只需要到第一層 if (TreeStack.Count > 0) { var outerTree = TreeStack.Peek(); return(new Evaluator().EvaluateTree(outerTree)); } else if (TreeStack.Count == 0) { return(0); } else { throw new Exception("資料有誤"); } }
public void TestCopyToValidation() { TreeStack <int> stack = CreateTreeStack(Enumerable.Range(0, 10)); Assert.Throws <ArgumentNullException>("dest", () => stack.CopyTo(null !, 0)); Assert.Throws <ArgumentOutOfRangeException>("dstIndex", () => stack.CopyTo(new int[stack.Count], -1)); Assert.Throws <ArgumentException>(string.Empty, () => stack.CopyTo(new int[stack.Count], 1)); ICollection collection = stack; Assert.Throws <ArgumentNullException>("dest", () => collection.CopyTo(null !, 0)); Assert.Throws <ArgumentOutOfRangeException>("dstIndex", () => collection.CopyTo(new int[collection.Count], -1)); Assert.Throws <ArgumentOutOfRangeException>("dstIndex", () => collection.CopyTo(Array.CreateInstance(typeof(int), new[] { stack.Count }, new[] { 1 }), 0)); Assert.Throws <ArgumentException>(string.Empty, () => collection.CopyTo(new int[collection.Count], collection.Count + 1)); Assert.Throws <ArgumentException>(null, () => collection.CopyTo(new int[stack.Count, 1], 0)); collection.CopyTo(Array.CreateInstance(typeof(int), new[] { stack.Count }, new[] { 1 }), 1); }
public bool MoveNext() { if (IsEnumerationFinished) { return(false); } var next = MoveNextCore(); Version++; if (next == false) { Stack = EnumerationFinished; } return(next); }
public void TestContains() { Random random = new Random(); var stack = new TreeStack <int>(branchingFactor: 4); for (int i = 0; i < 2 * 4 * 4; i++) { int value = random.Next(stack.Count + 1); stack.Push(i); // Use stack.Contains(i) since this is a targeted collection API test #pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection Assert.True(stack.Contains(i)); #pragma warning restore xUnit2017 // Do not use Contains() to check if a value exists in a collection } stack.Validate(ValidationRules.None); }
public void TestTrimExcess() { Random random = new Random(); TreeStack <int> stack = new TreeStack <int>(branchingFactor: 4); Stack <int> reference = new Stack <int>(); for (int i = 0; i < (2 * 4 * 4) + 2; i++) { stack.Push(i); reference.Push(i); } for (int i = 0; i < 2; i++) { stack.Pop(); reference.Pop(); } stack.Validate(ValidationRules.None); // In the first call to TrimExcess, items will move stack.TrimExcess(); stack.Validate(ValidationRules.RequirePacked); Assert.Equal(reference, stack); // In the second call, the list is already packed so nothing will move stack.TrimExcess(); stack.Validate(ValidationRules.RequirePacked); Assert.Equal(reference, stack); TreeStack <int> empty = new TreeStack <int>(); empty.Validate(ValidationRules.RequirePacked); empty.TrimExcess(); empty.Validate(ValidationRules.RequirePacked); TreeStack <int> single = CreateTreeStack(Enumerable.Range(0, 1)); single.Validate(ValidationRules.RequirePacked); single.TrimExcess(); single.Validate(ValidationRules.RequirePacked); }
private void setupHeader(INamedItem item) { var stack = new TreeStack() { Orientation = Orientation.Horizontal, Background = new SolidColorBrush(Colors.Transparent) }; _image = new Image(); stack.Children.Add(_image); _label = new Label { Content = item.Name.GetLabelTextWithoutHotkey(), Background = new SolidColorBrush(Colors.Transparent) }; stack.Children.Add(_label); Header = stack; Icon = Icon.Unknown; }
public void Reset() { Stack = null; CurrentNode = default; HasCurrentNode = false; }
public void TestTreeStackConstructor() { TreeStack <int> stack = new TreeStack <int>(); Assert.Empty(stack); }