-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationTest.java
More file actions
80 lines (70 loc) · 2.6 KB
/
Copy pathPermutationTest.java
File metadata and controls
80 lines (70 loc) · 2.6 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
75
76
77
78
79
80
package enigma;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.Timeout;
import static org.junit.Assert.*;
import static enigma.TestUtils.*;
/** The suite of all JUnit tests for the Permutation class.
* @author
*/
public class PermutationTest {
/** Testing time limit. */
@Rule
public Timeout globalTimeout = Timeout.seconds(5);
/* ***** TESTING UTILITIES ***** */
private Permutation perm;
private String alpha = UPPER_STRING;
/** Check that perm 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 checkPerm(String testId,
String fromAlpha, String toAlpha) {
int N = fromAlpha.length();
assertEquals(testId + " (wrong length)", N, perm.size());
for (int i = 0; i < N; i += 1) {
char c = fromAlpha.charAt(i), e = toAlpha.charAt(i);
assertEquals(msg(testId, "wrong translation of '%c'", c),
e, perm.permute(c));
assertEquals(msg(testId, "wrong inverse of '%c'", e),
c, perm.invert(e));
int ci = alpha.indexOf(c), ei = alpha.indexOf(e);
assertEquals(msg(testId, "wrong translation of %d", ci),
ei, perm.permute(ci));
assertEquals(msg(testId, "wrong inverse of %d", ei),
ci, perm.invert(ei));
}
}
/* ***** TESTS ***** */
@Test
public void checkIdTransform() {
perm = new Permutation("(AELTPHQXRU)"
+ " (BKNW)"
+ " (CMOY)"
+ " (DFG)"
+ " (IV)"
+ " (JZ) (S)", UPPER);
checkPerm("identity",
"AELTPHQXRUBKNWCMOYDFGIVJZS",
"ELTPHQXRUAKNWBMOYCFGDVIZJS");
}
@Test
public void testInvertChar() {
Permutation p = new Permutation("(PNH) (ABDFIKLZYXW) (JC)",
new CharacterRange('A', 'Z'));
assertEquals(p.invert('B'), 'A');
assertEquals(p.invert('G'), 'G');
}
@Test
public void testPermuteChar() {
Permutation p = new Permutation("(PNH) (ABDFIKLZYXW) "
+ "(JC)", new CharacterRange('A', 'Z'));
assertEquals(p.permute('P'), 'N');
}
@Test
public void testDerangement() {
Permutation p = new Permutation("(PNH) "
+ "(ABDFIKLZYXW) (JC)", new CharacterRange('A', 'Z'));
assertEquals(p.derangement(), true);
}
}