Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #66 Add Kata Diamond #70

Merged
merged 2 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/com/ordestiny/tdd/kata/Diamond.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ordestiny.tdd.kata;

import java.util.Arrays;

public class Diamond {

public final static char BASE_CHARACTER = 'A';

private Diamond() {

}

/**
* Build a string that represents a diamond based on a given character which is the widest point character in the
* constructed diamond.
*
* @param widestPointChar
* The character that is used to construct the widest points (diagonal) in th diamond.
* @return Constructed diamond string based on the given widest point character.
* @throws IllegalArgumentException
* If the argument provided as the widest point character is out of the A-Z range.
* @implNote For sake of simplicity, spaces and new lines are not included in the Diamond
* string.
*/
public static String buildDiamond(char widestPointChar) throws IllegalArgumentException {
if (!Character.isUpperCase(widestPointChar)) {
throw new IllegalArgumentException("Supplied widest point character is not valid. Only A-Z is accepted.");
}

StringBuilder leftPart = new StringBuilder();
int offset = widestPointChar - BASE_CHARACTER;
for (int i = 0; i < offset; i++) {
for (int j = 0; j < i + 1; j++) {
leftPart.append((char) (BASE_CHARACTER + i));
}
}
char[] diagonalString = new char[offset + 1];
Arrays.fill(diagonalString, widestPointChar);
StringBuilder rightPart = new StringBuilder(leftPart).reverse();
return leftPart + String.valueOf(diagonalString) + rightPart;
}
}
54 changes: 54 additions & 0 deletions src/test/java/com/ordestiny/tdd/kata/DiamondTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ordestiny.tdd.kata;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;

public class DiamondTest {

@Test
public void buildDiamond_whenWidestPointCharIsNull_returnException() {
assertThrows(IllegalArgumentException.class, () -> Diamond.buildDiamond('1'));
}

@Test
public void buildDiamond_validWidestPointChars_returnCorrectDiamondString() {
Map<Character, String> diamonds = new HashMap<>();
diamonds.put('A', "A");
diamonds.put('B', "ABBA");
diamonds.put('C', "ABBCCCBBA");
diamonds.put('D', "ABBCCCDDDDCCCBBA");
diamonds.put('E', "ABBCCCDDDDEEEEEDDDDCCCBBA");
diamonds.forEach(this::verifyDiamondString);
}

private void verifyDiamondString(char widestPointChar, String diamond) {
assertEquals(Diamond.buildDiamond(widestPointChar), diamond);
}

@Test
public void buildDiamond_widestPointCharSetToCharGreaterThanA_returnedValueHasCorrectNumberOfRepetitions() {
// In each generated diamond string, the widest point character is repeated one time more than its distance to
// the base character
for (char widestPointChar = 'B'; widestPointChar <= 'Z'; widestPointChar++) {
//assume
int diagonalLength = widestPointChar - Diamond.BASE_CHARACTER + 1;

//act
String diamondString = Diamond.buildDiamond(widestPointChar);

//assert
char[] diagonal = new char[diagonalLength];
Arrays.fill(diagonal, widestPointChar);

assertTrue(diamondString.contains(String.valueOf(diagonal)));
assertEquals(Math.pow(diagonalLength, 2), diamondString.length());
}
}

}