Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Tests/Interpreter/Expressions/StandardTests.cs @ 18242

Last change on this file since 18242 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: 4.4 KB
Line 
1namespace HeuristicLab.Tests.Interpreter.Expressions {
2  using System;
3  using System.Collections.Generic;
4  using System.Diagnostics;
5  using System.Linq;
6
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Parser;
9
10  using Microsoft.VisualStudio.TestTools.UnitTesting;
11
12  [TestClass]
13  public class StandardTests {
14    [TestMethod]
15    [TestProperty("Time", "Short")]
16    [TestCategory("ExpressionTest")]
17    [TestCategory("StandartTests")]
18    public void TestGetHashCode() {
19      var expressionTypes = from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
20                            from assemblyType in domainAssembly.GetTypes()
21                            where
22                            !assemblyType.IsAbstract && assemblyType.IsSubclassOf(typeof(Expression))
23                            && (assemblyType.GetConstructor(Type.EmptyTypes) != null)
24                            select assemblyType;
25
26      var expressions = expressionTypes.Select(et => Activator.CreateInstance(et) as Expression);
27      var ids = expressions.Select(e => new { Expression = e, Id = e.GetHashCode() });
28
29      var duplicateGroups = ids.GroupBy(s => s.Id).Where(grp => grp.Count() > 2).ToList();
30
31      if (!duplicateGroups.Any())
32        return;
33
34      Debug.WriteLine("Duplicates: ");
35      foreach (var group in duplicateGroups) {
36        foreach (var duplicate in group) {
37          string name;
38          if (!ExpressionTable.TypeToNameTable.TryGetValue(duplicate.Expression.GetType(), out name)) {
39            name = duplicate.Expression.ToString();
40          }
41
42          Debug.WriteLine("Id: {0}, Expression: {1}", duplicate.Id, name);
43        }
44
45        Debug.WriteLine("");
46      }
47
48      Assert.Fail();
49    }
50
51    [TestMethod]
52    [TestProperty("Time", "Short")]
53    [TestCategory("ExpressionTest")]
54    [TestCategory("StandartTests")]
55    public void TestExpandGetHashCode() {
56      var expand = PushParser.Parse("( A B C D E F )");
57
58      expand.GetHashCode();
59    }
60
61    [TestMethod]
62    [TestProperty("Time", "Short")]
63    [TestCategory("ExpressionTest")]
64    [TestCategory("StandartTests")]
65    public void TestExecExpandCopy() {
66      var expressions = new Expression[]
67                          {
68                            new IntegerPushExpression(2), new FloatPushExpression(2.0),
69                            new BooleanPushExpression(true), new NameDefineXExecExpression("A")
70                          };
71
72      var list = new PushProgram(expressions);
73      var clone = list.Copy();
74
75      Assert.AreNotSame(list, clone);
76      Assert.AreEqual(list, clone);
77    }
78
79    [TestMethod]
80    [TestProperty("Time", "Short")]
81    [TestCategory("ExpressionTest")]
82    [TestCategory("StandartTests")]
83    public void TestExecExpandRecursiveCopy() {
84      var expressions = new List<Expression>
85                          {
86                            new IntegerPushExpression(2),
87                            new FloatPushExpression(2.1),
88                            new BooleanPushExpression(true),
89                            new NameDefineXExecExpression("A")
90                          };
91
92      var list = new PushProgram(expressions.ToArray());
93
94      expressions.Add(list);
95
96      var list2 = new PushProgram(expressions.ToArray());
97      var clone = list2.Copy();
98
99      Assert.AreSame(list, clone.Expressions.Last());
100      Assert.AreEqual(list, clone.Expressions.Last());
101
102      Assert.AreNotSame(list2, clone);
103      Assert.AreEqual(list2, clone);
104    }
105
106    [TestMethod]
107    [TestProperty("Time", "Short")]
108    [TestCategory("ExpressionTest")]
109    [TestCategory("StandartTests")]
110    public void TestExecExpandRecursiveGetHashCode() {
111      var expressions = new List<Expression>
112                          {
113                            new IntegerPushExpression(2),
114                            new FloatPushExpression(2.0),
115                            new BooleanPushExpression(true),
116                            new NameDefineXExecExpression("A")
117                          };
118
119      var list = new PushProgram(expressions.ToArray());
120
121      expressions.Add(list);
122      var list2 = new PushProgram(expressions.ToArray());
123      var list3 = new PushProgram(expressions.ToArray());
124
125      Assert.AreEqual(list2.GetHashCode(), list3.GetHashCode());
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.