Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created November 15, 2012 04:23
Show Gist options
  • Save masaru-b-cl/4076617 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/4076617 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Stack
{
public class EmptyStackException : Exception
{
}
public class Stack
{
private int[] values = new int[10];
public bool IsEmpty
{
get
{
return size == 0;
}
}
public void Push(int value)
{
this.values[size] = value;
size++;
}
public int Top
{
get
{
AssertEmpty();
return this.values[size-1];
}
}
private void AssertEmpty()
{
if (IsEmpty)
{
throw new EmptyStackException();
}
}
private int size;
public int Size
{
get
{
return size;
}
}
public void Pop()
{
AssertEmpty();
size--;
}
}
[TestClass]
public class StackTest
{
private Stack stack;
[TestInitialize]
public void SetUp()
{
stack = new Stack();
}
[TestMethod]
public void Stack作成後は空()
{
Assert.IsTrue(stack.IsEmpty);
}
[TestMethod]
public void Push後にTopで同じ値が取得できる()
{
stack.Push(1);
Assert.AreEqual(1, stack.Top);
Assert.IsFalse(stack.IsEmpty);
}
[TestMethod]
public void Push後にSizeが増える()
{
stack.Push(1);
Assert.AreEqual(1, stack.Size);
stack.Push(1);
Assert.AreEqual(2, stack.Size);
}
[TestMethod]
public void Push前にPopしたら例外発生()
{
try
{
stack.Pop();
Assert.Fail();
}
catch (EmptyStackException)
{
}
}
[TestMethod]
public void Push前にTopしたら例外発生()
{
try
{
var top = stack.Top;
Assert.Fail();
}
catch (EmptyStackException)
{
}
}
[TestMethod]
public void Push後にPopしたら例外なしでTopを取り除く()
{
stack.Push(1);
stack.Pop();
Assert.AreEqual(0, stack.Size);
}
[TestMethod]
public void Stack構造になっている()
{
stack.Push(1);
stack.Push(2);
Assert.AreEqual(2, stack.Top);
stack.Pop();
Assert.AreEqual(1, stack.Top);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment