[15771] | 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;
|
---|
[14727] | 7 |
|
---|
[15771] | 8 | namespace HeuristicLab.Tests.Interpreter.Expressions {
|
---|
[14727] | 9 | [TestClass]
|
---|
| 10 | public class StandardTests {
|
---|
| 11 | [TestMethod]
|
---|
[15017] | 12 | [TestProperty("Time", "Short")]
|
---|
| 13 | [TestCategory("ExpressionTest")]
|
---|
| 14 | [TestCategory("StandartTests")]
|
---|
[14727] | 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);
|
---|
[15017] | 24 | var ids = expressions.Select(e => new { Expression = e, Id = e.GetHashCode() });
|
---|
[14727] | 25 |
|
---|
[15017] | 26 | var duplicateGroups = ids.GroupBy(s => s.Id).Where(grp => grp.Count() > 2).ToList();
|
---|
[14727] | 27 |
|
---|
[15017] | 28 | if (!duplicateGroups.Any())
|
---|
[14727] | 29 | return;
|
---|
| 30 |
|
---|
| 31 | Debug.WriteLine("Duplicates: ");
|
---|
[15017] | 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 | }
|
---|
[14727] | 38 |
|
---|
[15017] | 39 | Debug.WriteLine("Id: {0}, Expression: {1}", duplicate.Id, name);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | Debug.WriteLine("");
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[14727] | 45 | Assert.Fail();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | [TestMethod]
|
---|
[15017] | 49 | [TestProperty("Time", "Short")]
|
---|
| 50 | [TestCategory("ExpressionTest")]
|
---|
| 51 | [TestCategory("StandartTests")]
|
---|
[14727] | 52 | public void TestExpandGetHashCode() {
|
---|
[14733] | 53 | var expand = PushParser.Parse("( A B C D E F )");
|
---|
[14727] | 54 |
|
---|
| 55 | expand.GetHashCode();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | [TestMethod]
|
---|
[15017] | 59 | [TestProperty("Time", "Short")]
|
---|
| 60 | [TestCategory("ExpressionTest")]
|
---|
| 61 | [TestCategory("StandartTests")]
|
---|
[14727] | 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 |
|
---|
[14745] | 69 | var list = new PushProgram(expressions);
|
---|
[14727] | 70 | var clone = list.Copy();
|
---|
| 71 |
|
---|
| 72 | Assert.AreNotSame(list, clone);
|
---|
| 73 | Assert.AreEqual(list, clone);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | [TestMethod]
|
---|
[15017] | 77 | [TestProperty("Time", "Short")]
|
---|
| 78 | [TestCategory("ExpressionTest")]
|
---|
| 79 | [TestCategory("StandartTests")]
|
---|
[14727] | 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 |
|
---|
[14745] | 89 | var list = new PushProgram(expressions.ToArray());
|
---|
[14727] | 90 |
|
---|
| 91 | expressions.Add(list);
|
---|
| 92 |
|
---|
[14745] | 93 | var list2 = new PushProgram(expressions.ToArray());
|
---|
[14727] | 94 | var clone = list2.Copy();
|
---|
| 95 |
|
---|
[14746] | 96 | Assert.AreSame(list, clone.Expressions.Last());
|
---|
| 97 | Assert.AreEqual(list, clone.Expressions.Last());
|
---|
[14727] | 98 |
|
---|
| 99 | Assert.AreNotSame(list2, clone);
|
---|
| 100 | Assert.AreEqual(list2, clone);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | [TestMethod]
|
---|
[15017] | 104 | [TestProperty("Time", "Short")]
|
---|
| 105 | [TestCategory("ExpressionTest")]
|
---|
| 106 | [TestCategory("StandartTests")]
|
---|
[14727] | 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 |
|
---|
[14745] | 116 | var list = new PushProgram(expressions.ToArray());
|
---|
[14727] | 117 |
|
---|
| 118 | expressions.Add(list);
|
---|
[14745] | 119 | var list2 = new PushProgram(expressions.ToArray());
|
---|
| 120 | var list3 = new PushProgram(expressions.ToArray());
|
---|
[14727] | 121 |
|
---|
| 122 | Assert.AreEqual(list2.GetHashCode(), list3.GetHashCode());
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | } |
---|