Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Tests/Interpreter/Expressions/VectorExpressionTests.cs @ 16399

Last change on this file since 16399 was 15017, checked in by pkimmesw, 8 years ago

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 15.2 KB
Line 
1namespace HeuristicLab.Tests.Interpreter.Expressions {
2  using System.Collections.Generic;
3  using System.Linq;
4
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
7
8  using Microsoft.VisualStudio.TestTools.UnitTesting;
9
10  public abstract class VectorExpressionTests<T> : CommonTests<IReadOnlyList<T>> {
11
12    protected abstract IPushStack<T> LiteralStack { get; }
13    protected abstract IReadOnlyList<T> GetLiterals(int count);
14
15    [TestMethod]
16    [TestProperty("Time", "Short")]
17    [TestCategory("ExpressionTest")]
18    [TestCategory("VectorExpressionTest")]
19    public void TestVectorButLast() {
20      var vector = GetValues(1)[0];
21      Stack.Push(vector);
22
23      var result = vector.Take(vector.Count - 1);
24      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".BUTLAST");
25      interpreter.Run(expression);
26
27      Assert.IsTrue(result.SequenceEqual(Stack.Top));
28    }
29
30    [TestMethod]
31    [TestProperty("Time", "Short")]
32    [TestCategory("ExpressionTest")]
33    [TestCategory("VectorExpressionTest")]
34    public void TestVectorConcat() {
35      var vectors = GetValues(2);
36      Stack.Push(vectors);
37
38      var result = vectors.SelectMany(x => x);
39      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".CONCAT");
40      interpreter.Run(expression);
41
42      Assert.IsTrue(result.SequenceEqual(Stack.Top));
43    }
44
45    [TestMethod]
46    [TestProperty("Time", "Short")]
47    [TestCategory("ExpressionTest")]
48    [TestCategory("VectorExpressionTest")]
49    public void TestVectorConj() {
50      var vectors = GetValues(1);
51      var literals = GetLiterals(1);
52
53      Stack.Push(vectors);
54      LiteralStack.Push(literals);
55
56      var result = new List<T>(vectors[0]);
57      result.AddRange(literals);
58
59      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".CONJ");
60      interpreter.Run(expression);
61
62      Assert.IsTrue(result.SequenceEqual(Stack.Top));
63    }
64
65    [TestMethod]
66    [TestProperty("Time", "Short")]
67    [TestCategory("ExpressionTest")]
68    [TestCategory("VectorExpressionTest")]
69    public void TestVectorContains() {
70      var vectors = Get2Different();
71      var literalContained = vectors[0].Last();
72      var literalNotContained = vectors[1].FirstOrDefault(x => !vectors[0].Contains(x));
73      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".CONTAINS");
74
75      Stack.Push(vectors[0], vectors[0]);
76      LiteralStack.Push(literalContained, literalNotContained);
77
78      interpreter.Run(expression);
79      interpreter.Run(expression);
80
81      Assert.IsTrue(interpreter.BooleanStack[0]);
82      Assert.IsFalse(interpreter.BooleanStack[1]);
83    }
84
85    [TestMethod]
86    [TestProperty("Time", "Short")]
87    [TestCategory("ExpressionTest")]
88    [TestCategory("VectorExpressionTest")]
89    public void TestVectorEmpty() {
90      var emptyVector = new List<T>();
91      var noneEmptyVector = GetValues(1)[0];
92      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".EMPTYVECTOR");
93
94      Stack.Push(emptyVector, noneEmptyVector);
95      interpreter.Run(expression);
96      interpreter.Run(expression);
97
98      Assert.IsTrue(interpreter.BooleanStack[0]);
99      Assert.IsFalse(interpreter.BooleanStack[1]);
100    }
101
102    [TestMethod]
103    [TestProperty("Time", "Short")]
104    [TestCategory("ExpressionTest")]
105    [TestCategory("VectorExpressionTest")]
106    public void TestVectorFirst() {
107      var vector = GetValues(1)[0];
108      var emptyVector = new List<T>();
109      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".FIRST");
110
111      Stack.Push(emptyVector);
112      interpreter.Run(expression);
113      Assert.IsTrue(LiteralStack.IsEmpty);
114
115      Stack.Push(vector);
116      interpreter.Run(expression);
117      Assert.AreEqual(vector.First(), LiteralStack.Top);
118    }
119
120    [TestMethod]
121    [TestProperty("Time", "Short")]
122    [TestCategory("ExpressionTest")]
123    [TestCategory("VectorExpressionTest")]
124    public void TestVectorIndexOf() {
125      var vector = GetValues(1)[0].Distinct().ToList();
126      var emptyVector = new List<T>();
127      var literal = vector.Last();
128      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".INDEXOF");
129
130      Stack.Push(emptyVector);
131      LiteralStack.Push(literal);
132      interpreter.Run(expression);
133      Assert.IsTrue(LiteralStack == interpreter.IntegerStack ? LiteralStack.Count == 1 : LiteralStack.IsEmpty);
134
135      Stack.Push(vector);
136      LiteralStack.Push(literal);
137      interpreter.Run(expression);
138      Assert.AreEqual(vector.Count - 1, interpreter.IntegerStack.Top);
139    }
140
141    [TestMethod]
142    [TestProperty("Time", "Short")]
143    [TestCategory("ExpressionTest")]
144    [TestCategory("VectorExpressionTest")]
145    public void TestVectorIterate() {
146      var vector = GetValues(1)[0];
147      var emptyVector = new List<T>();
148      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".ITERATE");
149      var bodyExpression = new ExecNoopExpression();
150
151      Stack.Push(emptyVector);
152      interpreter.Run(new PushProgram(new[] { expression, bodyExpression }));
153      Assert.IsTrue(LiteralStack.IsEmpty);
154      Assert.AreEqual(0, Stack.Top.Count);
155
156      Stack.Push(vector);
157      interpreter.Run(new PushProgram(new[] { bodyExpression, expression }));
158      Assert.AreEqual(vector.Count, LiteralStack.Count);
159      Assert.IsTrue(LiteralStack.Reverse().SequenceEqual(vector));
160    }
161
162
163    [TestMethod]
164    [TestProperty("Time", "Short")]
165    [TestCategory("ExpressionTest")]
166    [TestCategory("VectorExpressionTest")]
167    public void TestVectorLast() {
168      var vector = GetValues(1)[0];
169      var emptyVector = new List<T>();
170      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".LAST");
171
172      Stack.Push(emptyVector);
173      interpreter.Run(expression);
174      Assert.IsTrue(LiteralStack.IsEmpty);
175
176      Stack.Push(vector);
177      interpreter.Run(expression);
178      Assert.AreEqual(vector.Last(), LiteralStack.Top);
179    }
180
181    [TestMethod]
182    [TestProperty("Time", "Short")]
183    [TestCategory("ExpressionTest")]
184    [TestCategory("VectorExpressionTest")]
185    public void TestVectorLength() {
186      var vector = GetValues(1)[0];
187      var emptyVector = new List<T>();
188      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".LENGTH");
189
190      Stack.Push(emptyVector);
191      interpreter.Run(expression);
192      Assert.AreEqual(0, interpreter.IntegerStack.Top);
193
194      Stack.Push(vector);
195      interpreter.Run(expression);
196      Assert.AreEqual(vector.Count, interpreter.IntegerStack.Top);
197    }
198
199    [TestMethod]
200    [TestProperty("Time", "Short")]
201    [TestCategory("ExpressionTest")]
202    [TestCategory("VectorExpressionTest")]
203    public void TestVectorNth() {
204      var vector = GetValues(1)[0];
205      var emptyVector = new List<T>();
206      var index = vector.Count / 2;
207      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".NTH");
208
209      Stack.Push(emptyVector);
210      interpreter.IntegerStack.Push(index);
211      interpreter.Run(expression);
212      Assert.IsTrue(LiteralStack == interpreter.IntegerStack ? LiteralStack.Count == 1 : LiteralStack.IsEmpty);
213
214      Stack.Push(vector);
215      interpreter.IntegerStack.Push(index);
216      interpreter.Run(expression);
217      Assert.AreEqual(vector[index], LiteralStack.Top);
218    }
219
220    [TestMethod]
221    [TestProperty("Time", "Short")]
222    [TestCategory("ExpressionTest")]
223    [TestCategory("VectorExpressionTest")]
224    public void TestVectorOccurrenceOf() {
225      var vector = GetValues(1)[0];
226      var literal = vector.Last();
227      var result = vector.Count(x => x.Equals(literal));
228      var emptyVector = new List<T>();
229      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".OCCURRENCEOF");
230
231      Stack.Push(emptyVector);
232      LiteralStack.Push(literal);
233      interpreter.Run(expression);
234      Assert.AreEqual(0, interpreter.IntegerStack.Top);
235
236      Stack.Push(vector);
237      LiteralStack.Push(literal);
238      interpreter.Run(expression);
239      Assert.AreEqual(result, interpreter.IntegerStack.Top);
240    }
241
242    [TestMethod]
243    [TestProperty("Time", "Short")]
244    [TestCategory("ExpressionTest")]
245    [TestCategory("VectorExpressionTest")]
246    public void TestVectorPushAll() {
247      var vector = GetValues(1)[0];
248      var emptyVector = new List<T>();
249      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".PUSHALL");
250
251      Stack.Push(emptyVector);
252      interpreter.Run(expression);
253      Assert.IsTrue(LiteralStack.IsEmpty);
254
255      Stack.Push(vector);
256      interpreter.Run(expression);
257      Assert.IsTrue(LiteralStack.Reverse().SequenceEqual(vector));
258    }
259
260    [TestMethod]
261    [TestProperty("Time", "Short")]
262    [TestCategory("ExpressionTest")]
263    [TestCategory("VectorExpressionTest")]
264    public void TestVectorRemove() {
265      var vector = GetValues(1)[0];
266      var emptyVector = new List<T>();
267      var literal = vector.Last();
268      var result = vector.Where(x => !x.Equals(literal));
269      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".REMOVE");
270
271      Stack.Push(emptyVector);
272      LiteralStack.Push(literal);
273      interpreter.Run(expression);
274      Assert.AreEqual(0, Stack.Top.Count);
275
276      Stack.Push(vector);
277      LiteralStack.Push(literal);
278      interpreter.Run(expression);
279      Assert.IsTrue(Stack.Top.SequenceEqual(result));
280    }
281
282    [TestMethod]
283    [TestProperty("Time", "Short")]
284    [TestCategory("ExpressionTest")]
285    [TestCategory("VectorExpressionTest")]
286    public void TestVectorReplace() {
287      var vector = GetValues(1)[0];
288      var emptyVector = new List<T>();
289      var literal = vector.Last();
290      var literalTarget = vector.First();
291      var result = vector.Select(x => x.Equals(literal) ? literalTarget : x);
292      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".REPLACE");
293
294      Stack.Push(emptyVector);
295      LiteralStack.Push(literal, literalTarget);
296      interpreter.Run(expression);
297      Assert.AreEqual(0, Stack.Top.Count);
298
299      Stack.Push(vector);
300      LiteralStack.Push(literal, literalTarget);
301      interpreter.Run(expression);
302      Assert.IsTrue(Stack.Top.SequenceEqual(result));
303    }
304
305    [TestMethod]
306    [TestProperty("Time", "Short")]
307    [TestCategory("ExpressionTest")]
308    [TestCategory("VectorExpressionTest")]
309    public void TestVectorReplaceFirst() {
310      var vector = GetValues(1)[0];
311      var emptyVector = new List<T>();
312      var literal = vector.Last();
313      var literalTarget = vector.First(x => !x.Equals(literal));
314      var firstReplaced = false;
315      var result = vector.Select(x => {
316        if (!x.Equals(literal) || firstReplaced) return x;
317        firstReplaced = true;
318        return literalTarget;
319      }).ToArray();
320
321      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".REPLACEFIRST");
322
323      Stack.Push(emptyVector);
324      LiteralStack.Push(literal, literalTarget);
325      interpreter.Run(expression);
326      Assert.AreEqual(0, Stack.Top.Count);
327
328      Stack.Push(vector);
329      LiteralStack.Push(literal, literalTarget);
330      interpreter.Run(expression);
331      Assert.IsTrue(Stack.Top.SequenceEqual(result));
332    }
333
334    [TestMethod]
335    [TestProperty("Time", "Short")]
336    [TestCategory("ExpressionTest")]
337    [TestCategory("VectorExpressionTest")]
338    public void TestVectorRest() {
339      var vector = GetValues(1)[0];
340      var emptyVector = new List<T>();
341      var result = vector.Skip(1);
342      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".REST");
343
344      Stack.Push(emptyVector);
345      interpreter.Run(expression);
346      Assert.AreEqual(0, Stack.Top.Count);
347
348      Stack.Push(vector);
349      interpreter.Run(expression);
350      Assert.IsTrue(Stack.Top.SequenceEqual(result));
351    }
352
353    [TestMethod]
354    [TestProperty("Time", "Short")]
355    [TestCategory("ExpressionTest")]
356    [TestCategory("VectorExpressionTest")]
357    public void TestVectorReverse() {
358      var vector = GetValues(1)[0];
359      var emptyVector = new List<T>();
360      var result = new List<T>(vector);
361      result.Reverse();
362      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".REVERSE");
363
364      Stack.Push(emptyVector);
365      interpreter.Run(expression);
366      Assert.AreEqual(emptyVector, Stack.Top);
367
368      Stack.Push(vector);
369      interpreter.Run(expression);
370      Assert.IsTrue(Stack.Top.SequenceEqual(result));
371    }
372
373    [TestMethod]
374    [TestProperty("Time", "Short")]
375    [TestCategory("ExpressionTest")]
376    [TestCategory("VectorExpressionTest")]
377    public void TestVectorSet() {
378      var vector = GetValues(1)[0];
379      var emptyVector = new List<T>();
380      var index = vector.Count / 2;
381      var literal = vector.Last();
382      var result = vector.ToArray();
383      result[index] = literal;
384      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".SET");
385
386      Stack.Push(emptyVector);
387      LiteralStack.Push(literal);
388      interpreter.IntegerStack.Push(index);
389      interpreter.Run(expression);
390      Assert.AreEqual(emptyVector, Stack.Top);
391
392      Stack.Push(vector);
393      interpreter.IntegerStack.Push(index);
394      LiteralStack.Push(literal);
395      interpreter.Run(expression);
396      Assert.IsTrue(Stack.Top.SequenceEqual(result));
397    }
398
399    [TestMethod]
400    [TestProperty("Time", "Short")]
401    [TestCategory("ExpressionTest")]
402    [TestCategory("VectorExpressionTest")]
403    public void TestVectorSub() {
404      var vector = GetValues(1)[0];
405      var emptyVector = new List<T>();
406      var start = 0;
407      var end = vector.Count / 2;
408      var result = vector.Skip(start).Take(end).ToArray();
409      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".SUB");
410
411      Stack.Push(emptyVector);
412      interpreter.IntegerStack.Push(start, end);
413      interpreter.Run(expression);
414      Assert.AreEqual(emptyVector, Stack.Top);
415
416      Stack.Push(vector);
417      interpreter.IntegerStack.Push(start, end);
418      interpreter.Run(expression);
419      Assert.IsTrue(Stack.Top.SequenceEqual(result));
420    }
421
422    [TestMethod]
423    [TestProperty("Time", "Short")]
424    [TestCategory("ExpressionTest")]
425    [TestCategory("VectorExpressionTest")]
426    public void TestVectorTake() {
427      var vector = GetValues(1)[0];
428      var emptyVector = new List<T>();
429      var count = vector.Count / 2;
430      var result = vector.Take(count).ToArray();
431      var expression = ExpressionTable.GetStatelessExpression(TypeName + ".TAKE");
432
433      Stack.Push(emptyVector);
434      interpreter.IntegerStack.Push(count);
435      interpreter.Run(expression);
436      Assert.AreEqual(emptyVector, Stack.Top);
437
438      Stack.Push(vector);
439      interpreter.IntegerStack.Push(count);
440      interpreter.Run(expression);
441      Assert.IsTrue(Stack.Top.SequenceEqual(result));
442
443      Stack.Push(vector);
444      interpreter.IntegerStack.Push(count * -1);
445      interpreter.Run(expression);
446      Assert.IsTrue(Stack.Top.SequenceEqual(result));
447
448      Stack.Push(vector);
449      interpreter.IntegerStack.Push(count + vector.Count);
450      interpreter.Run(expression);
451      Assert.IsTrue(Stack.Top.SequenceEqual(result));
452    }
453  }
454}
Note: See TracBrowser for help on using the repository browser.