Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15779 was 15746, checked in by lkammere, 7 years ago

#2886: Refactor grammar enumeration alg.

File size: 4.7 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    [TestMethod]
97    [TestCategory("TreeHashing")]
98    public void NonterminalHashing() {
99      SymbolString s1 = new SymbolString(new Symbol[] { varA, varA, grammar.Expr, grammar.Addition, grammar.Addition });
100      SymbolString s2 = new SymbolString(new Symbol[] { varA, grammar.Expr, grammar.Addition });
101
102      int hash1 = grammar.CalcHashCode(s1);
103      int hash2 = grammar.CalcHashCode(s2);
104
105      Assert.AreEqual(hash1, hash2);
106    }
107
108
109    #region parser
110
111    [TestMethod]
112    [TestCategory("Parser")]
113    public void InfixParserSimpleTest() {
114      SymbolString postfix = new SymbolString(new[] { varA, varB, varC, grammar.Addition, grammar.Addition });
115      SymbolString infix = new SymbolString(new[] { varA, grammar.Addition, varB, grammar.Addition, varC });
116
117      Assert.AreEqual(infix.ToString(), grammar.PostfixToInfixParser(postfix).ToString());
118    }
119
120    [TestMethod]
121    [TestCategory("Parser")]
122    public void InfixParserComplexTest() {
123      SymbolString postfix = new SymbolString(new[] { varB, varA, varB, varC, grammar.Multiplication, grammar.Addition, grammar.Addition});
124      SymbolString infix = new SymbolString(new[] { varB, grammar.Addition, varA, grammar.Addition, varB, grammar.Multiplication, varC });
125
126      Assert.AreEqual(infix.ToString(), grammar.PostfixToInfixParser(postfix).ToString());
127    }
128
129    #endregion
130  }
131}
Note: See TracBrowser for help on using the repository browser.