Skip to content

Instantly share code, notes, and snippets.

@rohinomiya
Created August 19, 2012 10:31
Show Gist options
  • Save rohinomiya/3394172 to your computer and use it in GitHub Desktop.
Save rohinomiya/3394172 to your computer and use it in GitHub Desktop.
NUnitで自作クラスのユニットテストを行う ref: http://qiita.com/items/47f09523f1b9dfa015b1
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("RenameAlignDigitTest")]
using System;
namespace Rohinomiya
{
public class MyCompare
{
public MyCompare()
{
}
/// <summary>
/// 数値 current がlower ~ higher の範囲内か?
/// </summary>
/// <param name="lower">区間(開始)</param>
/// <param name="current">比較される値</param>
/// <param name="higher">区間(終了)</param>
/// <param name="inclusive">閉区間か?(境界値を含む) (初期値:true)</param>
/// <returns>範囲内であればtrue</returns>
public static bool Between(int lower, int current, int higher, bool inclusive = true)
{
if(lower > higher) Swap(ref lower,ref higher);
return inclusive ?
(lower <= current && current <= higher) :
(lower < current && current < higher);
}
/// <summary>
/// 2つの値を交換する
/// </summary>
/// <param name="a">値A</param>
/// <param name="b">値B</param>
public static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
}
}
using System;
using NUnit.Framework;
namespace Rohinomiya
{
[TestFixture]
public class MyCompareTest
{
// [Test]
// public void SwapTest()
// {
// int a = 3;
// int b = 5;
//
// MyCompare.Swap(ref a,ref b);
// Assert.AreEqual(5,a);
// Assert.AreEqual(3,b);
//
// a = -1;
// b = 0;
//
// MyCompare.Swap(ref a,ref b);
// Assert.AreEqual(0,a);
// Assert.AreEqual(-1,b);
// }
[TestCase(3,5,5,3)]
[TestCase(-1,0,0,-1)]
public void SwapTest(int expectedA, int expectedB, int a, int b)
{
MyCompare.Swap(ref a,ref b);
Assert.AreEqual(expectedA,a);
Assert.AreEqual(expectedB,b);
}
// [Test]
// public void BetweenTest()
// {
// Assert.AreEqual(true,MyCompare.Between(1,2,3));
// Assert.AreEqual(true,MyCompare.Between(1,3,3));
// Assert.AreEqual(true,MyCompare.Between(1,1,3));
//
// Assert.AreEqual(false,MyCompare.Between(1,1,3,false));
// Assert.AreEqual(false,MyCompare.Between(1,3,3,false));
// Assert.AreEqual(true,MyCompare.Between(1,2,3,false));
//
// Assert.AreEqual(true,MyCompare.Between(3,2,1));
// Assert.AreEqual(true,MyCompare.Between(3,3,1));
// Assert.AreEqual(true,MyCompare.Between(3,1,1));
//
// Assert.AreEqual(false,MyCompare.Between(3,1,1,false));
// Assert.AreEqual(false,MyCompare.Between(3,3,1,false));
// Assert.AreEqual(true,MyCompare.Between(3,2,1,false));
// }
[TestCase(true,1,2,3,true)]
[TestCase(true,1,3,3,true)]
[TestCase(true,1,1,3,true)]
[TestCase(false,1,1,3,false)]
[TestCase(false,1,3,3,false)]
[TestCase(true,1,2,3,false)]
[TestCase(true,3,2,1,true)]
[TestCase(true,3,3,1,true)]
[TestCase(true,3,1,1,true)]
[TestCase(false,3,1,1,false)]
[TestCase(false,3,3,1,false)]
[TestCase(true,3,2,1,false)]
public void BetweenTest(bool expected, int lower, int current, int higher, bool inclusive = true)
{
bool result = MyCompare.Between(lower, current, higher, inclusive);
Assert.AreEqual(expected, result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment