Created
March 21, 2011 08:40
-
-
Save irof/879185 to your computer and use it in GitHub Desktop.
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
package tddbc; | |
import static org.junit.Assert.*; | |
import static org.hamcrest.CoreMatchers.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.junit.Test; | |
public class FizzBuzzTest { | |
@Test | |
public void 三はFizz() throws Exception { | |
String result = fizzBuzz(3); | |
assertThat(result, is("Fizz")); | |
} | |
@Test | |
public void 三の倍数はFizz() throws Exception { | |
String result = fizzBuzz(6); | |
assertThat(result, is("Fizz")); | |
} | |
@Test | |
public void 五はBuzz() throws Exception { | |
String result = fizzBuzz(5); | |
assertThat(result, is("Buzz")); | |
} | |
@Test | |
public void 五の倍数はBuzz() throws Exception { | |
String result = fizzBuzz(250); | |
assertThat(result, is("Buzz")); | |
result = fizzBuzz(260); | |
assertThat(result, is("Buzz")); | |
} | |
@Test(expected = IllegalArgumentException.class) | |
public void 零の場合例外() throws Exception { | |
fizzBuzz(0); | |
} | |
@Test(expected = IllegalArgumentException.class) | |
public void マイナス値も例外() throws Exception { | |
fizzBuzz(-15); | |
} | |
@Test | |
public void 一の場合1を返す() { | |
String result = fizzBuzz(1); | |
assertThat(result, is("1")); | |
} | |
@Test | |
public void 三の倍数でも五の倍数でもない() throws Exception { | |
String result = fizzBuzz(13); | |
assertThat(result, is("13")); | |
} | |
@Test | |
public void 十五はFizzBuzz() throws Exception { | |
String result = fizzBuzz(15); | |
assertThat(result, is("FizzBuzz")); | |
} | |
@Test | |
public void 十五の倍数はFizzBuzz() throws Exception { | |
String result = fizzBuzz(1500); | |
assertThat(result, is("FizzBuzz")); | |
} | |
@Test | |
public void 閉区間のFizzBuzz_同じ数字() throws Exception { | |
List<String> result = fizzBuzz(1, 1); | |
assertThat(result, is(notNullValue())); | |
assertThat(result.size(), is(1)); | |
assertThat(result.get(0), is("1")); | |
} | |
@Test | |
public void 閉区間のFizzBuzz() throws Exception { | |
List<String> result = fizzBuzz(1, 10); | |
assertThat(result, is(notNullValue())); | |
assertThat(result.size(), is(10)); | |
assertThat(result.get(0), is("1")); | |
assertThat(result.get(1), is("2")); | |
assertThat(result.get(2), is("Fizz")); | |
assertThat(result.get(3), is("4")); | |
assertThat(result.get(4), is("Buzz")); | |
assertThat(result.get(5), is("Fizz")); | |
assertThat(result.get(6), is("7")); | |
assertThat(result.get(7), is("8")); | |
assertThat(result.get(8), is("Fizz")); | |
assertThat(result.get(9), is("Buzz")); | |
} | |
@Test(expected = IllegalArgumentException.class) | |
public void 閉区間のFizzBuzz_第一引数より第二引数のほうが小さい() throws Exception { | |
fizzBuzz(6, 1); | |
} | |
private List<String> fizzBuzz(int i, int j) { | |
if (j < i) throw new IllegalArgumentException(); | |
List<String> list = new ArrayList<String>(); | |
for (int k = i; k <= j; k++) | |
list.add(fizzBuzz(k)); | |
return list; | |
} | |
private String fizzBuzz(int n) { | |
if (n < 1) throw new IllegalArgumentException(); | |
StringBuilder s = new StringBuilder(); | |
if (n % 3 == 0) s.append("Fizz"); | |
if (n % 5 == 0) s.append("Buzz"); | |
if (s.length() == 0) s.append(n); | |
return s.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment