1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Globalization;
|
---|
5 | using HeuristicLab.Algorithms.Bandits;
|
---|
6 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
|
---|
9 | [TestClass]
|
---|
10 | public class TestSequence {
|
---|
11 |
|
---|
12 | [TestMethod]
|
---|
13 | public void TestCtorAndProperties() {
|
---|
14 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
15 | {
|
---|
16 | var s = new Sequence("A");
|
---|
17 | Assert.AreEqual(1, s.Length);
|
---|
18 | Assert.AreEqual(false, s.IsTerminal);
|
---|
19 | Assert.AreEqual(0, s.FirstNonTerminalIndex);
|
---|
20 | Assert.AreEqual('A', s.FirstNonTerminal);
|
---|
21 | }
|
---|
22 | {
|
---|
23 | var s = new Sequence("AB");
|
---|
24 | Assert.AreEqual(2, s.Length);
|
---|
25 | Assert.AreEqual(false, s.IsTerminal);
|
---|
26 | Assert.AreEqual(0, s.FirstNonTerminalIndex);
|
---|
27 | Assert.AreEqual('A', s.FirstNonTerminal);
|
---|
28 | }
|
---|
29 | {
|
---|
30 | var s = new Sequence("aAB");
|
---|
31 | Assert.AreEqual(3, s.Length);
|
---|
32 | Assert.AreEqual(false, s.IsTerminal);
|
---|
33 | Assert.AreEqual(1, s.FirstNonTerminalIndex);
|
---|
34 | Assert.AreEqual('A', s.FirstNonTerminal);
|
---|
35 | }
|
---|
36 | {
|
---|
37 | var s = new Sequence("aaaaA");
|
---|
38 | Assert.AreEqual(5, s.Length);
|
---|
39 | Assert.AreEqual(false, s.IsTerminal);
|
---|
40 | Assert.AreEqual(4, s.FirstNonTerminalIndex);
|
---|
41 | Assert.AreEqual('A', s.FirstNonTerminal);
|
---|
42 | }
|
---|
43 | {
|
---|
44 | var s = new Sequence("aaaa");
|
---|
45 | Assert.AreEqual(4, s.Length);
|
---|
46 | Assert.AreEqual(true, s.IsTerminal);
|
---|
47 | Assert.AreEqual(-1, s.FirstNonTerminalIndex);
|
---|
48 | try {
|
---|
49 | var nt = s.FirstNonTerminal;
|
---|
50 | Assert.Fail();
|
---|
51 | } catch (IndexOutOfRangeException e) {
|
---|
52 | }
|
---|
53 | }
|
---|
54 | {
|
---|
55 | var s = new Sequence("aaaaA");
|
---|
56 | var t = new Sequence(s);
|
---|
57 | Assert.AreEqual(5, t.Length);
|
---|
58 | Assert.AreEqual(false, t.IsTerminal);
|
---|
59 | Assert.AreEqual(4, t.FirstNonTerminalIndex);
|
---|
60 | Assert.AreEqual('A', t.FirstNonTerminal);
|
---|
61 |
|
---|
62 | t.ReplaceAt(0, 1, new Sequence("Z"));
|
---|
63 | Assert.AreEqual(5, t.Length);
|
---|
64 | Assert.AreEqual(false, t.IsTerminal);
|
---|
65 | Assert.AreEqual(0, t.FirstNonTerminalIndex);
|
---|
66 | Assert.AreEqual('Z', t.FirstNonTerminal);
|
---|
67 |
|
---|
68 | // s must not be changed
|
---|
69 | Assert.AreEqual(5, s.Length);
|
---|
70 | Assert.AreEqual(false, s.IsTerminal);
|
---|
71 | Assert.AreEqual(4, s.FirstNonTerminalIndex);
|
---|
72 | Assert.AreEqual('A', s.FirstNonTerminal);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | [TestMethod]
|
---|
77 | public void TestReplaceAt() {
|
---|
78 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
79 | {
|
---|
80 | var s = new Sequence("A");
|
---|
81 | var t = new Sequence("B");
|
---|
82 |
|
---|
83 | s.ReplaceAt(0, 1, t);
|
---|
84 | Assert.AreEqual('B', s.FirstNonTerminal);
|
---|
85 | Assert.AreEqual(1, s.Length);
|
---|
86 | }
|
---|
87 | {
|
---|
88 | var s = new Sequence("A");
|
---|
89 | var t = new Sequence("b");
|
---|
90 |
|
---|
91 | s.ReplaceAt(0, 1, t);
|
---|
92 | Assert.AreEqual(-1, s.FirstNonTerminalIndex);
|
---|
93 | Assert.AreEqual(1, s.Length);
|
---|
94 | }
|
---|
95 | {
|
---|
96 | var s = new Sequence("a");
|
---|
97 | var t = new Sequence("B");
|
---|
98 |
|
---|
99 | s.ReplaceAt(0, 1, t);
|
---|
100 | Assert.AreEqual('B', s.FirstNonTerminal);
|
---|
101 | Assert.AreEqual(1, s.Length);
|
---|
102 | }
|
---|
103 | {
|
---|
104 | var s = new Sequence("AB");
|
---|
105 | var t = new Sequence("a");
|
---|
106 |
|
---|
107 | s.ReplaceAt(0, 1, t);
|
---|
108 | Assert.AreEqual(1, s.FirstNonTerminalIndex);
|
---|
109 | Assert.AreEqual('B', s.FirstNonTerminal);
|
---|
110 | Assert.AreEqual(2, s.Length);
|
---|
111 | }
|
---|
112 | {
|
---|
113 | var s = new Sequence("AB");
|
---|
114 | var t = new Sequence("aa");
|
---|
115 |
|
---|
116 | s.ReplaceAt(0, 1, t);
|
---|
117 | Assert.AreEqual(2, s.FirstNonTerminalIndex);
|
---|
118 | Assert.AreEqual('B', s.FirstNonTerminal);
|
---|
119 | Assert.AreEqual(3, s.Length);
|
---|
120 | Assert.AreEqual("aaB", s.ToString());
|
---|
121 | }
|
---|
122 | {
|
---|
123 | var s = new Sequence("AB");
|
---|
124 | var t = new Sequence("bb");
|
---|
125 |
|
---|
126 | s.ReplaceAt(1, 1, t);
|
---|
127 | Assert.AreEqual(0, s.FirstNonTerminalIndex);
|
---|
128 | Assert.AreEqual('A', s.FirstNonTerminal);
|
---|
129 | Assert.AreEqual(3, s.Length);
|
---|
130 | Assert.AreEqual("Abb", s.ToString());
|
---|
131 | }
|
---|
132 | {
|
---|
133 | var s = new Sequence("AB");
|
---|
134 | var t = new Sequence("aA");
|
---|
135 |
|
---|
136 | s.ReplaceAt(0, 1, t);
|
---|
137 | Assert.AreEqual(1, s.FirstNonTerminalIndex);
|
---|
138 | Assert.AreEqual('A', s.FirstNonTerminal);
|
---|
139 | Assert.AreEqual(3, s.Length);
|
---|
140 | Assert.AreEqual("aAB", s.ToString());
|
---|
141 | }
|
---|
142 | {
|
---|
143 | var s = new Sequence("AaaB");
|
---|
144 | var t = new Sequence("a");
|
---|
145 |
|
---|
146 | s.ReplaceAt(0, 3, t);
|
---|
147 | Assert.AreEqual(1, s.FirstNonTerminalIndex);
|
---|
148 | Assert.AreEqual('B', s.FirstNonTerminal);
|
---|
149 | Assert.AreEqual(2, s.Length);
|
---|
150 | Assert.AreEqual("aB", s.ToString());
|
---|
151 | }
|
---|
152 | {
|
---|
153 | var s = new Sequence("AaaB");
|
---|
154 | var t = new Sequence("aAa");
|
---|
155 |
|
---|
156 | s.ReplaceAt(0, 3, t);
|
---|
157 | Assert.AreEqual(1, s.FirstNonTerminalIndex);
|
---|
158 | Assert.AreEqual('A', s.FirstNonTerminal);
|
---|
159 | Assert.AreEqual("aAaB", s.ToString());
|
---|
160 | }
|
---|
161 | {
|
---|
162 | var s = new Sequence("aaaA");
|
---|
163 | var t = new Sequence("aaa");
|
---|
164 |
|
---|
165 | s.ReplaceAt(3, 1, t);
|
---|
166 | Assert.AreEqual(-1, s.FirstNonTerminalIndex);
|
---|
167 | Assert.AreEqual("aaaaaa", s.ToString());
|
---|
168 | }
|
---|
169 | {
|
---|
170 | var s = new Sequence("aaaA");
|
---|
171 | var t = new Sequence("aaa");
|
---|
172 |
|
---|
173 | s.ReplaceAt(1, 3, t);
|
---|
174 | Assert.AreEqual(-1, s.FirstNonTerminalIndex);
|
---|
175 | Assert.AreEqual("aaaa", s.ToString());
|
---|
176 | }
|
---|
177 | {
|
---|
178 | var s = new Sequence("aaaA");
|
---|
179 | var t = new Sequence("aaa");
|
---|
180 |
|
---|
181 | s.ReplaceAt(0, 4, t);
|
---|
182 | Assert.AreEqual(-1, s.FirstNonTerminalIndex);
|
---|
183 | Assert.AreEqual("aaa", s.ToString());
|
---|
184 | }
|
---|
185 | {
|
---|
186 | var s = new Sequence("aaaA");
|
---|
187 | var t = new Sequence("aAaAA");
|
---|
188 |
|
---|
189 | s.ReplaceAt(0, 4, t);
|
---|
190 | Assert.AreEqual(1, s.FirstNonTerminalIndex);
|
---|
191 | Assert.AreEqual("aAaAA", s.ToString());
|
---|
192 | }
|
---|
193 |
|
---|
194 | // testing length checks
|
---|
195 | {
|
---|
196 | var s = new Sequence("aA");
|
---|
197 | var t = new Sequence("aA");
|
---|
198 |
|
---|
199 | try {
|
---|
200 | s.ReplaceAt(0, 4, t);
|
---|
201 | Assert.Fail();
|
---|
202 | } catch (ArgumentException) { }
|
---|
203 |
|
---|
204 | s.ReplaceAt(0, 2, t); // should work
|
---|
205 |
|
---|
206 | try {
|
---|
207 | s.ReplaceAt(0, 3, t);
|
---|
208 | Assert.Fail();
|
---|
209 | } catch (ArgumentException) { }
|
---|
210 |
|
---|
211 | try {
|
---|
212 | s.ReplaceAt(1, 2, t);
|
---|
213 | Assert.Fail();
|
---|
214 | } catch (ArgumentException) { }
|
---|
215 |
|
---|
216 | s.ReplaceAt(1, 1, new Sequence("A")); // should work
|
---|
217 |
|
---|
218 | try {
|
---|
219 | s.ReplaceAt(-1, 2, t);
|
---|
220 | Assert.Fail();
|
---|
221 | } catch (ArgumentException) { }
|
---|
222 |
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | [TestMethod]
|
---|
227 | public void TestSubsequence() {
|
---|
228 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
229 | {
|
---|
230 | var s = new Sequence("AAaaaAA");
|
---|
231 | var sub = s.Subsequence(0, 7);
|
---|
232 |
|
---|
233 | Assert.AreEqual(0, sub.FirstNonTerminalIndex);
|
---|
234 | Assert.AreEqual("AAaaaAA", sub.ToString());
|
---|
235 | }
|
---|
236 | {
|
---|
237 | var s = new Sequence("AAaaaAA");
|
---|
238 | var sub = s.Subsequence(0, 3);
|
---|
239 |
|
---|
240 | Assert.AreEqual(0, sub.FirstNonTerminalIndex);
|
---|
241 | Assert.AreEqual("AAa", sub.ToString());
|
---|
242 | }
|
---|
243 | {
|
---|
244 | var s = new Sequence("AAaaaAA");
|
---|
245 | var sub = s.Subsequence(2, 3);
|
---|
246 |
|
---|
247 | Assert.AreEqual(-1, sub.FirstNonTerminalIndex);
|
---|
248 | Assert.AreEqual("aaa", sub.ToString());
|
---|
249 | }
|
---|
250 | {
|
---|
251 | var s = new Sequence("AAaaaAA");
|
---|
252 | var sub = s.Subsequence(2, 4);
|
---|
253 |
|
---|
254 | Assert.AreEqual(3, sub.FirstNonTerminalIndex);
|
---|
255 | Assert.AreEqual("aaaA", sub.ToString());
|
---|
256 | }
|
---|
257 | {
|
---|
258 | var s = new Sequence("AAaaaAA");
|
---|
259 | var sub = s.Subsequence(5, 2);
|
---|
260 |
|
---|
261 | Assert.AreEqual(0, sub.FirstNonTerminalIndex);
|
---|
262 | Assert.AreEqual("AA", sub.ToString());
|
---|
263 | }
|
---|
264 | {
|
---|
265 | var s = new Sequence("aaaAA");
|
---|
266 | var sub = s.Subsequence(2, 3);
|
---|
267 |
|
---|
268 | Assert.AreEqual(1, sub.FirstNonTerminalIndex);
|
---|
269 | Assert.AreEqual("aAA", sub.ToString());
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | [TestMethod]
|
---|
274 | public void TestReadonlySequence() {
|
---|
275 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
276 | {
|
---|
277 | var s = new ReadonlySequence("AAaaaAA");
|
---|
278 | var sub = s.Subsequence(0, 7);
|
---|
279 |
|
---|
280 | Assert.AreEqual(0, sub.FirstNonTerminalIndex);
|
---|
281 | Assert.AreEqual("AAaaaAA", sub.ToString());
|
---|
282 | }
|
---|
283 | {
|
---|
284 | var s = new ReadonlySequence("AAaaaAA");
|
---|
285 | var sub = s.Subsequence(0, 3);
|
---|
286 |
|
---|
287 | Assert.AreEqual(0, sub.FirstNonTerminalIndex);
|
---|
288 | Assert.AreEqual("AAa", sub.ToString());
|
---|
289 |
|
---|
290 | sub = sub.Subsequence(1, 2);
|
---|
291 | Assert.AreEqual(0, sub.FirstNonTerminalIndex);
|
---|
292 | Assert.AreEqual("Aa", sub.ToString());
|
---|
293 | }
|
---|
294 | {
|
---|
295 | var s = new ReadonlySequence("AAaaaAA");
|
---|
296 | var sub = s.Subsequence(2, 3);
|
---|
297 |
|
---|
298 | Assert.AreEqual(-1, sub.FirstNonTerminalIndex);
|
---|
299 | Assert.AreEqual("aaa", sub.ToString());
|
---|
300 | }
|
---|
301 | {
|
---|
302 | var s = new ReadonlySequence("AAaaaAA");
|
---|
303 | var sub = s.Subsequence(2, 4);
|
---|
304 |
|
---|
305 | Assert.AreEqual(3, sub.FirstNonTerminalIndex);
|
---|
306 | Assert.AreEqual("aaaA", sub.ToString());
|
---|
307 | }
|
---|
308 | {
|
---|
309 | var s = new ReadonlySequence("AAaaaAA");
|
---|
310 | var sub = s.Subsequence(5, 2);
|
---|
311 |
|
---|
312 | Assert.AreEqual(0, sub.FirstNonTerminalIndex);
|
---|
313 | Assert.AreEqual("AA", sub.ToString());
|
---|
314 | }
|
---|
315 | {
|
---|
316 | var s = new ReadonlySequence("aaaAA");
|
---|
317 | var sub = s.Subsequence(2, 3);
|
---|
318 |
|
---|
319 | Assert.AreEqual(1, sub.FirstNonTerminalIndex);
|
---|
320 | Assert.AreEqual("aAA", sub.ToString());
|
---|
321 | }
|
---|
322 |
|
---|
323 | }
|
---|
324 | }
|
---|
325 | }
|
---|