1   /*
2    * ====================================================================
3    * 
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2003 Nick Lothian. All rights reserved.
7    *
8    * Redistribution and use in source and binary forms, with or without
9    * modification, are permitted provided that the following conditions
10   * are met:
11   *
12   * 1. Redistributions of source code must retain the above copyright
13   *    notice, this list of conditions and the following disclaimer. 
14   *
15   * 2. Redistributions in binary form must reproduce the above copyright
16   *    notice, this list of conditions and the following disclaimer in
17   *    the documentation and/or other materials provided with the
18   *    distribution.
19   *
20   * 3. The end-user documentation included with the redistribution, if
21   *    any, must include the following acknowlegement:  
22   *       "This product includes software developed by the 
23   *        developers of Classifier4J (http://classifier4j.sf.net/)."
24   *    Alternately, this acknowlegement may appear in the software itself,
25   *    if and wherever such third-party acknowlegements normally appear.
26   *
27   * 4. The name "Classifier4J" must not be used to endorse or promote 
28   *    products derived from this software without prior written 
29   *    permission. For written permission, please contact   
30   *    http://sourceforge.net/users/nicklothian/.
31   *
32   * 5. Products derived from this software may not be called 
33   *    "Classifier4J", nor may "Classifier4J" appear in their names 
34   *    without prior written permission. For written permission, please 
35   *    contact http://sourceforge.net/users/nicklothian/.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   */
51  
52  package net.sf.classifier4J.bayesian;
53  
54  import junit.framework.TestCase;
55  
56  
57  public class AbstractWordsDataSourceSupport extends TestCase {
58  	
59  	IWordsDataSource wordsDataSource = null;
60  
61  	/***
62  	 * Constructor for AbstractWordsDataSourceTst.
63  	 * @param arg0
64  	 */
65  	public AbstractWordsDataSourceSupport(String arg0) {
66  		super(arg0);
67  	}
68  
69  
70  	public void testEmptySource() throws Exception {
71  		WordProbability wp = wordsDataSource.getWordProbability("myWord");		
72  		assertNull(wp);
73  	}
74  
75  	public void testAddMatch() throws Exception {
76  		wordsDataSource.addMatch("myWord");
77  		WordProbability wp = wordsDataSource.getWordProbability("myWord");
78                  assertNotNull(wp);
79  		assertEquals(1, wp.getMatchingCount());
80  		assertEquals(0, wp.getNonMatchingCount());
81  		
82  		wordsDataSource.addMatch("myWord");
83  		
84  		wp = wordsDataSource.getWordProbability("myWord");
85                  assertNotNull(wp);
86  		assertEquals(2, wp.getMatchingCount());
87  		assertEquals(0, wp.getNonMatchingCount());				
88  	}
89  
90  	public void testAddNonMatch() throws Exception {			
91  		wordsDataSource.addNonMatch("myWord");
92  		WordProbability wp = wordsDataSource.getWordProbability("myWord");
93                  assertNotNull(wp);
94  		assertEquals(0, wp.getMatchingCount());
95  		assertEquals(1, wp.getNonMatchingCount());
96  		
97  		wordsDataSource.addNonMatch("myWord");
98  		
99  		wp = wordsDataSource.getWordProbability("myWord");
100                 assertNotNull(wp);
101 		assertEquals(0, wp.getMatchingCount());
102 		assertEquals(2, wp.getNonMatchingCount());		
103 	}
104 
105 
106 	public void testAddMultipleMatches() throws Exception {
107 		String word = "myWord";
108 		int count = 10;
109 		for (int i=0; i < count; i++) {
110 			wordsDataSource.addMatch(word);
111 		}
112 		WordProbability wp = wordsDataSource.getWordProbability(word);	
113 		assertNotNull(wp);	
114 		assertEquals(count, wp.getMatchingCount());				
115 	}
116 
117 
118 	public void testAddMultipleNonMatches() throws Exception {
119 		String word = "myWord";
120 		int count = 10;
121 		for (int i=0; i < count; i++) {
122 			wordsDataSource.addNonMatch(word);
123 		}
124 		WordProbability wp = wordsDataSource.getWordProbability(word);
125 		assertNotNull(wp);		
126 		assertEquals(count, wp.getNonMatchingCount());				
127 	}
128 
129 
130 	public void testMultipleWrites() throws Exception {
131 		long startTime = System.currentTimeMillis();	
132 		
133 		String word = "myWord";
134 		int count = 500;
135 		for (int i=0; i < count; i++) {
136 			wordsDataSource.addNonMatch(word + count);
137 		}				
138 		long endTime = System.currentTimeMillis();
139 	}
140 
141 }