Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/Test/TestGrammar.cs @ 17328

Last change on this file since 17328 was 10051, checked in by gkronber, 11 years ago

#2026 worked on a plugin for grammars (work-in-progress)

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26using HeuristicLab.Grammars;
27namespace Test {
28  [TestClass]
29  public class TestGrammar {
30    [TestMethod]
31    public void TestSimpleGrammar() {
32      var gStr =
33        @"S -> A
34          A->a B
35          B ->S b | S a|C b
36          C-> a  aa   aaa
37         ";
38      var g = HeuristicLab.Grammars.Grammar.FromString(gStr);
39      Assert.AreEqual(g.StartSymbol, new Symbol("S"));
40      Assert.AreEqual(g.TerminalSymbols.Count(), 4);
41      Assert.AreEqual(g.NonTerminalSymbols.Count(), 4);
42      Assert.IsTrue(g.TerminalSymbols.Contains(new Symbol("aaa")));
43      Assert.IsTrue(g.TerminalSymbols.Contains(new Symbol("aa")));
44      Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("S")));
45      Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("B")));
46      Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("C")));
47      Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("A")));
48    }
49
50
51    [TestMethod]
52    public void TestAttributedGrammar() {
53      var gStr =
54        @"S -> A
55          A<int a0, out int a1>->a B<a0, a1>
56          B<int a0, ref int a1> ->S b | S a|C<a0, a1> b
57          C<int a0, ref int a1> -> a  aa   aaa
58         ";
59      var g = HeuristicLab.Grammars.AttributedGrammar.FromString(gStr);
60      Assert.AreEqual(g.StartSymbol, new Symbol("S"));
61      Assert.AreEqual(g.TerminalSymbols.Count(), 4);
62      Assert.AreEqual(g.NonTerminalSymbols.Count(), 4);
63      Assert.IsTrue(g.TerminalSymbols.Contains(new Symbol("aaa")));
64      Assert.IsTrue(g.TerminalSymbols.Contains(new Symbol("aa")));
65      Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("S")));
66      Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("B")));
67      Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("A")));
68      Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("C")));
69    }
70
71
72    [TestMethod]
73    public void TestLanguage() {
74      var gStr =
75        @"S -> a B | b A
76          A-> a | a S | b A A
77          B -> b | b S | a B B
78         ";
79      var g = HeuristicLab.Grammars.Grammar.FromString(gStr);
80      var l = new Language(g);
81      var s2 = new string[][]
82        {
83          new string[] {"a", "b"},
84          new string[] {"b", "a"}
85        };
86      var s4 = new string[][]
87        {
88          new [] {"a", "a", "b", "b"},
89          new [] {"a", "b", "a", "b"},
90          new [] {"a", "b", "b", "a"},
91          new [] {"b", "b", "a", "a"},
92          new [] {"b", "a", "b", "a"},
93          new [] {"b", "a", "a", "b"}
94        };
95
96
97      var l2 = l.Where(s => s.Count() == 2).Take(2);
98      var l4 = l.Where(s => s.Count() == 4).Take(6);
99
100      Assert.IsTrue(l2.Intersect(s2, new SequenceComparer()).Count() == l2.Count());
101      Assert.IsTrue(l4.Intersect(s4, new SequenceComparer()).Count() == l4.Count());
102    }
103  }
104
105  public class SequenceComparer : IEqualityComparer<IEnumerable<string>> {
106    public bool Equals(IEnumerable<string> x, IEnumerable<string> y) {
107      return x.Count() == y.Count() &&
108        x.Zip(y, (s, t) => s == t).All(eq => eq);
109    }
110
111    public int GetHashCode(IEnumerable<string> obj) {
112      if (!obj.Any()) return 0;
113      else return obj.First().GetHashCode();
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.