Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Tests/Interpreter/Expressions/StandardTests.cs @ 14733

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

#2665 Storable problem data, Renamings due to typos, Removed GP from class names

File size: 3.6 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    public void TestGetHashCode() {
16      var expressionTypes = from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
17                            from assemblyType in domainAssembly.GetTypes()
18                            where
19                            !assemblyType.IsAbstract && assemblyType.IsSubclassOf(typeof(Expression))
20                            && (assemblyType.GetConstructor(Type.EmptyTypes) != null)
21                            select assemblyType;
22
23      var expressions = expressionTypes.Select(et => Activator.CreateInstance(et) as Expression);
24      var ids = expressions.Select(e => new { Epxression = e, Id = e.GetHashCode() });
25
26      var duplicates = ids.GroupBy(s => s).SelectMany(grp => grp.Skip(1));
27
28      if (!duplicates.Any())
29        return;
30
31      Debug.WriteLine("Duplicates: ");
32      foreach (var duplicate in duplicates)
33         Debug.WriteLine("Id: {0}, Expression: {1}", duplicate.Id, duplicate.Epxression);
34
35      Assert.Fail();
36    }
37
38    [TestMethod]
39    public void TestExpandGetHashCode() {
40      var expand = PushParser.Parse("( A B C D E F )");
41
42      expand.GetHashCode();
43    }
44
45    [TestMethod]
46    public void TestExecExpandCopy() {
47      var expressions = new Expression[]
48                          {
49                            new IntegerPushExpression(2), new FloatPushExpression(2.0),
50                            new BooleanPushExpression(true), new NameDefineXExecExpression("A")
51                          };
52
53      var list = new ExecExpandExpression(expressions);
54      var clone = list.Copy();
55
56      Assert.AreNotSame(list, clone);
57      Assert.AreEqual(list, clone);
58    }
59
60    [TestMethod]
61    public void TestExecExpandRecursiveCopy() {
62      var expressions = new List<Expression>
63                          {
64                            new IntegerPushExpression(2),
65                            new FloatPushExpression(2.1),
66                            new BooleanPushExpression(true),
67                            new NameDefineXExecExpression("A")
68                          };
69
70      var list = new ExecExpandExpression(expressions.ToArray());
71
72      expressions.Add(list);
73
74      var list2 = new ExecExpandExpression(expressions.ToArray());
75      var clone = list2.Copy();
76
77      Assert.AreSame(list, clone.State.Expressions.Last());
78      Assert.AreEqual(list, clone.State.Expressions.Last());
79
80      Assert.AreNotSame(list2, clone);
81      Assert.AreEqual(list2, clone);
82    }
83
84    [TestMethod]
85    public void TestExecExpandRecursiveGetHashCode() {
86      var expressions = new List<Expression>
87                          {
88                            new IntegerPushExpression(2),
89                            new FloatPushExpression(2.0),
90                            new BooleanPushExpression(true),
91                            new NameDefineXExecExpression("A")
92                          };
93
94      var list = new ExecExpandExpression(expressions.ToArray());
95
96      expressions.Add(list);
97      var list2 = new ExecExpandExpression(expressions.ToArray());
98      var list3 = new ExecExpandExpression(expressions.ToArray());
99
100      Assert.AreEqual(list2.GetHashCode(), list3.GetHashCode());
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.