[14727] | 1 | namespace 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)
|
---|
[14746] | 33 | Debug.WriteLine("Id: {0}, Expression: {1}", duplicate.Id, duplicate.Epxression);
|
---|
[14727] | 34 |
|
---|
| 35 | Assert.Fail();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | [TestMethod]
|
---|
| 39 | public void TestExpandGetHashCode() {
|
---|
[14733] | 40 | var expand = PushParser.Parse("( A B C D E F )");
|
---|
[14727] | 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 |
|
---|
[14745] | 53 | var list = new PushProgram(expressions);
|
---|
[14727] | 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 |
|
---|
[14745] | 70 | var list = new PushProgram(expressions.ToArray());
|
---|
[14727] | 71 |
|
---|
| 72 | expressions.Add(list);
|
---|
| 73 |
|
---|
[14745] | 74 | var list2 = new PushProgram(expressions.ToArray());
|
---|
[14727] | 75 | var clone = list2.Copy();
|
---|
| 76 |
|
---|
[14746] | 77 | Assert.AreSame(list, clone.Expressions.Last());
|
---|
| 78 | Assert.AreEqual(list, clone.Expressions.Last());
|
---|
[14727] | 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 |
|
---|
[14745] | 94 | var list = new PushProgram(expressions.ToArray());
|
---|
[14727] | 95 |
|
---|
| 96 | expressions.Add(list);
|
---|
[14745] | 97 | var list2 = new PushProgram(expressions.ToArray());
|
---|
| 98 | var list3 = new PushProgram(expressions.ToArray());
|
---|
[14727] | 99 |
|
---|
| 100 | Assert.AreEqual(list2.GetHashCode(), list3.GetHashCode());
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | } |
---|