source: branches/2886_SymRegGrammarEnumeration/Test/TreeHashingTest.cs @ 15725

Last change on this file since 15725 was 15725, checked in by lkammere, 20 months ago

#2886: Refactor tree hash function.

File size: 4.3 KB
Line 
1using System.Linq;
2using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
3using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5
6namespace Test {
7  [TestClass]
8  public class TreeHashingTest {
9
10    private Grammar grammar;
11    private TerminalSymbol varA;
12    private TerminalSymbol varB;
13    private TerminalSymbol varC;
14
15    [TestInitialize]
16    public void InitTest() {
17      grammar = new Grammar(new[] { "a", "b", "c" });
18
19      varA = grammar.Var.VariableTerminalSymbols.First(s => s.StringRepresentation == "a");
20      varB = grammar.Var.VariableTerminalSymbols.First(s => s.StringRepresentation == "b");
21      varC = grammar.Var.VariableTerminalSymbols.First(s => s.StringRepresentation == "c");
22    }
23
24    [TestMethod]
25    [TestCategory("TreeHashing")]
26    public void SimpleEqualityAddition() {
27      SymbolString s1 = new SymbolString(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
28      SymbolString s2 = new SymbolString(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
29
30      int hash1 = grammar.CalcHashCode(s1);
31      int hash2 = grammar.CalcHashCode(s2);
32
33      Assert.AreEqual(hash1, hash2);
34    }
35
36    [TestMethod]
37    [TestCategory("TreeHashing")]
38    public void SimpleInequalityAddition() {
39      SymbolString s1 = new SymbolString(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
40      SymbolString s2 = new SymbolString(new[] { varB, varB, grammar.Addition, varB, grammar.Addition });
41
42      int hash1 = grammar.CalcHashCode(s1);
43      int hash2 = grammar.CalcHashCode(s2);
44
45      Assert.AreNotEqual(hash1, hash2);
46    }
47
48    [TestMethod]
49    [TestCategory("TreeHashing")]
50    public void CommutativityAddition() {
51      SymbolString s1 = new SymbolString(new[] { varA, varB, grammar.Addition });
52      SymbolString s2 = new SymbolString(new[] { varB, varA, grammar.Addition });
53
54      int hash1 = grammar.CalcHashCode(s1);
55      int hash2 = grammar.CalcHashCode(s2);
56
57      Assert.AreEqual(hash1, hash2);
58    }
59
60    [TestMethod]
61    [TestCategory("TreeHashing")]
62    public void AssociativityAddition() {
63      SymbolString s1 = new SymbolString(new[] { varA, varB, grammar.Addition, varA, grammar.Addition });
64      SymbolString s2 = new SymbolString(new[] { varA, varB, varA, grammar.Addition, grammar.Addition });
65
66      int hash1 = grammar.CalcHashCode(s1);
67      int hash2 = grammar.CalcHashCode(s2);
68
69      Assert.AreEqual(hash1, hash2);
70    }
71
72    [TestMethod]
73    [TestCategory("TreeHashing")]
74    public void RepeatedAddition() {
75      SymbolString s1 = new SymbolString(new[] { varA, varA, grammar.Addition, varA, grammar.Addition });
76      SymbolString s2 = new SymbolString(new[] { varA });
77
78      int hash1 = grammar.CalcHashCode(s1);
79      int hash2 = grammar.CalcHashCode(s2);
80
81      Assert.AreEqual(hash1, hash2);
82    }
83
84    [TestMethod]
85    [TestCategory("TreeHashing")]
86    public void ComplexInequality() {
87      SymbolString s1 = new SymbolString(new[] { varA, varA, varA, grammar.Multiplication, grammar.Multiplication });
88      SymbolString s2 = new SymbolString(new[] { varA, varA, varA, grammar.Multiplication, grammar.Addition });
89
90      int hash1 = grammar.CalcHashCode(s1);
91      int hash2 = grammar.CalcHashCode(s2);
92
93      Assert.AreNotEqual(hash1, hash2);
94    }
95
96
97    #region parser
98
99    [TestMethod]
100    [TestCategory("Parser")]
101    public void InfixParserSimpleTest() {
102      SymbolString postfix = new SymbolString(new[] { varA, varB, varC, grammar.Addition, grammar.Addition });
103      SymbolString infix = new SymbolString(new[] { varA, grammar.Addition, varB, grammar.Addition, varC });
104
105      Assert.AreEqual(infix.ToString(), grammar.PostfixToInfixParser(postfix).ToString());
106    }
107
108    [TestMethod]
109    [TestCategory("Parser")]
110    public void InfixParserComplexTest() {
111      SymbolString postfix = new SymbolString(new[] { varB, varA, varB, varC, grammar.Multiplication, grammar.Addition, grammar.Addition});
112      SymbolString infix = new SymbolString(new[] { varB, grammar.Addition, varA, grammar.Addition, varB, grammar.Multiplication, varC });
113
114      Assert.AreEqual(infix.ToString(), grammar.PostfixToInfixParser(postfix).ToString());
115    }
116
117    #endregion
118  }
119}
Note: See TracBrowser for help on using the repository browser.