Package net.sourceforge.chaperon.test

Source Code of net.sourceforge.chaperon.test.FirstSetTestCase

/*
*  Copyright (C) Chaperon. All rights reserved.
*  -------------------------------------------------------------------------
*  This software is published under the terms of the Apache Software License
*  version 1.1, a copy of which has been included  with this distribution in
*  the LICENSE file.
*/

package net.sourceforge.chaperon.test;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import net.sourceforge.chaperon.build.EmptyList;
import net.sourceforge.chaperon.build.FirstSetCollection;
import net.sourceforge.chaperon.model.grammar.Grammar;
import net.sourceforge.chaperon.model.grammar.Production;
import net.sourceforge.chaperon.model.symbol.Nonterminal;
import net.sourceforge.chaperon.model.symbol.SymbolList;
import net.sourceforge.chaperon.model.symbol.SymbolSet;
import net.sourceforge.chaperon.model.symbol.Terminal;

public class FirstSetTestCase extends TestCase
{
  private Terminal plus;
  private Terminal mult;
  private Terminal bopen;
  private Terminal bclose;
  private Terminal id;
  private Nonterminal E;
  private Nonterminal Eprime;
  private Nonterminal T;
  private Nonterminal Tprime;
  private Nonterminal F;
  private EmptyList emptylist;
  private Grammar grammar;

  public FirstSetTestCase(String name)
  {
    super(name);
  }

  public void setUp()
  {
    emptylist = new EmptyList();

    plus = new Terminal("plus");
    mult = new Terminal("mult");
    bopen = new Terminal("bopen");
    bclose = new Terminal("bclose");
    id = new Terminal("id");

    E = new Nonterminal("E");
    Eprime = new Nonterminal("E'");
    T = new Nonterminal("T");
    Tprime = new Nonterminal("T'");
    F = new Nonterminal("F");

    grammar = new Grammar();

    // E -> T E'
    Production production = new Production(E);
    production.getDefinition().addSymbol(T);
    production.getDefinition().addSymbol(Eprime);
    grammar.addProduction(production);

    // E' -> + T E'
    production = new Production(Eprime);
    production.getDefinition().addSymbol(plus);
    production.getDefinition().addSymbol(T);
    production.getDefinition().addSymbol(Eprime);
    grammar.addProduction(production);

    // E' ->
    production = new Production(Eprime);
    grammar.addProduction(production);

    // T -> F T'
    production = new Production(T);
    production.getDefinition().addSymbol(F);
    production.getDefinition().addSymbol(Tprime);
    grammar.addProduction(production);

    // T' -> * F T'
    production = new Production(Tprime);
    production.getDefinition().addSymbol(mult);
    production.getDefinition().addSymbol(F);
    production.getDefinition().addSymbol(Tprime);
    grammar.addProduction(production);

    // T' ->
    production = new Production(Tprime);
    grammar.addProduction(production);

    // F -> bopen E bclose
    production = new Production(F);
    production.getDefinition().addSymbol(bopen);
    production.getDefinition().addSymbol(E);
    production.getDefinition().addSymbol(bclose);
    grammar.addProduction(production);

    // F -> id
    production = new Production(F);
    production.getDefinition().addSymbol(id);
    grammar.addProduction(production);
  }

  public void testFirstOfASymbol()
  {
    FirstSetCollection sets = new FirstSetCollection(grammar)/*, new ConsoleLogger());*/

    SymbolSet result = new SymbolSet();
    result.addSymbol(id);
    result.addSymbol(bopen);
    assertEquals("Test if sets are equal", result, sets.getFirstSet(E));
    assertEquals("Test if sets are equal", result, sets.getFirstSet(T));
    assertEquals("Test if sets are equal", result, sets.getFirstSet(F));

    result = new SymbolSet();
    result.addSymbol(plus);
    result.addSymbol(emptylist);
    assertEquals("Test if sets are equal", result, sets.getFirstSet(Eprime));

    result = new SymbolSet();
    result.addSymbol(mult);
    result.addSymbol(emptylist);
    assertEquals("Test if sets are equal", result, sets.getFirstSet(Tprime));
  }

  public void testFirstOfASymbolList()
  {
    FirstSetCollection sets = new FirstSetCollection(grammar)/*, new ConsoleLogger());*/

    SymbolList list = new SymbolList();
    list.addSymbol(Eprime);

    SymbolSet result = new SymbolSet();
    result.addSymbol(plus);
    result.addSymbol(emptylist);
    assertEquals("Test if sets are equal", result, sets.getFirstSet(list));

    list = new SymbolList();
    list.addSymbol(Eprime);
    list.addSymbol(id);
    list.addSymbol(Tprime);

    result = new SymbolSet();
    result.addSymbol(plus);
    result.addSymbol(id);
    assertEquals("Test if sets are equal", result, sets.getFirstSet(list));

    list = new SymbolList();
    list.addSymbol(Eprime);
    list.addSymbol(Tprime);

    result = new SymbolSet();
    result.addSymbol(plus);
    result.addSymbol(mult);
    result.addSymbol(emptylist);
    assertEquals("Test if sets are equal", result, sets.getFirstSet(list));

    list = new SymbolList();

    result = new SymbolSet();
    result.addSymbol(emptylist);
    assertEquals("Test if sets are equal", result, sets.getFirstSet(list));
  }

  public static Test suite()
  {
    return new TestSuite(FirstSetTestCase.class);
  }
}
TOP

Related Classes of net.sourceforge.chaperon.test.FirstSetTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.