using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using HeuristicLab.Problems.ProgramSynthesis; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Tests.Interpreter.Expressions { [TestClass] public class StandardTests { [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StandartTests")] 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 { Expression = e, Id = e.GetHashCode() }); var duplicateGroups = ids.GroupBy(s => s.Id).Where(grp => grp.Count() > 2).ToList(); if (!duplicateGroups.Any()) return; Debug.WriteLine("Duplicates: "); foreach (var group in duplicateGroups) { foreach (var duplicate in group) { string name; if (!ExpressionTable.TypeToNameTable.TryGetValue(duplicate.Expression.GetType(), out name)) { name = duplicate.Expression.ToString(); } Debug.WriteLine("Id: {0}, Expression: {1}", duplicate.Id, name); } Debug.WriteLine(""); } Assert.Fail(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StandartTests")] public void TestExpandGetHashCode() { var expand = PushParser.Parse("( A B C D E F )"); expand.GetHashCode(); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StandartTests")] 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] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StandartTests")] 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] [TestProperty("Time", "Short")] [TestCategory("ExpressionTest")] [TestCategory("StandartTests")] 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()); } } }