namespace HeuristicLab.Tests.Interpreter.Expressions { using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; using HeuristicLab.Problems.ProgramSynthesis.Push.Parser; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class StandardTests { [TestMethod] public void TestGetHashCode() { var expressionTypes = from domainAssembly in AppDomain.CurrentDomain.GetAssemblies() from assemblyType in domainAssembly.GetTypes() where !assemblyType.IsAbstract && assemblyType.IsSubclassOf(typeof(Expression)) && (assemblyType.GetConstructor(Type.EmptyTypes) != null) select assemblyType; var expressions = expressionTypes.Select(et => Activator.CreateInstance(et) as Expression); var ids = expressions.Select(e => new { Epxression = e, Id = e.GetHashCode() }); var duplicates = ids.GroupBy(s => s).SelectMany(grp => grp.Skip(1)); if (!duplicates.Any()) return; Debug.WriteLine("Duplicates: "); foreach (var duplicate in duplicates) Debug.WriteLine("Id: {0}, Expression: {1}", duplicate.Id, duplicate.Epxression); Assert.Fail(); } [TestMethod] public void TestExpandGetHashCode() { var expand = PushParser.Parse("( A B C D E F )"); expand.GetHashCode(); } [TestMethod] public void TestExecExpandCopy() { var expressions = new Expression[] { new IntegerPushExpression(2), new FloatPushExpression(2.0), new BooleanPushExpression(true), new NameDefineXExecExpression("A") }; var list = new PushProgram(expressions); var clone = list.Copy(); Assert.AreNotSame(list, clone); Assert.AreEqual(list, clone); } [TestMethod] public void TestExecExpandRecursiveCopy() { var expressions = new List { new IntegerPushExpression(2), new FloatPushExpression(2.1), new BooleanPushExpression(true), new NameDefineXExecExpression("A") }; var list = new PushProgram(expressions.ToArray()); expressions.Add(list); var list2 = new PushProgram(expressions.ToArray()); var clone = list2.Copy(); Assert.AreSame(list, clone.Expressions.Last()); Assert.AreEqual(list, clone.Expressions.Last()); Assert.AreNotSame(list2, clone); Assert.AreEqual(list2, clone); } [TestMethod] public void TestExecExpandRecursiveGetHashCode() { var expressions = new List { new IntegerPushExpression(2), new FloatPushExpression(2.0), new BooleanPushExpression(true), new NameDefineXExecExpression("A") }; var list = new PushProgram(expressions.ToArray()); expressions.Add(list); var list2 = new PushProgram(expressions.ToArray()); var list3 = new PushProgram(expressions.ToArray()); Assert.AreEqual(list2.GetHashCode(), list3.GetHashCode()); } } }