Package lexer

Examples of lexer.Alphabet


import lexer.Alphabet;

class TestAlphabet {
  public static void main(String args[]) throws Exception {
    // test Alphabet()
    Alphabet alpha = new Alphabet();
    if (alpha.size() != 0) {
      throw new Exception("Alphabet() size error");
    }

    // test Alphabet(java.lang.String s)
    StringBuffer sb = new StringBuffer();
    for (char ch='a'; ch<'z'; ch++) {
      sb.append(ch);
      sb.append(ch); // each character two times in a row
    }
    String str = new String(sb);
    alpha = new Alphabet(str);
    if (alpha.size() != str.length()/2) {
      if (alpha.size() != 0) {
        throw new Exception("Alphabet(String) size error");
      }
    }
    for (int i = 0; i<str.length()/2; i++) {
      if (alpha.getSymbol(i).charValue() != str.charAt(2*i)) {
        throw new Exception("Alphabet(String) content error");
      }
    }

    // test Alphabet(Alphabet alpha)
    Alphabet alpha2 = new Alphabet(alpha);
    if (alpha2 == alpha) {
      throw new Exception("Alphabet(Alphabet) same reference");
    }
    if (!alpha2.equals(alpha)) {
      throw new Exception("Alphabet(Alphabet) not the same");
    }

    // Add symbol
    int oldSize = alpha.size();
    alpha.addSymbol(alpha.getSymbol(0).charValue()); // already in there
    if (oldSize != alpha.size()) {
      throw new Exception("addSymbol(char) addad a symbol twice");
    }
    char symbol = '+';
    alpha.addSymbol(symbol);
    if (oldSize+1 != alpha.size()) {
      throw new Exception("addSymbol(char) symbol not added");
    }
    if (symbol != alpha.getSymbol(oldSize).charValue()) {
      throw new Exception("addSymbol(char) symbol not added properly");
    }

    // Test clear
    alpha2.clear();
    if (alpha2.size() != 0) {
      throw new Exception("clear(char) size error");
    }

    // Test toString()
    System.out.println(alpha.toString());

    // Test Other
    alpha = new Alphabet("0124301");
    for (char ch = 'a'; ch<='f'; ch++) {
      alpha.addSymbol(ch);
    }
    alpha.print();
    for (char ch = 'd'; ch<='h'; ch++) {
View Full Code Here

TOP

Related Classes of lexer.Alphabet

Copyright © 2018 www.massapicom. 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.