update standards version
[debian/freetts] / tests / LexiconTest.java
1 /**
2  * Copyright 2001 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8 package tests;
9 import junit.framework.*;
10 import java.util.*;
11 import java.io.*;
12 import com.sun.speech.freetts.*;
13 import com.sun.speech.freetts.lexicon.Lexicon;
14 import com.sun.speech.freetts.en.us.CMULexicon;
15
16 /**
17  * JUnit tests Tests for the LexiconTest class
18  * 
19  * @version 1.0
20  */
21 public class LexiconTest extends TestCase {
22     BufferedReader reader = null;
23     Lexicon lex = null;
24     
25     /**
26      * Creates the set of LexiconTest
27      * 
28      * @param  name the name of the test.
29      */
30     public LexiconTest(String name) {
31         super(name);
32     }
33
34
35     /**
36      * Common code run before each test
37      */
38     protected void setUp() {
39         try {
40             lex = CMULexicon.getInstance(true);
41             assertTrue("Lexicon Created", lex != null);
42             reader = new BufferedReader(new FileReader("LEX.txt"));
43             assertTrue("Data File opened", reader != null);
44         } catch (IOException e) {
45             e.printStackTrace();
46         }
47         
48     }
49     
50     /**
51      * Common code run after each test
52      */
53     protected void tearDown() {
54     } 
55
56
57     /**
58      * Tests to see that we succeed
59      */
60     public void testSuccess() {
61         assertTrue("Should succeed", true);
62     }
63
64
65     /**
66      * Tests that Lexicon matches those from the standard results.
67      */
68     public void testLexicon() {
69         String word;
70         String pos;
71         int i;
72         String flite_phones;
73         String[] lex_phone_array;
74         StringBuffer lex_phones;
75         String line;
76         try {
77             while ((line = reader.readLine()) != null) {
78                 if (line.startsWith("***")) {
79                     continue;
80                 }
81                 i = line.indexOf(' ');
82                 word = line.substring(0,i);
83                 line = line.substring(i+1);
84                 i = line.indexOf(' ');
85                 pos = line.substring(0,i);
86                 flite_phones = line.substring(i+1);
87                 lex_phone_array = lex.getPhones(word, pos);
88                 assertTrue("Phones returned for " + word + pos
89                            + " is not null: ",
90                            lex_phone_array != null);
91                 lex_phones = new StringBuffer("(");
92                 for (i = 0; i < lex_phone_array.length; i++) {
93                     if (i != 0) {
94                         lex_phones.append(" ");
95                     }
96                     lex_phones.append(lex_phone_array[i]);
97                 }
98                 lex_phones.append(")");
99                 assertTrue("Phones returned for " + word + pos
100                            + " are identical "
101                            + "(Our phones: " + lex_phones + ", "
102                            + "Flite phones: " + flite_phones + "): ",
103                            flite_phones.equals(lex_phones.toString()));
104             }
105         } catch (IOException e) {
106             assertTrue("FILE IO problem: ", false);
107         }
108     }
109             
110     /**
111      * Factory method that creates the test suite.
112      * 
113      * @return the test suite.
114      */
115     public static Test suite() {
116         return new TestSuite(LexiconTest.class);
117     } 
118
119
120
121     /**
122      * Main entry point for this test suite.
123      * 
124      * @param  args    the command line arguments.
125      */
126     public static void main(String[] args) {
127         junit.textui.TestRunner.run(suite());
128     } 
129 }