1 import lexer.Alphabet; 2 3 class TestAlphabet { 4 public static void main(String args[]) throws Exception { 5 Alphabet alpha = new Alphabet(); 7 if (alpha.size() != 0) { 8 throw new Exception ("Alphabet() size error"); 9 } 10 11 StringBuffer sb = new StringBuffer (); 13 for (char ch='a'; ch<'z'; ch++) { 14 sb.append(ch); 15 sb.append(ch); } 17 String str = new String (sb); 18 alpha = new Alphabet(str); 19 if (alpha.size() != str.length()/2) { 20 if (alpha.size() != 0) { 21 throw new Exception ("Alphabet(String) size error"); 22 } 23 } 24 for (int i = 0; i<str.length()/2; i++) { 25 if (alpha.getSymbol(i).charValue() != str.charAt(2*i)) { 26 throw new Exception ("Alphabet(String) content error"); 27 } 28 } 29 30 Alphabet alpha2 = new Alphabet(alpha); 32 if (alpha2 == alpha) { 33 throw new Exception ("Alphabet(Alphabet) same reference"); 34 } 35 if (!alpha2.equals(alpha)) { 36 throw new Exception ("Alphabet(Alphabet) not the same"); 37 } 38 39 int oldSize = alpha.size(); 41 alpha.addSymbol(alpha.getSymbol(0).charValue()); if (oldSize != alpha.size()) { 43 throw new Exception ("addSymbol(char) addad a symbol twice"); 44 } 45 char symbol = '+'; 46 alpha.addSymbol(symbol); 47 if (oldSize+1 != alpha.size()) { 48 throw new Exception ("addSymbol(char) symbol not added"); 49 } 50 if (symbol != alpha.getSymbol(oldSize).charValue()) { 51 throw new Exception ("addSymbol(char) symbol not added properly"); 52 } 53 54 alpha2.clear(); 56 if (alpha2.size() != 0) { 57 throw new Exception ("clear(char) size error"); 58 } 59 60 System.out.println(alpha.toString()); 62 63 alpha = new Alphabet("0124301"); 65 for (char ch = 'a'; ch<='f'; ch++) { 66 alpha.addSymbol(ch); 67 } 68 alpha.print(); 69 for (char ch = 'd'; ch<='h'; ch++) { 70 if (!alpha.containsSymbol(ch)) { 71 System.out.println(ch+" is not in the alphabet"); 72 } else { 73 int idx = alpha.idxSymbol(ch); 74 System.out.println("["+ch+"]="+idx); 75 Character cObj = alpha.getSymbol(idx); 76 if (cObj.charValue() != ch) { 77 throw new Exception ("Inconsistency found: idxSymbol - getSymbol"); 78 } 79 } 80 } 81 } 82 } 83 | Popular Tags |