KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > utilTests > SortTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/utilTests/SortTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/31 16:42:32 $
10
// $Revision: 1.12 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.tests.utilTests;
28
29 import java.io.File JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import org.htmlparser.tests.ParserTestCase;
36 import org.htmlparser.util.sort.Ordered;
37 import org.htmlparser.util.sort.Sort;
38 import org.htmlparser.util.sort.Sortable;
39
40 /**
41  * Sort testing.
42  */

43 public class SortTest extends ParserTestCase
44 {
45     static
46     {
47         System.setProperty ("org.htmlparser.tests.utilTests.SortTest", "SortTest");
48     }
49
50     /**
51      * Creates a new instance of SortTest
52      */

53     public SortTest (String JavaDoc name)
54     {
55         super (name);
56     }
57
58     /**
59      * A class implementing the Ordered interface.
60      */

61     class Item implements Ordered
62     {
63         String JavaDoc mData;
64
65         public Item (String JavaDoc data)
66         {
67             mData = data;
68         }
69
70         public int compare (Object JavaDoc o)
71         {
72             return (mData.compareTo (((Item)o).mData));
73         }
74
75         public String JavaDoc toString ()
76         {
77             return (mData);
78         }
79     }
80
81     /**
82      * A class implementing the Sortable interface.
83      */

84     class List extends Vector JavaDoc implements Sortable
85     {
86         List (String JavaDoc words)
87         {
88             StringTokenizer JavaDoc toks;
89
90             toks = new StringTokenizer JavaDoc (words);
91             outer:
92             while (toks.hasMoreTokens ())
93                addElement (new Item (toks.nextToken ()));
94             Sort.QuickSort ((Sortable)this);
95         }
96
97         //
98
// Sortable interface
99
//
100

101         public int first ()
102         {
103             return (0);
104         }
105         public int last ()
106         {
107             return (size () - 1);
108         }
109         public Ordered fetch (int index, Ordered reuse)
110         {
111             return ((Ordered)elementAt (index));
112         }
113         public void swap (int i, int j)
114         {
115             Object JavaDoc o = elementAt (i);
116             setElementAt (elementAt (j), i);
117             setElementAt (o, j);
118         }
119     }
120
121     /**
122      * A subclass implementing the Ordered interface.
123      */

124     class SortableFile extends File JavaDoc implements Ordered
125     {
126         public SortableFile (String JavaDoc name)
127         {
128             super (name);
129         }
130
131         public SortableFile (File JavaDoc dir, String JavaDoc name)
132         {
133             super (dir, name);
134         }
135
136         public int compare (Object JavaDoc o)
137         {
138             long ret;
139
140             File JavaDoc f = (File JavaDoc)o;
141
142             ret = lastModified () - f.lastModified ();
143             if (ret < Integer.MIN_VALUE)
144                 ret = Integer.MIN_VALUE;
145             if (ret > Integer.MAX_VALUE)
146                 ret = Integer.MAX_VALUE;
147
148             return ((int)ret);
149         }
150     }
151
152     /**
153      * Test the operation of the static quicksort algorithm.
154      */

155     public void testQuickSort ()
156     {
157         Item[] words =
158         {
159             new Item ("gazelle"),
160             new Item ("infant"),
161             new Item ("toenail"),
162             new Item ("breast"),
163             new Item ("Derrick"),
164             new Item ("toast"),
165             new Item ("caretaker"),
166         };
167
168         Sort.QuickSort (words);
169
170
171         assertEquals ("element 0 wrong ", "Derrick", words[0].mData);
172         assertEquals ("element 1 wrong ", "breast", words[1].mData);
173         assertEquals ("element 2 wrong ", "caretaker", words[2].mData);
174         assertEquals ("element 3 wrong ", "gazelle", words[3].mData);
175         assertEquals ("element 4 wrong ", "infant", words[4].mData);
176         assertEquals ("element 5 wrong ", "toast", words[5].mData);
177         assertEquals ("element 6 wrong ", "toenail", words[6].mData);
178
179     }
180
181     /**
182      * Test the operation of quicksort on a sortable list.
183      */

184     public void testSortList ()
185     {
186         List list = new List (
187             "'Twas brillig and the slithy toves " +
188             "Did gyre and gimble in the wabe " +
189             "All mimsy were the borogroves " +
190             "And the mome raths outgrabe.");
191         StringBuffer JavaDoc b = new StringBuffer JavaDoc ();
192         for (Enumeration JavaDoc e = list.elements (); e.hasMoreElements ();)
193         {
194             if (0 != b.length ())
195                 b.append (' ');
196             b.append (e.nextElement ());
197         }
198         assertEquals ("wrong ordering",
199               "'Twas All And Did and and borogroves "
200             + "brillig gimble gyre in mimsy mome outgrabe. "
201             + "raths slithy the the the the toves wabe were",
202             b.toString ());
203
204     }
205
206     /**
207      * Test the operation of quicksort on a vector of ordered items.
208      */

209     public void testSortVector ()
210     {
211         // sort a directory by date (oldest first)
212
Vector JavaDoc directory = new Vector JavaDoc ();
213         File JavaDoc dir = new File JavaDoc (".");
214         String JavaDoc[] listing = dir.list ();
215         for (int i = 0; i < listing.length; i++)
216         {
217             File JavaDoc f = new SortableFile (dir, listing[i]);
218             if (f.isFile ())
219                 directory.addElement (f);
220         }
221
222         Sort.QuickSort (directory);
223
224         // pull one out and test it's insertion ordinal
225
int index = directory.size () * 2 / 3;
226         SortableFile test =
227             (SortableFile)directory.elementAt (index);
228         directory.removeElementAt (index);
229         int ordinal = Sort.bsearch (directory, test);
230         assertEquals ("ordinal not correct value", index, ordinal);
231
232         // test the ordering of the objects
233
directory.insertElementAt (test, ordinal);
234         Date JavaDoc last = null;
235         for (int i = 0; i < directory.size (); i++)
236         {
237             File JavaDoc f = (File JavaDoc)directory.elementAt (i);
238             String JavaDoc name = f.getName ();
239             Date JavaDoc date = new Date JavaDoc (f.lastModified ());
240             if (null != last)
241                 assertTrue ("file " + name + " has a date before", !date.before (last));
242             last = date;
243         }
244     }
245 }
246
Popular Tags