Conversation
3125d75 to
8c940cf
Compare
| using System; | ||
| using Stack; | ||
|
|
||
| namespace StackCalculator; |
There was a problem hiding this comment.
namespace лучше в самом начале писать
| /// </summary> | ||
| public class Calculator | ||
| { | ||
| private IStack<float> Stack = new StackOnLists<float>(); |
There was a problem hiding this comment.
По условию, в коде класса «Стековый калькулятор» не должно быть ни одного упоминания конкретных реализаций стека, даже если очень хочется.
| /// <returns>Expression value</returns> | ||
| public float CountTheExpressionInPostfixForm(string[] inputString) | ||
| { | ||
| int number = 0; |
There was a problem hiding this comment.
Слишком рано объявлен. Надо его объявить в месте инициализации: if (int.TryParse(inputString[i], out int number))
Stack/Stack/IStack.cs
Outdated
| /// <summary> | ||
| /// A class representing the stack interface | ||
| /// </summary> | ||
| abstract public class IStack<T> |
There was a problem hiding this comment.
Это должен был быть интерфейс, а не абстрактный класс
Stack/Stack/IStack.cs
Outdated
| /// Function that returns the number of elements in the stack | ||
| /// </summary> | ||
| /// <returns>Number of elements in stack</returns> | ||
| abstract public int ReturnNumberOfElements(); |
There was a problem hiding this comment.
Идеологически правильнее NumberOfElements();, "Return" тут несколько избыточно.
Stack/Stack/StackOnLists.cs
Outdated
| numberOfElements--; | ||
| return value; | ||
| } | ||
| return default(T); |
There was a problem hiding this comment.
| return default(T); | |
| return default; |
Stack/Stack/StackOnLists.cs
Outdated
| if (head == null) | ||
| { | ||
| throw new StackException("Stack is empty"); | ||
| } | ||
| return head == null ? default(T) : head.Value; |
Stack/StackTest/StackTest.cs
Outdated
| private static IEnumerable<TestCaseData> Stacks | ||
| => new TestCaseData[] | ||
| { | ||
| new TestCaseData(new StackOnArray<int>()), | ||
| new TestCaseData(new StackOnLists<int>()), | ||
| }; |
There was a problem hiding this comment.
| private static IEnumerable<TestCaseData> Stacks | |
| => new TestCaseData[] | |
| { | |
| new TestCaseData(new StackOnArray<int>()), | |
| new TestCaseData(new StackOnLists<int>()), | |
| }; | |
| private static IEnumerable<TestCaseData> Stacks | |
| => new TestCaseData[] | |
| { | |
| new TestCaseData(new StackOnArray<int>()), | |
| new TestCaseData(new StackOnLists<int>()), | |
| }; |
Stack/StackTest/StackTest.cs
Outdated
| public void CheckTopOfTheStackAfterPush(IStack<int> stack) | ||
| { | ||
| stack?.Push(1); | ||
| Assert.AreEqual(stack?.ReturnTopOfTheStack(), 1); |
There was a problem hiding this comment.
Наоборот, сначала Expected, затем Actual. Иначе если тест не пройдёт, сообщение об ошибке будет лживым.
Stack/StackTest/StackTest.cs
Outdated
| [TestCaseSource(nameof(Stacks))] | ||
| public void CheckTopOfEmptyStack(IStack<int> stack) | ||
| { | ||
| var exception = Assert.Throws<StackException>(() => stack?.ReturnTopOfTheStack()); |
There was a problem hiding this comment.
"?" тут не нужен, стек гарантированно не null
--Each class in a separate file --Changed tests --The stack calculator class knows nothing about the stack implementation
yurii-litvinov
left a comment
There was a problem hiding this comment.
Неаккуратно, но по существу всё ок, зачтена.
| } | ||
|
|
||
|
|
||
| [TestCaseSource(nameof(Stacks))] |
There was a problem hiding this comment.
| } | |
| [TestCaseSource(nameof(Stacks))] | |
| } | |
| [TestCaseSource(nameof(Stacks))] |
| /// <summary> | ||
| /// A class for testing a stack calculator | ||
| /// </summary> | ||
| public class TestsStackCalculator |
There was a problem hiding this comment.
| public class TestsStackCalculator | |
| public class StackCalculatorTests |
Так каноничнее
| public void ShouldThrowsDivideByZeroExceptionWhenDivideByZero(IStack<float> stack) | ||
| { | ||
| string[] firstArray = { "123", "0", "/" }; | ||
| Assert.Throws<System.DivideByZeroException>(() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray, stack)); |
There was a problem hiding this comment.
Вообще, калькулятор должен принимать строку, а не массив, но ладно :)
| public void ShouldThrowsInvalidCharacterExceptionWhenExpressionContainsInvalidCharacter(IStack<float> stack) | ||
| { | ||
| string[] firstArray = { "123", "0", "a" }; | ||
| Assert.Throws<InvalidCharacterException> (() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray, stack)); |
There was a problem hiding this comment.
| Assert.Throws<InvalidCharacterException> (() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray, stack)); | |
| Assert.Throws<InvalidCharacterException>(() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray, stack)); |
| } | ||
|
|
||
|
|
||
| [TestCaseSource(nameof(Stacks))] |
There was a problem hiding this comment.
| } | |
| [TestCaseSource(nameof(Stacks))] | |
| } | |
| [TestCaseSource(nameof(Stacks))] |
yurii-litvinov
left a comment
There was a problem hiding this comment.
Сам калькулятор со стеками ещё всё-таки надо немного поправить
| /// Function for counting expressions in postfix form | ||
| /// </summary> | ||
| /// <returns>Expression value</returns> | ||
| public float CountTheExpressionInPostfixForm(string[] inputString, IStack<float> stack) |
There was a problem hiding this comment.
Нет-нет, хочется, чтобы калькулятор принимал строку, а не массив строк
| catch (StackIsEmptyException) | ||
| { | ||
| throw; | ||
| } |
There was a problem hiding this comment.
Если вы думаете, что ваша жизнь бессмысленна, посмотрите на эти строки кода :)
| /// </summary> | ||
| public class IncorrectExpressionException : Exception | ||
| { | ||
| public IncorrectExpressionException() : base() { } |
There was a problem hiding this comment.
Это тоже нечто ненужное. Если в классе не объявлен ни один конструктор, компилятор сам генерирует как раз это.
| { | ||
| if (values != null && numberOfElements == values.Length) | ||
| { | ||
| Array.Resize(ref values, values.Length + 20); |
There was a problem hiding this comment.
Лучше увеличивать сразу вдвое. Иначе как у Вас операция Push работает в среднем за линию, а если стек растёт вдвое, то в среднем за константу.
| } | ||
|
|
||
| numberOfElements++; | ||
| if (values == null) |
There was a problem hiding this comment.
Кажется, nullability-анализ гарантирует, что values не null, так что это довольно бессмысленная проверка
| } | ||
|
|
||
| T topOfSTack = values[numberOfElements - 1]; | ||
| Array.Clear(values, numberOfElements - 1, 1); |
There was a problem hiding this comment.
Это хитрый способ записать values[numberOfElements - 1] = default;
No description provided.