-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationTest.java
More file actions
99 lines (81 loc) · 2.93 KB
/
PermutationTest.java
File metadata and controls
99 lines (81 loc) · 2.93 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 Mohammed Abu-Sharkh
*/
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("", UPPER);
checkPerm("identity", UPPER_STRING, UPPER_STRING);
}
@Test
public void splitterTest() {
String cycles = "(ABCDE) (FGH) (IJKL)";
String[] expected = {"ABCDE", "FGH", "IJKL"};
assertArrayEquals(expected, Permutation.splitter(cycles));
}
Alphabet letters = new Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
@Test
public void permuteTestwCyclicFeature() {
perm = new Permutation("(ZADT) (YUR)", letters);
assertEquals('D', perm.permute('A'));
assertEquals('Z', perm.permute('T'));
assertEquals('Y', perm.permute('R'));
}
@Test
public void invertTestwCyclicFeature() {
perm = new Permutation("(ZADT) (YUR)", letters);
assertEquals('D', perm.invert('T'));
assertEquals('T', perm.invert('Z'));
assertEquals('U', perm.invert('R'));
}
@Test
public void permTestUsingInts() {
Permutation test = new Permutation(NAVALA.get("III"), UPPER);
assertEquals(1, test.permute(0));
assertEquals(7, test.permute(3));
}
@Test
public void invTestUsingInts() {
Permutation test = new Permutation(NAVALA.get("III"), UPPER);
assertEquals(15, test.invert(4));
assertEquals(6, test.invert(2));
}
}