Carlos Obregón
Java Champion with 15 years of experience in software programming
import org.junit.*;
public class GameTest {
@Test
public void gutterGame() {
}
}
import org.junit.*;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;
public class GameTest {
@Test
public void gutterGame() {
Game game = new Game();
for (int times = 1; times <= 20; ++times) {
game.roll(0);
}
assertThat(game.score(), is(0));
}
}
public class Game {
public void roll(int pins) {
}
public int score() {
return 0;
}
}
public class Game {
public void roll(int pins) {
}
public int score() {
return 0;
}
}
@Test
public void allOnes() {
Game game = new Game();
for (int times = 1; times <= 20; ++times) {
game.roll(1);
}
assertThat(game.score(), is(20));
}
Expected: is <20>
but: was <0>
at GameTest.allOnes(GameTest.java:25)
public class Game {
private int score = 0;
public void roll(int pins) {
score += pins;
}
public int score() {
return score;
}
}
public class GameTest {
@Test
public void gutterGame() {
Game game = new Game(); // repetition
// repetition and code at a lower
// level of abstraction
for (int times = 1; times <= 20; ++times) {
game.roll(0);
}
assertThat(game.score(), is(0));
}
@Test
public void allOnes() {
Game game = new Game(); // ditto
// ditto
for (int times = 1; times <= 20; ++times) {
game.roll(1);
}
assertThat(game.score(), is(20));
}
}
public class GameTest {
private Game game;
@Before
public void init() {
game = new Game();
}
@Test
public void gutterGame() {
rollMany(20, 0);
assertThat(game.score(), is(0));
}
@Test
public void allOnes() {
rollMany(20, 1);
assertThat(game.score(), is(19));
}
private void rollMany(int times, int pins) {
for (int n = 1; n <= times; ++n) {
game.roll(pins);
}
}
}
@Test
public void oneSpare() {
game.roll(5);
game.roll(5); // spare
game.roll(3);
rollMany(17, 0);
assertThat(game.score(), is(16));
}
//@Test
public void oneSpare() {
game.roll(5);
game.roll(5); // spare
game.roll(3);
rollMany(17, 0);
assertThat(game.score(), is(16));
}
public class Game {
private int[] rolls = new int[21];
private int currentRoll = 0;
public void roll(int pins) {
rolls[currentRoll] = pins;
++currentRoll;
}
public int score() {
int score = 0;
for (int i = 0; i < rolls.length; ++i) {
score += rolls[i];
}
return score;
}
}
@Test
public void oneSpare() {
game.roll(5);
game.roll(5); // spare
game.roll(3);
rollMany(17, 0);
assertThat(game.score(), is(16));
}
public class Game {
private int[] rolls = new int[21];
private int currentRoll = 0;
public void roll(int pins) {
rolls[currentRoll] = pins;
++currentRoll;
}
public int score() {
int score = 0;
for (int i = 0; i < rolls.length; ++i) {
score += rolls[i];
}
return score;
}
}
public class Game {
private int[] rolls = new int[21];
private int currentRoll = 0;
public void roll(int pins) {
rolls[currentRoll] = pins;
++currentRoll;
}
public int score() {
int score = 0;
int frameIndex = 0;
for (int frame = 1; frame < 10; ++frame) {
if (rolls[frameIndex] + rolls[frameIndex + 1] == 10) {
score += 10 + rolls[frameIndex + 2];
} else {
score += rolls[frameIndex] + rolls[frameIndex + 1];
}
frameIndex += 2;
}
return score;
}
}
public class Game {
private int[] rolls = new int[21];
private int currentRoll = 0;
public void roll(int pins) {
rolls[currentRoll] = pins;
++currentRoll;
}
public int score() {
int score = 0;
int frameIndex = 0;
for (int frame = 1; frame < 10; ++frame) {
if (rolls[frameIndex] + rolls[frameIndex + 1] == 10) {
score += 10 + rolls[frameIndex + 2];
} else {
score += rolls[frameIndex] + rolls[frameIndex + 1];
}
frameIndex += 2;
}
return score;
}
}
public int score() { // look ma, is there's an array?!
int score = 0;
int frameIndex = 0;
for (int frame = 1; frame <= 10; ++frame) {
if (isSpare(frameIndex)) {
score += spareBonus(frameIndex);
} else {
score += frameScore(frameIndex);
}
frameIndex += 2;
}
return score;
}
private int spareBonus(int frameIndex) {
return 10 + rolls[frameIndex + 2];
}
private boolean isSpare(int frameIndex) {
return rolls[frameIndex] + rolls[frameIndex + 1] == 10;
}
@Test
public void oneSpare() {
game.roll(5);
game.roll(5); // spare
game.roll(3);
rollMany(17, 0);
assertThat(game.score(), is(16));
}
@Test
public void oneSpare() {
rollSpare();
game.roll(3);
rollMany(17, 0);
assertThat(game.score(), is(16));
}
private void rollSpare() {
game.roll(5);
game.roll(5);
}
@Test
public void oneStrike() {
game.roll(10); // strike
game.roll(3);
game.roll(4);
rollMany(17, 0);
assertThat(game.score(), is(24));
}
public int score() {
int score = 0;
int frameIndex = 0;
for (int frame = 1; frame <= 10; ++frame) {
if (rolls[frameIndex] == 10) {
score += 10 + rolls[frameIndex + 1] + rolls[frameIndex + 2];
++frameIndex;
} else if (isSpare(frameIndex)) {
score += spareBonus(frameIndex);
frameIndex += 2;
} else {
score += frameScore(frameIndex);
frameIndex += 2;
}
}
return score;
}
@Test
public void oneStrike() {
rollStrike();
game.roll(3);
game.roll(4);
rollMany(17, 0);
assertThat(game.score(), is(24));
}
private void rollStrike() {
game.roll(10);
}
public int score() {
int score = 0;
int frameIndex = 0;
for (int frame = 1; frame <= 10; ++frame) {
if (isStrike(frameIndex)) {
score += strikeBonus(frameIndex);
++frameIndex;
} else if (isSpare(frameIndex)) {
score += spareBonus(frameIndex);
frameIndex += 2;
} else {
score += frameScore(frameIndex);
frameIndex += 2;
}
}
return score;
}
private boolean isStrike(int frameIndex) {
return rolls[frameIndex] == 10;
}
private int strikeBonus(int frameIndex) {
return 10 + rolls[frameIndex + 1] + rolls[frameIndex + 2];
}
@Test
public void perfectGame() {
rollMany(12, 10);
assertThat(game.score(), is(300));
}
public int score() {
int score = 0;
int frameIndex = 0;
for (int frame = 1; frame <= 10; ++frame) {
if (isStrike(frameIndex)) {
score += strikeBonus(frameIndex);
++frameIndex;
} else if (isSpare(frameIndex)) {
score += spareBonus(frameIndex);
frameIndex += 2;
} else {
score += frameScore(frameIndex);
frameIndex += 2;
}
}
return score;
}
public static boolean isPalindrome(String s) {
// not really Java, but you get the idea!
return s.equals(s.reverse());
}
@Test
public void anEmptyStringIsPalindrome() {
return assertThat(isPalindrome(""), is(true));
}
@Test
public void aOneLetterStringIsPalindrome() {
return assertThat(isPalindrome("a"), is(true));
}
@Test
public void abIsNotPalindrome() {
return assertThat(isPalindrome("ab"), is(false));
}
@Test
public void radarIsPalindrome() {
return assertThat(isPalindrome("radar"), is(true));
}
@Test
public void notAllWordsThatStartAndEndWithSameCharacterArePalindrome() {
return assertThat(isPalindrome("reader"), is(true));
}
@Test
public void palindromeCheckingIsCaseInsensitive() {
return assertThat(isPalindrome("Radar"), is(true));
}
@Test
public void palindromeCheckingIgnoreWhiteSpace() {
return assertThat(isPalindrome("A man a plan a canal Panama"), is(true));
}
@Test
public void palindromeCheckingIgnoreNoneLetters() {
return assertThat(isPalindrome("A man, a plan, a canal - Panama"), is(true));
}
By Carlos Obregón