시스템 안내 |
숙련된 Java 개발자이며 테스트 기반 개발 전문가입니다. 기본 목표는 깔끔하고 효율적이며 유지보수 가능한 Java 코드를 작성하고 모든 기능을 철저히 테스트하는 것입니다.
|
저는 Java 개발자이며 테스트 기반 개발 방법론을 따르고 있습니다. 로봇을 작동하는 데 사용되는 다음 코드가 있습니다. 곧 청소 기능을 추가하기 위해 리팩터링할 예정입니다. 리팩터링 프로세스 중에 아무것도 손상시키지 않고 코드의 동작을 이해하기 위해 기존 코드에 몇 가지 캐릭터화 테스트를 추가하려고 합니다.
- 다음 코드를 참고하고
RoboCleaner 클래스의 메서드에 JUnit 4를 사용하여 단위 테스트를 작성합니다.
- 테스트에 의미 있는 이름을 지정하고 모든 특이 사례를 다루어야 합니다.
public class RoboCleaner {
int x, y;
Direction direction;
public RoboCleaner(int initialX, int initialY, Direction initialDirection) {
this.x = initialX;
this.y = initialY;
this.direction = initialDirection;
}
public void moveForward(int steps) {
switch (direction) {
case NORTH:
y += steps;
break;
case SOUTH:
y -= steps;
break;
case EAST:
x += steps;
break;
case WEST:
x -= steps;
break;
}
}
public void turnLeft() {
direction = direction.turnLeft();
}
public void turnRight() {
direction = direction.turnRight();
}
public String getPosition() {
return String.format("Position: (%d, %d), Facing: %s", x, y, direction);
}
public enum Direction {
NORTH, EAST, SOUTH, WEST;
private static final Direction[] leftTurns = {WEST, NORTH, EAST, SOUTH};
private static final Direction[] rightTurns = {EAST, SOUTH, WEST, NORTH};
public Direction turnLeft() {
return leftTurns[ordinal()];
}
public Direction turnRight() {
return rightTurns[ordinal()];
}
}
}
응답
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RoboCleanerTest {
private RoboCleaner roboCleaner;
@Before
public void setUp() {
roboCleaner = new RoboCleaner(0, 0, RoboCleaner.Direction.NORTH);
}
@Test
public void testInitialPosition() {
assertEquals("Position: (0, 0), Facing: NORTH", roboCleaner.getPosition());
}
@Test
public void testMoveForwardNorth() {
roboCleaner.moveForward(5);
assertEquals("Position: (0, 5), Facing: NORTH", roboCleaner.getPosition());
}
@Test
public void testMoveForwardSouth() {
roboCleaner.direction = RoboCleaner.Direction.SOUTH;
roboCleaner.moveForward(3);
assertEquals("Position: (0, -3), Facing: SOUTH", roboCleaner.getPosition());
}
@Test
public void testMoveForwardEast() {
roboCleaner.direction = RoboCleaner.Direction.EAST;
roboCleaner.moveForward(7);
assertEquals("Position: (7, 0), Facing: EAST", roboCleaner.getPosition());
}
@Test
public void testMoveForwardWest() {
roboCleaner.direction = RoboCleaner.Direction.WEST;
roboCleaner.moveForward(2);
assertEquals("Position: (-2, 0), Facing: WEST", roboCleaner.getPosition());
}
@Test
public void testMoveForwardZeroSteps() {
roboCleaner.moveForward(0);
assertEquals("Position: (0, 0), Facing: NORTH", roboCleaner.getPosition());
}
@Test
public void testMoveForwardNegativeSteps() {
roboCleaner.moveForward(-5); //Should not affect position
assertEquals("Position: (0, 0), Facing: NORTH", roboCleaner.getPosition());
}
@Test
public void testTurnLeft() {
roboCleaner.turnLeft();
assertEquals("Position: (0, 0), Facing: WEST", roboCleaner.getPosition());
}
@Test
public void testTurnLeftFromWest() {
roboCleaner.direction = RoboCleaner.Direction.WEST;
roboCleaner.turnLeft();
assertEquals("Position: (0, 0), Facing: SOUTH", roboCleaner.getPosition());
}
@Test
public void testTurnRight() {
roboCleaner.turnRight();
assertEquals("Position: (0, 0), Facing: EAST", roboCleaner.getPosition());
}
@Test
public void testTurnRightFromEast() {
roboCleaner.direction = RoboCleaner.Direction.EAST;
roboCleaner.turnRight();
assertEquals("Position: (0, 0), Facing: SOUTH", roboCleaner.getPosition());
}
@Test
public void testMultipleTurns() {
roboCleaner.turnLeft();
roboCleaner.turnRight();
roboCleaner.turnRight();
assertEquals("Position: (0, 0), Facing: EAST", roboCleaner.getPosition());
}
@Test
public void testComplexMovement() {
roboCleaner.moveForward(2);
roboCleaner.turnRight();
roboCleaner.moveForward(3);
roboCleaner.turnLeft();
roboCleaner.moveForward(1);
assertEquals("Position: (3, 3), Facing: NORTH", roboCleaner.getPosition());
}
@Test
public void testNegativeCoordinates(){
roboCleaner = new RoboCleaner(-1,-2, RoboCleaner.Direction.SOUTH);
roboCleaner.moveForward(1);
assertEquals("Position: (-1, -3), Facing: SOUTH", roboCleaner.getPosition());
}
}
|
모델: |
gemini-1.5-flash-002 |
강도: |
0.2 |
최대 출력 토큰: |
8192 |
최상위 K: |
40.0 |
최상위 P: |
0.95 |
|