#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using HeuristicLab.Grammars; namespace Test { [TestClass] public class TestGrammar { [TestMethod] public void TestSimpleGrammar() { var gStr = @"S -> A A->a B B ->S b | S a|C b C-> a aa aaa "; var g = HeuristicLab.Grammars.Grammar.FromString(gStr); Assert.AreEqual(g.StartSymbol, new Symbol("S")); Assert.AreEqual(g.TerminalSymbols.Count(), 4); Assert.AreEqual(g.NonTerminalSymbols.Count(), 4); Assert.IsTrue(g.TerminalSymbols.Contains(new Symbol("aaa"))); Assert.IsTrue(g.TerminalSymbols.Contains(new Symbol("aa"))); Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("S"))); Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("B"))); Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("C"))); Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("A"))); } [TestMethod] public void TestAttributedGrammar() { var gStr = @"S -> A A->a B B ->S b | S a|C b C -> a aa aaa "; var g = HeuristicLab.Grammars.AttributedGrammar.FromString(gStr); Assert.AreEqual(g.StartSymbol, new Symbol("S")); Assert.AreEqual(g.TerminalSymbols.Count(), 4); Assert.AreEqual(g.NonTerminalSymbols.Count(), 4); Assert.IsTrue(g.TerminalSymbols.Contains(new Symbol("aaa"))); Assert.IsTrue(g.TerminalSymbols.Contains(new Symbol("aa"))); Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("S"))); Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("B"))); Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("A"))); Assert.IsTrue(g.NonTerminalSymbols.Contains(new Symbol("C"))); } [TestMethod] public void TestLanguage() { var gStr = @"S -> a B | b A A-> a | a S | b A A B -> b | b S | a B B "; var g = HeuristicLab.Grammars.Grammar.FromString(gStr); var l = new Language(g); var s2 = new string[][] { new string[] {"a", "b"}, new string[] {"b", "a"} }; var s4 = new string[][] { new [] {"a", "a", "b", "b"}, new [] {"a", "b", "a", "b"}, new [] {"a", "b", "b", "a"}, new [] {"b", "b", "a", "a"}, new [] {"b", "a", "b", "a"}, new [] {"b", "a", "a", "b"} }; var l2 = l.Where(s => s.Count() == 2).Take(2); var l4 = l.Where(s => s.Count() == 4).Take(6); Assert.IsTrue(l2.Intersect(s2, new SequenceComparer()).Count() == l2.Count()); Assert.IsTrue(l4.Intersect(s4, new SequenceComparer()).Count() == l4.Count()); } } public class SequenceComparer : IEqualityComparer> { public bool Equals(IEnumerable x, IEnumerable y) { return x.Count() == y.Count() && x.Zip(y, (s, t) => s == t).All(eq => eq); } public int GetHashCode(IEnumerable obj) { if (!obj.Any()) return 0; else return obj.First().GetHashCode(); } } }