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