-
Notifications
You must be signed in to change notification settings - Fork 85
[BCSD Lab 임아리] 1차시 미션 제출합니다. #105
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
Changes from all commits
07d1d2b
b1fb8fa
c84322e
8f42c55
932fac5
9925733
0964cff
60207fc
bc1e5b6
53de118
3c536b7
c887270
d640c8c
06bbd9f
0ab48d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
public class SimpleCalculator { | ||
public int add(int firstNum, int secondNum){ | ||
return firstNum + secondNum; | ||
public int add(int a, int b) { | ||
return Math.addExact(a, b); | ||
} | ||
public int multiply(int firstNum, int secondNum){ | ||
return firstNum * secondNum; | ||
} | ||
public int divide(int firstNum, int secondNum){ | ||
return firstNum / secondNum; | ||
|
||
public int minus(int a, int b) { | ||
return Math.subtractExact(a, b); | ||
} | ||
public int minus(int firstNum, int secondNum){ | ||
return firstNum - secondNum; | ||
|
||
public int multiply(int a, int b) { | ||
return Math.multiplyExact(a, b); | ||
} | ||
|
||
public int divide(int a, int b) { | ||
if (b == 0) { | ||
throw new ArithmeticException("0으로 나눌 수 없습니다."); | ||
} | ||
if (a == Integer.MIN_VALUE && b == -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오... int 형의 음수와 양수의 절댓값이 음수가 더 크니까 해당 케이스에서 오버플로우가 발생할 수 있네요. |
||
throw new ArithmeticException("정수 범위를 벗어났습니다."); | ||
} | ||
return a / b; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,64 @@ | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class SimpleCalculatorTest { | ||
|
||
private final SimpleCalculator calc = new SimpleCalculator(); | ||
|
||
@Nested | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 어떤 역할을 하는 어노테이션인가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 중첩된 테스트 클래스를 정의할 때 붙이는 어노테이션입니다! Junit5랑 AssertJ 테스트로 나누고 싶어서 어노테이션을 쓰고 나누려고 했는데... 지금 보니 코드가 그 부분을 제대로 설명하지 못하고 있어보이네요. 이 부분도 수정하겠습니다! |
||
class SimpleTest { | ||
class TestWithJunit5 { | ||
|
||
@Test | ||
void addTest(){ | ||
SimpleCalculator calc = new SimpleCalculator(); | ||
void addTest() { | ||
assertEquals(3, calc.add(1, 2)); | ||
assertEquals(5, calc.add(3, 2)); | ||
} | ||
|
||
@Test | ||
void minusTest(){ | ||
SimpleCalculator calc = new SimpleCalculator(); | ||
void minusTest() { | ||
assertEquals(3, calc.minus(5, 2)); | ||
assertEquals(-2, calc.minus(3, 5)); | ||
} | ||
|
||
@Test | ||
void multiply(){ | ||
SimpleCalculator calc = new SimpleCalculator(); | ||
void multiply() { | ||
assertEquals(10, calc.multiply(5, 2)); | ||
assertEquals(15, calc.multiply(3, 5)); | ||
} | ||
|
||
@Test | ||
void divide(){ | ||
SimpleCalculator calc = new SimpleCalculator(); | ||
void divide() { | ||
assertEquals(5, calc.divide(10, 2)); | ||
assertEquals(30, calc.divide(180, 6)); | ||
} | ||
} | ||
|
||
@Nested | ||
class TestWithAssertJ { | ||
|
||
@Test | ||
void simpleCalcTest(){ | ||
assertThat(calc.add(1, 2)).isEqualTo(3); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://velog.io/@doolchong/assertEqual%EA%B3%BC-assertThat 이런 장점들이 존재합니다! 이번 미션에서는 테스트 코드를 학습하는 것을 중점으로 두었기 때문에 AssertJ, Junit5 모두 사용해보고 싶어서 이런식으로 작성해보았습니다. 이 부분도 장단점에 맞게 수정해보겠습니다! |
||
assertThat(calc.minus(1,2)).isEqualTo(-1); | ||
assertThat(calc.divide(4, 2)).isEqualTo(2); | ||
assertThat(calc.multiply(3,2)).isEqualTo(6); | ||
} | ||
|
||
@Test | ||
void errorTestDivideZero() { | ||
assertThatThrownBy(() -> calc.divide(2, 0)).isInstanceOf(ArithmeticException.class); | ||
} | ||
|
||
@Test | ||
void errorTestOverFlow() { | ||
assertThatThrownBy(() -> calc.add(Integer.MAX_VALUE, 1)).isInstanceOf(ArithmeticException.class); | ||
assertThatThrownBy(() -> calc.minus(Integer.MAX_VALUE, -3)).isInstanceOf(ArithmeticException.class); | ||
assertThatThrownBy(() -> calc.multiply(Integer.MAX_VALUE, 3)).isInstanceOf(ArithmeticException.class); | ||
assertThatThrownBy(() -> calc.divide(Integer.MIN_VALUE, -1)).isInstanceOf(ArithmeticException.class); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addExact()
나subtractExact()
함수들은 일단+
,-
랑 어떤점이 다른가요?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
내부적으로 long으로 연산한 후 int 범위를 벗어나면 ArithmeticException을 던집니다! 예전 코드에서는 그저 +, - 만 사용하여 계산하였기 때문에 오버플로우 처리가 되지 않았습니다. 그래서 이번에는 추가했습니다!