From ccfbb60ff25706b6503dafcd81dfe6d5b14060d2 Mon Sep 17 00:00:00 2001 From: htabuchi Date: Tue, 22 Jul 2025 17:52:33 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E2=80=9Cpaiza=E3=81=AE=E5=95=8F=E9=A1=8CB1?= =?UTF-8?q?28=E3=81=AE=E5=9B=9E=E7=AD=94=E3=81=A8=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit a@ v;;:aa assssssssssssssssssssssssssssssssoooooooooooooo --- htabuchi/src/FormatInfo.java | 49 +++++++ htabuchi/src/SimpleCode.java | 197 ++++++++++++++++++++++++++ htabuchi/test/FormatInfoTest.java | 84 +++++++++++ htabuchi/test/SimpleCodeTest.java | 227 ++++++++++++++++++++++++++++++ 4 files changed, 557 insertions(+) create mode 100644 htabuchi/src/FormatInfo.java create mode 100644 htabuchi/src/SimpleCode.java create mode 100644 htabuchi/test/FormatInfoTest.java create mode 100644 htabuchi/test/SimpleCodeTest.java diff --git a/htabuchi/src/FormatInfo.java b/htabuchi/src/FormatInfo.java new file mode 100644 index 0000000..a979c14 --- /dev/null +++ b/htabuchi/src/FormatInfo.java @@ -0,0 +1,49 @@ +/** + * Paiza問題B128:簡易的二次元バーコード
+ * create 2025/07/18 + * + * @author 田渕日奈子 + * @version 1.0 + */ + + +/** + * 表示しやすくコードをまとめる + */ + +public class FormatInfo { + + public FormatInfo(final String oneRowCode, final String secondRowCode, final String thirdRowCode) { + this.oneRowCode = oneRowCode; + this.secondRowCode = secondRowCode; + this.thirdRowCode = thirdRowCode; + } + + private String oneRowCode; + private String secondRowCode; + private String thirdRowCode; + + public String getOneRowCode() { + return this.oneRowCode; + } + + public String getSecondRowCode() { + return this.secondRowCode; + } + + public String getThirdRowCode() { + return this.thirdRowCode; + } + + public void setOneRowCode(String oneRowCode) { + this.oneRowCode = this.oneRowCode + oneRowCode; + } + + public void setSecondRowCode(String secondRowCode) { + this.secondRowCode = this.secondRowCode + secondRowCode; + } + + public void setThirdRowCode(String thirdRowCode) { + this.thirdRowCode = this.thirdRowCode + thirdRowCode; + } +} diff --git a/htabuchi/src/SimpleCode.java b/htabuchi/src/SimpleCode.java new file mode 100644 index 0000000..b34d7b7 --- /dev/null +++ b/htabuchi/src/SimpleCode.java @@ -0,0 +1,197 @@ +/** + * Paiza問題B128:簡易的二次元バーコード
+ * create 2025/07/18 + * + * @author 田渕日奈子 + * @version 1.0 + */ + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Scanner; +import java.util.stream.Collectors; + +public class SimpleCode { + + /** + * 数字の場合分けによる各コード作成 + */ + public enum Num { + NUM_VALUE_1(1), NUM_VALUE_2(2), NUM_VALUE_3(3), NUM_VALUE_4(4), NUM_VALUE_5(5), NUM_VALUE_6(6), NUM_VALUE_7(7), + NUM_VALUE_8(8), NUM_VALUE_9(9); + + Num(final int numValue) { + this.numValue = numValue; + this.numMatrix = new String[ROW][COLUMN]; + + for (int row = 0; row < ROW; row++) { + for (int column = 0; column < COLUMN; column++) { + numMatrix[row][column] = "."; + } + } + + makeCode(numValue); + } + + final private int numValue; + private String[][] numMatrix; + final static int ROW = 3; // コードの縦幅 + final static int COLUMN = 3; // コードの横幅 + + /** + * 数字に応じてコードを生成する + */ + public void makeCode(final int numValue) { + int countRun = 0; // 横幅に到達したか確かめる変数 + + for (int row = 0; row < ROW; row++) { + for (int column = 0; column < COLUMN; column++) { + if (countRun == numValue) + break; + this.numMatrix[row][column] = "#"; + countRun++; + } + if (countRun == numValue) + break; + } + } + + public String[][] getNumMatrix() { + return numMatrix; + } + + public int getNumValue() { + return numValue; + } + + } + + /* メイン関数 */ + public static void main(String[] args) { + SimpleCode simpleCode = new SimpleCode(); + List resultArray = simpleCode.finaLizeCode(simpleCode.scanAllNum()); + List cleanFormat = cleanFormat(resultArray); + displayResult(cleanFormat); + } + + /** + * 数字の情報を読み取る + * + * @return numIntArray コードの値である数字をまとめたList + */ + public List scanAllNum() { + Scanner sc = new Scanner(System.in); + String numSentence = ""; + List numStrArray; // 1文字ずつ格納したString型のList + List numIntArray = null; // 型を変更した後のList + + try { + numSentence = sc.nextLine(); // お題のすべての数字 + + numStrArray = changeStringList(numSentence); + numIntArray = changeListType(numStrArray); + + } catch (NumberFormatException e) { + throw new NumberFormatException("エラー: 入力が見つかりません。"); + } catch (NoSuchElementException e) { + throw new NoSuchElementException("エラー: 要素が見つかりません。"); + } finally { + sc.close(); + } + + return numIntArray; + } + + /** + * 1文字ずつ分割してArrayListに変換 + * + * @param sentence String型の文 + * @return stringList String型の配列 + */ + public List changeStringList(final String sentence) { + final List stringList = Arrays.asList(sentence.split("")); + return stringList; + } + + /** + * Listの型変換 String→int + * + * @param strList String型のList配列 + * @return intList Integer型のList配列 + */ + public List changeListType(final List strList) { + List intList = strList.stream().map(str -> Integer.parseInt(str)).collect(Collectors.toList()); + return intList; + } + + /** + * Numを用いてコードを判別して結果としてListにまとめる + * + * @param numIntArray お題の数字をまとめたList + * @return resultArray お題の数字から導かれたコードのList + */ + public List finaLizeCode(final List numIntArray) { + List resultArray = new ArrayList<>(); // 結果格納用 + for (int numValue : numIntArray) { + for (Num num : Num.values()) { + if (numValue == num.getNumValue()) { + resultArray.add(num.getNumMatrix()); + } + } + } + + return resultArray; + } + + /** + * 3つのコードを1行として表示できるListを生成する + * + * @param resultArray お題の数字から導かれたコードのList + * @return formatInfo 3つのコードを1行表示としたList + */ + public static List cleanFormat(List resultArray) { + final int maxColumn = 3; // 横に並べることができるコードの最大数 + + List formatInfo = new ArrayList<>(); // 結果表示用List + int count = 0; // コード全体の横幅を超えないか数える + + FormatInfo format = new FormatInfo("", "", ""); + + for (String[][] result : resultArray) { + + for (int column = 0; column < maxColumn; column++) { + format.setOneRowCode(result[0][column]); + format.setSecondRowCode(result[1][column]); + format.setThirdRowCode(result[2][column]); + } + count++; + if (count == maxColumn) { + formatInfo.add(format); + format = new FormatInfo("", "", ""); + count = 0; + continue; + } + } + /* 今回の場合はないが偶数分の数字入れられた場合の対処 */ + formatInfo.add(format); + + return formatInfo; + } + + /** + * 結果のコードをまとめたListから横幅にあてはまるように表示を行う + * + * @param resultArray お題の数字から導かれたコードのList + */ + public static void displayResult(final List formatInfo) { + for (FormatInfo oneCodeRow : formatInfo) { + System.out.println(oneCodeRow.getOneRowCode()); + System.out.println(oneCodeRow.getSecondRowCode()); + System.out.println(oneCodeRow.getThirdRowCode()); + } + } + +} diff --git a/htabuchi/test/FormatInfoTest.java b/htabuchi/test/FormatInfoTest.java new file mode 100644 index 0000000..bb55819 --- /dev/null +++ b/htabuchi/test/FormatInfoTest.java @@ -0,0 +1,84 @@ +/** + * Paiza問題B128:簡易的二次元バーコード
+ * テストコード
+ * create 2025/07/22 + * + * @author 田渕日奈子 + * @version 1.0 + */ + + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; + +import B128_htabuchi.FormatInfo; + +@RunWith(Enclosed.class) +public class FormatInfoTest { + + public static class GetTest { + FormatInfo formatInfo; + + @Before + public void setUp() { + formatInfo = new FormatInfo("One", "Second", "Third"); + } + + @Test + public void getOneRowCodeで正しい値を返す() { + assertEquals("One", formatInfo.getOneRowCode()); + } + + @Test + public void getSecondRowCodeで正しい値を返す() { + assertEquals("Second", formatInfo.getSecondRowCode()); + } + + @Test + public void getThirdRowCodeで正しい値を返す() { + assertEquals("Third", formatInfo.getThirdRowCode()); + } + + } + + public static class SetTest { + FormatInfo formatInfo; + String message; + + @Before + public void setUp() { + formatInfo = new FormatInfo("One", "Second", "Third"); + message = "sentence"; + } + + @Test + public void setOneRowCodeで正しい値を返す() { + String expectedSentence = "Onesentence"; + formatInfo.setOneRowCode(message); + + assertEquals(expectedSentence, formatInfo.getOneRowCode()); + } + + @Test + public void setSecondRowCodeで正しい値を返す() { + String expectedSentence = "Secondsentence"; + formatInfo.setSecondRowCode(message); + + assertEquals(expectedSentence, formatInfo.getSecondRowCode()); + } + + @Test + public void setThirdRowCodeで正しい値を返す() { + String expectedSentence = "Thirdsentence"; + formatInfo.setThirdRowCode(message); + + assertEquals(expectedSentence, formatInfo.getThirdRowCode()); + } + + } + +} diff --git a/htabuchi/test/SimpleCodeTest.java b/htabuchi/test/SimpleCodeTest.java new file mode 100644 index 0000000..d48796d --- /dev/null +++ b/htabuchi/test/SimpleCodeTest.java @@ -0,0 +1,227 @@ +/** + * Paiza問題B128:簡易的二次元バーコード
+ * テストコード
+ * create 2025/07/22 + * + * @author 田渕日奈子 + * @version 1.0 + */ + + +import static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Scanner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; + +import B128_htabuchi.FormatInfo; +import B128_htabuchi.SimpleCode; +import B128_htabuchi.SimpleCode.Num; + +@RunWith(Enclosed.class) +public class SimpleCodeTest { + + public static class EnumTest { + SimpleCode simpleCode; + + @Before + public void setUp() { + simpleCode = new SimpleCode(); + } + + @Test + public void enumが正しくgetNumMatrixを生成して返す() { + Num num = Num.NUM_VALUE_1; + final String[][] expectedMatrix = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; + + assertArrayEquals(num.getNumMatrix(), expectedMatrix); + } + + @Test + public void enumが正しくgetNumValueを生成して返す() { + Num num = Num.NUM_VALUE_1; + final int expectedNum = 1; + + assertEquals(expectedNum, num.getNumValue()); + } + } + + public static class ScanAllNumTest { + SimpleCode simpleCode; + Scanner sc; + + @Before + public void setUp() { + simpleCode = new SimpleCode(); + System.setIn(new ByteArrayInputStream("".getBytes())); + } + + @Test(expected = NumberFormatException.class) + public void Scannerに入力値がない() { + final String input = "\n"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + simpleCode.scanAllNum(); + } + + @Test + public void scanAllNumがInteger型のListを返す() { + final String input = "123456"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + + List actualList = simpleCode.scanAllNum(); + final Integer expectedNum = 1; + + assertEquals(expectedNum, actualList.get(0)); + } + + @Test(expected = NoSuchElementException.class) + public void scanAllNumが空白のListを返す() { + final String input = ""; + System.setIn(new ByteArrayInputStream(input.getBytes())); + + simpleCode.scanAllNum(); + } + + } + + public static class ChangeStringListTest { + SimpleCode simpleCode; + + @Before + public void setUp() { + simpleCode = new SimpleCode(); + } + + @Test(expected = NullPointerException.class) + public void changeStringListにnullを渡す_例外を返す場合() { + final String sentence = null; + simpleCode.changeStringList(sentence); + } + + @Test + public void changeStringListに空白を渡す_空のリストを返す場合() { + final String sentence = ""; + List actualList = simpleCode.changeStringList(sentence); + + assertEquals(List.of(""), actualList); + } + + @Test + public void changeStringListに数字文字列を渡す_正常に動作する場合() { + final String sentence = "123"; + List actualList = simpleCode.changeStringList(sentence); + + assertEquals(List.of("1", "2", "3"), actualList); + } + } + + public static class ChangeListTypeTest { + SimpleCode simpleCode; + + @Before + public void setUp() { + simpleCode = new SimpleCode(); + } + + @Test + public void changeStringListに1を渡して正常にコードを返す() { + final List strList = List.of("1"); + List actualList = simpleCode.changeListType(strList); + final List expectedList = List.of(1); + + assertEquals(expectedList, actualList); + } + + @Test(expected = NumberFormatException.class) + public void changeStringListにAを渡して例外を返す() { + final List strList = List.of("A"); + simpleCode.changeListType(strList); + } + + } + + public static class FinaLizeCodeTest { + SimpleCode simpleCode; + + @Before + public void setUp() { + simpleCode = new SimpleCode(); + } + + @Test + public void finaLizeCodeに1を渡して正常にコードを返す() { + final List numIntArray = List.of(1); + List actualArray = simpleCode.finaLizeCode(numIntArray); + final String[][] expectedMatrix = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; + + assertArrayEquals(expectedMatrix, actualArray.get(0)); + } + + @Test + public void finaLizeCodeに0を渡して空白リストを返す_enumに当てはまらない場合() { + final List numIntArray = List.of(0); + List actualArray = simpleCode.finaLizeCode(numIntArray); + + assertTrue(actualArray.isEmpty()); + } + + @Test + public void finaLizeCodeに空リストを渡して空のリストを返す() { + final List numIntArray = List.of(); + List actualArray = simpleCode.finaLizeCode(numIntArray); + + assertTrue(actualArray.isEmpty()); + } + } + + public static class CleanFormatTest { + + @Test + public void cleanFormatに1つの行列を渡して正常にコードを返す() { + final String[][] oneArray = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; + List resultArray = new ArrayList<>(); + resultArray.add(oneArray); + + List actualArray = SimpleCode.cleanFormat(resultArray); + final String expectedOneRowCode = "#.."; + + assertEquals(expectedOneRowCode, actualArray.get(0).getOneRowCode()); + } + + @Test + public void cleanFormatに4つの行列を渡して正常にコードを返す() { + final String[][] oneArray = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; + final String[][] secondArray = { { "#", "#", "." }, { ".", ".", "." }, { ".", ".", "." } }; + final String[][] thirdArray = { { "#", "#", "#" }, { ".", ".", "." }, { ".", ".", "." } }; + final String[][] fourthArray = { { "#", "#", "#" }, { "#", ".", "." }, { ".", ".", "." } }; + List resultArray = new ArrayList<>(); + + resultArray.add(oneArray); + resultArray.add(secondArray); + resultArray.add(thirdArray); + resultArray.add(fourthArray); + + List actualArray = SimpleCode.cleanFormat(resultArray); + final String expectedOneRowCode = "###"; + + assertEquals(expectedOneRowCode, actualArray.get(1).getOneRowCode()); + } + + @Test(expected = NullPointerException.class) + public void cleanFormatにnullの行列を渡して例外を返す() { + List resultArray = null; + SimpleCode.cleanFormat(resultArray); + } + } + + public static class displayResultTest { + /* cleanFormatが正しいリストを生成しているかで確認 */ + } +} -- GitLab From 5e6329eed39c020e84fe5d4395ae127290d2f6ac Mon Sep 17 00:00:00 2001 From: htabuchi Date: Thu, 24 Jul 2025 14:20:08 +0900 Subject: [PATCH 2/2] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB128?= =?UTF-8?q?=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htabuchi/src/FormatInfo.java | 23 ++- htabuchi/src/SimpleCode.java | 120 ++++++++-------- htabuchi/test/FormatInfoTest.java | 30 ++-- htabuchi/test/SimpleCodeTest.java | 224 ++++++++++++++++++++---------- 4 files changed, 232 insertions(+), 165 deletions(-) diff --git a/htabuchi/src/FormatInfo.java b/htabuchi/src/FormatInfo.java index a979c14..6427920 100644 --- a/htabuchi/src/FormatInfo.java +++ b/htabuchi/src/FormatInfo.java @@ -3,28 +3,27 @@ * create 2025/07/18 * * @author 田渕日奈子 - * @version 1.0 + * @version 2.0 */ /** - * 表示しやすくコードをまとめる + * 表示用にコードをまとめるクラス */ - public class FormatInfo { - public FormatInfo(final String oneRowCode, final String secondRowCode, final String thirdRowCode) { - this.oneRowCode = oneRowCode; + public FormatInfo(final String firstRowCode, final String secondRowCode, final String thirdRowCode) { + this.firstRowCode = firstRowCode; this.secondRowCode = secondRowCode; this.thirdRowCode = thirdRowCode; } - private String oneRowCode; + private String firstRowCode; private String secondRowCode; private String thirdRowCode; - public String getOneRowCode() { - return this.oneRowCode; + public String getFirstRowCode() { + return this.firstRowCode; } public String getSecondRowCode() { @@ -35,15 +34,15 @@ public class FormatInfo { return this.thirdRowCode; } - public void setOneRowCode(String oneRowCode) { - this.oneRowCode = this.oneRowCode + oneRowCode; + public void addFirstRowCode(String firstRowCode) { + this.firstRowCode = this.firstRowCode + firstRowCode; } - public void setSecondRowCode(String secondRowCode) { + public void addSecondRowCode(String secondRowCode) { this.secondRowCode = this.secondRowCode + secondRowCode; } - public void setThirdRowCode(String thirdRowCode) { + public void addThirdRowCode(String thirdRowCode) { this.thirdRowCode = this.thirdRowCode + thirdRowCode; } } diff --git a/htabuchi/src/SimpleCode.java b/htabuchi/src/SimpleCode.java index b34d7b7..7785dd7 100644 --- a/htabuchi/src/SimpleCode.java +++ b/htabuchi/src/SimpleCode.java @@ -3,10 +3,11 @@ * create 2025/07/18 * * @author 田渕日奈子 - * @version 1.0 + * @version 2.0 */ +import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -25,46 +26,44 @@ public class SimpleCode { Num(final int numValue) { this.numValue = numValue; - this.numMatrix = new String[ROW][COLUMN]; - - for (int row = 0; row < ROW; row++) { - for (int column = 0; column < COLUMN; column++) { - numMatrix[row][column] = "."; - } - } + this.numMatrix = new String[][]{ + {".",".","."}, + {".",".","."}, + {".",".","."} + }; makeCode(numValue); } - final private int numValue; + private int numValue; private String[][] numMatrix; - final static int ROW = 3; // コードの縦幅 - final static int COLUMN = 3; // コードの横幅 - + private final int rowCode = 3; // コードの縦幅 + private final int columnCode = 3; // コードの横幅 + /** * 数字に応じてコードを生成する */ public void makeCode(final int numValue) { - int countRun = 0; // 横幅に到達したか確かめる変数 - - for (int row = 0; row < ROW; row++) { - for (int column = 0; column < COLUMN; column++) { - if (countRun == numValue) + int replacedCount = 0; // 「.」を「#」に置き換えた回数 + + for (int row = 0; row < rowCode; row++) { + for (int column = 0; column < columnCode; column++) { + if (replacedCount == numValue) break; this.numMatrix[row][column] = "#"; - countRun++; + replacedCount++; } - if (countRun == numValue) + if (replacedCount == numValue) break; } } public String[][] getNumMatrix() { - return numMatrix; + return this.numMatrix; } public int getNumValue() { - return numValue; + return this.numValue; } } @@ -72,37 +71,32 @@ public class SimpleCode { /* メイン関数 */ public static void main(String[] args) { SimpleCode simpleCode = new SimpleCode(); - List resultArray = simpleCode.finaLizeCode(simpleCode.scanAllNum()); - List cleanFormat = cleanFormat(resultArray); - displayResult(cleanFormat); + List resultList = simpleCode.summarizeCode(simpleCode.scanAllNum(System.in)); + List cleanFormat = cleanFormat(resultList); + simpleCode.displayResult(cleanFormat); } /** * 数字の情報を読み取る * - * @return numIntArray コードの値である数字をまとめたList + * @inputStream inputStream 入力ストリーム + * @return numIntList コードの値である数字をまとめたList */ - public List scanAllNum() { - Scanner sc = new Scanner(System.in); - String numSentence = ""; - List numStrArray; // 1文字ずつ格納したString型のList - List numIntArray = null; // 型を変更した後のList + public List scanAllNum(InputStream inputStream) { + List numIntList = new ArrayList<>(); // 型を変更した後のList - try { - numSentence = sc.nextLine(); // お題のすべての数字 - - numStrArray = changeStringList(numSentence); - numIntArray = changeListType(numStrArray); + try (Scanner sc = new Scanner(inputStream)){ + final String numSentence = sc.nextLine(); // お題のすべての数字 + final List numStrList = changeStringList(numSentence); // 1文字ずつ格納したString型のList + numIntList = changeListType(numStrList); } catch (NumberFormatException e) { - throw new NumberFormatException("エラー: 入力が見つかりません。"); + throw new IllegalArgumentException("エラー: 数字以外が入力されました。", e); } catch (NoSuchElementException e) { - throw new NoSuchElementException("エラー: 要素が見つかりません。"); - } finally { - sc.close(); - } + throw new IllegalArgumentException("エラー: 入力が見つかりません。", e); + } - return numIntArray; + return numIntList; } /** @@ -130,55 +124,53 @@ public class SimpleCode { /** * Numを用いてコードを判別して結果としてListにまとめる * - * @param numIntArray お題の数字をまとめたList - * @return resultArray お題の数字から導かれたコードのList + * @param numIntList お題の数字をまとめたList + * @return resultList お題の数字から導かれたコードのList */ - public List finaLizeCode(final List numIntArray) { - List resultArray = new ArrayList<>(); // 結果格納用 - for (int numValue : numIntArray) { + public List summarizeCode(final List numIntList) { + List resultList = new ArrayList<>(); // 結果格納用 + for (int numValue : numIntList) { for (Num num : Num.values()) { if (numValue == num.getNumValue()) { - resultArray.add(num.getNumMatrix()); + resultList.add(num.getNumMatrix()); } } } - return resultArray; + return resultList; } /** * 3つのコードを1行として表示できるListを生成する * - * @param resultArray お題の数字から導かれたコードのList - * @return formatInfo 3つのコードを1行表示としたList + * @param resultList お題の数字から導かれたコードのList + * @return formatInfoList 3つのコードを1行表示としたList */ - public static List cleanFormat(List resultArray) { + public static List cleanFormat(final List resultList) { final int maxColumn = 3; // 横に並べることができるコードの最大数 - List formatInfo = new ArrayList<>(); // 結果表示用List + List formatInfoList = new ArrayList<>(); // 結果表示用List int count = 0; // コード全体の横幅を超えないか数える FormatInfo format = new FormatInfo("", "", ""); - for (String[][] result : resultArray) { + for (String[][] result : resultList) { for (int column = 0; column < maxColumn; column++) { - format.setOneRowCode(result[0][column]); - format.setSecondRowCode(result[1][column]); - format.setThirdRowCode(result[2][column]); + format.addFirstRowCode(result[0][column]); + format.addSecondRowCode(result[1][column]); + format.addThirdRowCode(result[2][column]); } count++; if (count == maxColumn) { - formatInfo.add(format); + formatInfoList.add(format); format = new FormatInfo("", "", ""); count = 0; continue; } } - /* 今回の場合はないが偶数分の数字入れられた場合の対処 */ - formatInfo.add(format); - - return formatInfo; + + return formatInfoList; } /** @@ -186,9 +178,9 @@ public class SimpleCode { * * @param resultArray お題の数字から導かれたコードのList */ - public static void displayResult(final List formatInfo) { - for (FormatInfo oneCodeRow : formatInfo) { - System.out.println(oneCodeRow.getOneRowCode()); + public void displayResult(final List formatInfoList) { + for (FormatInfo oneCodeRow : formatInfoList) { + System.out.println(oneCodeRow.getFirstRowCode()); System.out.println(oneCodeRow.getSecondRowCode()); System.out.println(oneCodeRow.getThirdRowCode()); } diff --git a/htabuchi/test/FormatInfoTest.java b/htabuchi/test/FormatInfoTest.java index bb55819..1e46094 100644 --- a/htabuchi/test/FormatInfoTest.java +++ b/htabuchi/test/FormatInfoTest.java @@ -4,7 +4,7 @@ * create 2025/07/22 * * @author 田渕日奈子 - * @version 1.0 + * @version 2.0 */ @@ -25,21 +25,21 @@ public class FormatInfoTest { @Before public void setUp() { - formatInfo = new FormatInfo("One", "Second", "Third"); + formatInfo = new FormatInfo("First", "Second", "Third"); } @Test - public void getOneRowCodeで正しい値を返す() { - assertEquals("One", formatInfo.getOneRowCode()); + public void getFirstRowCodeで正しい値を返す_Firstを返す場合() { + assertEquals("First", formatInfo.getFirstRowCode()); } @Test - public void getSecondRowCodeで正しい値を返す() { + public void getSecondRowCodeで正しい値を返す_Secondを返す場合() { assertEquals("Second", formatInfo.getSecondRowCode()); } @Test - public void getThirdRowCodeで正しい値を返す() { + public void getThirdRowCodeで正しい値を返す_Thirdを返す場合() { assertEquals("Third", formatInfo.getThirdRowCode()); } @@ -51,30 +51,30 @@ public class FormatInfoTest { @Before public void setUp() { - formatInfo = new FormatInfo("One", "Second", "Third"); + formatInfo = new FormatInfo("First", "Second", "Third"); message = "sentence"; } @Test - public void setOneRowCodeで正しい値を返す() { - String expectedSentence = "Onesentence"; - formatInfo.setOneRowCode(message); + public void setFirstRowCodeで正しい値を返す__Firstにsentenceをつなげる場合() { + String expectedSentence = "Firstsentence"; + formatInfo.addFirstRowCode(message); - assertEquals(expectedSentence, formatInfo.getOneRowCode()); + assertEquals(expectedSentence, formatInfo.getFirstRowCode()); } @Test - public void setSecondRowCodeで正しい値を返す() { + public void setSecondRowCodeで正しい値を返す__Secondにsentenceをつなげる場合() { String expectedSentence = "Secondsentence"; - formatInfo.setSecondRowCode(message); + formatInfo.addSecondRowCode(message); assertEquals(expectedSentence, formatInfo.getSecondRowCode()); } @Test - public void setThirdRowCodeで正しい値を返す() { + public void setThirdRowCodeで正しい値を返す__Thirdにsentenceをつなげる場合() { String expectedSentence = "Thirdsentence"; - formatInfo.setThirdRowCode(message); + formatInfo.addThirdRowCode(message); assertEquals(expectedSentence, formatInfo.getThirdRowCode()); } diff --git a/htabuchi/test/SimpleCodeTest.java b/htabuchi/test/SimpleCodeTest.java index d48796d..f0b1067 100644 --- a/htabuchi/test/SimpleCodeTest.java +++ b/htabuchi/test/SimpleCodeTest.java @@ -4,21 +4,26 @@ * create 2025/07/22 * * @author 田渕日奈子 - * @version 1.0 + * @version 2.0 */ import static org.junit.Assert.*; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; -import java.util.NoSuchElementException; -import java.util.Scanner; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.experimental.runners.Enclosed; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; import B128_htabuchi.FormatInfo; @@ -28,65 +33,84 @@ import B128_htabuchi.SimpleCode.Num; @RunWith(Enclosed.class) public class SimpleCodeTest { + @RunWith(Theories.class) public static class EnumTest { - SimpleCode simpleCode; - - @Before - public void setUp() { - simpleCode = new SimpleCode(); + + static class EnumInfo{ + private Num actualEnum; + private int expectedValue; + private String[][] expectedMatrix; + + public EnumInfo(final Num actualEnum, final int expectedValue, final String[][] expectedMatrix) { + this.actualEnum = actualEnum; + this.expectedValue = expectedValue; + this.expectedMatrix = expectedMatrix; + } } - @Test - public void enumが正しくgetNumMatrixを生成して返す() { - Num num = Num.NUM_VALUE_1; - final String[][] expectedMatrix = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; - - assertArrayEquals(num.getNumMatrix(), expectedMatrix); + @DataPoints + public static EnumInfo[] enumParams = { + new EnumInfo(Num.NUM_VALUE_1, 1, + new String[][] { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_2, 2, + new String[][] { { "#", "#", "." }, { ".", ".", "." }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_3, 3, + new String[][] { { "#", "#", "#" }, { ".", ".", "." }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_4, 4, + new String[][] { { "#", "#", "#" }, { "#", ".", "." }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_5, 5, + new String[][] { { "#", "#", "#" }, { "#", "#", "." }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_6, 6, + new String[][] { { "#", "#", "#" }, { "#", "#", "#" }, { ".", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_7, 7, + new String[][] { { "#", "#", "#" }, { "#", "#", "#" }, { "#", ".", "." } }), + new EnumInfo(Num.NUM_VALUE_8, 8, + new String[][] { { "#", "#", "#" }, { "#", "#", "#" }, { "#", "#", "." } }), + new EnumInfo(Num.NUM_VALUE_9, 9, + new String[][] { { "#", "#", "#" }, { "#", "#", "#" }, { "#", "#", "#" } }) }; + + @Theory + public void enumが正しくgetNumMatrixを生成して返す(EnumInfo param) throws Exception{ + assertArrayEquals(param.expectedMatrix, param.actualEnum.getNumMatrix()); } - @Test - public void enumが正しくgetNumValueを生成して返す() { - Num num = Num.NUM_VALUE_1; - final int expectedNum = 1; - - assertEquals(expectedNum, num.getNumValue()); + @Theory + public void enumが正しくgetNumValueを生成して返す(EnumInfo param) throws Exception{ + assertEquals(param.expectedValue, param.actualEnum.getNumValue()); } } public static class ScanAllNumTest { SimpleCode simpleCode; - Scanner sc; @Before public void setUp() { simpleCode = new SimpleCode(); - System.setIn(new ByteArrayInputStream("".getBytes())); } - @Test(expected = NumberFormatException.class) - public void Scannerに入力値がない() { + @Test(expected = IllegalArgumentException.class) + public void Scannerに例外を返す_入力値がない場合() { final String input = "\n"; - System.setIn(new ByteArrayInputStream(input.getBytes())); - simpleCode.scanAllNum(); + final byte[] actualBytes = input.getBytes(StandardCharsets.UTF_8); + simpleCode.scanAllNum(new ByteArrayInputStream(actualBytes)); } @Test public void scanAllNumがInteger型のListを返す() { final String input = "123456"; - System.setIn(new ByteArrayInputStream(input.getBytes())); + final byte[] actualBytes = input.getBytes(StandardCharsets.UTF_8); + final List actualList = simpleCode.scanAllNum(new ByteArrayInputStream(actualBytes)); - List actualList = simpleCode.scanAllNum(); - final Integer expectedNum = 1; + final List expectedNum = List.of(1, 2, 3, 4, 5, 6); - assertEquals(expectedNum, actualList.get(0)); + assertEquals(expectedNum, actualList); } - @Test(expected = NoSuchElementException.class) - public void scanAllNumが空白のListを返す() { + @Test(expected = IllegalArgumentException.class) + public void scanAllNumが例外を返す_値がない場合() { final String input = ""; - System.setIn(new ByteArrayInputStream(input.getBytes())); - - simpleCode.scanAllNum(); + final byte[] actualBytes = input.getBytes(StandardCharsets.UTF_8); + simpleCode.scanAllNum(new ByteArrayInputStream(actualBytes)); } } @@ -108,7 +132,7 @@ public class SimpleCodeTest { @Test public void changeStringListに空白を渡す_空のリストを返す場合() { final String sentence = ""; - List actualList = simpleCode.changeStringList(sentence); + final List actualList = simpleCode.changeStringList(sentence); assertEquals(List.of(""), actualList); } @@ -116,7 +140,7 @@ public class SimpleCodeTest { @Test public void changeStringListに数字文字列を渡す_正常に動作する場合() { final String sentence = "123"; - List actualList = simpleCode.changeStringList(sentence); + final List actualList = simpleCode.changeStringList(sentence); assertEquals(List.of("1", "2", "3"), actualList); } @@ -131,16 +155,16 @@ public class SimpleCodeTest { } @Test - public void changeStringListに1を渡して正常にコードを返す() { + public void changeStringListが正常にコードを返す_1を渡した場合() { final List strList = List.of("1"); - List actualList = simpleCode.changeListType(strList); + final List actualList = simpleCode.changeListType(strList); final List expectedList = List.of(1); assertEquals(expectedList, actualList); } @Test(expected = NumberFormatException.class) - public void changeStringListにAを渡して例外を返す() { + public void changeStringListが例外を返す_Aを渡した場合() { final List strList = List.of("A"); simpleCode.changeListType(strList); } @@ -156,62 +180,76 @@ public class SimpleCodeTest { } @Test - public void finaLizeCodeに1を渡して正常にコードを返す() { - final List numIntArray = List.of(1); - List actualArray = simpleCode.finaLizeCode(numIntArray); + public void summarizeCode正常にコードを返す_1を渡した場合() { + final List numIntList = List.of(1); + final List actualList = simpleCode.summarizeCode(numIntList); final String[][] expectedMatrix = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; - assertArrayEquals(expectedMatrix, actualArray.get(0)); + assertArrayEquals(expectedMatrix, actualList.get(0)); } @Test - public void finaLizeCodeに0を渡して空白リストを返す_enumに当てはまらない場合() { - final List numIntArray = List.of(0); - List actualArray = simpleCode.finaLizeCode(numIntArray); + public void summarizeCodeが空白リストを返す_0を渡してenumに当てはまらない場合() { + final List numIntList = List.of(0); + final List actualList = simpleCode.summarizeCode(numIntList); - assertTrue(actualArray.isEmpty()); + assertTrue(actualList.isEmpty()); } @Test - public void finaLizeCodeに空リストを渡して空のリストを返す() { - final List numIntArray = List.of(); - List actualArray = simpleCode.finaLizeCode(numIntArray); + public void summarizeCodeが空のリストを返す_空リストを渡した場合() { + final List numIntList = List.of(); + final List actualList = simpleCode.summarizeCode(numIntList); - assertTrue(actualArray.isEmpty()); + assertTrue(actualList.isEmpty()); } } public static class CleanFormatTest { @Test - public void cleanFormatに1つの行列を渡して正常にコードを返す() { - final String[][] oneArray = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; - List resultArray = new ArrayList<>(); - resultArray.add(oneArray); + public void cleanFormatに3つの行列を渡して正常にコードを返す() { + final String[][] firstArray = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; + final String[][] secondArray = { { "#", "#", "." }, { ".", ".", "." }, { ".", ".", "." } }; + final String[][] thirdArray = { { "#", "#", "#" }, { ".", ".", "." }, { ".", ".", "." } }; + final List resultList = new ArrayList<>(); + resultList.add(firstArray); + resultList.add(secondArray); + resultList.add(thirdArray); - List actualArray = SimpleCode.cleanFormat(resultArray); - final String expectedOneRowCode = "#.."; + final List actualArray = SimpleCode.cleanFormat(resultList); - assertEquals(expectedOneRowCode, actualArray.get(0).getOneRowCode()); + assertEquals("#..##.###", actualArray.get(0).getFirstRowCode()); + assertEquals(".........", actualArray.get(0).getSecondRowCode()); + assertEquals(".........", actualArray.get(0).getThirdRowCode()); } @Test - public void cleanFormatに4つの行列を渡して正常にコードを返す() { - final String[][] oneArray = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; + public void cleanFormatに6つの行列を渡して正常にコードを返す() { + final String[][] firstArray = { { "#", ".", "." }, { ".", ".", "." }, { ".", ".", "." } }; final String[][] secondArray = { { "#", "#", "." }, { ".", ".", "." }, { ".", ".", "." } }; final String[][] thirdArray = { { "#", "#", "#" }, { ".", ".", "." }, { ".", ".", "." } }; final String[][] fourthArray = { { "#", "#", "#" }, { "#", ".", "." }, { ".", ".", "." } }; - List resultArray = new ArrayList<>(); - - resultArray.add(oneArray); - resultArray.add(secondArray); - resultArray.add(thirdArray); - resultArray.add(fourthArray); - - List actualArray = SimpleCode.cleanFormat(resultArray); - final String expectedOneRowCode = "###"; - - assertEquals(expectedOneRowCode, actualArray.get(1).getOneRowCode()); + final String[][] fifthArray = { { "#", "#", "#" }, { "#", "#", "." }, { ".", ".", "." } }; + final String[][] sixthArray = { { "#", "#", "#" }, { "#", "#", "#" }, { ".", ".", "." } }; + + List resultList = new ArrayList<>(); + + resultList.add(firstArray); + resultList.add(secondArray); + resultList.add(thirdArray); + resultList.add(fourthArray); + resultList.add(fifthArray); + resultList.add(sixthArray); + + List actualArray = SimpleCode.cleanFormat(resultList); + + assertEquals("#..##.###", actualArray.get(0).getFirstRowCode()); + assertEquals(".........", actualArray.get(0).getSecondRowCode()); + assertEquals(".........", actualArray.get(0).getThirdRowCode()); + assertEquals("#########", actualArray.get(1).getFirstRowCode()); + assertEquals("#..##.###", actualArray.get(1).getSecondRowCode()); + assertEquals(".........", actualArray.get(1).getThirdRowCode()); } @Test(expected = NullPointerException.class) @@ -221,7 +259,45 @@ public class SimpleCodeTest { } } - public static class displayResultTest { - /* cleanFormatが正しいリストを生成しているかで確認 */ + public static class DisplayResultTest { + private final PrintStream standardOut = System.out; + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + FormatInfo formatInfo; + SimpleCode simpleCode; + + @Before + public void setUp() { + System.setOut(new PrintStream(outputStreamCaptor)); + simpleCode = new SimpleCode(); + } + + @Test + public void displayResultが正しい表示をする_3つのコードの場合() { + List formatInfoList = new ArrayList<>(); + formatInfoList.add(new FormatInfo("#..##.###",".........",".........")); + + simpleCode.displayResult(formatInfoList); + + assertEquals("#..##.###\r\n.........\r\n.........\r\n", outputStreamCaptor.toString()); + + } + + @Test + public void displayResultが正しい表示をする_6つのコードの場合() { + List formatInfoList = new ArrayList<>(); + formatInfoList.add(new FormatInfo("#..##.###",".........",".........")); + formatInfoList.add(new FormatInfo("#########","#..##.###",".........")); + + simpleCode.displayResult(formatInfoList); + + assertEquals("#..##.###\r\n.........\r\n.........\r\n#########\r\n#..##.###\r\n.........\r\n", outputStreamCaptor.toString()); + + } + + @After + public void tearDown() { + System.setOut(standardOut); + } } } -- GitLab