KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > util > TestPrefixStringMatcher


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.util;
5
6 import junit.framework.TestCase;
7
8 /** Unit tests for PrefixStringMatcher. */
9 public class TestPrefixStringMatcher extends TestCase {
10   public TestPrefixStringMatcher(String JavaDoc name) {
11     super(name);
12   }
13
14   private final static int NUM_TEST_ROUNDS= 20;
15   private final static int MAX_TEST_PREFIXES= 100;
16   private final static int MAX_PREFIX_LEN= 10;
17   private final static int NUM_TEST_INPUTS_PER_ROUND= 100;
18   private final static int MAX_INPUT_LEN= 20;
19
20   private final static char[] alphabet=
21     new char[] {
22       'a', 'b', 'c', 'd',
23 // 'e', 'f', 'g', 'h', 'i', 'j',
24
// 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
25
// 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4',
26
// '5', '6', '7', '8', '9', '0'
27
};
28
29   private String JavaDoc makeRandString(int minLen, int maxLen) {
30     int len= minLen + (int) (Math.random() * (maxLen - minLen));
31     char[] chars= new char[len];
32     
33     for (int pos= 0; pos < len; pos++) {
34       chars[pos]= alphabet[(int) (Math.random() * alphabet.length)];
35     }
36     
37     return new String JavaDoc(chars);
38   }
39   
40   public void testPrefixMatcher() {
41     int numMatches= 0;
42     int numInputsTested= 0;
43
44     for (int round= 0; round < NUM_TEST_ROUNDS; round++) {
45
46       // build list of prefixes
47
int numPrefixes= (int) (Math.random() * MAX_TEST_PREFIXES);
48       String JavaDoc[] prefixes= new String JavaDoc[numPrefixes];
49       for (int i= 0; i < numPrefixes; i++) {
50         prefixes[i]= makeRandString(0, MAX_PREFIX_LEN);
51       }
52
53       PrefixStringMatcher prematcher= new PrefixStringMatcher(prefixes);
54
55       // test random strings for prefix matches
56
for (int i= 0; i < NUM_TEST_INPUTS_PER_ROUND; i++) {
57         String JavaDoc input= makeRandString(0, MAX_INPUT_LEN);
58         boolean matches= false;
59         int longestMatch= -1;
60         int shortestMatch= -1;
61
62         for (int j= 0; j < prefixes.length; j++) {
63
64           if ((prefixes[j].length() > 0)
65               && input.startsWith(prefixes[j])) {
66
67             matches= true;
68             int matchSize= prefixes[j].length();
69
70             if (matchSize > longestMatch)
71               longestMatch= matchSize;
72
73             if ( (matchSize < shortestMatch)
74                  || (shortestMatch == -1) )
75               shortestMatch= matchSize;
76           }
77
78         }
79
80         if (matches)
81           numMatches++;
82
83         numInputsTested++;
84
85         assertTrue( "'" + input + "' should " + (matches ? "" : "not ")
86                     + "match!",
87                     matches == prematcher.matches(input) );
88         if (matches) {
89           assertTrue( shortestMatch
90                       == prematcher.shortestMatch(input).length());
91           assertTrue( input.substring(0, shortestMatch).equals(
92                         prematcher.shortestMatch(input)) );
93
94           assertTrue( longestMatch
95                       == prematcher.longestMatch(input).length());
96           assertTrue( input.substring(0, longestMatch).equals(
97                         prematcher.longestMatch(input)) );
98
99         }
100       }
101     }
102
103     System.out.println("got " + numMatches + " matches out of "
104                        + numInputsTested + " tests");
105   }
106
107 }
108
Popular Tags