KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > lexerTests > PageIndexTests


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/lexerTests/PageIndexTests.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/01/02 16:24:56 $
10
// $Revision: 1.13 $
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.lexerTests;
28
29 import org.htmlparser.lexer.PageIndex;
30 import org.htmlparser.tests.ParserTestCase;
31
32 public class PageIndexTests extends ParserTestCase
33 {
34
35     static
36     {
37         System.setProperty ("org.htmlparser.tests.lexerTests.PageIndexTests", "PageIndexTests");
38     }
39
40     /**
41      * Test the end-of-line index class.
42      */

43     public PageIndexTests (String JavaDoc name)
44     {
45         super (name);
46     }
47
48     public void testAppend1 ()
49     {
50         PageIndex index;
51         int pos;
52         int[] list;
53
54         index = new PageIndex (null);
55
56         for (int i = 0; i < 10000; i++)
57         {
58             pos = index.row (i);
59             assertTrue ("append not at end", pos == i);
60             assertTrue ("wrong position", pos == index.add (i));
61         }
62
63         list = index.get ();
64         for (int i = 0; i < 10000; i++)
65             assertTrue ("wrong value", list[i] == i);
66     }
67
68     public void testAppend2 ()
69     {
70         PageIndex index;
71         int pos;
72         int[] list;
73
74         index = new PageIndex (null);
75
76         for (int i = 0; i < 10000; i++)
77         {
78             pos = index.row (i + 42);
79             assertTrue ("append not at end", pos == i);
80             assertTrue ("wrong position", pos == index.add (i + 42));
81         }
82
83         list = index.get ();
84         for (int i = 0; i < 10000; i++)
85             assertTrue ("wrong value", list[i] == i + 42);
86     }
87
88     public void testAppend3 ()
89     {
90         PageIndex index;
91         int pos;
92         int[] list;
93
94         index = new PageIndex (null);
95
96         for (int i = 0; i < 10000; i++)
97         {
98             pos = index.row (i * 42);
99             assertTrue ("append not at end", pos == i);
100             assertTrue ("wrong position", pos == index.add (i * 42));
101         }
102
103         list = index.get ();
104         for (int i = 0; i < 10000; i++)
105             assertTrue ("wrong value", list[i] == i * 42);
106     }
107
108     public void testInsert ()
109     {
110         PageIndex index;
111         double d;
112         int n;
113         int pos;
114         int[] list;
115
116         index = new PageIndex (null);
117
118         for (int i = 0; i < 10000; i++)
119         {
120             d = Math.random ();
121             d -= 0.5;
122             n = (int)(d * 100838);
123             pos = index.row (n);
124
125             // test for correct position
126
if (0 <= pos - 1)
127                 assertTrue ("search error less " + pos + " " + index.elementAt (pos - 1) + " " + n, index.elementAt (pos - 1) <= n);
128             if (pos + 1 < index.size ())
129                 assertTrue ("search error greater " + pos + " " + index.elementAt (pos + 1) + " " + n, index.elementAt (pos + 1) > n);
130         }
131
132         list = index.get ();
133         n = Integer.MIN_VALUE;
134         for (int i = 0; i < list.length; i++)
135         {
136             assertTrue ("wrong order", list[i] > n);
137             n = list[i];
138         }
139     }
140 }
141
Popular Tags