-
Notifications
You must be signed in to change notification settings - Fork 97
이가연 레이싱카 미션 1단계, 2단계 제출합니다 #59
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
base: gy102912
Are you sure you want to change the base?
Changes from 5 commits
cfb6707
271b314
39f7cd3
a06f16a
6c116c2
f6fc534
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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| public class RacingCar { | ||
| private String name; | ||
|
|
||
| public RacingCar(String name) { | ||
| setName(name); | ||
| } | ||
|
|
||
| public int moveByRandom(int random){ | ||
| if (random >= 4){ | ||
| return 1; //go | ||
| } | ||
| return 0; //stop | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| if (name == null || name.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("Racing car name cannot be empty"); | ||
| } | ||
| this.name = name; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Random; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class RacingContest { | ||
| private static final Random random = new Random(); | ||
| private final RacingCar[] RACING_CARS; | ||
|
||
| private final int rounds; | ||
|
|
||
| public RacingContest(RacingCar[] racingCars, int racingRounds) { | ||
| RACING_CARS = racingCars; | ||
| rounds = racingRounds; | ||
| } | ||
|
|
||
| public int[] start(){ | ||
| int[] distances = new int[RACING_CARS.length]; | ||
|
||
|
|
||
| for (int r = 0; r < rounds; r++) { | ||
| for (int c = 0; c < RACING_CARS.length; c++) { | ||
| distances[c] += RACING_CARS[c].moveByRandom(random.nextInt(10)); | ||
|
||
| } | ||
| } | ||
|
|
||
| return distances; | ||
| } | ||
|
|
||
| public ArrayList<String> ranking(int[] distances){ | ||
| int maxDist = Arrays.stream(distances).max().getAsInt(); // winner decision | ||
| ArrayList<String> winners = new ArrayList<>(); | ||
|
||
|
|
||
| for (int i = 0; i < distances.length; i++) { | ||
| if (distances[i] == maxDist) { | ||
| winners.add(RACING_CARS[i].getName()); | ||
| } | ||
| } | ||
|
|
||
| return winners; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class TestRacingCar { | ||
|
|
||
| private RacingCar racingCar; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| racingCar = new RacingCar("myRacingCar"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("레이싱카 전진 확인") | ||
| public void testGo(){ | ||
| //given | ||
| int random = 9; | ||
|
|
||
| //when | ||
| int actual = racingCar.moveByRandom(random); | ||
| int expected = 1; | ||
|
|
||
| //then | ||
| assertThat(actual).isEqualTo(expected); | ||
| } | ||
| @Test | ||
| @DisplayName("레이싱카 정지 확인") | ||
| public void testStop(){ | ||
| //given | ||
| int random = 0; | ||
|
|
||
| //when | ||
| int actual = racingCar.moveByRandom(random); | ||
| int expected = 0; | ||
|
|
||
| //then | ||
| assertThat(actual).isEqualTo(expected); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("레이싱카의 이름이 유효하지 않을 시 에러 발생") | ||
| public void testValidName(){ | ||
| //given | ||
|
|
||
| //when | ||
|
|
||
| //then | ||
| assertThatThrownBy(() -> { | ||
| racingCar.setName(null); | ||
| }).isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class TestRacingContest { | ||
| private static int round; | ||
| private static RacingContest contest; | ||
| private static final String[] names = new String[]{"neo", "brie", "brown"}; | ||
|
|
||
| @BeforeAll | ||
| public static void setUp() { | ||
| RacingCar[] participants = new RacingCar[]{ | ||
| new RacingCar(names[0]), | ||
| new RacingCar(names[1]), | ||
| new RacingCar(names[2]) | ||
| }; | ||
| round = 10; | ||
| contest = new RacingContest(participants, round); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("경기 결과 모든 차의 주행거리가 0 이상이고 주어진 횟수 round 이하인지 확인") | ||
| public void testStart() { | ||
|
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. start의 기능에 대한 테스트가 조금 모자라게 느껴지는 것 같아요. 조금 더 명확하고 효과적인 테스트는 어떻게 작성할 수 있을까요?? |
||
| //given | ||
| int round = contest.getRounds(); | ||
|
|
||
| //when | ||
| int[] distances = contest.start(); | ||
| int maxDist = Arrays.stream(distances).max().getAsInt(); | ||
| int minDist = Arrays.stream(distances).min().getAsInt(); | ||
|
|
||
| //then | ||
| assertThat(maxDist).isBetween(0, round + 1); | ||
| assertThat(minDist).isBetween(0, round + 1); | ||
| } | ||
| @Test | ||
| @DisplayName("경기 결과를 바탕으로 우승자를 가려낼 수 있는지 확인") | ||
| public void testRanking() { | ||
| //given | ||
| int[] distances = new int[]{0, round, round}; | ||
| String[] winners = new String[]{names[1], names[2]}; | ||
|
|
||
| //when | ||
| ArrayList<String> actual = contest.ranking(distances); | ||
| ArrayList<String> expected = new ArrayList<>(Arrays.asList(winners)); | ||
|
||
|
|
||
| //then | ||
| assertThat(actual).usingRecursiveComparison().isEqualTo(expected); | ||
|
||
| } | ||
| } | ||
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.
setName이란 메서드가 RacingCar 내부 외에서 호출되는 일이 있나요?