using NUnit Framework namespace Wankuma Sorter Sample Test

  • Slides: 38
Download presentation

最初のテスト (コード) using NUnit. Framework; namespace Wankuma. Sorter. Sample. Test { [Test. Fixture] public

最初のテスト (コード) using NUnit. Framework; namespace Wankuma. Sorter. Sample. Test { [Test. Fixture] public class Wankuma. Sorter. Test { [Test] [Description("初めての NUnit テスト")] public void First. Test() { Assert. Fail("ちゃんと Assert できました! (^^; "); } } } ---- src 00. sln ---- • このテストは失敗します ( レッド ) 失敗させようとして、 失敗した → ちゃんと動いている ! わんくま同盟 名古屋勉強会 #3

テストケース : n = 2 ( テストコード ) [Test] [Description("n=2 のとき。 逆順なら入れ替え。")] public void

テストケース : n = 2 ( テストコード ) [Test] [Description("n=2 のとき。 逆順なら入れ替え。")] public void Sort. Test. With 2 Items() { int[] input = { 5, 3 }; Wankuma. Sorter sorter = new Wankuma. Sorter(); int[] result = sorter. Sort(input); Collection. Assert. Are. Equal( new int[] { 3, 5 }, result ); } ※ ちゃんとテストは失敗しますね? これから実装することに意味がある、 というものです わんくま同盟 名古屋勉強会 #3

テストケース : n = 3 ( 製品コード ) そこで、 for 文で書き直すことにします。 public int[] Sort(int[]

テストケース : n = 3 ( 製品コード ) そこで、 for 文で書き直すことにします。 public int[] Sort(int[] input) { if (input. Length == 1) return input; for (int i = 0; i < (input. Length - 1); i++) { if (input[i] > input[i+1]) { Swap(ref input[i], ref input[i+1]); } } return input; } わんくま同盟 名古屋勉強会 #3

テストプローブの挿入 (製品コード ) • オールグリーンを確認したら、 プローブ を仕込みます。 #if DEBUG public static int Test. Compare.

テストプローブの挿入 (製品コード ) • オールグリーンを確認したら、 プローブ を仕込みます。 #if DEBUG public static int Test. Compare. Count; #endif private static bool Is. Not. In. Order(int lead, int trail ) { #if DEBUG Test. Compare. Count++; #endif return (lead > trail); } わんくま同盟 名古屋勉強会 #3

 • そして、 内側のループに入る前に _swap. Count をゼロ クリアして、出てきたときにゼロのままだったら、 ソ ート終了と判定します。 public int[] Sort(int[] input)

• そして、 内側のループに入る前に _swap. Count をゼロ クリアして、出てきたときにゼロのままだったら、 ソ ート終了と判定します。 public int[] Sort(int[] input) { if (input. Length == 1) return input; } for (int stop = input. Length - 1; stop > 0; stop--) { this. _swap. Count = 0; for (int i = 0; i < stop; i++) { if (Is. Not. In. Order(input[i], input[i + 1])) { Swap(ref input[i], ref input[i + 1]); } } if ( this. _swap. Count == 0 ) break; } return input; // ---- src 04. sln ---- わんくま同盟 名古屋勉強会 #3