From 6b4f9cb0dbde15cfc4ad50fa5a87182a0efe6f6f Mon Sep 17 00:00:00 2001 From: gyim1345 Date: Fri, 5 Jun 2020 22:22:07 +0900 Subject: [PATCH 1/2] Step 1 test learning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1단계 학습테스트 연습 및 실습 --- .../CharMethods.java" | 7 +++ .../SetMethods.java" | 10 +++ .../StringMethods.java" | 11 ++++ .../CharMethodsTest.java" | 51 ++++++++++++++++ .../SetMethodsTest.java" | 61 +++++++++++++++++++ .../StringMethodsTest.java" | 44 +++++++++++++ test | 1 + 7 files changed, 185 insertions(+) create mode 100644 "src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/CharMethods.java" create mode 100644 "src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/SetMethods.java" create mode 100644 "src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/StringMethods.java" create mode 100644 "src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/CharMethodsTest.java" create mode 100644 "src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/SetMethodsTest.java" create mode 100644 "src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/StringMethodsTest.java" create mode 100644 test diff --git "a/src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/CharMethods.java" "b/src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/CharMethods.java" new file mode 100644 index 0000000..61e7c80 --- /dev/null +++ "b/src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/CharMethods.java" @@ -0,0 +1,7 @@ +package Step1_학습테스트; + +public class CharMethods { + public char charAt(String string, int index) { + return string.charAt(index); + } +} diff --git "a/src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/SetMethods.java" "b/src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/SetMethods.java" new file mode 100644 index 0000000..3449768 --- /dev/null +++ "b/src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/SetMethods.java" @@ -0,0 +1,10 @@ +package Step1_학습테스트; + +import java.util.Set; + +public class SetMethods { + public int getSize(Set set) { + return set.size(); + } + +} diff --git "a/src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/StringMethods.java" "b/src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/StringMethods.java" new file mode 100644 index 0000000..5f6335e --- /dev/null +++ "b/src/main/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/StringMethods.java" @@ -0,0 +1,11 @@ +package Step1_학습테스트; + +public class StringMethods { + public String[] splitStrings(String string) { + return string.split(""); + } + + public static String subString(String string, int a, int b) { + return string.substring(a,b); + } +} diff --git "a/src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/CharMethodsTest.java" "b/src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/CharMethodsTest.java" new file mode 100644 index 0000000..34b5431 --- /dev/null +++ "b/src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/CharMethodsTest.java" @@ -0,0 +1,51 @@ +package Step1_학습테스트; + +import Step1_학습테스트.CharMethods; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +@DisplayName("Char Methods 테스트") +public class CharMethodsTest { + private CharMethods charMethods; + @BeforeEach + void setUp() { + charMethods = new CharMethods(); + } + + @DisplayName("charAt 테스트") + @ParameterizedTest + @CsvSource(value = {"abc:0:a", "abc:1:b", "abc:2:c"}, delimiter = ':') + public void charIOTest(String string,int index, char expected) { + assertThat(charMethods.charAt(string, index)).isEqualTo(expected); + } + + @DisplayName("charAt Error Index out of bound 테스트") + @Test + public void charErrorTest() { + + assertThatThrownBy(() -> { + charMethods.charAt("abc", 3); + }).isInstanceOf(IndexOutOfBoundsException.class) + .hasMessageContaining("String index out of range: 3"); + +// Throwable thrown = catchThrowable(() -> { throw new Exception("String index out of range: 3"); }); +// assertThat(thrown).isInstanceOf(Exception.class) .hasMessageContaining("String index out of range: 3"); + + assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> charMethods.charAt("abc", 3)) + .withMessage("String index out of range: 3") + .withMessageContaining("String") + .withNoCause(); + +// assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> { throw new IndexOutOfBoundsException("NO!"); }) +// .withMessage("NO!") +// .withMessageContaining("NO") +// .withNoCause(); + + } +} diff --git "a/src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/SetMethodsTest.java" "b/src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/SetMethodsTest.java" new file mode 100644 index 0000000..30deb2b --- /dev/null +++ "b/src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/SetMethodsTest.java" @@ -0,0 +1,61 @@ +package Step1_학습테스트; + +import Step1_학습테스트.SetMethods; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; +import static org.junit.jupiter.api.Assertions.assertTrue; + + +import java.util.HashSet; +import java.util.Set; + +import static org.assertj.core.api.Assertions.*; + +@DisplayName("set Method Test") +public class SetMethodsTest { + private Set numbers; + private SetMethods setMethods; + @BeforeEach + void setUp() { + setMethods = new SetMethods(); + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + @DisplayName("set 사이즈 테스트") + @Test + public void setSizeTest() { + int setSize = setMethods.getSize(numbers); + assertThat(setSize).isEqualTo(3); + } + + @DisplayName("set 내용물 확인 테스트") + @ParameterizedTest + @ValueSource(ints = {1,1,2,3}) + public void setContainsTest(int number) { + assertThat(numbers.contains(number)).isTrue(); + } + +// @Test +// public void setThrowErrorTest() { +// assertThat(numbers.contains(true)).isEqualTo(false); +// assertThat(numbers.contains(4)).isEqualTo(false); +// assertThat(numbers.contains(5)).isEqualTo(false); +// } + + @DisplayName("set 입력 출력 테스트") + @ParameterizedTest + @CsvSource(value = {"1:true", "2:true", "3:true", "4:false", "5:false" }, delimiter = ':') + public void setIOTest(int input, boolean expected) { + assertThat(numbers.contains(input)).isEqualTo(expected); + + } + +} diff --git "a/src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/StringMethodsTest.java" "b/src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/StringMethodsTest.java" new file mode 100644 index 0000000..724a4d8 --- /dev/null +++ "b/src/test/java/Step1_\355\225\231\354\212\265\355\205\214\354\212\244\355\212\270/StringMethodsTest.java" @@ -0,0 +1,44 @@ +package Step1_학습테스트; + +import Step1_학습테스트.StringMethods; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +@DisplayName("String Method 테스트") +public class StringMethodsTest { + private StringMethods stringMethods; + + @BeforeEach + void setUp() { + stringMethods = new StringMethods(); + } + + @DisplayName("String split 테스트") + @Test + public void splitStringTest() { + String[] splittedString = stringMethods.splitStrings("12"); + assertThat(splittedString).contains("1","2"); + assertThat(splittedString).containsExactly("1","2"); + assertThat(splittedString).isEqualTo(new String[]{"1","2"}); + + String[] splittedString1 = stringMethods.splitStrings("1"); + assertThat(splittedString1).isEqualTo(new String[]{"1"}); + assertThat(splittedString1).contains("1"); + assertThat(splittedString1).containsExactly("1"); + + } + + @DisplayName("String subString 테스트") + @Test + public void subStringTest() { + String modifiedString = stringMethods.subString("(1,2)", 1,4); + assertThat(modifiedString).isEqualTo("1,2"); + assertThat(modifiedString).doesNotContain("("); + assertThat(modifiedString).doesNotContain(")"); + } +} + + diff --git a/test b/test new file mode 100644 index 0000000..028e2d8 --- /dev/null +++ b/test @@ -0,0 +1 @@ +test pull request \ No newline at end of file From fe3d2696418ebc152704396c2e17d6b6407c9a72 Mon Sep 17 00:00:00 2001 From: gyim1345 Date: Fri, 5 Jun 2020 22:32:12 +0900 Subject: [PATCH 2/2] Step 2 Calculator --- .../Step2_Calculator/ArithmeticOperation.java | 41 ++++++++++++ .../java/Step2_Calculator/Calculator.java | 17 +++++ .../java/Step2_Calculator/CalculatorApp.java | 11 +++ .../Step2_Calculator/InputErrorHandler.java | 17 +++++ .../java/Step2_Calculator/InputExtactor.java | 7 ++ src/main/java/Step2_Calculator/InputView.java | 16 +++++ .../java/Step2_Calculator/OutputView.java | 9 +++ src/main/java/empty.txt | 1 + .../java/Step2_Calculator/CalculatorTest.java | 67 +++++++++++++++++++ 9 files changed, 186 insertions(+) create mode 100644 src/main/java/Step2_Calculator/ArithmeticOperation.java create mode 100644 src/main/java/Step2_Calculator/Calculator.java create mode 100644 src/main/java/Step2_Calculator/CalculatorApp.java create mode 100644 src/main/java/Step2_Calculator/InputErrorHandler.java create mode 100644 src/main/java/Step2_Calculator/InputExtactor.java create mode 100644 src/main/java/Step2_Calculator/InputView.java create mode 100644 src/main/java/Step2_Calculator/OutputView.java create mode 100644 src/test/java/Step2_Calculator/CalculatorTest.java diff --git a/src/main/java/Step2_Calculator/ArithmeticOperation.java b/src/main/java/Step2_Calculator/ArithmeticOperation.java new file mode 100644 index 0000000..f0a53a9 --- /dev/null +++ b/src/main/java/Step2_Calculator/ArithmeticOperation.java @@ -0,0 +1,41 @@ +package Step2_Calculator; + +public class ArithmeticOperation { + + public static int arithmeticOperation(int acc, int curr, String arithmeticOperator) { + + if(arithmeticOperator.equals("+")){ + return sum(acc, curr); + } + + if(arithmeticOperator.equals("-")){ + return subtract(acc, curr); + } + + if(arithmeticOperator.equals("/")){ + return divide(acc, curr); + } + + if(arithmeticOperator.equals("*")){ + return multiply(acc, curr); + } + + return acc; + } + + private static int sum(int acc, int curr) { + return acc + curr; + } + + private static int divide(int acc, int curr) { + return acc / curr; + } + + private static int multiply(int acc, int curr) { + return acc * curr; + } + + private static int subtract(int acc, int curr) { + return acc - curr; + } +} diff --git a/src/main/java/Step2_Calculator/Calculator.java b/src/main/java/Step2_Calculator/Calculator.java new file mode 100644 index 0000000..7d3ed78 --- /dev/null +++ b/src/main/java/Step2_Calculator/Calculator.java @@ -0,0 +1,17 @@ +package Step2_Calculator; + +public class Calculator { + private int accumulator; + + public int calculate(String s) { + InputErrorHandler.checkCalculatorIllegalArgument(s); + String[] arithmetics = InputExtactor.extractCalculatorInputs(s); + + accumulator = Integer.parseInt(arithmetics[0]); + + for(int i = 1; i< arithmetics.length; i += 2) { + accumulator = ArithmeticOperation.arithmeticOperation(accumulator, Integer.parseInt(arithmetics[i+1]), arithmetics[i]); + } + return accumulator; + } +} diff --git a/src/main/java/Step2_Calculator/CalculatorApp.java b/src/main/java/Step2_Calculator/CalculatorApp.java new file mode 100644 index 0000000..3875721 --- /dev/null +++ b/src/main/java/Step2_Calculator/CalculatorApp.java @@ -0,0 +1,11 @@ +package Step2_Calculator; + +public class CalculatorApp { + public static void main(String[] args) { + InputView.startCalculator(); + String input = InputView.inputArithmeticOperations(); + Calculator calculator = new Calculator(); + int result = calculator.calculate(input); + OutputView.showOutput(result); + } +} diff --git a/src/main/java/Step2_Calculator/InputErrorHandler.java b/src/main/java/Step2_Calculator/InputErrorHandler.java new file mode 100644 index 0000000..bed5bb2 --- /dev/null +++ b/src/main/java/Step2_Calculator/InputErrorHandler.java @@ -0,0 +1,17 @@ +package Step2_Calculator; + +public class InputErrorHandler { + public static void checkCalculatorIllegalArgument(String s){ + String numbersToSingleLength = s.replaceAll("-?\\d+", "1"); + String noSpace = numbersToSingleLength.replaceAll(" ", ""); + if(numbersToSingleLength.length() != noSpace.length()*2-1) { + throw new IllegalArgumentException("Wrong input Format! Must be (number) (arithmetic Operator) (number) ... so on with no null inputs"); + } + + if(!s.matches("[\\d+*/\\-]+")){ + System.out.println("asdasdasd"); + throw new IllegalArgumentException("Wrong inputs!!!"); + } + + } +} diff --git a/src/main/java/Step2_Calculator/InputExtactor.java b/src/main/java/Step2_Calculator/InputExtactor.java new file mode 100644 index 0000000..6e137a1 --- /dev/null +++ b/src/main/java/Step2_Calculator/InputExtactor.java @@ -0,0 +1,7 @@ +package Step2_Calculator; + +public class InputExtactor { + public static String[] extractCalculatorInputs(String s) { + return s.split(" "); + } +} diff --git a/src/main/java/Step2_Calculator/InputView.java b/src/main/java/Step2_Calculator/InputView.java new file mode 100644 index 0000000..e81272d --- /dev/null +++ b/src/main/java/Step2_Calculator/InputView.java @@ -0,0 +1,16 @@ +package Step2_Calculator; + +import java.util.Scanner; + +public class InputView { + private static Scanner scanner; + + public static void startCalculator(){ + System.out.print("Input Arithmetic Operations : "); + } + public static String inputArithmeticOperations() { + scanner = new Scanner(System.in); + return scanner.nextLine(); + } + +} diff --git a/src/main/java/Step2_Calculator/OutputView.java b/src/main/java/Step2_Calculator/OutputView.java new file mode 100644 index 0000000..747a357 --- /dev/null +++ b/src/main/java/Step2_Calculator/OutputView.java @@ -0,0 +1,9 @@ +package Step2_Calculator; + +public class OutputView { + + public static void showOutput(int result) { + System.out.println("Result :" + result); + } + +} diff --git a/src/main/java/empty.txt b/src/main/java/empty.txt index e69de29..24c6c86 100644 --- a/src/main/java/empty.txt +++ b/src/main/java/empty.txt @@ -0,0 +1 @@ +ㄷㄱㅀㄴㄷㄹ \ No newline at end of file diff --git a/src/test/java/Step2_Calculator/CalculatorTest.java b/src/test/java/Step2_Calculator/CalculatorTest.java new file mode 100644 index 0000000..cf02e0a --- /dev/null +++ b/src/test/java/Step2_Calculator/CalculatorTest.java @@ -0,0 +1,67 @@ +package Step2_Calculator; + +import Step2_Calculator.Calculator; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +import static org.assertj.core.api.Assertions.*; + +public class CalculatorTest { + private Calculator calculator; + + @BeforeEach + void setUp() { + calculator = new Calculator(); + } + + @DisplayName("test calculator") + @ParameterizedTest + @CsvSource(value = {"2 + 3 * 4 / 2:10", "1:1", "0 * 10 * 11 + 3: 3"}, delimiter = ':') + public void calculatorTestArithmeticOperations(String arithmetic, int result) { + assertThat(calculator.calculate(arithmetic)).isEqualTo(result); + } + + @DisplayName("null input") + @Test + public void calculatorTestWithNull() { + assertThatIllegalArgumentException().isThrownBy(() -> { + calculator.calculate("null"); + }); + } + + @DisplayName("wrong input") + @Test + public void calculatorTestWithWrongInput() { + assertThatIllegalArgumentException().isThrownBy(() -> { + calculator.calculate("!"); + }); + } + + @DisplayName("wrong format") + @Test + public void calculatorTestWithWrongFormat() { + assertThatIllegalArgumentException().isThrownBy(() -> { + calculator.calculate("4 + a"); + }); + } + + @DisplayName("space inputs") + @Test + public void calculatorTestWithSpaceInputs() { + assertThatIllegalArgumentException().isThrownBy(() -> { + calculator.calculate("4 + 1 * 2"); + }); + } + + @DisplayName("negative inputs") + @Test + public void calculatorTestWithNegativeInputs() { + assertThat(calculator.calculate("-4 - 1 * 2")).isEqualTo(-10); + } + + +}