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