1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.sequence;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Random;
23
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28
29 public class SequencesComparatorTest {
30
31 private List<String> before;
32 private List<String> after;
33 private int[] length;
34
35 @Test
36 public void testLength() {
37 for (int i = 0; i < before.size(); ++i) {
38 final SequencesComparator<Character> comparator =
39 new SequencesComparator<>(sequence(before.get(i)),
40 sequence(after.get(i)));
41 Assert.assertEquals(length[i], comparator.getScript().getModifications());
42 }
43 }
44
45 @Test
46 public void testExecution() {
47 final ExecutionVisitor<Character> ev = new ExecutionVisitor<>();
48 for (int i = 0; i < before.size(); ++i) {
49 ev.setList(sequence(before.get(i)));
50 new SequencesComparator<>(sequence(before.get(i)),
51 sequence(after.get(i))).getScript().visit(ev);
52 Assert.assertEquals(after.get(i), ev.getString());
53 }
54 }
55
56 @Test
57 public void testMinimal() {
58 final String[] shadokAlph = new String[] {
59 new String("GA"),
60 new String("BU"),
61 new String("ZO"),
62 new String("MEU")
63 };
64 final List<String> sentenceBefore = new ArrayList<>();
65 final List<String> sentenceAfter = new ArrayList<>();
66 sentenceBefore.add(shadokAlph[0]);
67 sentenceBefore.add(shadokAlph[2]);
68 sentenceBefore.add(shadokAlph[3]);
69 sentenceBefore.add(shadokAlph[1]);
70 sentenceBefore.add(shadokAlph[0]);
71 sentenceBefore.add(shadokAlph[0]);
72 sentenceBefore.add(shadokAlph[2]);
73 sentenceBefore.add(shadokAlph[1]);
74 sentenceBefore.add(shadokAlph[3]);
75 sentenceBefore.add(shadokAlph[0]);
76 sentenceBefore.add(shadokAlph[2]);
77 sentenceBefore.add(shadokAlph[1]);
78 sentenceBefore.add(shadokAlph[3]);
79 sentenceBefore.add(shadokAlph[2]);
80 sentenceBefore.add(shadokAlph[2]);
81 sentenceBefore.add(shadokAlph[0]);
82 sentenceBefore.add(shadokAlph[1]);
83 sentenceBefore.add(shadokAlph[3]);
84 sentenceBefore.add(shadokAlph[0]);
85 sentenceBefore.add(shadokAlph[3]);
86
87 final Random random = new Random(4564634237452342L);
88
89 for (int nbCom = 0; nbCom <= 40; nbCom+=5) {
90 sentenceAfter.clear();
91 sentenceAfter.addAll(sentenceBefore);
92 for (int i = 0; i<nbCom; i++) {
93 if (random.nextInt(2) == 0) {
94 sentenceAfter.add(random.nextInt(sentenceAfter.size() + 1),
95 shadokAlph[random.nextInt(4)]);
96 } else {
97 sentenceAfter.remove(random.nextInt(sentenceAfter.size()));
98 }
99 }
100
101 final SequencesComparator<String> comparator =
102 new SequencesComparator<>(sentenceBefore, sentenceAfter);
103 Assert.assertTrue(comparator.getScript().getModifications() <= nbCom);
104 }
105 }
106
107 @Test
108 public void testShadok() {
109 final int lgMax = 5;
110 final String[] shadokAlph = new String[] {
111 new String("GA"),
112 new String("BU"),
113 new String("ZO"),
114 new String("MEU")
115 };
116 List<List<String>> shadokSentences = new ArrayList<>();
117 for (int lg=0; lg<lgMax; ++lg) {
118 final List<List<String>> newTab = new ArrayList<>();
119 newTab.add(new ArrayList<String>());
120 for (final String element : shadokAlph) {
121 for (final List<String> sentence : shadokSentences) {
122 final List<String> newSentence = new ArrayList<>(sentence);
123 newSentence.add(element);
124 newTab.add(newSentence);
125 }
126 }
127 shadokSentences = newTab;
128 }
129
130 final ExecutionVisitor<String> ev = new ExecutionVisitor<>();
131
132 for (int i = 0; i < shadokSentences.size(); ++i) {
133 for (final List<String> shadokSentence : shadokSentences) {
134 ev.setList(shadokSentences.get(i));
135 new SequencesComparator<>(shadokSentences.get(i),
136 shadokSentence).getScript().visit(ev);
137
138 final StringBuilder concat = new StringBuilder();
139 for (final String s : shadokSentence) {
140 concat.append(s);
141 }
142 Assert.assertEquals(concat.toString(), ev.getString());
143 }
144 }
145 }
146
147 private List<Character> sequence(final String string) {
148 final List<Character> list = new ArrayList<>();
149 for (int i = 0; i < string.length(); ++i) {
150 list.add(new Character(string.charAt(i)));
151 }
152 return list;
153 }
154
155 private class ExecutionVisitor<T> implements CommandVisitor<T> {
156
157 private List<T> v;
158 private int index;
159
160 public void setList(final List<T> array) {
161 v = new ArrayList<>(array);
162 index = 0;
163 }
164
165 @Override
166 public void visitInsertCommand(final T object) {
167 v.add(index++, object);
168 }
169
170 @Override
171 public void visitKeepCommand(final T object) {
172 ++index;
173 }
174
175 @Override
176 public void visitDeleteCommand(final T object) {
177 v.remove(index);
178 }
179
180 public String getString() {
181 final StringBuilder buffer = new StringBuilder();
182 for (final T c : v) {
183 buffer.append(c);
184 }
185 return buffer.toString();
186 }
187
188 }
189
190 @Before
191 public void setUp() {
192
193 before = Arrays.asList(
194 "bottle",
195 "nematode knowledge",
196 "",
197 "aa",
198 "prefixed string",
199 "ABCABBA",
200 "glop glop",
201 "coq",
202 "spider-man");
203
204 after = Arrays.asList(
205 "noodle",
206 "empty bottle",
207 "",
208 "C",
209 "prefix",
210 "CBABAC",
211 "pas glop pas glop",
212 "ane",
213 "klingon");
214
215 length = new int[] {
216 6,
217 16,
218 0,
219 3,
220 9,
221 5,
222 8,
223 6,
224 13
225 };
226
227 }
228
229 @After
230 public void tearDown() {
231 before = null;
232 after = null;
233 length = null;
234 }
235
236 }