-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfiltrationTest.java
executable file
·156 lines (134 loc) · 6 KB
/
InfiltrationTest.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import org.junit.Test;
import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by Ziver on 2016-09-20.
*/
public class InfiltrationTest {
@Test
public void breakDecrypt(){
String[] input = new String[0];
assertEquals(null, Infiltration.breakDecrypt(input));
breakDecrypt(null, "");
// breakDecrypt("we will avenge our dead parrot arr",
// "ex eoii jpxbmx cvz uxju sjzzcn jzz");
//
// breakDecrypt(null, "wl jkd");
//
// breakDecrypt(null, "dyd jkl cs");
//
// breakDecrypt(null, "bee");
breakDecrypt("be", "be");
breakDecrypt("be", "ab");
// Test Cases from Per Persson
breakDecrypt(null,
"abc bcd cde def efg fgh ghi hij ijk jkl klm lmn mno nop opq pqr qrs rst stu tuv uvw vwx wxy xyz yza zab ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bc bd be xhfo zywq");
breakDecrypt(null, "aaaaaaaaa");
breakDecrypt("be our rum mbeu be be", "be our rum mbeu be be");
breakDecrypt("sable ship dead blood", "sable ship dead blood");
//breakDecrypt(null, "abc abc abc abc abc abc def def def def def def abc abc abc abc abc abc def def def def def def abc abc abc abc abc abc def def def def def def abc abc abc abc abc abc def def def def def def abc def");
breakDecrypt(null, "be eb our uor ruo oru uro rou rum urm rmu mur umr mru");
breakDecrypt("be our rum", "be our rum");
breakDecrypt("be dead", "eb abca");
breakDecrypt(null, "ef abca");
breakDecrypt(null, "abcdefghijklmnopqrstuvwxyz");
}
@Test
public void timeTest(){
// Test Case from Per Persson
breakDecrypt(null, "abc abc abc abc abc abc def def def def def def abc abc abc abc abc abc def def def def def def abc abc abc abc abc abc def def def def def def abc abc abc abc abc abc def def def def def def abc def");
}
public void breakDecrypt(String expect, String in){
String[] input = (in.isEmpty() ? new String[0] : in.split(" "));
Infiltration.SubstitutionMap map = Infiltration.breakDecrypt(input);
if (map == null)
assertEquals(expect, map);
else
assertEquals(expect, map.decrypt(input));
}
@Test
public void randomTestCase(){
for (int i=0; i <200_000; i++) {
decryptCompare(generateLine());
}
}
public void decryptCompare(String in){
//System.out.println(in);
//System.out.flush();
String[] input = in.split(" ");
//String expect = InfiltrationCamp.breakDecrypt(in);
Infiltration.SubstitutionMap map = Infiltration.breakDecrypt(input);
/*if (map == null)
assertEquals(expect, map);
else
assertEquals(expect, map.decrypt(input));
*/
}
public String generateLine(){
StringBuilder str = new StringBuilder();
int nrOfWords = 1+(int)(Math.random()*20);
for (int i=0; i<nrOfWords; ++i){
if (i != 0) str.append(' ');
int nrOfLetters = 1+(int)(Math.random()*20);
for (int j=0; j < nrOfLetters; j++) {
str.append((char)('a' + (int)(Math.random()*26)));
}
}
return str.toString();
}
@Test
public void mapKeyValidForWord(){
Infiltration.SubstitutionMap map = new Infiltration.SubstitutionMap();
assertTrue(map.keyValidForWord("abcd", "abcd"));
map.applywordToMap("abcd", "abcd");
assertTrue(map.keyValidForWord("abcd", "abcd"));
mapKeyValidForWord("xbcd", "abcd", "abcd", "abcd");
mapKeyValidForWord("axcd", "abcd", "abcd", "abcd");
mapKeyValidForWord("abxd", "abcd", "abcd", "abcd");
mapKeyValidForWord("abcx", "abcd", "abcd", "abcd");
mapKeyValidForWord("abcd", "xbcd", "abcd", "abcd");
mapKeyValidForWord("abcd", "axcd", "abcd", "abcd");
mapKeyValidForWord("abcd", "abxd", "abcd", "abcd");
mapKeyValidForWord("abcd", "abcx", "abcd", "abcd");
// check duplicate references
map = new Infiltration.SubstitutionMap();
assertFalse(map.keyValidForWord("fkcegyr", "captain"));
assertFalse(map.keyValidForWord("captain", "fkcegyr"));
assertTrue (map.keyValidForWord("be", "ab"));
}
private void mapKeyValidForWord(String encAdd, String decAdd, String encValid, String decValid){
Infiltration.SubstitutionMap map = new Infiltration.SubstitutionMap();
map.applywordToMap(encAdd, decAdd);
assertEquals(encAdd.length(), map.size());
assertFalse(map.keyValidForWord(encValid, decValid));
}
@Test
public void mapCopy(){
Infiltration.SubstitutionMap original = new Infiltration.SubstitutionMap();
original.applywordToMap("abcd", "abcd");
Infiltration.SubstitutionMap copy = original.copy();
assertEquals(original.hashCode(), copy.hashCode());
original.applywordToMap("abcd", "lmnk");
assertNotEquals(original.hashCode(), copy.hashCode());
assertEquals(4, copy.size());
assertEquals(4, original.size());
assertEquals('l', original.decode('a'));
assertEquals('m', original.decode('b'));
assertEquals('n', original.decode('c'));
assertEquals('k', original.decode('d'));
assertEquals('a', copy.decode('a'));
assertEquals('b', copy.decode('b'));
assertEquals('c', copy.decode('c'));
assertEquals('d', copy.decode('d'));
assertEquals('a', original.encode('l'));
assertEquals('b', original.encode('m'));
assertEquals('c', original.encode('n'));
assertEquals('d', original.encode('k'));
assertEquals('a', copy.encode('a'));
assertEquals('b', copy.encode('b'));
assertEquals('c', copy.encode('c'));
assertEquals('d', copy.encode('d'));
}
}