[Htmlparser-cvs] htmlparser/src/org/htmlparser/tests/utilTests SortTest.java,NONE,1.1 AllTests.java,
Brought to you by:
derrickoswald
From: <der...@us...> - 2003-08-10 23:33:40
|
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/utilTests In directory sc8-pr-cvs1:/tmp/cvs-serv1528/tests/utilTests Modified Files: AllTests.java Added Files: SortTest.java Log Message: Second drop for new io subsystem. --- NEW FILE: SortTest.java --- // HTMLParser Library v1_3_20030727 - A java-based parser for HTML // Copyright (C) Dec 31, 2000 Somik Raha // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // For any questions or suggestions, you can write to me at : // Email :so...@in... // // Postal Address : // Somik Raha // Extreme Programmer & Coach // Industrial Logic Corporation // 2583 Cedar Street, Berkeley, // CA 94708, USA // Website : http://www.industriallogic.com package org.htmlparser.tests.utilTests; import java.io.File; import java.util.StringTokenizer; import java.util.Date; import java.util.Enumeration; import java.util.Vector; import junit.framework.TestCase; import org.htmlparser.util.sort.Ordered; import org.htmlparser.util.sort.Sort; import org.htmlparser.util.sort.Sortable; /** * Sort testing. */ public class SortTest extends TestCase { /** * Creates a new instance of SortTest */ public SortTest (String name) { super (name); } /** * A class implementing the Ordered interface. */ class Item implements Ordered { String mData; public Item (String data) { mData = data; } public int compare (Object o) { return (mData.compareTo (((Item)o).mData)); } public String toString () { return (mData); } } /** * A class implementing the Sortable interface. */ class List extends Vector implements Sortable { List (String words) { StringTokenizer toks; toks = new StringTokenizer (words); outer: while (toks.hasMoreTokens ()) addElement (new Item (toks.nextToken ())); Sort.QuickSort ((Sortable)this); } // // Sortable interface // public int first () { return (0); } public int last () { return (size () - 1); } public Ordered fetch (int index, Ordered reuse) { return ((Ordered)elementAt (index)); } public void swap (int i, int j) { Object o = elementAt (i); setElementAt (elementAt (j), i); setElementAt (o, j); } } /** * A subclass implementing the Ordered interface. */ class SortableFile extends File implements Ordered { public SortableFile (String name) { super (name); } public SortableFile (File dir, String name) { super (dir, name); } public int compare (Object o) { long ret; File f = (File)o; ret = lastModified () - f.lastModified (); if (ret < (long)Integer.MIN_VALUE) ret = Integer.MIN_VALUE; if (ret > (long)Integer.MAX_VALUE) ret = Integer.MAX_VALUE; return ((int)ret); } } /** * Test the operation of the static quicksort algorithm. */ public void testQuickSort () { Item[] words = { new Item ("gazelle"), new Item ("infant"), new Item ("toenail"), new Item ("breast"), new Item ("Derrick"), new Item ("toast"), new Item ("caretaker"), }; Sort.QuickSort (words); assertEquals ("element 0 wrong ", "Derrick", words[0].mData); assertEquals ("element 1 wrong ", "breast", words[1].mData); assertEquals ("element 2 wrong ", "caretaker", words[2].mData); assertEquals ("element 3 wrong ", "gazelle", words[3].mData); assertEquals ("element 4 wrong ", "infant", words[4].mData); assertEquals ("element 5 wrong ", "toast", words[5].mData); assertEquals ("element 6 wrong ", "toenail", words[6].mData); } /** * Test the operation of quicksort on a sortable list. */ public void testSortList () { List list = new List ( "'Twas brillig and the slithy toves " + "Did gyre and gimble in the wabe " + "All mimsy were the borogroves " + "And the mome raths outgrabe."); StringBuffer b = new StringBuffer (); for (Enumeration e = list.elements (); e.hasMoreElements ();) { if (0 != b.length ()) b.append (' '); b.append (e.nextElement ()); } assertEquals ("wrong ordering", "'Twas All And Did and and borogroves " + "brillig gimble gyre in mimsy mome outgrabe. " + "raths slithy the the the the toves wabe were", b.toString ()); } /** * Test the operation of quicksort on a vector of ordered items. */ public void testSortVector () { // sort a directory by date (oldest first) Vector directory = new Vector (); File dir = new File ("."); String[] listing = dir.list (); for (int i = 0; i < listing.length; i++) { File f = new SortableFile (dir, listing[i]); if (f.isFile ()) directory.addElement (f); } Sort.QuickSort (directory); // pull one out and test it's insertion ordinal int index = directory.size () * 2 / 3; SortableFile test = (SortableFile)directory.elementAt (index); directory.removeElementAt (index); int ordinal = Sort.bsearch (directory, test); assertEquals ("ordinal not correct value", index, ordinal); // test the ordering of the objects directory.insertElementAt (test, ordinal); Date last = null; for (int i = 0; i < directory.size (); i++) { File f = (File)directory.elementAt (i); String name = f.getName (); Date date = new Date (f.lastModified ()); if (null != last) assertTrue ("file " + name + " has a date before", !date.before (last)); last = date; } } } Index: AllTests.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/utilTests/AllTests.java,v retrieving revision 1.41 retrieving revision 1.42 diff -C2 -d -r1.41 -r1.42 *** AllTests.java 27 Jul 2003 19:19:23 -0000 1.41 --- AllTests.java 10 Aug 2003 23:33:37 -0000 1.42 *************** *** 110,129 **** } ! /** ! * Insert the method's description here. ! * Creation date: (6/17/2001 6:07:15 PM) ! * @return junit.framework.TestSuite ! */ ! public static TestSuite suite() { ! TestSuite suite = new TestSuite("Utility Tests"); ! ! suite.addTestSuite(BeanTest.class); ! suite.addTestSuite(CharacterTranslationTest.class); ! suite.addTestSuite(HTMLLinkProcessorTest.class); ! suite.addTestSuite(HTMLParserUtilsTest.class); ! suite.addTestSuite(HTMLTagParserTest.class); ! suite.addTestSuite(NodeListTest.class); ! return suite; ! } } --- 110,131 ---- } ! /** ! * Insert the method's description here. ! * Creation date: (6/17/2001 6:07:15 PM) ! * @return junit.framework.TestSuite ! */ ! public static TestSuite suite() ! { ! TestSuite suite = new TestSuite("Utility Tests"); ! suite.addTestSuite(BeanTest.class); ! suite.addTestSuite(CharacterTranslationTest.class); ! suite.addTestSuite(HTMLLinkProcessorTest.class); ! suite.addTestSuite(HTMLParserUtilsTest.class); ! suite.addTestSuite(HTMLTagParserTest.class); ! suite.addTestSuite(NodeListTest.class); ! suite.addTestSuite(SortTest.class); ! ! return suite; ! } } |