Skip to content

Commit 37eff0b

Browse files
cdpaivajmrunkle
andauthored
robot-name: added new test (exercism#2065)
* New test for Robot Name * Update Robot.java * Update Robot.java * Update RobotTest.java * Update RobotTest.java * Update exercises/practice/robot-name/src/test/java/RobotTest.java Co-authored-by: Jason Runkle <jmrunkle@gmail.com> * Removed recursive call * Update exercises/practice/robot-name/src/test/java/RobotTest.java Co-authored-by: Jason Runkle <jmrunkle@gmail.com> Co-authored-by: Jason Runkle <jmrunkle@gmail.com>
1 parent 4a72fcf commit 37eff0b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

exercises/practice/robot-name/.meta/src/reference/java/Robot.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1+
import java.util.HashSet;
12
import java.util.Random;
3+
import java.util.Set;
24

35
public class Robot {
46

57
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
68
private String name;
79
private final Random random;
10+
private static Set<String> nameLog = new HashSet<>();
811

912
public Robot() {
1013
random = new Random();
1114
assignNewName();
1215
}
1316

1417
private void assignNewName() {
15-
name = String.format("%s%d", prefix(), suffix());
18+
String newName = String.format("%s%d", prefix(), suffix());
19+
while (!nameLog.add(newName)) {
20+
newName = String.format("%s%d", prefix(), suffix());
21+
}
22+
this.name = newName;
1623
}
1724

1825
public String getName() {

exercises/practice/robot-name/src/test/java/RobotTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import static org.assertj.core.api.Assertions.assertThat;
22

3-
import org.junit.Test;
4-
import org.junit.Ignore;
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
56
import org.junit.Before;
7+
import org.junit.Ignore;
8+
import org.junit.Test;
69

710
public class RobotTest {
811

@@ -34,6 +37,17 @@ public void resetName() {
3437
assertThat(name).isNotEqualTo(name2);
3538
assertIsValidName(name2);
3639
}
40+
41+
@Ignore("Remove to run test")
42+
@Test
43+
public void robotNamesAreUnique() {
44+
Set<String> robotNames = new HashSet<>();
45+
int sampleSize = 5000;
46+
for (int i = 0; i < sampleSize; i++) {
47+
robotNames.add(new Robot().getName());
48+
}
49+
assertThat(robotNames).hasSize(sampleSize);
50+
}
3751

3852
private static void assertIsValidName(String name) {
3953
assertThat(name).matches(EXPECTED_ROBOT_NAME_PATTERN);

0 commit comments

Comments
 (0)