システム指示 |
熟練した 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 |
TopK: |
40.0 |
TopP: |
0.95 |
|