-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
397 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
public class Calculator { | ||
//Basic Calculator | ||
//인자 2개를 받아 정수 결과를 반환 | ||
//사칙연산을 위한 메서드 4개를 제공 | ||
|
||
public int add(int left, int right){ | ||
return left+right; | ||
} | ||
|
||
public int sub(int left, int right){ | ||
return left-right; | ||
} | ||
|
||
public int mul(int left, int right){ | ||
return left*right; | ||
} | ||
|
||
public int div(int left, int right){ | ||
if(right == 0){ //우변이 0인 경우 -> 0으로 나누는 문제 | ||
throw new RuntimeException("0으로는 나눌 수 없습니다"); | ||
} | ||
return left/right; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
public class StringCalculator { | ||
|
||
|
||
public int addAll(String input) { | ||
|
||
//빈 문자열 처리 | ||
if(input == null || input.isEmpty()) return 0; | ||
|
||
//기본 구분자 정규표현식 (, 또는 :) | ||
String regex = "[,|:]"; | ||
|
||
//커스텀 구분자를 사용하는 경우 | ||
if (input.startsWith("//")) { | ||
//잘못된 구문에 대한 예외 처리 | ||
int formatEndIndex = input.indexOf("\n"); | ||
if (formatEndIndex == -1){ // "\n" 을 찾을 수 없는 경우 | ||
throw new RuntimeException("올바르지 않은 커스텀 구분자 지정 구문입니다."); | ||
} | ||
|
||
regex = "[,|:|" + Pattern.quote(input.substring(2, formatEndIndex)) + "]"; //Pattern.quote를 이용하여 특수문자를 안전하게 처리 | ||
input = input.substring(formatEndIndex+1); //커스텀 구분자 지정하는 부분을 제거 | ||
|
||
}else if(input.contains("\n")) | ||
{ | ||
// "\n"은 있지만 "//"으로 시작하지 않는 경우에 RuntimeException 던지기 | ||
throw new RuntimeException("올바르지 않은 커스텀 구분자 지정 구문입니다."); | ||
} | ||
|
||
return calculateSum(input,regex); | ||
} | ||
|
||
private int calculateSum(String numbers, String regex) { | ||
String[] tokens = numbers.split(regex); | ||
|
||
List<Integer> parsedNumbers = new ArrayList<>(); | ||
|
||
for(String token : tokens){ | ||
try{ | ||
int parsedNumber = Integer.parseInt(token); | ||
if(parsedNumber < 0){ | ||
throw new RuntimeException("음수가 입력되었습니다."); | ||
} | ||
|
||
parsedNumbers.add(parsedNumber); | ||
}catch (NumberFormatException e){ | ||
throw new RuntimeException("숫자 이외의 값이 입력되었습니다."); | ||
} | ||
} | ||
|
||
//숫자를 모두 더해 반환 | ||
int result = 0; | ||
for(int number : parsedNumbers){ | ||
try{ | ||
result = Math.addExact(result,number); //Math.addExact 정적 메서드로 오버플로우 발생 시 예외 처리 | ||
}catch(ArithmeticException e){ | ||
throw new RuntimeException("계산 결과값이 너무 큽니다."); | ||
} | ||
|
||
} | ||
return result; | ||
|
||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.Nested; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
|
||
@DisplayName("초간단 계산기 단위 테스트") | ||
public class TestCalculator { | ||
|
||
Calculator calculator = new Calculator(); | ||
|
||
@Nested | ||
@DisplayName("초간단 계산기 기능별 단위 테스트") | ||
class CalculateFeatureTest { | ||
@Test | ||
@DisplayName("덧셈 테스트") | ||
void testAdd(){ | ||
assertEquals(5, calculator.add(2 ,3)); | ||
assertEquals(8, calculator.add(-2 ,10)); | ||
assertEquals(-4,calculator.add(6,-10)); | ||
assertEquals(-10, calculator.add(-4,-6)); | ||
} | ||
|
||
@Test | ||
@DisplayName("뺄셈 테스트") | ||
void testSub(){ | ||
assertEquals(-1, calculator.sub(2 ,3)); | ||
assertEquals(-12, calculator.sub(-2 ,10)); | ||
assertEquals(16,calculator.sub(6,-10)); | ||
assertEquals(2, calculator.sub(-4,-6)); | ||
} | ||
|
||
@Test | ||
@DisplayName("곱셈 테스트") | ||
void testMul(){ | ||
assertEquals(6, calculator.mul(2 ,3)); | ||
assertEquals(-20, calculator.mul(-2 ,10)); | ||
assertEquals(-60, calculator.mul(6,-10)); | ||
assertEquals(24, calculator.mul(-4,-6)); | ||
} | ||
|
||
@Test | ||
@DisplayName("나눗셈 테스트") | ||
void testDiv(){ | ||
assertEquals(0, calculator.div(2 ,3)); | ||
assertEquals(1, calculator.div(3 ,2)); | ||
assertEquals(-1, calculator.div(3,-2)); | ||
assertEquals(2, calculator.div(-4,-2)); | ||
assertEquals(0, calculator.div(0,-10)); | ||
//0으로 나누는 예외 | ||
assertThrows(RuntimeException.class, () -> calculator.div(6,0)); | ||
|
||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.Nested; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
|
||
public class TestCalculatorAssertJ { | ||
Calculator calculator = new Calculator(); | ||
|
||
@Nested | ||
@DisplayName("초간단 계산기 기능별 단위 테스트") | ||
class CalculateFeatureTestAssertJ { | ||
@Test | ||
@DisplayName("덧셈 테스트") | ||
void testAdd(){ | ||
/* | ||
assertEquals(5, calculator.add(2 ,3)); | ||
assertEquals(8, calculator.add(-2 ,10)); | ||
assertEquals(-4,calculator.add(6,-10)); | ||
assertEquals(-10, calculator.add(-4,-6)); | ||
*/ | ||
assertThat(5).isEqualTo(calculator.add(2,3)); | ||
assertThat(8).isEqualTo(calculator.add(-2,10)); | ||
assertThat(-4).isEqualTo(calculator.add(6,-10)); | ||
assertThat(-10).isEqualTo(calculator.add(-4,-6)); | ||
|
||
} | ||
|
||
@Test | ||
@DisplayName("뺄셈 테스트") | ||
void testSub(){ | ||
/* | ||
assertEquals(-1, calculator.sub(2 ,3)); | ||
assertEquals(-12, calculator.sub(-2 ,10)); | ||
assertEquals(16,calculator.sub(6,-10)); | ||
assertEquals(2, calculator.sub(-4,-6)); | ||
*/ | ||
|
||
assertThat(-1).isEqualTo(calculator.sub(2,3)); | ||
assertThat(-12).isEqualTo(calculator.sub(-2,10)); | ||
assertThat(16).isEqualTo(calculator.sub(6,-10)); | ||
assertThat(2).isEqualTo(calculator.sub(-4,-6)); | ||
} | ||
|
||
@Test | ||
@DisplayName("곱셈 테스트") | ||
void testMul(){ | ||
/* | ||
assertEquals(6, calculator.mul(2 ,3)); | ||
assertEquals(-20, calculator.mul(-2 ,10)); | ||
assertEquals(-60, calculator.mul(6,-10)); | ||
assertEquals(24, calculator.mul(-4,-6)); | ||
*/ | ||
|
||
assertThat(6).isEqualTo(calculator.mul(2,3)); | ||
assertThat(-20).isEqualTo(calculator.mul(-2,10)); | ||
assertThat(-60).isEqualTo(calculator.mul(6,-10)); | ||
assertThat(24).isEqualTo(calculator.mul(-4,-6)); | ||
|
||
} | ||
|
||
@Test | ||
@DisplayName("나눗셈 테스트") | ||
void testDiv(){ | ||
/* | ||
assertEquals(0, calculator.div(2 ,3)); | ||
assertEquals(1, calculator.div(3 ,2)); | ||
assertEquals(-1, calculator.div(3,-2)); | ||
assertEquals(2, calculator.div(-4,-2)); | ||
assertEquals(0, calculator.div(0,-10)); | ||
//0으로 나누는 예외 | ||
assertThrows(RuntimeException.class, () -> calculator.div(6,0)); | ||
*/ | ||
|
||
assertThat(0).isEqualTo(calculator.div(2,3)); | ||
assertThat(1).isEqualTo(calculator.div(3,2)); | ||
assertThat(-1).isEqualTo(calculator.div(3,-2)); | ||
assertThat(2).isEqualTo(calculator.div(-4,-2)); | ||
assertThat(0).isEqualTo(calculator.div(0,-10)); | ||
|
||
//0으로 나누는 예외 | ||
assertThatThrownBy(() -> calculator.div(6,0)); | ||
|
||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.Nested; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
|
||
@DisplayName("문자열 계산기 단위 테스트") | ||
public class TestStringCalculator { | ||
|
||
StringCalculator stringCalculator = new StringCalculator(); | ||
|
||
@Nested | ||
@DisplayName("문자열 계산기 기능 테스트") | ||
class StringCalculatorFeatureTest { | ||
|
||
@Test | ||
@DisplayName("빈 문자열 케이스") | ||
void testEmptyString(){ | ||
assertEquals(0, stringCalculator.addAll("")); | ||
} | ||
|
||
@Test | ||
@DisplayName("올바른 입력 케이스") | ||
void testValidInputs(){ | ||
//기본 구분자 사용 | ||
assertEquals(10,stringCalculator.addAll("1:2:3:4")); | ||
assertEquals(10,stringCalculator.addAll("1,2,3,4")); | ||
assertEquals(6,stringCalculator.addAll("1,2,3")); | ||
assertEquals(6,stringCalculator.addAll("1:2:3")); | ||
assertEquals(3,stringCalculator.addAll("1,2")); | ||
assertEquals(3,stringCalculator.addAll("1:2")); | ||
assertEquals(1,stringCalculator.addAll("1")); | ||
|
||
//커스텀 구분자 사용 | ||
assertEquals(10,stringCalculator.addAll("//;\n1;2;3;4")); | ||
assertEquals(6,stringCalculator.addAll("//;\n1;2;3")); | ||
assertEquals(3,stringCalculator.addAll("//;\n1;2")); | ||
assertEquals(1,stringCalculator.addAll("//;\n1")); | ||
|
||
assertEquals(10,stringCalculator.addAll("//^\n1^2^3^4")); | ||
assertEquals(6,stringCalculator.addAll("//^\n1^2^3")); | ||
assertEquals(3,stringCalculator.addAll("//^\n1^2")); | ||
assertEquals(1,stringCalculator.addAll("//^\n1")); | ||
} | ||
|
||
@Test | ||
@DisplayName("RuntimeException 발생 케이스") | ||
void testRuntimeException() { | ||
//숫자 이외의 값 | ||
assertThrows(RuntimeException.class, () -> stringCalculator.addAll("1,a,3")); | ||
assertThrows(RuntimeException.class, () -> stringCalculator.addAll("1:a:3")); | ||
assertThrows(RuntimeException.class, () -> stringCalculator.addAll("//^\n1^a^3")); | ||
|
||
//음수 입력 케이스 | ||
assertThrows(RuntimeException.class, () -> stringCalculator.addAll("1,-2,3")); | ||
assertThrows(RuntimeException.class, () -> stringCalculator.addAll("1:-2:3")); | ||
assertThrows(RuntimeException.class, () -> stringCalculator.addAll("//^\n1^-2^3")); | ||
|
||
//잘못된 커스텀 지정자 구문 | ||
assertThrows(RuntimeException.class, () -> stringCalculator.addAll("//;\t1;2;3")); | ||
assertThrows(RuntimeException.class, () -> stringCalculator.addAll("//;1;2;3")); | ||
assertThrows(RuntimeException.class, () -> stringCalculator.addAll("1^2^3")); | ||
assertThrows(RuntimeException.class, () -> stringCalculator.addAll("^\n1^2^3")); | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.