-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingRotorTest.java
More file actions
74 lines (60 loc) · 2.29 KB
/
MovingRotorTest.java
File metadata and controls
74 lines (60 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package enigma;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.Timeout;
import static org.junit.Assert.*;
import java.util.HashMap;
import static enigma.TestUtils.*;
/** The suite of all JUnit tests for the Permutation class.
* @author
*/
public class MovingRotorTest {
/** Testing time limit. */
@Rule
public Timeout globalTimeout = Timeout.seconds(5);
/* ***** TESTING UTILITIES ***** */
private Rotor rotor;
private String alpha = UPPER_STRING;
/** Check that rotor has an alphabet whose size is that of
* FROMALPHA and TOALPHA and that maps each character of
* FROMALPHA to the corresponding character of FROMALPHA, and
* vice-versa. TESTID is used in error messages. */
private void checkRotor(String testId,
String fromAlpha, String toAlpha) {
int N = fromAlpha.length();
assertEquals(testId + " (wrong length)", N, rotor.size());
for (int i = 0; i < N; i += 1) {
char c = fromAlpha.charAt(i), e = toAlpha.charAt(i);
int ci = alpha.indexOf(c), ei = alpha.indexOf(e);
assertEquals(msg(testId, "wrong translation of %d (%c)", ci, c),
ei, rotor.convertForward(ci));
assertEquals(msg(testId, "wrong inverse of %d (%c)", ei, e),
ci, rotor.convertBackward(ei));
}
}
/** Set the rotor to the one with given NAME and permutation as
* specified by the NAME entry in ROTORS, with given NOTCHES. */
private void setRotor(String name, HashMap<String, String> rotors,
String notches) {
rotor = new MovingRotor(name, new Permutation(rotors.get(name), UPPER),
notches);
}
/* ***** TESTS ***** */
@Test
public void checkRotorAtA() {
setRotor("I", NAVALA, "");
checkRotor("Rotor I (A)", UPPER_STRING, NAVALA_MAP.get("I"));
}
@Test
public void checkRotorAdvance() {
setRotor("I", NAVALA, "");
rotor.advance();
checkRotor("Rotor I advanced", UPPER_STRING, NAVALB_MAP.get("I"));
}
@Test
public void checkRotorSet() {
setRotor("I", NAVALA, "");
rotor.set(25);
checkRotor("Rotor I set", UPPER_STRING, NAVALZ_MAP.get("I"));
}
}