Last active
December 10, 2015 21:08
-
-
Save disktnk/4492983 to your computer and use it in GitHub Desktop.
JUnit実践入門を読んでいて,Theoryアノテーションを試してみたいと思ってたところ,ちょうどよさげな問題を解いたブログ「3年間の進歩 - uehaj's blog」( http://uehaj.hatenablog.com/entry/2013/01/09/125407 ) がポストされていたので,やってみた.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Cards { | |
public String[] deal(int playerNum, String deck) { | |
if (playerNum < 1 || deck == null) { | |
return null; | |
} | |
String[] results = new String[playerNum]; | |
Arrays.fill(results, ""); | |
int mod = deck.length() % playerNum; | |
for (int i = 0; i < deck.length() - mod; i++) { | |
int playerId = i % playerNum; | |
results[playerId] += deck.charAt(i); | |
} | |
return results; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.hamcrest.CoreMatchers.is; | |
import static org.hamcrest.CoreMatchers.nullValue; | |
import static org.junit.Assert.assertThat; | |
import org.junit.Test; | |
import org.junit.experimental.theories.DataPoints; | |
import org.junit.experimental.theories.Theories; | |
import org.junit.experimental.theories.Theory; | |
import org.junit.runner.RunWith; | |
@RunWith(Theories.class) | |
public class CardsTest { | |
@DataPoints | |
public static Fixture[] DATAS = new Fixture[] { | |
new Fixture(6, "012345012345012345", | |
new String[] { "000", "111", "222", "333", "444", "555" }), | |
new Fixture(4, "111122223333", new String[] { "123", "123", "123", "123" }), | |
new Fixture(1, "012345012345012345", new String[] { "012345012345012345" }), | |
new Fixture(6, "01234", new String[] { "", "", "", "", "", "" }), | |
new Fixture(2, "", new String[] { "", "" }), }; | |
@Theory | |
public void testDeal(Fixture f) { | |
Cards sut = new Cards(); | |
String[] actual = sut.deal(f.playerNum, f.deck); | |
assertThat(actual, is(f.expected)); | |
} | |
@Test | |
public void testDealForEmpty() { | |
Cards sut = new Cards(); | |
String[] actual = sut.deal(0, "1234"); | |
assertThat(actual, is(nullValue())); | |
String[] actual2 = sut.deal(1, null); | |
assertThat(actual2, is(nullValue())); | |
} | |
static class Fixture { | |
final int playerNum; | |
final String deck; | |
final String[] expected; | |
public Fixture(int playerNum, String deck, String[] expected) { | |
this.playerNum = playerNum; | |
this.deck = deck; | |
this.expected = expected; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment