1 | using System.Linq;
|
---|
2 | using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
|
---|
3 | using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
|
---|
4 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
5 |
|
---|
6 | namespace 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 | }
|
---|